2025-10-23 18:43:38 +09:00
using Newtonsoft.Json ;
using Newtonsoft.Json.Linq ;
using Palmmedia.ReportGenerator.Core ;
using System ;
2025-10-14 16:38:35 +09:00
using System.Collections.Generic ;
using System.Linq ;
2025-10-23 18:43:38 +09:00
using System.Net.Http ;
using System.Net.Sockets ;
using System.Text ;
using System.Threading ;
using System.Threading.Tasks ;
using UdpClientLib ;
using UnityEngine ;
public interface IProgramModel
{
}
2025-10-14 16:38:35 +09:00
2025-10-23 18:43:38 +09:00
public class ProgramModel : IProgramModel
2025-10-14 16:38:35 +09:00
{
2025-10-23 18:43:38 +09:00
private string baseUrl ;
HttpClient httpClient = new HttpClient ( ) ;
2025-10-14 16:38:35 +09:00
2025-10-23 18:43:38 +09:00
private List < JobInfoDTO > allProgramsCache = new List < JobInfoDTO > ( ) ;
2025-10-14 16:38:35 +09:00
public RobotProgram CurrentProgram { get ; private set ; }
2025-10-23 18:43:38 +09:00
public ProgramModel ( string hostip , int tcpPort )
{
baseUrl = $"http://{hostip}:{tcpPort}" ;
}
public async Task InitializeAsync ( )
{
await LoadAllPrograms ( ) ;
return ;
}
public async Task < bool > CheckProgramExists ( string jobProgramName )
2025-10-14 16:38:35 +09:00
{
2025-10-23 18:43:38 +09:00
string requestUri = $"{baseUrl}/file_manager/file_exist?pathname=project/jobs/{jobProgramName}" ;
HttpResponseMessage result = await httpClient . GetAsync ( requestUri ) ;
string jsonResponse = await result . Content . ReadAsStringAsync ( ) ;
return jsonResponse . Equals ( "true" ) ;
2025-10-14 16:38:35 +09:00
}
2025-10-23 18:43:38 +09:00
public async Task < bool > CreateNewProgram ( string userInputId )
2025-10-14 16:38:35 +09:00
{
if ( string . IsNullOrEmpty ( userInputId ) ) return false ;
string newProgramId = $"{userInputId}.job" ;
2025-10-23 18:43:38 +09:00
if ( await CheckProgramExists ( newProgramId ) )
2025-10-14 16:38:35 +09:00
{
2025-10-23 18:43:38 +09:00
Debug . LogError ( "<22> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> ̹<EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> մϴ<D5B4> ." ) ;
2025-10-14 16:38:35 +09:00
return false ;
}
2025-10-23 18:43:38 +09:00
else
Debug . Log ( $"{newProgramId} <20> <> <EFBFBD> <EFBFBD> " ) ;
2025-10-14 16:38:35 +09:00
2025-10-23 18:43:38 +09:00
string robotModelName ;
try
{
robotModelName = await GetRobotModelNameAsync ( ) ;
}
catch ( Exception e )
{
Debug . LogError ( $"<22> κ<EFBFBD> <20> <EFBFBD> <F0B5A8B8> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ߽<EFBFBD> <DFBD> ϴ<EFBFBD> : {e.Message}" ) ;
return false ;
}
2025-10-14 16:38:35 +09:00
2025-10-23 18:43:38 +09:00
NewJobRequestDTO newJob = new NewJobRequestDTO
{
fname = newProgramId ,
model_name = robotModelName ,
n_add_ax = 0
} ;
string jsonString = JsonConvert . SerializeObject ( newJob ) ;
HttpContent jsonPayload = new StringContent ( jsonString , Encoding . UTF8 , "application/json" ) ;
string requestUri = $"{baseUrl}/project/jobs/create_job" ;
try
{
HttpResponseMessage result = await httpClient . PostAsync ( requestUri , jsonPayload ) ;
if ( result . IsSuccessStatusCode )
{
await LoadProgram ( userInputId ) ;
return true ;
}
else
return false ;
}
catch ( Exception e )
{
Debug . LogError ( $"<22> <> <EFBFBD> α <CEB1> <20> <> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> : {userInputId}, {e.Message}" ) ;
return false ;
}
2025-10-14 16:38:35 +09:00
}
2025-10-23 18:43:38 +09:00
private async Task < string > GetRobotModelNameAsync ( )
2025-10-14 16:38:35 +09:00
{
2025-10-23 18:43:38 +09:00
string requestUri = $"{baseUrl}/project/rgen" ;
HttpResponseMessage result = await httpClient . GetAsync ( requestUri ) ;
string jsonResponse = await result . Content . ReadAsStringAsync ( ) ;
JObject data = JObject . Parse ( jsonResponse ) ;
string modelName = ( string ) data . SelectToken ( "robot_model" ) ;
if ( string . IsNullOrEmpty ( modelName ) )
2025-10-14 16:38:35 +09:00
{
2025-10-23 18:43:38 +09:00
throw new Exception ( "<22> κ<EFBFBD> <20> <> <EFBFBD> <EFBFBD> API <20> <> <EFBFBD> 信<EFBFBD> <E4BFA1> <20> <EFBFBD> <F0B5A8B8> <EFBFBD> ã<> <C3A3> <20> <> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ϴ<EFBFBD> ." ) ;
2025-10-14 16:38:35 +09:00
}
2025-10-23 18:43:38 +09:00
return modelName ;
2025-10-14 16:38:35 +09:00
}
2025-10-24 11:55:43 +09:00
public async Task < bool > GetRobotMotorStateAsync ( )
{
string requestUri = $"{baseUrl}/project/rgen" ;
HttpResponseMessage result = await httpClient . GetAsync ( requestUri ) ;
string jsonResponse = await result . Content . ReadAsStringAsync ( ) ;
JObject data = JObject . Parse ( jsonResponse ) ;
int motorState = ( int ) data . SelectToken ( "enable_state" ) ;
if ( motorState = = 2 | | motorState = = 256 )
return true ;
else if ( motorState = = 1 )
return false ;
else
{
throw new Exception ( "<22> κ<EFBFBD> <20> <> <EFBFBD> <EFBFBD> API <20> <> <EFBFBD> 信<EFBFBD> <E4BFA1> <20> <> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> ¸ <EFBFBD> ã<> <C3A3> <20> <> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ϴ<EFBFBD> ." ) ;
}
}
2025-10-23 18:43:38 +09:00
public async Task < bool > LoadProgram ( string programId )
2025-10-14 16:38:35 +09:00
{
2025-10-23 18:43:38 +09:00
string requestUri = $"{baseUrl}/file_manager/files?pathname=project/jobs/{programId}&common" ;
2025-10-14 16:38:35 +09:00
2025-10-23 18:43:38 +09:00
HttpResponseMessage result = await httpClient . GetAsync ( requestUri ) ;
string rawTextContent = await result . Content . ReadAsStringAsync ( ) ;
2025-10-14 16:38:35 +09:00
2025-10-23 18:43:38 +09:00
if ( string . IsNullOrEmpty ( rawTextContent ) )
{
return false ;
}
2025-10-14 16:38:35 +09:00
2025-10-23 18:43:38 +09:00
CurrentProgram = new RobotProgram ( programId , rawTextContent ) ;
return true ;
2025-10-14 16:38:35 +09:00
}
2025-10-23 18:43:38 +09:00
private async Task LoadAllPrograms ( )
2025-10-14 16:38:35 +09:00
{
2025-10-23 18:43:38 +09:00
allProgramsCache . Clear ( ) ;
string jsonResponse = null ;
string wrappedJson = null ;
try
2025-10-14 16:38:35 +09:00
{
2025-10-24 11:55:43 +09:00
HttpResponseMessage result = await httpClient . GetAsync ( $"{baseUrl}/project/jobs_info" ) ;
2025-10-23 18:43:38 +09:00
jsonResponse = await result . Content . ReadAsStringAsync ( ) ;
wrappedJson = $"{{\" jobs \ ":{jsonResponse}}}" ;
JobListWrapper wrapper = JsonUtility . FromJson < JobListWrapper > ( wrappedJson ) ;
if ( wrapper ! = null & & wrapper . jobs ! = null )
{
allProgramsCache = wrapper . jobs ;
}
else
{
Debug . LogError ( "<22> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> job <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> Ľ<EFBFBD> <C4BD> <EFBFBD> <20> <> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ϴ<EFBFBD> : " + jsonResponse ) ;
}
}
catch ( ArgumentException e )
{
Debug . LogError ( $"JSON <20> Ľ<EFBFBD> <20> <> <EFBFBD> <EFBFBD> : {e.Message}" ) ;
}
catch ( Exception e )
{
Debug . LogError ( $"LoadAllPrograms <20> <> <20> <> <20> <> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> : {e.Message}" ) ;
2025-10-14 16:38:35 +09:00
}
}
2025-10-23 18:43:38 +09:00
public List < string > GetAllProgramIds ( )
2025-10-14 16:38:35 +09:00
{
2025-10-23 18:43:38 +09:00
List < string > ids = new List < string > ( ) ;
foreach ( var jobInfo in allProgramsCache )
2025-10-14 16:38:35 +09:00
{
2025-10-23 18:43:38 +09:00
ids . Add ( jobInfo . fname ) ;
2025-10-14 16:38:35 +09:00
}
2025-10-23 18:43:38 +09:00
return ids ;
2025-10-14 16:38:35 +09:00
}
}