2025-11-04 11:02:02 +09:00
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using NUnit.Framework.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using TMPro;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.SocialPlatforms;
|
|
|
|
|
using UVC.Core;
|
|
|
|
|
using UVC.Data;
|
|
|
|
|
using UVC.Data.Core;
|
|
|
|
|
using UVC.Data.Mqtt;
|
|
|
|
|
using UVC.Factory.Component;
|
|
|
|
|
using UVC.Pool;
|
|
|
|
|
|
|
|
|
|
namespace Simulator.Data.Transport
|
|
|
|
|
{
|
|
|
|
|
public enum AGVDataType
|
|
|
|
|
{
|
|
|
|
|
Moving,
|
|
|
|
|
Loading,
|
|
|
|
|
UnLoading
|
|
|
|
|
}
|
|
|
|
|
public class AGVManager : SingletonScene<AGVManager>
|
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<string, string> prefabPaths = new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{"agv","prefabs/agv"},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private GameObjectPool<AGV>? agvPool;
|
|
|
|
|
public GameObjectPool<AGV> AGVPool
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (agvPool == null)
|
|
|
|
|
{
|
|
|
|
|
//Debug.LogError("Pool is not initialized. Please call InitializePoolAsync first.");
|
|
|
|
|
}
|
|
|
|
|
return agvPool!;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private DataMapper agvMoveMapper;
|
|
|
|
|
private DataMapper agvLoadMapper;
|
|
|
|
|
private DataMapper agvUnLoadMapper;
|
|
|
|
|
|
|
|
|
|
protected override void Init()
|
|
|
|
|
{
|
|
|
|
|
InitializeAGVPoolAsync().ContinueWith(() =>
|
|
|
|
|
{
|
|
|
|
|
var agvMoveDataMask = new DataMask();
|
|
|
|
|
agvMoveDataMask.ObjectName = "agv"; // AGV 객체의 이름을 설정합니다.
|
|
|
|
|
agvMoveDataMask.ObjectIdKey = "component_id"; // AGV의 고유 식별자로 사용할 키를 설정합니다.
|
|
|
|
|
agvMoveDataMask["component_id"] = "";
|
|
|
|
|
agvMoveDataMask["event_name"] = "";
|
|
|
|
|
agvMoveDataMask["timestamp"] = new DateTime();
|
|
|
|
|
agvMoveDataMask["data"] = new DataMask()
|
|
|
|
|
{
|
|
|
|
|
["from_node"] = "",
|
|
|
|
|
["to_node"] = "",
|
|
|
|
|
["timing"] = new DataMask()
|
|
|
|
|
{
|
|
|
|
|
["duration"] = new DataMask()
|
|
|
|
|
{
|
|
|
|
|
["real_seconds"] = 0.0f
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
agvMoveMapper = new DataMapper(agvMoveDataMask);
|
|
|
|
|
|
|
|
|
|
var agvLoadDataMask = new DataMask();
|
|
|
|
|
agvLoadDataMask.ObjectName = "agv"; // AGV 객체의 이름을 설정합니다.
|
|
|
|
|
agvLoadDataMask.ObjectIdKey = "component_id"; // AGV의 고유 식별자로 사용할 키를 설정합니다.
|
|
|
|
|
agvLoadDataMask["component_id"] = "";
|
|
|
|
|
agvLoadDataMask["event_name"] = "";
|
|
|
|
|
agvLoadDataMask["timestamp"] = new DateTime();
|
|
|
|
|
agvLoadDataMask["data"] = new DataMask()
|
|
|
|
|
{
|
|
|
|
|
["from_node"] = "",
|
|
|
|
|
["to_node"] = "",
|
|
|
|
|
["entity_ids"] = new List<string>(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
agvLoadMapper = new DataMapper(agvLoadDataMask);
|
|
|
|
|
|
|
|
|
|
var agvUnLoadDataMask = new DataMask();
|
|
|
|
|
agvUnLoadDataMask.ObjectName = "agv"; // AGV 객체의 이름을 설정합니다.
|
|
|
|
|
agvUnLoadDataMask.ObjectIdKey = "component_id"; // AGV의 고유 식별자로 사용할 키를 설정합니다.
|
|
|
|
|
agvUnLoadDataMask["component_id"] = "";
|
|
|
|
|
agvUnLoadDataMask["event_name"] = "";
|
|
|
|
|
agvUnLoadDataMask["timestamp"] = new DateTime();
|
|
|
|
|
agvUnLoadDataMask["data"] = new DataMask()
|
|
|
|
|
{
|
|
|
|
|
["component"]="",
|
|
|
|
|
["entity_ids"] = new List<string>(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
agvUnLoadMapper = new DataMapper(agvUnLoadDataMask);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-11 09:42:47 +09:00
|
|
|
public void SpawnAGV(List<AGVData> datas)
|
2025-11-04 11:02:02 +09:00
|
|
|
{
|
|
|
|
|
foreach (var data in datas)
|
|
|
|
|
{
|
|
|
|
|
var agv = agvPool.GetItem($"{data.name}");
|
|
|
|
|
agv.data = data;
|
|
|
|
|
agv.UpdatePosition(AGVNodeManager.Instance.GetNodePosition(data.initial_node));
|
2025-11-11 09:42:47 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InitAGV(List<AGVData> datas)
|
|
|
|
|
{
|
|
|
|
|
foreach (var data in datas)
|
|
|
|
|
{
|
2025-11-04 11:02:02 +09:00
|
|
|
DataRepository.Instance.MqttReceiver.AddTopic($"simulation/{SimulationConfig.SimulationCode}/components/+/{data.name}/+/moving");
|
|
|
|
|
var mqttConfigAGVMoving = new MqttSubscriptionConfig($"simulation/{SimulationConfig.SimulationCode}/components/+/{data.name}/+/moving");
|
|
|
|
|
mqttConfigAGVMoving.SetDataMapper(agvMoveMapper);
|
|
|
|
|
mqttConfigAGVMoving.SetHandler((value) => OnUpdateData(value, AGVDataType.Moving));
|
|
|
|
|
DataRepository.Instance.MqttReceiver.Add(mqttConfigAGVMoving);
|
|
|
|
|
DataRepository.Instance.MqttReceiver.AddTopic($"simulation/{SimulationConfig.SimulationCode}/components/+/{data.name}/+/loading_end");
|
|
|
|
|
var mqttConfigAGVLoading = new MqttSubscriptionConfig($"simulation/{SimulationConfig.SimulationCode}/components/+/{data.name}/+/loading_end");
|
|
|
|
|
mqttConfigAGVLoading.SetDataMapper(agvLoadMapper);
|
|
|
|
|
mqttConfigAGVLoading.SetHandler((value) => OnUpdateData(value, AGVDataType.Loading));
|
|
|
|
|
DataRepository.Instance.MqttReceiver.Add(mqttConfigAGVLoading);
|
|
|
|
|
DataRepository.Instance.MqttReceiver.AddTopic($"simulation/{SimulationConfig.SimulationCode}/components/+/{data.name}/+/unloading_end");
|
|
|
|
|
var mqttConfigAGVUnLoading = new MqttSubscriptionConfig($"simulation/{SimulationConfig.SimulationCode}/components/+/{data.name}/+/unloading_end");
|
|
|
|
|
mqttConfigAGVUnLoading.SetDataMapper(agvUnLoadMapper);
|
|
|
|
|
mqttConfigAGVUnLoading.SetHandler((value) => OnUpdateData(value, AGVDataType.UnLoading));
|
|
|
|
|
DataRepository.Instance.MqttReceiver.Add(mqttConfigAGVUnLoading);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnUpdateData(IDataObject data, AGVDataType type)
|
|
|
|
|
{
|
|
|
|
|
if (data == null) return;
|
|
|
|
|
|
|
|
|
|
DataObject? obj = data as DataObject;
|
|
|
|
|
if (obj == null) return;
|
|
|
|
|
AGV? agv = agvPool.GetItem(obj.GetString("component_id")!);
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case AGVDataType.Moving:
|
|
|
|
|
agv.SetTargetPosition(obj);
|
|
|
|
|
break;
|
|
|
|
|
case AGVDataType.Loading:
|
|
|
|
|
agv.LoadEntity(obj);
|
|
|
|
|
break;
|
|
|
|
|
case AGVDataType.UnLoading:
|
|
|
|
|
//Debug.Log("unload");
|
|
|
|
|
//agv.UnLoadEntity(obj);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async UniTask InitializeAGVPoolAsync()
|
|
|
|
|
{
|
|
|
|
|
if (agvPool != null) return;
|
|
|
|
|
|
|
|
|
|
var prefab = await Resources.LoadAsync<GameObject>(prefabPaths["agv"]) as GameObject;
|
|
|
|
|
if (prefab == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"Prefab not found at path: {prefabPaths["agv"]}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
agvPool = new GameObjectPool<AGV>(prefab, transform);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|