Files
XRLib/Assets/Scripts/Simulator/Components/ComponentPoolManager.cs
2026-02-25 16:30:12 +09:00

308 lines
13 KiB
C#

using Cysharp.Threading.Tasks;
using System;
using System.Collections.Generic;
using UnityEngine;
using UVC.Data;
using UVC.Data.Core;
using UVC.Data.Mqtt;
using UVC.Pool;
namespace Simulator.Data
{
public class ComponentPoolManager
{
private readonly Transform _parentTransform;
private readonly Dictionary<string, string> _prefabPaths = new Dictionary<string, string>()
{
{"source","prefabs/pallet"},
{"queue","prefabs/queue"},
{"rack","prefabs/rack"},
{"sink","prefabs/Sink_Container"},
{"asrs","prefabs/ASRS"},
{"robotArm","prefabs/RobotArm"},
{"processor","prefabs/Processor"},
{"worker","prefabs/Worker"}
};
private GameObjectPool<SourceComponent> _sourcePool;
private GameObjectPool<SinkComponent> _sinkPool;
private GameObjectPool<QueueComponent> _queuePool;
private GameObjectPool<RackComponent> _rackPool;
private GameObjectPool<ASRSComponent> _asrsPool;
private GameObjectPool<RobotArmComponent> _robotArmPool;
private GameObjectPool<ProcessorComponent> _processorPool;
private GameObjectPool<WorkerComponent> _workerPool;
private readonly Dictionary<ComponentType, DataMapper> _dataMapperDict = new Dictionary<ComponentType, DataMapper>();
public IReadOnlyDictionary<ComponentType, DataMapper> DataMappers => _dataMapperDict;
public ComponentPoolManager(Transform parentTransform)
{
_parentTransform = parentTransform;
}
public void InitializeAllPools()
{
InitializeSourcePoolAsync().ContinueWith(() =>
{
var mask = new DataMask();
mask.ObjectName = "Source";
mask.ObjectIdKey = "component_id";
mask["component_id"] = "";
mask["event_name"] = "";
mask["timestamp"] = new DateTime();
mask["data"] = new DataMask()
{
["entity_ids"] = new List<string>(),
["total_entity"] = 0
};
_dataMapperDict.Add(ComponentType.Source, new DataMapper(mask));
});
InitializeSinkPoolAsync().ContinueWith(() =>
{
var mask = new DataMask();
mask.ObjectName = "Sink";
mask.ObjectIdKey = "component_id";
mask["component_id"] = "";
mask["event_name"] = "";
mask["timestamp"] = new DateTime();
mask["data"] = new DataMask()
{
["entity_ids"] = new List<string>(),
};
_dataMapperDict.Add(ComponentType.Sink, new DataMapper(mask));
});
InitializeQueuePoolAsync().ContinueWith(() =>
{
_dataMapperDict.Add(ComponentType.Queue, null);
});
InitializeRackPoolAsync().ContinueWith(() =>
{
var mask = new DataMask();
mask.ObjectName = "rack";
mask.ObjectIdKey = "component_id";
_dataMapperDict.Add(ComponentType.Rack, new DataMapper(mask));
});
InitializeAsrsPoolAsync().ContinueWith(() =>
{
var mask = new DataMask();
mask.ObjectName = "asrs";
mask.ObjectIdKey = "component_id";
_dataMapperDict.Add(ComponentType.ASRS, new DataMapper(mask));
});
InitializeRobotArmPoolAsync().ContinueWith(() =>
{
var mask = new DataMask();
mask.ObjectName = "RobotArm";
mask.ObjectIdKey = "component_id";
mask["component_id"] = "";
mask["event_name"] = "";
mask["timestamp"] = new DateTime();
mask["data"] = new DataMask()
{
["entity_id"] = "",
["processing_time"] = 0.0f,
["from"] = "",
["to"] = ""
};
_dataMapperDict.Add(ComponentType.RobotArm, new DataMapper(mask));
});
InitializeProcessorPoolAsync().ContinueWith(() =>
{
_dataMapperDict.Add(ComponentType.Processor, new DataMapper(new DataMask()));
});
InitializeWorkerPoolAsync().ContinueWith(() =>
{
_dataMapperDict.Add(ComponentType.Worker, new DataMapper(new DataMask()));
});
}
#region Pool Initialization
private async UniTask InitializeSourcePoolAsync()
{
if (_sourcePool != null) return;
var prefab = await Resources.LoadAsync<GameObject>(_prefabPaths["source"]) as GameObject;
if (prefab == null) { Debug.LogError($"Prefab not found at path: {_prefabPaths["source"]}"); return; }
_sourcePool = new GameObjectPool<SourceComponent>(prefab, _parentTransform);
}
private async UniTask InitializeSinkPoolAsync()
{
if (_sinkPool != null) return;
var prefab = await Resources.LoadAsync<GameObject>(_prefabPaths["sink"]) as GameObject;
if (prefab == null) { Debug.LogError($"Prefab not found at path: {_prefabPaths["sink"]}"); return; }
_sinkPool = new GameObjectPool<SinkComponent>(prefab, _parentTransform);
}
private async UniTask InitializeQueuePoolAsync()
{
if (_queuePool != null) return;
var prefab = await Resources.LoadAsync<GameObject>(_prefabPaths["queue"]) as GameObject;
if (prefab == null) { Debug.LogError($"Prefab not found at path: {_prefabPaths["queue"]}"); return; }
_queuePool = new GameObjectPool<QueueComponent>(prefab, _parentTransform);
}
private async UniTask InitializeRackPoolAsync()
{
if (_rackPool != null) return;
var prefab = await Resources.LoadAsync<GameObject>(_prefabPaths["rack"]) as GameObject;
if (prefab == null) { Debug.LogError($"Prefab not found at path: {_prefabPaths["rack"]}"); return; }
_rackPool = new GameObjectPool<RackComponent>(prefab, _parentTransform);
}
private async UniTask InitializeAsrsPoolAsync()
{
if (_asrsPool != null) return;
var prefab = await Resources.LoadAsync<GameObject>(_prefabPaths["asrs"]) as GameObject;
if (prefab == null) { Debug.LogError($"Prefab not found at path: {_prefabPaths["asrs"]}"); return; }
_asrsPool = new GameObjectPool<ASRSComponent>(prefab, _parentTransform);
}
private async UniTask InitializeRobotArmPoolAsync()
{
if (_robotArmPool != null) return;
var prefab = await Resources.LoadAsync<GameObject>(_prefabPaths["robotArm"]) as GameObject;
if (prefab == null) { Debug.LogError($"Prefab not found at path: {_prefabPaths["robotArm"]}"); return; }
_robotArmPool = new GameObjectPool<RobotArmComponent>(prefab, _parentTransform);
}
private async UniTask InitializeProcessorPoolAsync()
{
if (_processorPool != null) return;
var prefab = await Resources.LoadAsync<GameObject>(_prefabPaths["processor"]) as GameObject;
if (prefab == null) { Debug.LogError($"Prefab not found at path: {_prefabPaths["processor"]}"); return; }
_processorPool = new GameObjectPool<ProcessorComponent>(prefab, _parentTransform);
}
private async UniTask InitializeWorkerPoolAsync()
{
if (_workerPool != null) return;
var prefab = await Resources.LoadAsync<GameObject>(_prefabPaths["worker"]) as GameObject;
if (prefab == null) { Debug.LogError($"Prefab not found at path: {_prefabPaths["worker"]}"); return; }
_workerPool = new GameObjectPool<WorkerComponent>(prefab, _parentTransform);
}
#endregion
public Dictionary<string, ComponentBase> SpawnComponents(LogicDetailData data)
{
var registry = new Dictionary<string, ComponentBase>();
if (data.production_system != null)
{
if (data.production_system.sources != null)
{
foreach (var component in data.production_system.sources)
{
var source = _sourcePool.GetItem($"{component.name}");
source.componentType = ComponentType.Source;
source.SetComponent(component);
registry.Add(component.name, source);
}
}
if (data.production_system.sinks != null)
{
foreach (var component in data.production_system.sinks)
{
var sink = _sinkPool.GetItem($"{component.name}");
sink.componentType = ComponentType.Sink;
sink.SetComponent(component);
registry.Add(component.name, sink);
}
}
if (data.production_system.racks != null)
{
foreach (var component in data.production_system.racks)
{
var rack = _rackPool.GetItem($"{component.name}");
rack.componentType = ComponentType.Rack;
rack.SetComponent(component);
registry.Add(component.name, rack);
}
}
if (data.production_system.asrs != null)
{
foreach (var component in data.production_system.asrs)
{
var asrs = _asrsPool.GetItem($"{component.name}");
asrs.componentType = ComponentType.ASRS;
asrs.SetComponent(component);
registry.Add(component.name, asrs);
}
}
if (data.production_system.robot_arms != null)
{
foreach (var component in data.production_system.robot_arms)
{
var robotArm = _robotArmPool.GetItem($"{component.name}");
robotArm.componentType = ComponentType.RobotArm;
robotArm.SetComponent(component);
registry.Add(component.name, robotArm);
}
}
if (data.production_system.processors != null)
{
foreach (var component in data.production_system.processors)
{
var processor = _processorPool.GetItem($"{component.name}");
processor.componentType = ComponentType.Processor;
processor.SetComponent(component);
registry.Add(component.name, processor);
}
}
}
if (data.infrastructure != null)
{
if (data.infrastructure.queues != null)
{
foreach (var component in data.infrastructure.queues)
{
var queue = _queuePool.GetItem($"{component.name}");
queue.componentType = ComponentType.Queue;
queue.SetComponent(component);
registry.Add(component.name, queue);
}
}
if (data.infrastructure.resources != null)
{
foreach (var component in data.infrastructure.resources)
{
var worker = _workerPool.GetItem($"{component.name}");
worker.componentType = ComponentType.Worker;
worker.SetComponent(component);
registry.Add(component.name, worker);
}
}
}
return registry;
}
public ComponentBase FindActiveItem(ComponentType type, string componentId)
{
switch (type)
{
case ComponentType.Source: return _sourcePool?.FindActiveItem(componentId);
case ComponentType.Sink: return _sinkPool?.FindActiveItem(componentId);
case ComponentType.Queue: return _queuePool?.FindActiveItem(componentId);
case ComponentType.Rack: return _rackPool?.FindActiveItem(componentId);
case ComponentType.ASRS: return _asrsPool?.FindActiveItem(componentId);
case ComponentType.RobotArm: return _robotArmPool?.FindActiveItem(componentId);
case ComponentType.Processor:return _processorPool?.FindActiveItem(componentId);
case ComponentType.Worker: return _workerPool?.FindActiveItem(componentId);
default: return null;
}
}
}
}