<feat> TCP 좌표 패널 표기
This commit is contained in:
@@ -11,6 +11,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using UdpClientLib;
|
||||
using UnityEngine;
|
||||
using static DirectorySyncer;
|
||||
|
||||
public interface IProgramModel
|
||||
{
|
||||
@@ -19,26 +20,57 @@ public interface IProgramModel
|
||||
|
||||
public class ProgramModel : IProgramModel
|
||||
{
|
||||
private string baseUrl;
|
||||
private string tcpBaseUrl;
|
||||
private string udpBaseUrl;
|
||||
HttpClient httpClient = new HttpClient();
|
||||
private SingleUdpClient udpClientForHttp;
|
||||
public UdpClientManager manager = new UdpClientManager();
|
||||
|
||||
private List<JobInfoDTO> allProgramsCache = new List<JobInfoDTO>();
|
||||
public RobotProgram CurrentProgram { get; private set; }
|
||||
|
||||
public ProgramModel(string hostip, int tcpPort)
|
||||
private RobotData robotData;
|
||||
|
||||
private readonly object lockObject = new object();
|
||||
private bool hasNewData;
|
||||
|
||||
public bool isUdpLoopRunning = false;
|
||||
|
||||
public ProgramModel(string hostip, int tcpPort, int udpPort)
|
||||
{
|
||||
baseUrl = $"http://{hostip}:{tcpPort}";
|
||||
tcpBaseUrl = $"http://{hostip}:{tcpPort}";
|
||||
udpBaseUrl = $"http://{hostip}:{udpPort}";
|
||||
udpClientForHttp = manager.AddClient("Udp-client2", hostip, udpPort);
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
await LoadAllPrograms();
|
||||
hasNewData = false;
|
||||
isUdpLoopRunning = true;
|
||||
return;
|
||||
}
|
||||
|
||||
public bool IsNewDataAvailable()
|
||||
{
|
||||
lock (lockObject)
|
||||
{
|
||||
return hasNewData;
|
||||
}
|
||||
}
|
||||
|
||||
public RobotData GetLatestRobotData()
|
||||
{
|
||||
lock (lockObject)
|
||||
{
|
||||
hasNewData = false; // 데이터를 읽었으므로 플래그를 내림
|
||||
return robotData; // (데이터 복사본을 반환하는 것이 더 안전할 수 있음)
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> CheckProgramExists(string jobProgramName)
|
||||
{
|
||||
string requestUri = $"{baseUrl}/file_manager/file_exist?pathname=project/jobs/{jobProgramName}";
|
||||
string requestUri = $"{tcpBaseUrl}/file_manager/file_exist?pathname=project/jobs/{jobProgramName}";
|
||||
HttpResponseMessage result = await httpClient.GetAsync(requestUri);
|
||||
string jsonResponse = await result.Content.ReadAsStringAsync();
|
||||
|
||||
@@ -82,7 +114,7 @@ public class ProgramModel : IProgramModel
|
||||
string jsonString = JsonConvert.SerializeObject(newJob);
|
||||
HttpContent jsonPayload = new StringContent(jsonString, Encoding.UTF8, "application/json");
|
||||
|
||||
string requestUri = $"{baseUrl}/project/jobs/create_job";
|
||||
string requestUri = $"{tcpBaseUrl}/project/jobs/create_job";
|
||||
try
|
||||
{
|
||||
HttpResponseMessage result = await httpClient.PostAsync(requestUri, jsonPayload);
|
||||
@@ -103,7 +135,7 @@ public class ProgramModel : IProgramModel
|
||||
|
||||
private async Task<string> GetRobotModelNameAsync()
|
||||
{
|
||||
string requestUri = $"{baseUrl}/project/rgen";
|
||||
string requestUri = $"{tcpBaseUrl}/project/rgen";
|
||||
|
||||
HttpResponseMessage result = await httpClient.GetAsync(requestUri);
|
||||
string jsonResponse = await result.Content.ReadAsStringAsync();
|
||||
@@ -122,7 +154,7 @@ public class ProgramModel : IProgramModel
|
||||
|
||||
public async Task<bool> GetRobotMotorStateAsync()
|
||||
{
|
||||
string requestUri = $"{baseUrl}/project/rgen";
|
||||
string requestUri = $"{tcpBaseUrl}/project/rgen";
|
||||
|
||||
HttpResponseMessage result = await httpClient.GetAsync(requestUri);
|
||||
string jsonResponse = await result.Content.ReadAsStringAsync();
|
||||
@@ -140,9 +172,38 @@ public class ProgramModel : IProgramModel
|
||||
}
|
||||
}
|
||||
|
||||
public async Task GetTCPAsync()
|
||||
{
|
||||
while (isUdpLoopRunning)
|
||||
{
|
||||
try
|
||||
{
|
||||
//string requestUri = $"{udpBaseUrl}/project/robot/po_cur";
|
||||
string requestUri = $"{tcpBaseUrl}/project/robot/po_cur";
|
||||
|
||||
HttpResponseMessage result = await httpClient.GetAsync(requestUri);
|
||||
string jsonResponse = await result.Content.ReadAsStringAsync();
|
||||
|
||||
var tempRobotData = JsonConvert.DeserializeObject<RobotData>(jsonResponse, new JsonSerializerSettings { CheckAdditionalContent = false });
|
||||
|
||||
lock (lockObject)
|
||||
{
|
||||
robotData = tempRobotData;
|
||||
hasNewData = true;
|
||||
}
|
||||
await Task.Delay(50);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.Log(e);
|
||||
await Task.Delay(1000); // 에러 시 더 긴 대기
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> LoadProgram(string programId)
|
||||
{
|
||||
string requestUri = $"{baseUrl}/file_manager/files?pathname=project/jobs/{programId}&common";
|
||||
string requestUri = $"{tcpBaseUrl}/file_manager/files?pathname=project/jobs/{programId}&common";
|
||||
|
||||
HttpResponseMessage result = await httpClient.GetAsync(requestUri);
|
||||
string rawTextContent = await result.Content.ReadAsStringAsync();
|
||||
@@ -153,7 +214,7 @@ public class ProgramModel : IProgramModel
|
||||
}
|
||||
|
||||
CurrentProgram = new RobotProgram(programId, rawTextContent);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -165,7 +226,7 @@ public class ProgramModel : IProgramModel
|
||||
|
||||
try
|
||||
{
|
||||
HttpResponseMessage result = await httpClient.GetAsync($"{baseUrl}/project/jobs_info");
|
||||
HttpResponseMessage result = await httpClient.GetAsync($"{tcpBaseUrl}/project/jobs_info");
|
||||
jsonResponse = await result.Content.ReadAsStringAsync();
|
||||
|
||||
wrappedJson = $"{{\"jobs\":{jsonResponse}}}";
|
||||
@@ -199,4 +260,9 @@ public class ProgramModel : IProgramModel
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
isUdpLoopRunning = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user