357 lines
13 KiB
C#
357 lines
13 KiB
C#
|
|
using Newtonsoft.Json.Linq;
|
||
|
|
using UnityEngine;
|
||
|
|
using System;
|
||
|
|
using System.Collections.Concurrent;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine.Events;
|
||
|
|
using Octopus.Simulator.Networks;
|
||
|
|
using Newtonsoft.Json;
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_start
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public string processor_label;
|
||
|
|
public int processing_time;
|
||
|
|
public List<ProcessInputQueue> input_queues;
|
||
|
|
public List<ProcessInputStore> input_stores;
|
||
|
|
public string output_queue;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessInputQueue
|
||
|
|
{
|
||
|
|
public string queue_name;
|
||
|
|
public int required_items;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessInputStore
|
||
|
|
{
|
||
|
|
public string store_name;
|
||
|
|
public int required_items;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_using_queue
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public string processor_label;
|
||
|
|
public string queue_name;
|
||
|
|
public int queue_length;
|
||
|
|
public int required_items;
|
||
|
|
public int priority_order;
|
||
|
|
public string queue_type;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_using_store
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public string processor_label;
|
||
|
|
public string store_name;
|
||
|
|
public int store_length;
|
||
|
|
public int required_items;
|
||
|
|
public int priority_order;
|
||
|
|
public string queue_type;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_checking_inputs
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public List<ProcessInputQueue> queues;
|
||
|
|
public List<ProcessInputStore> stores;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_defects_detected
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public int defect_count;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_requesting_resource
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public string resource_name;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_resource_acquired
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public string resource_name;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_speed_factor_applied
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public string resource_name;
|
||
|
|
public float original_time;
|
||
|
|
public float adjusted_time;
|
||
|
|
public float speed_factor;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_batch_started
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public int product_count;
|
||
|
|
public int processing_time;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_batch_completed
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public int product_count;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_output_queue
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public string queue_name;
|
||
|
|
public int queue_length_before;
|
||
|
|
public int products_to_add;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class ProcessDataClass_waiting
|
||
|
|
{
|
||
|
|
public string processor_id;
|
||
|
|
public int missing;
|
||
|
|
}
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
*/
|
||
|
|
|
||
|
|
public override void GetData(string data)
|
||
|
|
{
|
||
|
|
var wrapclass = JsonConvert.DeserializeObject<SimulationDefaultJson>(data);
|
||
|
|
if (wrapclass._event.Contains(eventUsingQueue))
|
||
|
|
{
|
||
|
|
var processData_UsingQueue = JsonConvert.DeserializeObject<ProcessDataClass_using_queue>(wrapclass.data.ToString());
|
||
|
|
/*
|
||
|
|
string[] queueIDKey = { "data", "queue_name" };
|
||
|
|
string[] loadCountKey = { "data", "required_items" };
|
||
|
|
string queueID = GetJsonValue(currentData, queueIDKey)?.ToString();
|
||
|
|
int loadCount = GetJsonIntValue(currentData, loadCountKey);
|
||
|
|
*/
|
||
|
|
string queueID = processData_UsingQueue.queue_name;
|
||
|
|
int loadCount = processData_UsingQueue.required_items;
|
||
|
|
SimulationModel model = DataManager.I.GetModel(queueID);
|
||
|
|
SimulationModelStore storeModel = model.GetComponent<SimulationModelStore>();
|
||
|
|
for (int i = 0; i < loadCount; i++)
|
||
|
|
{
|
||
|
|
GameObject product = null;
|
||
|
|
product = storeModel.GetProduct();
|
||
|
|
/*
|
||
|
|
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 (wrapclass._event.Contains(eventUsingStore))
|
||
|
|
{
|
||
|
|
var processData_UsingStore = JsonConvert.DeserializeObject<ProcessDataClass_using_store>(wrapclass.data.ToString());
|
||
|
|
string queueID = processData_UsingStore.store_name;
|
||
|
|
int loadCount = processData_UsingStore.required_items;
|
||
|
|
SimulationModel model = DataManager.I.GetModel(queueID);
|
||
|
|
SimulationModelStore storeModel = model.GetComponent<SimulationModelStore>();
|
||
|
|
for (int i = 0; i < loadCount; i++)
|
||
|
|
{
|
||
|
|
GameObject product = null;
|
||
|
|
product = storeModel.GetProduct();
|
||
|
|
/*
|
||
|
|
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 (wrapclass._event.Contains(eventStartBatch))
|
||
|
|
{
|
||
|
|
var processData_BatchStart = JsonConvert.DeserializeObject<ProcessDataClass_batch_started>(wrapclass.data.ToString());
|
||
|
|
int productCount = processData_BatchStart.product_count;
|
||
|
|
processTime = processData_BatchStart.processing_time;
|
||
|
|
elapsedTime = 0;
|
||
|
|
startBatch = true;
|
||
|
|
onProcessStart?.Invoke();
|
||
|
|
}
|
||
|
|
else if (wrapclass._event.Contains(eventOutputQueue))
|
||
|
|
{
|
||
|
|
var processData_OutputQueue = JsonConvert.DeserializeObject<ProcessDataClass_output_queue>(wrapclass.data.ToString());
|
||
|
|
string queueID = processData_OutputQueue.queue_name;
|
||
|
|
int productCount = processData_OutputQueue.products_to_add;
|
||
|
|
SimulationModel model = DataManager.I.GetModel(queueID);
|
||
|
|
SimulationModelStore storeModel = model.GetComponent<SimulationModelStore>();
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|