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

200 lines
6.6 KiB
C#

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<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;
}
[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<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;
public override void GetData(string data)
{
var wrapclass = JsonConvert.DeserializeObject<SimulationDefaultJson>(data);
if (wrapclass._event.Contains(eventGenerateProduct))
{
GameObject gb = Instantiate(productPrefab, productPos);
gb.GetComponent<SimulationModelProduct>().SetParent(this.nodeID);
listProducts.Add(gb);
ProductSorting();
}
else if (wrapclass._event.Contains(eventGenerateDefect))
{
GameObject gb = Instantiate(defectPrefab, productPos);
gb.GetComponent<SimulationModelProduct>().SetParent(this.nodeID);
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)
{
SimulationModelStore storeModel = model.GetComponent<SimulationModelStore>();
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<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;
//storeModel.StoreProduct(product);
Destroy(product);
}
}
else if (wrapclass._event.Contains("source_statistics_update"))
{
SetBubble(JsonConvert.DeserializeObject<SourceDataClass_statistics>(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<Canvas_Bubble>().transform);
currentBubble.target = DataBubbleSocket;
currentBubble.worldOffset = new Vector3(0, 0, 0); // 필요에 따라 조절
currentBubble.GetComponent<RectTransform>().SetAsFirstSibling();
}
// 텍스트 갱신
currentBubble.SetMessage(msg);
currentBubble.SetDetail(processStatistics, logicType.source, LogicUIManager.instance.GetItemLabelByID(nodeID));
}
}