using System; using Newtonsoft.Json; using System.Collections.Generic; using UnityEngine; using System.Collections; using Newtonsoft.Json.Linq; using System.Collections.Concurrent; using Octopus.Simulator.Networks; using Octopus.Simulator; using static UnityEngine.InputSystem.HID.HID; #pragma warning disable CS8632 public class SimulationModelConveyor : SimulationModel { Vector3 startPosition; Vector3 endPosition; ConcurrentQueue dataQueue; 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 = this.transform.Find(nameof(startPosition)).transform.position; endPosition = this.transform.Find(nameof(endPosition)).transform.position; } // Update is called once per frame void Update() { if (simulationUI.isplaying) { if (moveTargetCargo != null) { startPosition = this.transform.Find(nameof(startPosition)).transform.position; endPosition = this.transform.Find(nameof(endPosition)).transform.position; elapseTime += Time.deltaTime; if (elapseTime < transportTime) { moveTargetCargo.transform.position = Vector3.Lerp(startPosition, endPosition, elapseTime / transportTime); } else if (elapseTime > transportTime) { //moveTargetCargo = null; elapseTime = 0; } } } } /* protected override IEnumerator RunSimulationCoroutine() { yield return new WaitUntil(() => !string.IsNullOrEmpty(nodeID)); while (dataQueue == null) { dataQueue = MQTTDataBase.Instance.GetDataQueue(nodeID); if (dataQueue == null) yield return null; else break; } while(true) { if (dataQueue.TryDequeue(out JObject currentData)) { string rawData = currentData.ToString(); Debug.Log(rawData); if ( rawData.Contains("conveyor_loading")) { //targetCargo = Instantiate(this.gameObject); //var resourcePath = "Models"; //var tmpModel = Resources.Load(string.Format("{0}/{1}", resourcePath, "Box_Pallet")); //targetCargo = Instantiate(tmpModel); SimulationModel model = new SimulationModel(); if (!string.IsNullOrEmpty(inputQueueID)) { model = DataManager.I.GetModel(inputQueueID); } else if (!string.IsNullOrEmpty(inputStoreID)) { model = DataManager.I.GetModel(inputStoreID); } SimulationModelStore storeModel = (SimulationModelStore)model; if ( moveTargetCargo == null) { yield return new WaitUntil(() => { moveTargetCargo = storeModel.GetProduct(); storedCargo = moveTargetCargo; return moveTargetCargo != null; }); moveTargetCargo.transform.position = startPosition; moveTargetCargo.transform.rotation = Quaternion.identity; } ConveyorLoadingEventMessage message = JsonConvert.DeserializeObject(rawData); Debug.Log(message); } if ( rawData.Contains("conveyor_started")) { ConveyorStartedEventMessage startDataMessage = JsonConvert.DeserializeObject(rawData); Debug.Log(startDataMessage.Data.InputQueues.ToString() + this.name); transportTime = (int)startDataMessage.Data.MoveTime; if ( startDataMessage.Data.InputQueues.Count != 0) { inputQueueID = startDataMessage.Data.InputQueues[0].Queue; } if ( startDataMessage.Data.InputStores.Count != 0) { inputStoreID = startDataMessage.Data.InputStores[0].Queue; } } if ( rawData.Contains("conveyor_unloading")) { if ( rawData.Contains("_to_queue")) { ConveyorUnloadingToQueueEventMessage message = JsonConvert.DeserializeObject(rawData); string destinationID = message.Data.QueueName; SimulationModel model = DataManager.I.GetModel(destinationID); SimulationModelStore storemodel = (SimulationModelStore)model; storemodel.StoreProduct(storedCargo); storedCargo = null; } else if ( rawData.Contains("_to_store")) { ConveyorUnloadingToStoreEventMessage message = JsonConvert.DeserializeObject(rawData); string destinationID = message.Data.StoreName; SimulationModel model = DataManager.I.GetModel(destinationID); SimulationModelStore storemodel = (SimulationModelStore)model; storemodel.StoreProduct(storedCargo); storedCargo = null; } } } yield return null; } } */ public override void GetData(string data) { var wrapclass = JsonConvert.DeserializeObject(data); if (wrapclass._event.Contains("conveyor_started")) { ConveyorStartedEventMessage startDataMessage = JsonConvert.DeserializeObject(data); //Debug.Log("conveyor"+startDataMessage.Data.InputQueues.ToString() + this.name); transportTime = (int)startDataMessage.Data.MoveTime; if (startDataMessage.Data.InputQueues.Count != 0) { inputQueueID = startDataMessage.Data.InputQueues[0].Queue; } if (startDataMessage.Data.InputStores.Count != 0) { inputStoreID = startDataMessage.Data.InputStores[0].Queue; } } if (wrapclass._event.Contains("conveyor_loading_from_store")) { Conveyor_Loading_From_Store loadingData = JsonConvert.DeserializeObject(wrapclass.data.ToString()); SimulationModel model; model = DataManager.I.GetModel(loadingData.store_name); SimulationModelStore storeModel = model.GetComponent(); if (moveTargetCargo == null) { moveTargetCargo = ProductManager.Instance.SpawnProduct(); storedCargo = moveTargetCargo; moveTargetCargo.transform.position = startPosition; moveTargetCargo.transform.rotation = Quaternion.identity; } } else if (wrapclass._event.Contains("conveyor_loading_from_queue")) { Conveyor_Loading_From_Queue loadingData = JsonConvert.DeserializeObject(wrapclass.data.ToString()); SimulationModel model; model = DataManager.I.GetModel(loadingData.queue_name); SimulationModelStore storeModel = model.GetComponent(); if (moveTargetCargo == null) { moveTargetCargo = ProductManager.Instance.SpawnProduct(); storedCargo = moveTargetCargo; moveTargetCargo.transform.position = startPosition; moveTargetCargo.transform.rotation = Quaternion.identity; } } if (wrapclass._event.Contains("conveyor_unloading")) { if (wrapclass._event.Contains("_to_queue")) { ConveyorUnloadingToQueueEventMessage message = JsonConvert.DeserializeObject(data); string destinationID = message.Data.QueueName; SimulationModel model = DataManager.I.GetModel(destinationID); SimulationModelStore storemodel = model.GetComponent(); moveTargetCargo = null; //storemodel.StoreProduct(storedCargo); Destroy(storedCargo); storedCargo = null; } else if (wrapclass._event.Contains("_to_store")) { ConveyorUnloadingToStoreEventMessage message = JsonConvert.DeserializeObject(data); string destinationID = message.Data.StoreName; SimulationModel model = DataManager.I.GetModel(destinationID); SimulationModelStore storemodel = model.GetComponent(); moveTargetCargo = null; //storemodel.StoreProduct(storedCargo); Destroy(storedCargo); storedCargo = null; } } if (wrapclass._event.Contains("conveyor_statistics_update")) { SetBubble(JsonConvert.DeserializeObject(wrapclass.data.ToString()).statistics.transported_count); } } public override void SetBubble(object data) { string msg = data.ToString(); if (currentBubble == null) { // 생성 currentBubble = Instantiate(bubbleUIPrefab, FindAnyObjectByType().transform); currentBubble.target = DataBubbleSocket; currentBubble.worldOffset = new Vector3(0, 1.0f, 0); // 필요에 따라 조절 currentBubble.GetComponent().SetAsFirstSibling(); } // 텍스트 갱신 currentBubble.SetMessage(msg); currentBubble.SetDetail(msg, logicType.conveyor, LogicUIManager.instance.GetItemLabelByID(nodeID)); } } [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? InputQueues { get; set; } [JsonProperty("input_stores")] public List? 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? 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? 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; }