using System.Collections.Concurrent; using System.Collections.Generic; using UnityEngine; using System; using System.Collections; using Octopus.Simulator.Networks; using Newtonsoft.Json; using Octopus.Simulator; [Serializable] public class SourceDataClass_start { public string generator_id; public string label; public float production_rate; public float cycle_time; public int batch_size; public int products_per_batch; public List input_queues; public List input_stores; public string output_queue; public bool has_resource; public int priority; public float defect_rate; } [Serializable] public class SourceDataClass_Queued { public string generator_id; public string label; public string product_id; public string queue; public int queue_length; public string queue_type; } [Serializable] public class SourceDataClass_Stored { public string generator_id; public string label; public string product_id; public string store; public int store_length; } [Serializable] public class SourceDataClass_statistics { public StatisticsSource statistics; } [Serializable] public class StatisticsSource { public int total_output; public int total_defects; } public class SimulationModelSource : SimulationModel { public string eventGenerateProduct = "product_generated"; public string eventGenerateDefect = "product_defective"; public string eventQueueProduct = "product_queued"; public string eventStoreProduct = "product_stored"; public GameObject productPrefab; public GameObject defectPrefab; public List listProducts = new List(); public List listDefects = new List(); public Transform productPos; public Vector3 productDistance = new Vector3(0, 0.321f, 0); int productCount = 0; int defectCount = 0; public override void GetData(string data) { var wrapclass = JsonConvert.DeserializeObject(data); if (wrapclass._event.Contains(eventGenerateProduct)) { GameObject gb = Instantiate(productPrefab, productPos); gb.GetComponent().SetParent(this.nodeID); listProducts.Add(gb); ProductSorting(); } else if (wrapclass._event.Contains(eventGenerateDefect)) { GameObject gb = Instantiate(defectPrefab, productPos); gb.GetComponent().SetParent(this.nodeID); listProducts.Add(gb); ProductSorting(); } else if (wrapclass._event.Contains(eventQueueProduct)) { var QueueData = JsonConvert.DeserializeObject(wrapclass.data.ToString()); string queueID = QueueData.queue; int queueCapacity = QueueData.queue_length; string storeType = QueueData.queue_type; SimulationModel model = DataManager.I.GetModel(queueID); GameObject product = null; if (listProducts.Count > 0) { product = listProducts[0]; listProducts.Remove(product); } else if (listDefects.Count > 0) { product = listDefects[0]; listDefects.Remove(product); } if (model != null && product != null) { SimulationModelStore storeModel = model.GetComponent(); storeModel.maxCapacity = queueCapacity; if (storeType == "fifo") { storeModel.storeType = StoreBehavior.FIFO; } else { storeModel.storeType = StoreBehavior.LIFO; } //storeModel.StoreProduct(product); Destroy(product); } } else if (wrapclass._event.Contains(eventStoreProduct)) { var StoreData = JsonConvert.DeserializeObject(wrapclass.data.ToString()); string queueID = StoreData.store; int queueCapacity = StoreData.store_length; SimulationModel model = DataManager.I.GetModel(queueID); GameObject product = null; if (listProducts.Count > 0) { product = listProducts[0]; listProducts.Remove(product); } else if (listDefects.Count > 0) { product = listDefects[0]; listDefects.Remove(product); } if (model != null && product != null) { SimulationModelStore storeModel = model.GetComponent(); storeModel.maxCapacity = queueCapacity; //storeModel.StoreProduct(product); Destroy(product); } } else if (wrapclass._event.Contains("source_statistics_update")) { SetBubble(JsonConvert.DeserializeObject(wrapclass.data.ToString()).statistics); } } void ProductSorting() { int interval = 1; if (productCount != listProducts.Count) { productCount = listProducts.Count; for (int i = 0; i < productCount; i++) { GameObject gb = listProducts[i]; gb.transform.localPosition = productDistance * interval; } } if (defectCount != listDefects.Count) { defectCount = listDefects.Count; for (int i = 0; i < defectCount; i++) { GameObject gb = listDefects[i]; gb.transform.localPosition = productDistance * interval; } } } public override void SetBubble(object data) { var processStatistics = data as StatisticsSource; string msg = $"{processStatistics.total_output}/{processStatistics.total_defects}"; if (currentBubble == null) { // »ý¼º currentBubble = Instantiate(bubbleUIPrefab, FindAnyObjectByType().transform); currentBubble.target = DataBubbleSocket; currentBubble.worldOffset = new Vector3(0, 0, 0); // Çʿ信 µû¶ó Á¶Àý currentBubble.GetComponent().SetAsFirstSibling(); } // ÅØ½ºÆ® °»½Å currentBubble.SetMessage(msg); currentBubble.SetDetail(processStatistics, logicType.source, LogicUIManager.instance.GetItemLabelByID(nodeID)); } }