<feat> 모터 상태 api통신
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
@@ -7,6 +8,8 @@ public class AppManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private ProgramView view;
|
||||
[SerializeField] private RobotController robotController;
|
||||
[SerializeField] private float motorStatePollInterval = 1.0f;
|
||||
ProgramPresenter presenter;
|
||||
private string hostip;
|
||||
private int tcpPort;
|
||||
private int udpPort;
|
||||
@@ -19,10 +22,14 @@ public class AppManager : MonoBehaviour
|
||||
ProgramModel model = new ProgramModel(hostip, tcpPort);
|
||||
await model.InitializeAsync();
|
||||
|
||||
ProgramPresenter presenter = new ProgramPresenter(model, view);
|
||||
presenter = new ProgramPresenter(model, view);
|
||||
presenter.RegisterControlledRobot(robotController);
|
||||
|
||||
await presenter.UpdateMotorStateAsync();
|
||||
|
||||
view.DisplayProgram(null);
|
||||
|
||||
StartCoroutine(PollMotorStateCoroutine());
|
||||
}
|
||||
|
||||
private void LoadConfig()
|
||||
@@ -116,4 +123,14 @@ public class AppManager : MonoBehaviour
|
||||
udpPort = defaultPort;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator PollMotorStateCoroutine()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
yield return new WaitForSeconds(motorStatePollInterval);
|
||||
|
||||
_ = presenter.UpdateMotorStateAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +120,26 @@ public class ProgramModel : IProgramModel
|
||||
return modelName;
|
||||
}
|
||||
|
||||
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("로봇 상태 API 응답에서 모터 상태를 찾을 수 없습니다.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> LoadProgram(string programId)
|
||||
{
|
||||
string requestUri = $"{baseUrl}/file_manager/files?pathname=project/jobs/{programId}&common";
|
||||
@@ -145,7 +165,7 @@ public class ProgramModel : IProgramModel
|
||||
|
||||
try
|
||||
{
|
||||
HttpResponseMessage result = await httpClient.GetAsync(new Uri($"{baseUrl}/project/jobs_info"));
|
||||
HttpResponseMessage result = await httpClient.GetAsync($"{baseUrl}/project/jobs_info");
|
||||
jsonResponse = await result.Content.ReadAsStringAsync();
|
||||
|
||||
wrappedJson = $"{{\"jobs\":{jsonResponse}}}";
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.ARSubsystems;
|
||||
|
||||
public class ProgramPresenter
|
||||
{
|
||||
@@ -7,6 +9,7 @@ public class ProgramPresenter
|
||||
private IProgramView view;
|
||||
private RobotController controlledRobot;
|
||||
private string _programId;
|
||||
private bool lastKnownMotorState = false;
|
||||
|
||||
public ProgramPresenter(ProgramModel model, IProgramView view)
|
||||
{
|
||||
@@ -26,12 +29,29 @@ public class ProgramPresenter
|
||||
this.controlledRobot = robot;
|
||||
}
|
||||
|
||||
public async Task UpdateMotorStateAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
bool currentState = await model.GetRobotMotorStateAsync();
|
||||
|
||||
if (currentState != lastKnownMotorState)
|
||||
{
|
||||
controlledRobot.SetMotorState(currentState);
|
||||
lastKnownMotorState = currentState;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning($"모터 상태 업데이트 실패: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public void OnApplicationStart()
|
||||
{
|
||||
if (controlledRobot != null)
|
||||
{
|
||||
Debug.Log("로봇 모터를 ON 상태로 설정합니다.");
|
||||
controlledRobot.SetMotorState(true);
|
||||
Debug.Log("제어할 로봇이 등록되었습니다.");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -43,7 +63,7 @@ public class ProgramPresenter
|
||||
{
|
||||
if (await model.CreateNewProgram(programId))
|
||||
{
|
||||
view.DisplayProgram(model.CurrentProgram);
|
||||
view.DisplayProgram(programId);
|
||||
view.HideProgramSelectPanel();
|
||||
OnApplicationStart();
|
||||
}
|
||||
@@ -68,7 +88,7 @@ public class ProgramPresenter
|
||||
{
|
||||
if(_programId != null && await model.LoadProgram(_programId))
|
||||
{
|
||||
view.DisplayProgram(model.CurrentProgram);
|
||||
view.DisplayProgram(_programId);
|
||||
view.HideProgramSelectPanel();
|
||||
view.HideProgramList();
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ using TMPro;
|
||||
public interface IProgramView
|
||||
{
|
||||
void ShowMessage(string message);
|
||||
void DisplayProgram(RobotProgram program);
|
||||
void DisplayProgram(string programId);
|
||||
void ShowProgramList(List<string> programIds);
|
||||
void HideProgramList();
|
||||
void HideProgramSelectPanel();
|
||||
@@ -107,16 +107,16 @@ public class ProgramView : MonoBehaviour, IProgramView
|
||||
Debug.LogWarning(message);
|
||||
}
|
||||
|
||||
public void DisplayProgram(RobotProgram program)
|
||||
public void DisplayProgram(string programId)
|
||||
{
|
||||
if (program == null)
|
||||
if (programId == null)
|
||||
{
|
||||
//currentProgramIdText.text = "No Program Loaded";
|
||||
//endpointListText.text = "";
|
||||
Debug.Log("No Program Loaded");
|
||||
return;
|
||||
}
|
||||
Debug.Log($"연결된 프로그램: {program.ProgramId}.job");
|
||||
Debug.Log($"연결된 프로그램: {programId}.job");
|
||||
|
||||
//currentProgramIdText.text = "Current: " + program.programId;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user