using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Palmmedia.ReportGenerator.Core; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Net.Mail; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using UdpClientLib; using UnityEditor.PackageManager; using UnityEngine; public interface IProgramModel { } public class ProgramModel : IProgramModel { private string tcpBaseUrl; private string udpBaseUrl; HttpClient httpClient = new HttpClient(); private SingleTcpClient tcpClient; private SingleUdpClient udpClient; public UdpClientManager manager = new UdpClientManager(); private List allProgramsCache = new List(); public RobotProgram CurrentProgram { get; private set; } private RobotData robotData; private RobotController robotController; private readonly object lockObject = new object(); private bool hasNewData; public bool IsMoving; public bool isError; private Vector3 startMovementPosition; private int currentToolNum = 0; public ProgramModel(string hostip, int tcpPort, int udpPort, RobotController robotController) { tcpBaseUrl = $"http://{hostip}:{tcpPort}"; udpBaseUrl = $"http://{hostip}:{udpPort}"; _ = HandleAsyncWork(hostip, tcpPort, udpPort); } public async Task HandleAsyncWork(string hostip, int tcpPort, int udpPort) { tcpClient = new SingleTcpClient(); await tcpClient.ConnectAsync("Tcp-Client", hostip, tcpPort); udpClient = manager.AddClient("Udp-client", hostip, udpPort); manager.PrintStatus(); } public async Task InitializeAsync() { await LoadAllPrograms(); hasNewData = false; IsMoving = false; isError = false; try { currentToolNum = await GetRobotToolNum(); } catch (Exception e) { Debug.LogWarning($"ÇöÀç Åø ¹øÈ£¸¦ °¡Á®¿À´Â µ¥ ½ÇÆÐÇß½À´Ï´Ù: {e.Message}. ±âº»°ª(0)À» »ç¿ëÇÕ´Ï´Ù."); currentToolNum = 1; } return; } public bool IsNewDataAvailable() { lock (lockObject) { return hasNewData; } } public RobotData GetLatestRobotData() { lock (lockObject) { hasNewData = false; // µ¥ÀÌÅ͸¦ ÀоúÀ¸¹Ç·Î Ç÷¡±×¸¦ ³»¸² return robotData; } } /// /// ÇÁ·Î±×·¥ »ý¼º/ºÒ·¯¿À±â ½Ã½ºÅÛ /// public async Task CheckProgramExists(string jobProgramName) { string requestUri = $"{tcpBaseUrl}/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) { string robotModelName; if (string.IsNullOrEmpty(userInputId)) return false; string newProgramId = $"{userInputId}.job"; if (await CheckProgramExists(newProgramId)) { Debug.LogError("ÆÄÀÏÀÌ ÀÌ¹Ì Á¸ÀçÇÕ´Ï´Ù."); return false; } else Debug.Log($"{newProgramId} »ý¼º"); 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 = $"{tcpBaseUrl}/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 = $"{tcpBaseUrl}/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 = $"{tcpBaseUrl}/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 == 0 || motorState == 256) return true; else if (motorState == 1) return false; else { throw new Exception("·Îº¿ »óÅ API ÀÀ´ä¿¡¼­ ¸ðÅÍ »óŸ¦ ãÀ» ¼ö ¾ø½À´Ï´Ù."); } } public async Task GetRobotToolNum() { string requestUri = $"{tcpBaseUrl}/project/rgen"; HttpResponseMessage result = await httpClient.GetAsync(requestUri); string jsonResponse = await result.Content.ReadAsStringAsync(); JObject data = JObject.Parse(jsonResponse); int toolNum = (int)data.SelectToken("tool_no"); return toolNum; } public async Task GetTCPAsync(CancellationToken token) { while (!token.IsCancellationRequested) { try { string requestUri = $"{tcpBaseUrl}/project/robot/po_cur"; HttpResponseMessage result = await httpClient.GetAsync(requestUri); string jsonResponse = await result.Content.ReadAsStringAsync(); var tempRobotData = JsonConvert.DeserializeObject(jsonResponse, new JsonSerializerSettings { CheckAdditionalContent = false }); lock (lockObject) { robotData = tempRobotData; hasNewData = true; } await Task.Delay(50); } catch (Exception e) { Debug.Log(e); await Task.Delay(1000); // ¿¡·¯ ½Ã ´õ ±ä ´ë±â } } } public async Task LoadProgram(string programId) { string requestUri = $"{tcpBaseUrl}/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($"{tcpBaseUrl}/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; } /// /// ·Îº¿ À§Ä¡ ±â·Ï ½Ã½ºÅÛ /// public async Task SavePointToProgramAsync(RobotData pointData, int index = -1) { if (CurrentProgram == null) { Debug.LogError("ÀúÀåÇÒ ÇÁ·Î±×·¥ÀÌ ·ÎµåµÇÁö ¾Ê¾Ò½À´Ï´Ù."); return false; } string jsonPayload; string requestUri; HttpContent content; object payload; try { if (index == -1) { // sno_ref = ¸¶Áö¸· ½ºÅÜÀÇ ¹øÈ£, ¾øÀ¸¸é 0 int sno_ref = (CurrentProgram.Steps.Count > 0) ? CurrentProgram.Steps.Last().StepNumber : 0; payload = new { adjust_branch = true, cmd_line = $"move P,spd=50%,accu=3,tool={currentToolNum}", fno_ref = -1, mechinfo = -1, move_cursor = 1, ofs = 0, save_file = true, set_cur_pose = -1, sno_ref = sno_ref }; requestUri = $"{tcpBaseUrl}/project/jobs/{CurrentProgram.ProgramId}/ins_cmd_line"; } else { // sno = ¼öÁ¤ÇÒ ½ºÅÜÀÇ ¹øÈ£ int sno = CurrentProgram.Steps[index].StepNumber; payload = new { mechinfo = -1, save_file = true, sno = sno }; requestUri = $"{tcpBaseUrl}/project/jobs/{CurrentProgram.ProgramId}/pose_modify"; } jsonPayload = JsonConvert.SerializeObject(payload); content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); HttpResponseMessage result = await httpClient.PostAsync(requestUri, content); if (result.IsSuccessStatusCode) { // ¼­¹ö ÀúÀåÀÌ ¼º°øÇϸé, ·ÎÄà ij½Ã¿¡µµ ¹Ý¿µ if (index == -1) CurrentProgram.AddStep(pointData, currentToolNum); // ·ÎÄà ij½Ã¿¡ Ãß°¡ else CurrentProgram.UpdateStep(index, pointData); // ·ÎÄà ij½Ã ¼öÁ¤ return true; } Debug.LogError($"SavePointToProgramAsync ½ÇÆÐ: {await result.Content.ReadAsStringAsync()}"); return false; } catch (Exception e) { Debug.LogError($"Æ÷ÀÎÆ® ÀúÀå ½ÇÆÐ: {e.Message}"); return false; } } // ¼­¹ö¿¡ Æ÷ÀÎÆ® »èÁ¦ ¿äû public async Task DeletePointFromProgramAsync(int index) { if (CurrentProgram == null || index < 0 || index >= CurrentProgram.Steps.Count) { Debug.LogError($"DeletePointFromProgramAsync: À߸øµÈ À妽º {index}"); return false; } // sno_ref = »èÁ¦ÇÒ ½ºÅÜÀÇ ¹øÈ£ (0-based index°¡ ¾Æ´Ô) int sno_ref = CurrentProgram.Steps[index].StepNumber; var payload = new { adjust_branch = true, fno_ref = 0, ofs = 0, save_file = true, sno_ref = sno_ref }; string jsonPayload = JsonConvert.SerializeObject(payload); HttpContent content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); string requestUri = $"{tcpBaseUrl}/project/jobs/{CurrentProgram.ProgramId}/del_cmd_line"; try { HttpResponseMessage result = await httpClient.PostAsync(requestUri, content); if (result.IsSuccessStatusCode) { // ¼­¹ö »èÁ¦ ¼º°ø ½Ã, ·ÎÄà ij½Ã¿¡¼­µµ »èÁ¦ ¹× ÀçÁ¤·Ä CurrentProgram.DeleteStep(index); return true; } Debug.LogError($"DeletePointFromProgramAsync ½ÇÆÐ: {await result.Content.ReadAsStringAsync()}"); return false; } catch (Exception e) { Debug.LogError($"Æ÷ÀÎÆ® »èÁ¦ ½ÇÆÐ: {e.Message}"); return false; } } // Ÿ°Ù Æ÷Áö¼ÇÀ¸·Î À̵¿ public async Task MoveToPoseTcpAsync(Vector3 position) { try { startMovementPosition.x = Convert.ToSingle(Math.Round(-1 * position.x * 1000, 2)); startMovementPosition.y = Convert.ToSingle(Math.Round(-1 * position.z * 1000, 2)); startMovementPosition.z = Convert.ToSingle(Math.Round(position.y * 1000, 2)); string jsonResponse = await tcpClient.SendPostRequestAsync("/project/robot/move_to_pose_manual", $"{{\"pose_tg\":{{\"crd\":\"robot\",\"_type\":\"Pose\",\"mechinfo\":1,\"x\":{startMovementPosition.x},\"y\":{startMovementPosition.y},\"z\":{startMovementPosition.z}, \"rx\":{robotData.rx}, \"ry\":{robotData.ry}, \"rz\":{robotData.rz}}}}}"); if (jsonResponse.Contains("200")) { Debug.Log("TCP POST (Move) ¸í·É Àü¼Û ¼º°ø"); IsMoving = true; return true; } else { Debug.LogError($"TCP POST (Move) ½ÇÆÐ"); return false; } } catch (Exception e) { Debug.Log(e); return false; } } // TCP POST À̵¿ ¸í·É üũ public async Task StartMovementCheckLoopAsync(CancellationToken token) { try { while (!token.IsCancellationRequested) { if (IsMoving) { await udpClient.SendFilledBytesAsync(new Dictionary { { 2, 0x20 } }); await Task.Delay(100); RobotData currentPose = null; lock (lockObject) { currentPose = this.robotData; } if (currentPose != null) { bool isApproximatelyX = Mathf.Approximately(startMovementPosition.x, Convert.ToSingle(Math.Round(currentPose.x, 2))); bool isApproximatelyY = Mathf.Approximately(startMovementPosition.y, Convert.ToSingle(Math.Round(currentPose.y, 2))); bool isApproximatelyZ = Mathf.Approximately(startMovementPosition.z, Convert.ToSingle(Math.Round(currentPose.z, 2))); if (isApproximatelyX && isApproximatelyY && isApproximatelyZ) { IsMoving = false; } } } else { await Task.Delay(500, token); } } } catch (TaskCanceledException) { Debug.Log("MovementCheckLoopAsync Canceled."); } catch (Exception e) { Debug.LogError($"MovementCheckLoopAsync Error: {e.Message}\n{e.StackTrace}"); } } //private async Task GetMovementState() //{ // while (!cancellationTokenSource.Token.IsCancellationRequested) // { // try // { // var jsonResponse = await tcpClient.SendGetRequestAsync("/project/rgen"); // var pasrsingJsonResponse = HttpResponseParser.ExtractJsonFromHttpResponse(jsonResponse); // var tempRobotData = JsonConvert.DeserializeObject(pasrsingJsonResponse, new JsonSerializerSettings { CheckAdditionalContent = false }); // jsonResponse = await tcpClient.SendGetRequestAsync($"/logManager/search?cat_p=E&id_min={Convert.ToInt32(tempRobotData.eid_last_err) - 3}&id_max={tempRobotData.eid_last_err}"); // pasrsingJsonResponse = HttpResponseParser.ExtractJsonFromHttpResponse(jsonResponse); // tempRobotData = JsonConvert.DeserializeObject(pasrsingJsonResponse, new JsonSerializerSettings { CheckAdditionalContent = false }); // isError = tempRobotData != null && tempRobotData.code != null && (tempRobotData.code.Contains("228") || tempRobotData.code.Contains("6037")); // Debug.Log(isError); // await Task.Delay(100); // } // catch (System.Exception) // { // } // } //} public async Task GetMovingState() { string jsonResponse = await tcpClient.SendGetRequestAsync("/project/robot/moving_to_pose_manual"); string parsingJsonResponse = HttpResponseParser.ExtractJsonFromHttpResponse(jsonResponse); return parsingJsonResponse; } }