Files
Simulation/Assets/Scripts/SimulationModels/SimulationModelSink.cs
2025-07-30 10:55:55 +09:00

211 lines
6.6 KiB
C#

using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System;
using UnityEngine;
using System.Collections;
using Octopus.Simulator.Networks;
using Newtonsoft.Json;
using Octopus.Simulator;
[Serializable]
public class SinkDataClass_Started
{
public string sink_id;
public string label;
public int sinking_interval;
public int sinking_amount;
public List<SinkInputQueues> input_queues;
}
[Serializable]
public class SinkInputQueues
{
public string queue_name;
public int required_items;
}
[Serializable]
public class SinkDataClass_items_shipped_successfully
{
public string sink_id;
public string sink_label;
public int current_sink_count;
public int total_shipped_count;
public int simulation_time_seconds;
public List<ShippedItemsDetails> shipped_items_details;
public int simulation_time;
}
[Serializable]
public class ShippedItemsDetails
{
public string product_id;
public float creation_time;
public bool is_defective;
}
[Serializable]
public class SinkDataClass_waiting_for_items
{
public string sink_id;
public string sink_label;
public int simulation_time;
public List<InputQueuesStatus> input_queues_status;
}
[Serializable]
public class InputQueuesStatus
{
public string queue_name;
public int required_items;
public int current_items;
}
[Serializable]
public class SinkDataClass_item_removed_from_queue
{
public string sink_id;
public string sink_label;
public string queue_name;
public string product_id;
public int remaining_queue_length;
}
[Serializable]
public class SinkDataClass_item_removed_from_store
{
public string sink_id;
public string sink_label;
public string store_name;
public string product_id;
public int remaining_store_length;
}
[Serializable]
public class SinkDataClass_Statistics
{
public StatisticsSink statistics;
}
[Serializable]
public class StatisticsSink
{
public int sink_items_count;
}
public class SimulationModelSink : SimulationModel
{
public string eventShipItem = "items_shipped_successfully";
public string eventRemoveItemQueue = "item_removed_from_queue";
public string eventRemoveItemStore = "item_removed_from_store";
public int totalShipped = 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.IsEmpty)
{
yield return null;
continue;
}
if (dataQueue.TryDequeue(out JObject currentData))
{
string eventKey = "_event";
if (currentData.ContainsKey(eventKey))
{
string value = currentData[eventKey].ToString();
if (value.Contains(eventShipItem))
{
}
else if (value.Contains(eventRemoveItem))
{
string[] queueIDKey = { "data", "queue_name" };
string[] queueRemainKey = { "data", "remaining_queue_length" };
string queueID = GetJsonValue(currentData, queueIDKey)?.ToString();
int queueCapacity = GetJsonIntValue(currentData, queueRemainKey);
SimulationModel model = DataManager.I.GetModel(queueID);
SimulationModelStore storeModel = (SimulationModelStore)model;
while (storeModel.storedProducts.Count > queueCapacity)
{
//Destroy(storeModel.GetProduct());
totalShipped++;
yield return null;
}
}
}
}
yield return null;
}
yield return null;
}
*/
public override void GetData(string data)
{
var wrapclass = JsonConvert.DeserializeObject<SimulationDefaultJson>(data);
if (wrapclass._event.Contains(eventShipItem))
{
}
else if (wrapclass._event.Contains(eventRemoveItemQueue))
{
var sinkDataClass_item_removed_from_queue = JsonConvert.DeserializeObject<SinkDataClass_item_removed_from_queue>(wrapclass.data.ToString());
string queueID = sinkDataClass_item_removed_from_queue.queue_name;
int queueCapacity = sinkDataClass_item_removed_from_queue.remaining_queue_length;
SimulationModel model = DataManager.I.GetModel(queueID);
SimulationModelStore storeModel = (SimulationModelStore)model;
while (storeModel.storedProducts.Count > queueCapacity)
{
//Destroy(storeModel.GetProduct());
totalShipped++;
}
}
else if (wrapclass._event.Contains(eventRemoveItemStore))
{
var sinkDataClass_item_removed_from_queue = JsonConvert.DeserializeObject<SinkDataClass_item_removed_from_store>(wrapclass.data.ToString());
string queueID = sinkDataClass_item_removed_from_queue.store_name;
int queueCapacity = sinkDataClass_item_removed_from_queue.remaining_store_length;
SimulationModel model = DataManager.I.GetModel(queueID);
SimulationModelStore storeModel = (SimulationModelStore)model;
while (storeModel.storedProducts.Count > queueCapacity)
{
//Destroy(storeModel.GetProduct());
totalShipped++;
}
}
else if (wrapclass._event.Contains("sink_statistics_update"))
{
SetBubble(JsonConvert.DeserializeObject<SinkDataClass_Statistics>(wrapclass.data.ToString()).statistics.sink_items_count);
}
}
public override void SetBubble(object data)
{
string msg = data.ToString();
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(msg, logicType.sink, LogicUIManager.instance.GetItemLabelByID(nodeID));
}
}