Files
HDRobotics/Assets/Scripts/AppManager.cs

145 lines
4.7 KiB
C#
Raw Normal View History

2025-10-24 11:55:43 +09:00
using System.Collections;
using System.Collections.Generic;
using System.IO;
2025-10-30 09:31:05 +09:00
using System.Threading;
using UnityEngine;
public class AppManager : MonoBehaviour
{
[SerializeField] private ProgramView view;
2025-10-24 14:36:33 +09:00
[SerializeField] private TCPView tcpView;
[SerializeField] private RobotController robotController;
2025-10-30 09:31:05 +09:00
[SerializeField] private InteractionView interactionView;
[SerializeField] private PointManagerView pointManagerView;
[SerializeField] private PathLineView pathLineView;
[SerializeField] private PopupView popupView;
2025-10-24 11:55:43 +09:00
[SerializeField] private float motorStatePollInterval = 1.0f;
2025-10-30 09:31:05 +09:00
private ProgramPresenter presenter;
private string hostip;
private int tcpPort;
private int udpPort;
private string configFileName = "config.cfg";
2025-10-30 09:31:05 +09:00
private CancellationToken cancellationToken;
async void Start()
{
LoadConfig();
2025-10-24 14:36:33 +09:00
ProgramModel model = new ProgramModel(hostip, tcpPort, udpPort);
await model.InitializeAsync();
2025-10-30 09:31:05 +09:00
_ = model.GetTCPAsync(cancellationToken);
2025-10-30 09:31:05 +09:00
presenter = new ProgramPresenter(model, view, tcpView, interactionView, pointManagerView, popupView, pathLineView);
presenter.RegisterControlledRobot(robotController);
2025-10-24 11:55:43 +09:00
await presenter.UpdateMotorStateAsync();
view.DisplayProgram(null);
2025-10-24 11:55:43 +09:00
StartCoroutine(PollMotorStateCoroutine());
}
private void LoadConfig()
{
// <20><EFBFBD><E2BABB> <20><><EFBFBD><EFBFBD> (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> ã<><C3A3> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>)
string defaultIp = "127.0.0.1";
int defaultPort = 8888;
string path = Path.Combine(Application.streamingAssetsPath, configFileName);
if (File.Exists(path))
{
try
{
var config = new Dictionary<string, string>();
string[] lines = File.ReadAllLines(path);
foreach (string line in lines)
{
if (string.IsNullOrWhiteSpace(line) || line.Trim().StartsWith("#"))
continue;
string[] parts = line.Split('=');
if (parts.Length == 2)
{
config[parts[0].Trim()] = parts[1].Trim();
}
}
if (config.ContainsKey("IP_ADDRESS"))
{
hostip = config["IP_ADDRESS"];
}
else
{
hostip = defaultIp;
Debug.LogWarning($"config <20><><EFBFBD>Ͽ<EFBFBD> IP_ADDRESS Ű<><C5B0> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>. <20><EFBFBD><E2BABB>({defaultIp}) <20><><EFBFBD><EFBFBD>.");
}
if (config.ContainsKey("TCP_PORT"))
{
if (int.TryParse(config["TCP_PORT"], out int parsedPort))
{
tcpPort = parsedPort;
}
else
{
tcpPort = defaultPort;
Debug.LogWarning($"config <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TCP_PORT <20><><EFBFBD><EFBFBD> <20>߸<EFBFBD><DFB8>Ǿ<EFBFBD><C7BE><EFBFBD><EFBFBD>ϴ<EFBFBD>. <20><EFBFBD><E2BABB>({defaultPort}) <20><><EFBFBD><EFBFBD>.");
}
}
else
{
tcpPort = defaultPort;
Debug.LogWarning($"config <20><><EFBFBD>Ͽ<EFBFBD> TCP_PORT Ű<><C5B0> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>. <20><EFBFBD><E2BABB>({defaultPort}) <20><><EFBFBD><EFBFBD>.");
}
if (config.ContainsKey("UDP_PORT"))
{
if (int.TryParse(config["UDP_PORT"], out int parsedPort))
{
udpPort = parsedPort;
}
else
{
udpPort = defaultPort;
Debug.LogWarning($"config <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UDP_PORT <20><><EFBFBD><EFBFBD> <20>߸<EFBFBD><DFB8>Ǿ<EFBFBD><C7BE><EFBFBD><EFBFBD>ϴ<EFBFBD>. <20><EFBFBD><E2BABB>({defaultPort}) <20><><EFBFBD><EFBFBD>.");
}
}
else
{
udpPort = defaultPort;
Debug.LogWarning($"config <20><><EFBFBD>Ͽ<EFBFBD> UDP_PORT Ű<><C5B0> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>. <20><EFBFBD><E2BABB>({defaultPort}) <20><><EFBFBD><EFBFBD>.");
}
Debug.Log($"Config <20>ε<EFBFBD> <20><><EFBFBD><EFBFBD>: {hostip}:{tcpPort}/{udpPort}");
}
catch (System.Exception e)
{
Debug.LogError($"Config <20><><EFBFBD><EFBFBD> <20>ε<EFBFBD> <20><> <20><><EFBFBD><EFBFBD> <20>߻<EFBFBD>: {e.Message}. <20><EFBFBD><E2BABB> <20><><EFBFBD><EFBFBD>.");
hostip = defaultIp;
tcpPort = defaultPort;
udpPort = defaultPort;
}
}
else
{
Debug.LogWarning($"{configFileName} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ã<><C3A3> <20><> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>. <20><EFBFBD><E2BABB>({defaultIp}:{defaultPort}) <20><><EFBFBD><EFBFBD>.");
hostip = defaultIp;
tcpPort = defaultPort;
udpPort = defaultPort;
}
}
2025-10-24 11:55:43 +09:00
private IEnumerator PollMotorStateCoroutine()
{
while (true)
{
yield return new WaitForSeconds(motorStatePollInterval);
_ = presenter.UpdateMotorStateAsync();
}
}
}