146 lines
6.3 KiB
C#
146 lines
6.3 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.Events;
|
|
using Octopus.Simulator.Networks;
|
|
|
|
public class SimulationModelProcess : SimulationModel
|
|
{
|
|
public string eventUsingQueue = "processor_using_queue";
|
|
public string eventUsingStore = "processor_using_store";
|
|
public string eventStartBatch = "processor_batch_started";
|
|
public string eventOutputQueue = "processor_output_queue";
|
|
public List<GameObject> listProducts = new List<GameObject>();
|
|
public Transform productPos;
|
|
public UnityEvent onProcessStart;
|
|
public UnityEvent onProcessEnd;
|
|
int productCount = 0;
|
|
float processTime = 0;
|
|
float elapsedTime = 0;
|
|
ConcurrentQueue<JObject> dataQueue;
|
|
bool startBatch = false;
|
|
// 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()
|
|
{
|
|
if (!startBatch) return;
|
|
elapsedTime += Time.deltaTime;
|
|
if (elapsedTime > processTime)
|
|
{
|
|
onProcessEnd?.Invoke();
|
|
startBatch = false;
|
|
}
|
|
}
|
|
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(eventUsingQueue))
|
|
{
|
|
string[] queueIDKey = { "data", "queue_name" };
|
|
string[] loadCountKey = { "data", "required_items" };
|
|
string queueID = GetJsonValue(currentData, queueIDKey)?.ToString();
|
|
int loadCount = GetJsonIntValue(currentData, loadCountKey);
|
|
SimulationModel model = DataManager.I.GetModel(queueID);
|
|
SimulationModelStore storeModel = (SimulationModelStore)model;
|
|
for (int i = 0; i < loadCount; i++)
|
|
{
|
|
GameObject product = null;
|
|
yield return new WaitUntil(() => {
|
|
product = storeModel.GetProduct();
|
|
return product != null;
|
|
});
|
|
product.transform.parent = productPos;
|
|
product.transform.localPosition = Vector3.zero;
|
|
product.transform.localRotation = Quaternion.identity;
|
|
listProducts.Add(product);
|
|
}
|
|
}
|
|
else if (value.Contains(eventUsingStore))
|
|
{
|
|
string[] queueIDKey = { "data", "store_name" };
|
|
string[] loadCountKey = { "data", "required_items" };
|
|
string queueID = GetJsonValue(currentData, queueIDKey)?.ToString();
|
|
int loadCount = GetJsonIntValue(currentData, loadCountKey);
|
|
SimulationModel model = DataManager.I.GetModel(queueID);
|
|
SimulationModelStore storeModel = (SimulationModelStore)model;
|
|
for (int i = 0; i < loadCount; i++)
|
|
{
|
|
GameObject product = null;
|
|
yield return new WaitUntil(() => {
|
|
product = storeModel.GetProduct();
|
|
return product != null;
|
|
});
|
|
product.transform.parent = productPos;
|
|
product.transform.localPosition = Vector3.zero;
|
|
product.transform.localRotation = Quaternion.identity;
|
|
listProducts.Add(product);
|
|
}
|
|
}
|
|
else if (value.Contains(eventStartBatch))
|
|
{
|
|
string[] productCountKey = { "data", "product_count" };
|
|
string[] processTimeKey = { "data", "processing_time" };
|
|
int productCount = GetJsonIntValue(currentData, productCountKey);
|
|
processTime = GetJsonFloatValue(currentData, processTimeKey);
|
|
elapsedTime = 0;
|
|
startBatch = true;
|
|
onProcessStart?.Invoke();
|
|
}
|
|
else if (value.Contains(eventOutputQueue))
|
|
{
|
|
string[] outputQueueKey = { "data", "queue_name" };
|
|
string[] productCountKey = { "data", "products_to_add" };
|
|
string queueID = GetJsonValue(currentData, outputQueueKey)?.ToString();
|
|
int productCount = GetJsonIntValue(currentData, productCountKey);
|
|
SimulationModel model = DataManager.I.GetModel(queueID);
|
|
SimulationModelStore storeModel = (SimulationModelStore)model;
|
|
for (int i = 0; i < productCount; i++)
|
|
{
|
|
if (listProducts.Count > 0)
|
|
{
|
|
GameObject product = listProducts[0];
|
|
storeModel.StoreProduct(product);
|
|
listProducts.Remove(product);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Trying to unload from empty transporter : " + nodeID);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
yield return null;
|
|
}
|
|
yield return null;
|
|
}
|
|
}
|