2025-05-26 18:06:25 +09:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Best.HTTP.JSON;
|
|
|
|
|
|
|
|
|
|
public class SimulationModelSource : SimulationModel
|
|
|
|
|
{
|
|
|
|
|
public string eventGenerateProduct = "product_generated";
|
|
|
|
|
public string eventGenerateDefect = "product_defective";
|
|
|
|
|
public string eventQueueProduct = "product_queued";
|
|
|
|
|
public GameObject productPrefab;
|
|
|
|
|
public GameObject defectPrefab;
|
|
|
|
|
public List<GameObject> listProducts = new List<GameObject>();
|
|
|
|
|
public List<GameObject> listDefects = new List<GameObject>();
|
|
|
|
|
public Transform productPos;
|
2025-05-27 14:53:01 +09:00
|
|
|
public Vector3 productDistance = new Vector3(0, 0.321f, 0);
|
2025-05-26 18:06:25 +09:00
|
|
|
//ConcurrentQueue<Dictionary<string, object>> dataQueue;
|
|
|
|
|
ConcurrentQueue<JObject> dataQueue;
|
|
|
|
|
int productCount = 0;
|
2025-05-27 14:53:01 +09:00
|
|
|
int defectCount = 0;
|
2025-05-26 18:06:25 +09:00
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
int interval = 0;
|
|
|
|
|
if (productCount != listProducts.Count)
|
|
|
|
|
{
|
|
|
|
|
productCount = listProducts.Count;
|
|
|
|
|
for (int i = 0; i < productCount; i++)
|
|
|
|
|
{
|
|
|
|
|
GameObject gb = listProducts[i];
|
2025-05-27 14:53:01 +09:00
|
|
|
gb.transform.localPosition = productDistance * interval;
|
2025-05-26 18:06:25 +09:00
|
|
|
interval++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (defectCount != listDefects.Count)
|
|
|
|
|
{
|
|
|
|
|
defectCount = listDefects.Count;
|
|
|
|
|
for (int i = 0; i < defectCount; i++)
|
|
|
|
|
{
|
|
|
|
|
GameObject gb = listDefects[i];
|
2025-05-27 14:53:01 +09:00
|
|
|
gb.transform.localPosition = productDistance * interval;
|
2025-05-26 18:06:25 +09:00
|
|
|
interval++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
protected override IEnumerator RunSimulationCoroutine()
|
|
|
|
|
{
|
2025-05-27 14:53:01 +09:00
|
|
|
yield return new WaitUntil(() => !string.IsNullOrEmpty(nodeID));
|
2025-05-26 18:06:25 +09:00
|
|
|
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))
|
|
|
|
|
{
|
2025-05-27 14:53:01 +09:00
|
|
|
string eventKey = "_event";
|
2025-05-26 18:06:25 +09:00
|
|
|
if (currentData.ContainsKey(eventKey))
|
|
|
|
|
{
|
|
|
|
|
string value = currentData[eventKey].ToString();
|
|
|
|
|
if (value.Contains(eventGenerateProduct))
|
2025-05-27 14:53:01 +09:00
|
|
|
{
|
2025-05-26 18:06:25 +09:00
|
|
|
GameObject gb = Instantiate(productPrefab, productPos);
|
|
|
|
|
listProducts.Add(gb);
|
|
|
|
|
}
|
|
|
|
|
else if (value.Contains(eventGenerateDefect))
|
2025-05-27 14:53:01 +09:00
|
|
|
{
|
2025-05-26 18:06:25 +09:00
|
|
|
GameObject gb = Instantiate(defectPrefab, productPos);
|
|
|
|
|
listProducts.Add(gb);
|
|
|
|
|
}
|
|
|
|
|
else if (value.Contains(eventQueueProduct))
|
2025-05-27 14:53:01 +09:00
|
|
|
{
|
|
|
|
|
string[] queueIDKey = { "data", "queue" };
|
|
|
|
|
string[] capacityKey = { "data", "queue_length" };
|
|
|
|
|
string[] storeTypeKey = { "data", "queue_type" };
|
|
|
|
|
string queueID = GetJsonValue(currentData, queueIDKey)?.ToString();
|
|
|
|
|
int queueCapacity = GetJsonIntValue(currentData, capacityKey);
|
|
|
|
|
string storeType = GetJsonValue(currentData, storeTypeKey)?.ToString();
|
2025-05-26 18:06:25 +09:00
|
|
|
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 = (SimulationModelStore)model;
|
|
|
|
|
storeModel.maxCapacity = queueCapacity;
|
|
|
|
|
storeModel.storeType = storeType;
|
|
|
|
|
storeModel.StoreProduct(product);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
}
|