using System; using Newtonsoft.Json; using System.Collections.Generic; using UnityEngine; using System.Collections; using Newtonsoft.Json.Linq; using System.Collections.Concurrent; using RTG; using Unity.VisualScripting; using Best.HTTP.JSON; public class SimulationModelConveyor : SimulationModel { Vector3 startPosition; Vector3 endPosition; ConcurrentQueue dataQueue; GameObject targetCargo; int transportTime; float elapseTime; string inputQueueID; string inputStoreID; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { 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 ( targetCargo != null) { startPosition = this.transform.Find(nameof(startPosition)).transform.position; endPosition = this.transform.Find(nameof(endPosition)).transform.position; elapseTime += Time.deltaTime; if (elapseTime < transportTime) { targetCargo.transform.position = Vector3.Lerp(startPosition, endPosition, elapseTime / transportTime); } else if ( elapseTime > transportTime ) { //targetCargo = null; elapseTime = 0; } } } protected override IEnumerator RunSimulationCoroutine() { Debug.Log("Start"); 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 ( targetCargo == null) { yield return new WaitUntil(() => { targetCargo = storeModel.GetProduct(); return targetCargo != null; }); targetCargo.transform.position = startPosition; targetCargo.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(targetCargo); targetCargo = 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(targetCargo); targetCargo = null; } } } yield return null; } } } [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; } [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 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("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")] public string? Queue { get; set; } [JsonProperty("required_items")] public int? RequiredItems { get; set; } } [Serializable] public class InputStore { [JsonProperty("store")] public string? Queue { get; set; } [JsonProperty("required_items")] public int? RequiredItems { get; set; } } [Serializable] public class ConveyorLoadingEventMessage { [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 ConveyorLoadingData? Data { get; set; } } [Serializable] public class ConveyorLoadingData { [JsonProperty("product_id")] public string? ProductId { get; set; } [JsonProperty("belt_load_percentage")] public float? BeltLoadPercentage { get; set; } [JsonProperty("first_item_position")] public float? FirstItemPosition { 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 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; } }