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 allProgramsCache = new List(); 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 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 CreateNewProgram(string userInputId) { if (string.IsNullOrEmpty(userInputId)) return false; string newProgramId = $"{userInputId}.job"; if (await CheckProgramExists(newProgramId)) { Debug.LogError("ÆÄÀÏÀÌ ÀÌ¹Ì Á¸ÀçÇÕ´Ï´Ù."); return false; } else Debug.Log($"{newProgramId} »ý¼º"); string robotModelName; try { robotModelName = await GetRobotModelNameAsync(); } catch (Exception e) { Debug.LogError($"·Îº¿ ¸ðµ¨¸íÀ» °¡Á®¿À´Â µ¥ ½ÇÆÐÇß½À´Ï´Ù: {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($"ÇÁ·Î±×·¥ »ý¼º ½ÇÆÐ: {userInputId}, {e.Message}"); return false; } } private async Task 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("·Îº¿ »óÅ API ÀÀ´ä¿¡¼­ ¸ðµ¨¸íÀ» ãÀ» ¼ö ¾ø½À´Ï´Ù."); } return modelName; } public async Task 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("·Îº¿ »óÅ API ÀÀ´ä¿¡¼­ ¸ðÅÍ »óŸ¦ ãÀ» ¼ö ¾ø½À´Ï´Ù."); } } public async Task 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 { HttpResponseMessage result = await httpClient.GetAsync($"{baseUrl}/project/jobs_info"); jsonResponse = await result.Content.ReadAsStringAsync(); wrappedJson = $"{{\"jobs\":{jsonResponse}}}"; JobListWrapper wrapper = JsonUtility.FromJson(wrappedJson); if (wrapper != null && wrapper.jobs != null) { allProgramsCache = wrapper.jobs; } else { Debug.LogError("¼­¹ö¿¡¼­ job ¸ñ·ÏÀ» ÆÄ½ÌÇÒ ¼ö ¾ø½À´Ï´Ù: " + jsonResponse); } } catch (ArgumentException e) { Debug.LogError($"JSON ÆÄ½Ì ¿À·ù: {e.Message}"); } catch (Exception e) { Debug.LogError($"LoadAllPrograms ¾Ë ¼ö ¾ø´Â ¿À·ù: {e.Message}"); } } public List GetAllProgramIds() { List ids = new List(); foreach (var jobInfo in allProgramsCache) { ids.Add(jobInfo.fname); } return ids; } }