2025-10-24 11:55:43 +09:00
|
|
|
|
using System.Collections;
|
2025-10-23 18:43:38 +09:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2025-10-30 09:31:05 +09:00
|
|
|
|
using System.Threading;
|
2025-10-14 16:38:35 +09:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
2025-10-23 18:43:38 +09:00
|
|
|
|
|
2025-10-14 16:38:35 +09:00
|
|
|
|
public class AppManager : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
[SerializeField] private ProgramView view;
|
2025-10-24 14:36:33 +09:00
|
|
|
|
[SerializeField] private TCPView tcpView;
|
2025-10-15 10:39:44 +09:00
|
|
|
|
[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;
|
2025-10-23 18:43:38 +09:00
|
|
|
|
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;
|
2025-10-14 16:38:35 +09:00
|
|
|
|
|
2025-10-23 18:43:38 +09:00
|
|
|
|
async void Start()
|
2025-10-14 16:38:35 +09:00
|
|
|
|
{
|
2025-10-23 18:43:38 +09:00
|
|
|
|
LoadConfig();
|
2025-10-14 16:38:35 +09:00
|
|
|
|
|
2025-10-24 14:36:33 +09:00
|
|
|
|
ProgramModel model = new ProgramModel(hostip, tcpPort, udpPort);
|
2025-10-23 18:43:38 +09:00
|
|
|
|
await model.InitializeAsync();
|
2025-10-30 09:31:05 +09:00
|
|
|
|
_ = model.GetTCPAsync(cancellationToken);
|
2025-10-23 18:43:38 +09:00
|
|
|
|
|
2025-10-30 09:31:05 +09:00
|
|
|
|
presenter = new ProgramPresenter(model, view, tcpView, interactionView, pointManagerView, popupView, pathLineView);
|
2025-10-15 10:39:44 +09:00
|
|
|
|
presenter.RegisterControlledRobot(robotController);
|
|
|
|
|
|
|
2025-10-24 11:55:43 +09:00
|
|
|
|
await presenter.UpdateMotorStateAsync();
|
2025-10-14 16:38:35 +09:00
|
|
|
|
view.DisplayProgram(null);
|
2025-10-24 11:55:43 +09:00
|
|
|
|
StartCoroutine(PollMotorStateCoroutine());
|
2025-10-14 16:38:35 +09:00
|
|
|
|
}
|
2025-10-23 18:43:38 +09:00
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-14 16:38:35 +09:00
|
|
|
|
}
|