561 lines
22 KiB
C#
561 lines
22 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 MoveDataClass_start
|
||
{
|
||
public string move_id;
|
||
public string move_label;
|
||
public string type;
|
||
public float move_time;
|
||
public int capacity;
|
||
public int products_per_batch;
|
||
public List<InputQueue> input_queues;
|
||
public string output_queue;
|
||
public string defect_queue;
|
||
public string required_resource;
|
||
public float return_time;
|
||
public int min_batch_size;
|
||
public float loading_delay;
|
||
public float unloading_delay;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClass_loading
|
||
{
|
||
public string move_id;
|
||
public string move_label;
|
||
public int loaded_count;
|
||
public List<string> source_queues;
|
||
public List<string> source_stores;
|
||
public float loading_delay;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClass_Request_Resource
|
||
{
|
||
public string move_id;
|
||
public string move_label;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClass_Resource_Acquired
|
||
{
|
||
public string resource;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClass_Speed_Factor_Applied
|
||
{
|
||
public float original;
|
||
public float adjusted;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClass_Move_Moving
|
||
{
|
||
public float time;
|
||
public float loading_delay;
|
||
public float unloading_delay;
|
||
public MoveMovingInput input;
|
||
public MoveMovingOutput output;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveMovingInput
|
||
{
|
||
public List<string> queues;
|
||
public List<string> stores;
|
||
}
|
||
[Serializable]
|
||
public class MoveMovingOutput
|
||
{
|
||
public string queue;
|
||
public string store;
|
||
public string defect_queue;
|
||
public string defect_store;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClass_Arrived
|
||
{
|
||
public int count;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClass_Unloading_Defect
|
||
{
|
||
public string product_id;
|
||
public string output_queue;
|
||
public int output_queue_length;
|
||
public float unloading_delay;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClass_Unloading_Defect_Store
|
||
{
|
||
public string product_id;
|
||
public string store_name;
|
||
public int store_length;
|
||
public float unloading_delay;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClass_Unloading
|
||
{
|
||
public string product_id;
|
||
public string output_queue;
|
||
public int output_queue_length;
|
||
public float unloading_delay;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClass_Unloading_Store
|
||
{
|
||
public string product_id;
|
||
public string store_name;
|
||
public int store_queue_length;
|
||
public float unloading_delay;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClass_returning
|
||
{
|
||
public float return_time;
|
||
}
|
||
|
||
[Serializable]
|
||
public class MoveDataClassStatistics
|
||
{
|
||
public statisticsMove statistics;
|
||
}
|
||
|
||
[Serializable]
|
||
public class statisticsMove
|
||
{
|
||
public int total_moved;
|
||
}
|
||
|
||
public class SimulationModelMove : SimulationModel
|
||
{
|
||
string eventLoading = "move_loading";
|
||
string eventMove = "move_moving";
|
||
string eventUnloading = "move_unloading";
|
||
string eventSpeed = "move_speed_factor_applied";
|
||
string eventReturning = "move_returning";
|
||
public List<GameObject> listProducts = new List<GameObject>();
|
||
public Transform productPos;
|
||
public Vector3 productDistance = new Vector3(0, 0.321f, 0);
|
||
public UnityEvent onMove;
|
||
public UnityEvent onWait;
|
||
ConcurrentQueue<JObject> dataQueue;
|
||
int productCount = 0;
|
||
Transform origin = null;
|
||
public Transform destination = null;
|
||
public float arrivalTime = 0;
|
||
public float elapsedTime = 0;
|
||
Vector3 originalPos;
|
||
Vector3 prevPos;
|
||
|
||
bool isRewind = false;
|
||
|
||
void Update()
|
||
{
|
||
if (simulationUI.isplaying)
|
||
{
|
||
int interval = 0;
|
||
if (productCount != listProducts.Count)
|
||
{
|
||
productCount = listProducts.Count;
|
||
for (int i = 0; i < productCount; i++)
|
||
{
|
||
GameObject gb = listProducts[i];
|
||
gb.transform.localPosition = productDistance;
|
||
interval++;
|
||
}
|
||
}
|
||
if (destination != null)
|
||
{
|
||
elapsedTime += Time.deltaTime * WebParameters.speed;
|
||
|
||
if (elapsedTime / arrivalTime > 1.0f)
|
||
{
|
||
transform.position = destination.position;
|
||
transform.forward = destination.forward;
|
||
destination = null;
|
||
}
|
||
else
|
||
{
|
||
transform.position = Vector3.Lerp(originalPos, destination.position, elapsedTime / arrivalTime);
|
||
Vector3 dir = transform.position - prevPos;
|
||
dir.y = 0;
|
||
dir.Normalize();
|
||
transform.forward = dir;
|
||
prevPos = transform.position;
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
/*
|
||
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";
|
||
string[] arrivalTimeKey = { "data", "time" };
|
||
string[] returnTimeKey = { "data", "return_time" };
|
||
string[] originalTimeKey = { "data", "original" };
|
||
string[] adjustedTimeKey = { "data", "adjusted" };
|
||
string[] inputQueueKey = { "data", "input", "queues" };
|
||
string[] inputStoreKey = { "data", "input", "stores" };
|
||
string[] outputQueueKey = { "data", "output", "queue" };
|
||
string[] outputStoreKey = { "data", "output", "store" };
|
||
string[] sourceQueueKey = { "data", "source_queues" };
|
||
string[] sourceStoreKey = { "data", "source_stores" };
|
||
string[] loadCountKey = { "data", "loaded_count" };
|
||
string[] unloadQueueKey = { "data", "queue_name" };
|
||
string[] unloadStoreKey = { "data", "store_name" };
|
||
|
||
if (currentData.ContainsKey(eventKey))
|
||
{
|
||
string value = currentData[eventKey].ToString();
|
||
|
||
|
||
if (value.Contains(eventLoading))
|
||
{
|
||
yield return null;
|
||
|
||
JArray sourceQueues = GetJsonArray(currentData, sourceQueueKey);
|
||
JArray sourceStores = GetJsonArray(currentData, sourceStoreKey);
|
||
int loadCount = GetJsonIntValue(currentData, loadCountKey);
|
||
if (sourceQueues != null && sourceQueues.HasValues)
|
||
{
|
||
string queueID = sourceQueues[0].ToString();
|
||
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 (sourceStores != null && sourceStores.HasValues)
|
||
{
|
||
string storeID = sourceStores[0].ToString();
|
||
SimulationModel model = DataManager.I.GetModel(storeID);
|
||
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(eventMove))
|
||
{
|
||
Debug.Log("MoveeventIncomming");
|
||
|
||
elapsedTime = 0;
|
||
arrivalTime = GetJsonFloatValue(currentData, arrivalTimeKey);
|
||
originalPos = transform.position;
|
||
prevPos = transform.position;
|
||
JArray originQueues = GetJsonArray(currentData, inputQueueKey);
|
||
JArray originStores = GetJsonArray(currentData, inputStoreKey);
|
||
string arrivalQueue = GetJsonValue(currentData, outputQueueKey)?.ToString();
|
||
string arrivalStore = GetJsonValue(currentData, outputStoreKey)?.ToString();
|
||
|
||
if (originQueues != null && originQueues.HasValues)
|
||
{
|
||
string queueID = originQueues[0].ToString();
|
||
SimulationModel model = DataManager.I.GetModel(queueID);
|
||
SimulationModelStore storeModel = (SimulationModelStore)model;
|
||
origin = storeModel.GetTransporterPosition();
|
||
}
|
||
else if ( originStores != null&& originStores.HasValues)
|
||
{
|
||
string storeID = originStores[0].ToString();
|
||
SimulationModel model = DataManager.I.GetModel(storeID);
|
||
SimulationModelStore storeModel = (SimulationModelStore)model;
|
||
origin = storeModel.GetTransporterPosition();
|
||
}
|
||
|
||
if (!string.IsNullOrEmpty(arrivalQueue))
|
||
{
|
||
SimulationModel model = DataManager.I.GetModel(arrivalQueue);
|
||
SimulationModelStore storeModel = (SimulationModelStore)model;
|
||
destination = storeModel.GetTransporterPosition();
|
||
}
|
||
else if (!string.IsNullOrEmpty(arrivalStore))
|
||
{
|
||
SimulationModel model = DataManager.I.GetModel(arrivalStore);
|
||
SimulationModelStore storeModel = (SimulationModelStore)model;
|
||
destination = storeModel.GetTransporterPosition();
|
||
}
|
||
}
|
||
else if (value.Contains(eventUnloading))
|
||
{
|
||
yield return null;
|
||
|
||
string targetID = "";
|
||
|
||
if ( value.Contains("unloading_store"))
|
||
{
|
||
targetID = GetJsonValue(currentData, unloadStoreKey)?.ToString();
|
||
}
|
||
else if ( value.Contains("unloading_queue"))
|
||
{
|
||
targetID = GetJsonValue(currentData, unloadQueueKey)?.ToString();
|
||
}
|
||
else
|
||
{
|
||
string[] tmpValue = { "data", "output_queue" };
|
||
|
||
targetID = GetJsonValue(currentData, tmpValue)?.ToString();
|
||
}
|
||
|
||
SimulationModel model = new SimulationModel();
|
||
model = DataManager.I.GetModel(targetID);
|
||
SimulationModelStore storeModel = (SimulationModelStore)model;
|
||
|
||
if (listProducts.Count > 0)
|
||
{
|
||
GameObject product = listProducts[0];
|
||
storeModel.StoreProduct(product);
|
||
listProducts.Remove(product);
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("Trying to unload from empty transporter : " + nodeID);
|
||
}
|
||
}
|
||
else if (value.Contains(eventSpeed))
|
||
{
|
||
float originalTime = GetJsonFloatValue(currentData, originalTimeKey);
|
||
float adjustedTime = GetJsonFloatValue(currentData, adjustedTimeKey);
|
||
arrivalTime += adjustedTime - originalTime;
|
||
elapsedTime = 0;
|
||
originalPos = transform.position;
|
||
prevPos = transform.position;
|
||
}
|
||
else if (value.Contains(eventReturning))
|
||
{
|
||
elapsedTime = 0;
|
||
arrivalTime = GetJsonFloatValue(currentData, returnTimeKey);
|
||
originalPos = transform.position;
|
||
prevPos = transform.position;
|
||
if (origin != null)
|
||
{
|
||
destination = origin;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
yield return null;
|
||
}
|
||
yield return null;
|
||
}
|
||
*/
|
||
public override void GetData(string data)
|
||
{
|
||
var wrapclass = JsonConvert.DeserializeObject<SimulationDefaultJson>(data);
|
||
|
||
if (wrapclass._event.Contains(eventLoading))
|
||
{
|
||
var moveData_loading = JsonConvert.DeserializeObject<MoveDataClass_loading>(wrapclass.data.ToString());
|
||
if (moveData_loading.source_queues != null && (moveData_loading.source_queues.Count >= 1))
|
||
{
|
||
string queueID = moveData_loading.source_queues[0].ToString();
|
||
SimulationModel model = DataManager.I.GetModel(queueID);
|
||
SimulationModelStore storeModel = (SimulationModelStore)model;
|
||
for (int i = 0; i < moveData_loading.loaded_count; i++)
|
||
{
|
||
GameObject product = null;
|
||
product = ProductManager.Instance.SpawnProduct();
|
||
if (product != null)
|
||
{
|
||
product.transform.parent = productPos;
|
||
product.transform.localPosition = Vector3.zero;
|
||
product.transform.localRotation = Quaternion.identity;
|
||
listProducts.Add(product);
|
||
}
|
||
}
|
||
}
|
||
if (moveData_loading.source_stores != null && (moveData_loading.source_stores.Count >= 1))
|
||
{
|
||
string storeID = moveData_loading.source_stores[0].ToString();
|
||
SimulationModel model = DataManager.I.GetModel(storeID);
|
||
SimulationModelStore storeModel = (SimulationModelStore)model;
|
||
for (int i = 0; i < moveData_loading.loaded_count; i++)
|
||
{
|
||
GameObject product = null;
|
||
product = ProductManager.Instance.SpawnProduct();
|
||
if (product != null)
|
||
{
|
||
product.transform.parent = productPos;
|
||
product.transform.localPosition = Vector3.zero;
|
||
product.transform.localRotation = Quaternion.identity;
|
||
listProducts.Add(product);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if (wrapclass._event.Contains(eventMove))
|
||
{
|
||
var moveData_Move = JsonConvert.DeserializeObject<MoveDataClass_Move_Moving>(wrapclass.data.ToString());
|
||
elapsedTime = 0;
|
||
arrivalTime = moveData_Move.time;
|
||
originalPos = transform.position;
|
||
prevPos = transform.position;
|
||
|
||
if (moveData_Move.input.queues != null && moveData_Move.input.queues.Count >= 1)
|
||
{
|
||
string queueID = moveData_Move.input.queues[0].ToString();
|
||
SimulationModel model = DataManager.I.GetModel(queueID);
|
||
SimulationModelStore storeModel = model.GetComponent<SimulationModelStore>();
|
||
origin = storeModel.GetTransporterPosition();
|
||
}
|
||
else if (moveData_Move.input.stores != null && moveData_Move.input.stores.Count >= 1)
|
||
{
|
||
string storeID = moveData_Move.input.stores[0].ToString();
|
||
SimulationModel model = DataManager.I.GetModel(storeID);
|
||
SimulationModelStore storeModel = model.GetComponent<SimulationModelStore>();
|
||
origin = storeModel.GetTransporterPosition();
|
||
}
|
||
|
||
if (!string.IsNullOrEmpty(moveData_Move.output.queue))
|
||
{
|
||
SimulationModel model = DataManager.I.GetModel(moveData_Move.output.queue);
|
||
SimulationModelStore storeModel = model.GetComponent<SimulationModelStore>();
|
||
destination = storeModel.GetTransporterPosition();
|
||
}
|
||
else if (!string.IsNullOrEmpty(moveData_Move.output.store))
|
||
{
|
||
SimulationModel model = DataManager.I.GetModel(moveData_Move.output.store);
|
||
SimulationModelStore storeModel = model.GetComponent<SimulationModelStore>();
|
||
destination = storeModel.GetTransporterPosition();
|
||
}
|
||
}
|
||
else if (wrapclass._event.Contains(eventUnloading))
|
||
{
|
||
string targetID = "";
|
||
|
||
if (wrapclass._event.Contains("defect_store"))
|
||
{
|
||
var moveData_unLoading = JsonConvert.DeserializeObject<MoveDataClass_Unloading_Defect_Store>(wrapclass.data.ToString());
|
||
targetID = moveData_unLoading.store_name;
|
||
}
|
||
else if (wrapclass._event.Contains("defect"))
|
||
{
|
||
var moveData_unLoading = JsonConvert.DeserializeObject<MoveDataClass_Unloading_Defect>(wrapclass.data.ToString());
|
||
targetID = moveData_unLoading.output_queue;
|
||
}
|
||
else if (wrapclass._event.Contains("unloading_store"))
|
||
{
|
||
//Debug.Log(wrapclass.data.ToString());
|
||
var moveData_unLoading = JsonConvert.DeserializeObject<MoveDataClass_Unloading_Store>(wrapclass.data.ToString());
|
||
targetID = moveData_unLoading.store_name;
|
||
}
|
||
else
|
||
{
|
||
var moveData_unLoading = JsonConvert.DeserializeObject<MoveDataClass_Unloading>(wrapclass.data.ToString());
|
||
targetID = moveData_unLoading.output_queue;
|
||
}
|
||
|
||
var model = DataManager.I.GetModel(targetID);
|
||
SimulationModelStore storeModel = model.GetComponent<SimulationModelStore>();
|
||
|
||
if (listProducts.Count > 0)
|
||
{
|
||
GameObject product = listProducts[0];
|
||
//storeModel.StoreProduct(product);
|
||
listProducts.Remove(product);
|
||
Destroy(product);
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("Trying to unload from empty transporter : " + nodeID);
|
||
}
|
||
}
|
||
if (wrapclass._event.Contains(eventSpeed))
|
||
{
|
||
var moveData_Speed = JsonConvert.DeserializeObject<MoveDataClass_Speed_Factor_Applied>(wrapclass.data.ToString());
|
||
float originalTime = moveData_Speed.original;
|
||
float adjustedTime = moveData_Speed.adjusted;
|
||
arrivalTime += adjustedTime - originalTime;
|
||
elapsedTime = 0;
|
||
originalPos = transform.position;
|
||
prevPos = transform.position;
|
||
}
|
||
if (wrapclass._event.Contains(eventReturning))
|
||
{
|
||
var moveData_Return = JsonConvert.DeserializeObject<MoveDataClass_returning>(wrapclass.data.ToString());
|
||
elapsedTime = 0;
|
||
arrivalTime = moveData_Return.return_time;
|
||
originalPos = transform.position;
|
||
prevPos = transform.position;
|
||
if (origin != null)
|
||
{
|
||
destination = origin;
|
||
}
|
||
}
|
||
if (wrapclass._event.Contains("move_statistics_update"))
|
||
{
|
||
SetBubble(JsonConvert.DeserializeObject<MoveDataClassStatistics>(wrapclass.data.ToString()).statistics.total_moved);
|
||
}
|
||
}
|
||
|
||
public override void SetBubble(object data)
|
||
{
|
||
string msg = data.ToString();
|
||
|
||
if (currentBubble == null)
|
||
{
|
||
// <20><><EFBFBD><EFBFBD>
|
||
currentBubble = Instantiate(bubbleUIPrefab, FindAnyObjectByType<Canvas_Bubble>().transform);
|
||
currentBubble.target = DataBubbleSocket;
|
||
currentBubble.worldOffset = new Vector3(0, 2.0f, 0); // <20>ʿ信 <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||
}
|
||
// <20>ؽ<EFBFBD>Ʈ <20><><EFBFBD><EFBFBD>
|
||
currentBubble.SetMessage(msg);
|
||
}
|
||
}
|