129 lines
4.9 KiB
C#
129 lines
4.9 KiB
C#
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;
|
|
public Vector3 productInterval;
|
|
//ConcurrentQueue<Dictionary<string, object>> dataQueue;
|
|
ConcurrentQueue<JObject> dataQueue;
|
|
int productCount = 0;
|
|
int defectCount = 0;
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
int interval = 0;
|
|
if (productCount != listProducts.Count)
|
|
{
|
|
productCount = listProducts.Count;
|
|
for (int i = 0; i < productCount; i++)
|
|
{
|
|
GameObject gb = listProducts[i];
|
|
gb.transform.localPosition = productInterval * interval;
|
|
interval++;
|
|
}
|
|
}
|
|
if (defectCount != listDefects.Count)
|
|
{
|
|
defectCount = listDefects.Count;
|
|
for (int i = 0; i < defectCount; i++)
|
|
{
|
|
GameObject gb = listDefects[i];
|
|
gb.transform.localPosition = productInterval * interval;
|
|
interval++;
|
|
}
|
|
}
|
|
}
|
|
protected override IEnumerator RunSimulationCoroutine()
|
|
{
|
|
yield return new WaitUntil(() => !string.IsNullOrEmpty(nodeID));
|
|
Debug.Log(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";
|
|
var productId = GetJsonValue(currentData, new string[] { "data", "product_id" })?.ToString();
|
|
if (currentData.ContainsKey(eventKey))
|
|
{
|
|
string value = currentData[eventKey].ToString();
|
|
if (value.Contains(eventGenerateProduct))
|
|
{
|
|
Debug.Log("#### Got Product");
|
|
GameObject gb = Instantiate(productPrefab, productPos);
|
|
listProducts.Add(gb);
|
|
}
|
|
else if (value.Contains(eventGenerateDefect))
|
|
{
|
|
Debug.Log("#### Got Defect");
|
|
GameObject gb = Instantiate(defectPrefab, productPos);
|
|
listProducts.Add(gb);
|
|
}
|
|
else if (value.Contains(eventQueueProduct))
|
|
{
|
|
Debug.Log("#### Pass To Queue");
|
|
string dataKey = "data";
|
|
string queueIDKey = "queue";
|
|
string capacityKey = "queue_length";
|
|
string storeTypeKey = "queue_type";
|
|
string queueID = currentData[dataKey][queueIDKey].ToString();
|
|
int queueCapacity = int.Parse(currentData[dataKey][capacityKey].ToString());
|
|
string storeType = currentData[dataKey][storeTypeKey].ToString();
|
|
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;
|
|
}
|
|
}
|