Files
Simulation/Assets/Scripts/SimulationModels/SimulationModelConveyor.cs
2025-08-04 17:26:24 +09:00

637 lines
16 KiB
C#

using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using UnityEngine;
using Octopus.Simulator;
#pragma warning disable CS8632
#region class
[Serializable]
public class ConveyorUnloadingToStoreEventMessage
{
[JsonProperty("type")] public string? Type { get; set; }
[JsonProperty("_event")] public string? Event { get; set; }
[JsonProperty("component_type")] public string? ComponentType { get; set; }
[JsonProperty("component_id")] public string? ComponentId { get; set; }
[JsonProperty("simulation_time")]
public double? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorUnloadingToStoreData? Data { get; set; }
[JsonProperty("timestamp")]
public string? Timestamp { get; set; }
}
[Serializable]
public class ConveyorUnloadingToStoreData
{
[JsonProperty("product_id")]
public string? ProductId { get; set; }
[JsonProperty("store_name")]
public string? StoreName { get; set; }
[JsonProperty("store_length")]
public int? StoreLength { get; set; }
[JsonProperty("destination_type")]
public string? DestinationType { get; set; }
[JsonProperty("is_defective")]
public bool? IsDefective { get; set; }
[JsonProperty("items_count")]
public int? ItemsCount { get; set; }
[JsonProperty("remaining_capacity")]
public int? RemainingCapacity { get; set; }
[JsonProperty("transported_count")]
public int? TransportedCount { get; set; }
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("belt_speed")]
public double? BeltSpeed { get; set; } = 1;
[JsonProperty("belt_length")]
public double? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class ConveyorUnloadingToQueueEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("simulation_time")]
public double? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorUnloadingToQueueData? Data { get; set; }
[JsonProperty("timestamp")]
public string? Timestamp { get; set; }
}
[Serializable]
public class ConveyorUnloadingToQueueData
{
[JsonProperty("product_id")]
public string? ProductId { get; set; }
[JsonProperty("queue_name")]
public string? QueueName { get; set; }
[JsonProperty("queue_length")]
public int? QueueLength { get; set; }
[JsonProperty("destination_type")]
public string? DestinationType { get; set; }
[JsonProperty("is_defective")]
public bool? IsDefective { get; set; }
[JsonProperty("items_count")]
public int? ItemsCount { get; set; }
[JsonProperty("remaining_capacity")]
public int? RemainingCapacity { get; set; }
[JsonProperty("transported_count")]
public int? TransportedCount { get; set; }
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("belt_speed")]
public double? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public double? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class ConveyorUnloadingEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("component_label")]
public string? ComponentLabel { get; set; }
[JsonProperty("simulation_time")]
public float? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorUnloadingData? Data { get; set; }
}
[Serializable]
public class ConveyorUnloadingData
{
[JsonProperty("product_id")]
public string? ProductId { get; set; }
[JsonProperty("product_position")]
public float? ProductPosition { get; set; }
[JsonProperty("travel_time")]
public float? TravelTime { get; set; }
[JsonProperty("is_defective")]
public bool? IsDefective { get; set; }
[JsonProperty("destination_queue")]
public string? DestinationQueue { get; set; }
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("belt_speed")]
public float? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public float? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class ConveyorStartedEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("component_label")]
public string? ComponentLabel { get; set; }
[JsonProperty("simulation_time")]
public float? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorStartedData? Data { get; set; }
}
[Serializable]
public class ConveyorStartedData
{
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("move_time")]
public float? MoveTime { get; set; }
[JsonProperty("belt_speed")]
public float? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public float? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("min_gap")]
public float? MinGap { get; set; }
[JsonProperty("input_queues")]
public List<InputQueue>? InputQueues { get; set; }
[JsonProperty("input_stores")]
public List<InputStore>? InputStores { get; set; }
[JsonProperty("output_queue")]
public string? OutputQueue { get; set; }
[JsonProperty("defect_queue")]
public string? DefectQueue { get; set; }
[JsonProperty("required_resource")]
public string? RequiredResource { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class InputQueue
{
[JsonProperty("queue_name")]
public string? Queue { get; set; }
[JsonProperty("required_items")]
public int? RequiredItems { get; set; }
}
[Serializable]
public class InputStore
{
[JsonProperty("store_name")]
public string? Queue { get; set; }
[JsonProperty("required_items")]
public int? RequiredItems { get; set; }
}
[Serializable]
public class Conveyor_Loading_From_Queue
{
public string conveyor_id;
public string conveyor_label;
public int? belt_speed = 1;
public int? belt_length = 1;
public int? capacity = 100;
public int? items_on_belt = 20;
public int transported_count = 5;
public string product_id;
public string queue_name;
public int queue_remaining;
public int items_count;
public int remaining_capacity;
public float belt_load_percentage;
}
[Serializable]
public class Conveyor_Loading_From_Store
{
public string conveyor_id;
public string conveyor_label;
public int? belt_speed = 1;
public int? belt_length = 1;
public int? capacity = 100;
public int? items_on_belt = 20;
public int transported_count = 5;
public string product_id;
public string store_name;
public int store_remaining;
public int items_count;
public int remaining_capacity;
public float belt_load_percentage;
}
[Serializable]
public class ConveyorEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("component_label")]
public string? ComponentLabel { get; set; }
[JsonProperty("simulation_time")]
public float? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorEventData? Data { get; set; }
}
[Serializable]
public class ConveyorEventData
{
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("move_time")]
public int? MoveTime { get; set; }
[JsonProperty("belt_speed")]
public float? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public float? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("min_gap")]
public float? MinGap { get; set; }
[JsonProperty("input_queues")]
public List<InputQueue>? InputQueues { get; set; }
[JsonProperty("output_queue")]
public string? OutputQueue { get; set; }
[JsonProperty("defect_queue")]
public string? DefectQueue { get; set; }
[JsonProperty("required_resource")]
public string? RequiredResource { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class ConveyorMovingEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("component_label")]
public string? ComponentLabel { get; set; }
[JsonProperty("simulation_time")]
public float? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorMovingData? Data { get; set; }
}
[Serializable]
public class ConveyorMovingData
{
[JsonProperty("item_positions")]
public List<float>? ItemPositions { get; set; }
[JsonProperty("average_position")]
public float? AveragePosition { get; set; }
[JsonProperty("current_speed")]
public float? CurrentSpeed { get; set; }
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("belt_speed")]
public float? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public float? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class ConveyorStatisticsData
{
public StatisticsConveyor statistics;
}
[Serializable]
public class StatisticsConveyor
{
public int transported_count;
}
#endregion
public class SimulationModelConveyor : SimulationModel
{
Vector3 startPosition;
Vector3 endPosition;
public GameObject moveTargetCargo;
public GameObject storedCargo;
int transportTime;
float elapseTime;
string inputQueueID;
string inputStoreID;
// Start is called once before the first execution of Update after the MonoBehaviour is created
protected override void init()
{
base.init();
startPosition = transform.Find(nameof(startPosition)).transform.position;
endPosition = transform.Find(nameof(endPosition)).transform.position;
}
// Update is called once per frame
void Update()
{
if (!simulationUI.isplaying)
return;
if (moveTargetCargo == null)
return;
startPosition = transform.Find(nameof(startPosition)).transform.position;
endPosition = transform.Find(nameof(endPosition)).transform.position;
elapseTime += Time.deltaTime;
if (transportTime <= 0)
return;
if (elapseTime < transportTime)
{
moveTargetCargo.transform.position = Vector3.Lerp(startPosition, endPosition, elapseTime / transportTime);
}
else
{
elapseTime = 0f;
// 완료 후 행동이 필요하면 여기에 추가 (예: 다음 상태로 이동)
}
}
public override void GetData(string data)
{
var wrapclass = JsonConvert.DeserializeObject<SimulationDefaultJson>(data);
switch (wrapclass._event)
{
case "conveyor_started":
SetStartPosition(wrapclass.data.ToString());
break;
case "conveyor_loading_from_store":
LoadingCargo(wrapclass.data.ToString());
break;
case "conveyor_loading_from_queue":
LoadingCargo(wrapclass.data.ToString());
break;
case "conveyor_unloading_to_queue":
UnloadingCargo(data, true);
break;
case "conveyor_unloading_to_store":
UnloadingCargo(data, false);
break;
case "conveyor_statistics_update":
SetBubble(JsonConvert.DeserializeObject<ConveyorStatisticsData>(wrapclass.data.ToString()).statistics.transported_count);
break;
}
}
private void SetStartPosition(string data)
{
var startData = JsonConvert.DeserializeObject<ConveyorStartedData>(data);
if (startData == null)
return;
transportTime = (int)(startData.MoveTime ?? transportTime);
if (startData.InputQueues != null && startData.InputQueues.Count > 0)
{
inputQueueID = startData.InputQueues[0].Queue ?? string.Empty;
}
if (startData.InputStores != null && startData.InputStores.Count > 0)
{
inputStoreID = startData.InputStores[0].Queue ?? string.Empty;
}
}
private void LoadingCargo(string data)
{
if (moveTargetCargo != null)
return;
InitCargo();
moveTargetCargo.transform.position = startPosition;
moveTargetCargo.transform.rotation = Quaternion.identity;
}
private void InitCargo()
{
if (moveTargetCargo != null)
return;
moveTargetCargo = ProductManager.Instance.SpawnProduct();
moveTargetCargo.GetComponent<SimulationModelProduct>().SetParent(this.nodeID);
storedCargo = moveTargetCargo;
}
private void UnloadingCargo(string data, bool isQueue)
{
if (isQueue)
{
var msg = JsonConvert.DeserializeObject<ConveyorUnloadingToQueueData>(data);
if (msg == null)
return;
string destinationID = msg.QueueName;
ProcessUnload(destinationID);
}
else
{
var msg = JsonConvert.DeserializeObject<ConveyorUnloadingToStoreData>(data);
if (msg == null)
return;
string destinationID = msg.StoreName;
ProcessUnload(destinationID);
}
}
private void ProcessUnload(string destinationID)
{
if (string.IsNullOrEmpty(destinationID))
return;
var model = DataManager.I.GetModel(destinationID);
if (model == null)
return;
moveTargetCargo = null;
if (storedCargo != null)
{
Destroy(storedCargo);
storedCargo = null;
}
}
public override void SetBubble(object data)
{
string msg = data.ToString();
if (currentBubble == null)
{
// 생성
currentBubble = Instantiate(bubbleUIPrefab, FindAnyObjectByType<Canvas_Bubble>().transform);
currentBubble.target = DataBubbleSocket;
currentBubble.worldOffset = new Vector3(0, 1.0f, 0); // 필요에 따라 조절
currentBubble.GetComponent<RectTransform>().SetAsFirstSibling();
}
// 텍스트 갱신
currentBubble.SetMessage(msg);
currentBubble.SetDetail(msg, logicType.conveyor, LogicUIManager.instance.GetItemLabelByID(nodeID));
}
}