Files
HDRobotics/Assets/Scripts/Model/ProgramModel.cs

202 lines
5.6 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Palmmedia.ReportGenerator.Core;
using System;
using System.Collections.Generic;
using System.Linq;
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
{
}
public class ProgramModel : IProgramModel
{
private string baseUrl;
HttpClient httpClient = new HttpClient();
private List<JobInfoDTO> allProgramsCache = new List<JobInfoDTO>();
public RobotProgram CurrentProgram { get; private set; }
public ProgramModel(string hostip, int tcpPort)
{
baseUrl = $"http://{hostip}:{tcpPort}";
}
public async Task InitializeAsync()
{
await LoadAllPrograms();
return;
}
public async Task<bool> CheckProgramExists(string jobProgramName)
{
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");
}
public async Task<bool> CreateNewProgram(string userInputId)
{
if (string.IsNullOrEmpty(userInputId)) return false;
string newProgramId = $"{userInputId}.job";
if (await CheckProgramExists(newProgramId))
{
Debug.LogError("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>̹<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>մϴ<D5B4>.");
return false;
}
else
Debug.Log($"{newProgramId} <20><><EFBFBD><EFBFBD>");
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;
}
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;
}
}
private async Task<string> GetRobotModelNameAsync()
{
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))
{
throw new Exception("<22>κ<EFBFBD> <20><><EFBFBD><EFBFBD> API <20><><EFBFBD><EFBFBD><E4BFA1> <20>𵨸<EFBFBD><F0B5A8B8><EFBFBD> ã<><C3A3> <20><> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>.");
}
return modelName;
}
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>.");
}
}
public async Task<bool> LoadProgram(string programId)
{
string requestUri = $"{baseUrl}/file_manager/files?pathname=project/jobs/{programId}&common";
HttpResponseMessage result = await httpClient.GetAsync(requestUri);
string rawTextContent = await result.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(rawTextContent))
{
return false;
}
CurrentProgram = new RobotProgram(programId, rawTextContent);
return true;
}
private async Task LoadAllPrograms()
{
allProgramsCache.Clear();
string jsonResponse = null;
string wrappedJson = null;
try
{
2025-10-24 11:55:43 +09:00
HttpResponseMessage result = await httpClient.GetAsync($"{baseUrl}/project/jobs_info");
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}");
}
}
public List<string> GetAllProgramIds()
{
List<string> ids = new List<string>();
foreach (var jobInfo in allProgramsCache)
{
ids.Add(jobInfo.fname);
}
return ids;
}
}