Files
Simulation/Assets/Scripts/SimulationModels/SimulationModelSource.cs

200 lines
6.6 KiB
C#
Raw Normal View History

2025-06-24 17:02:07 +09:00
using System.Collections.Concurrent;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Collections;
using Octopus.Simulator.Networks;
using Newtonsoft.Json;
2025-07-22 14:03:40 +09:00
using Octopus.Simulator;
2025-06-24 17:02:07 +09:00
[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<string> input_queues;
public List<string> 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;
}
2025-07-15 17:35:53 +09:00
[Serializable]
public class SourceDataClass_statistics
{
public StatisticsSource statistics;
}
[Serializable]
public class StatisticsSource
{
public int total_output;
public int total_defects;
}
2025-06-24 17:02:07 +09:00
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<GameObject> listProducts = new List<GameObject>();
public List<GameObject> listDefects = new List<GameObject>();
public Transform productPos;
public Vector3 productDistance = new Vector3(0, 0.321f, 0);
int productCount = 0;
int defectCount = 0;
2025-07-07 09:57:13 +09:00
2025-06-24 17:02:07 +09:00
public override void GetData(string data)
{
var wrapclass = JsonConvert.DeserializeObject<SimulationDefaultJson>(data);
if (wrapclass._event.Contains(eventGenerateProduct))
{
GameObject gb = Instantiate(productPrefab, productPos);
2025-08-04 17:26:24 +09:00
gb.GetComponent<SimulationModelProduct>().SetParent(this.nodeID);
2025-06-24 17:02:07 +09:00
listProducts.Add(gb);
ProductSorting();
}
else if (wrapclass._event.Contains(eventGenerateDefect))
{
GameObject gb = Instantiate(defectPrefab, productPos);
2025-08-04 17:26:24 +09:00
gb.GetComponent<SimulationModelProduct>().SetParent(this.nodeID);
2025-06-24 17:02:07 +09:00
listProducts.Add(gb);
ProductSorting();
}
else if (wrapclass._event.Contains(eventQueueProduct))
{
var QueueData = JsonConvert.DeserializeObject<SourceDataClass_Queued>(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)
{
2025-07-08 13:42:26 +09:00
SimulationModelStore storeModel = model.GetComponent<SimulationModelStore>();
2025-06-24 17:02:07 +09:00
storeModel.maxCapacity = queueCapacity;
2025-08-04 17:26:24 +09:00
if (storeType == "fifo")
{
storeModel.storeType = StoreBehavior.FIFO;
}
else
{
storeModel.storeType = StoreBehavior.LIFO;
}
2025-07-14 17:50:13 +09:00
//storeModel.StoreProduct(product);
Destroy(product);
2025-06-24 17:02:07 +09:00
}
}
2025-07-08 13:42:26 +09:00
else if (wrapclass._event.Contains(eventStoreProduct))
{
var StoreData = JsonConvert.DeserializeObject<SourceDataClass_Stored>(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<SimulationModelStore>();
storeModel.maxCapacity = queueCapacity;
2025-07-14 17:50:13 +09:00
//storeModel.StoreProduct(product);
Destroy(product);
2025-07-08 13:42:26 +09:00
}
}
2025-07-15 17:35:53 +09:00
else if (wrapclass._event.Contains("source_statistics_update"))
{
SetBubble(JsonConvert.DeserializeObject<SourceDataClass_statistics>(wrapclass.data.ToString()).statistics);
}
2025-06-24 17:02:07 +09:00
}
void ProductSorting()
{
2025-07-22 16:22:57 +09:00
int interval = 1;
2025-06-24 17:02:07 +09:00
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;
}
}
}
2025-07-15 17:35:53 +09:00
public override void SetBubble(object data)
{
var processStatistics = data as StatisticsSource;
string msg = $"{processStatistics.total_output}/{processStatistics.total_defects}";
if (currentBubble == null)
{
// <20><><EFBFBD><EFBFBD>
currentBubble = Instantiate(bubbleUIPrefab, FindAnyObjectByType<Canvas_Bubble>().transform);
currentBubble.target = DataBubbleSocket;
currentBubble.worldOffset = new Vector3(0, 0, 0); // <20>ʿ信 <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
2025-07-22 15:57:29 +09:00
currentBubble.GetComponent<RectTransform>().SetAsFirstSibling();
2025-07-15 17:35:53 +09:00
}
// <20>ؽ<EFBFBD>Ʈ <20><><EFBFBD><EFBFBD>
currentBubble.SetMessage(msg);
2025-07-22 15:57:29 +09:00
currentBubble.SetDetail(processStatistics, logicType.source, LogicUIManager.instance.GetItemLabelByID(nodeID));
2025-07-15 17:35:53 +09:00
}
2025-06-24 17:02:07 +09:00
}