145 lines
4.7 KiB
C#
145 lines
4.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using UnityEngine;
|
|
|
|
|
|
public class AppManager : MonoBehaviour
|
|
{
|
|
[SerializeField] private ProgramView view;
|
|
[SerializeField] private TCPView tcpView;
|
|
[SerializeField] private RobotController robotController;
|
|
|
|
[SerializeField] private InteractionView interactionView;
|
|
[SerializeField] private PointManagerView pointManagerView;
|
|
[SerializeField] private PathLineView pathLineView;
|
|
[SerializeField] private PopupView popupView;
|
|
|
|
[SerializeField] private float motorStatePollInterval = 1.0f;
|
|
private ProgramPresenter presenter;
|
|
private string hostip;
|
|
private int tcpPort;
|
|
private int udpPort;
|
|
private string configFileName = "config.cfg";
|
|
private CancellationToken cancellationToken;
|
|
|
|
async void Start()
|
|
{
|
|
LoadConfig();
|
|
|
|
ProgramModel model = new ProgramModel(hostip, tcpPort, udpPort);
|
|
await model.InitializeAsync();
|
|
_ = model.GetTCPAsync(cancellationToken);
|
|
|
|
presenter = new ProgramPresenter(model, view, tcpView, interactionView, pointManagerView, popupView, pathLineView);
|
|
presenter.RegisterControlledRobot(robotController);
|
|
|
|
await presenter.UpdateMotorStateAsync();
|
|
view.DisplayProgram(null);
|
|
StartCoroutine(PollMotorStateCoroutine());
|
|
}
|
|
|
|
private void LoadConfig()
|
|
{
|
|
// 기본값 설정 (파일을 못 찾을 경우 대비)
|
|
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 파일에 IP_ADDRESS 키가 없습니다. 기본값({defaultIp}) 사용.");
|
|
}
|
|
|
|
if (config.ContainsKey("TCP_PORT"))
|
|
{
|
|
if (int.TryParse(config["TCP_PORT"], out int parsedPort))
|
|
{
|
|
tcpPort = parsedPort;
|
|
}
|
|
else
|
|
{
|
|
tcpPort = defaultPort;
|
|
Debug.LogWarning($"config 파일의 TCP_PORT 값이 잘못되었습니다. 기본값({defaultPort}) 사용.");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
tcpPort = defaultPort;
|
|
Debug.LogWarning($"config 파일에 TCP_PORT 키가 없습니다. 기본값({defaultPort}) 사용.");
|
|
}
|
|
|
|
if (config.ContainsKey("UDP_PORT"))
|
|
{
|
|
if (int.TryParse(config["UDP_PORT"], out int parsedPort))
|
|
{
|
|
udpPort = parsedPort;
|
|
}
|
|
else
|
|
{
|
|
udpPort = defaultPort;
|
|
Debug.LogWarning($"config 파일의 UDP_PORT 값이 잘못되었습니다. 기본값({defaultPort}) 사용.");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
udpPort = defaultPort;
|
|
Debug.LogWarning($"config 파일에 UDP_PORT 키가 없습니다. 기본값({defaultPort}) 사용.");
|
|
}
|
|
|
|
Debug.Log($"Config 로드 성공: {hostip}:{tcpPort}/{udpPort}");
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.LogError($"Config 파일 로드 중 오류 발생: {e.Message}. 기본값 사용.");
|
|
hostip = defaultIp;
|
|
tcpPort = defaultPort;
|
|
udpPort = defaultPort;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"{configFileName} 파일을 찾을 수 없습니다. 기본값({defaultIp}:{defaultPort}) 사용.");
|
|
hostip = defaultIp;
|
|
tcpPort = defaultPort;
|
|
udpPort = defaultPort;
|
|
}
|
|
}
|
|
|
|
private IEnumerator PollMotorStateCoroutine()
|
|
{
|
|
while (true)
|
|
{
|
|
yield return new WaitForSeconds(motorStatePollInterval);
|
|
|
|
_ = presenter.UpdateMotorStateAsync();
|
|
}
|
|
}
|
|
}
|