This commit is contained in:
lwj
2025-05-29 09:25:40 +09:00
parent f25c610782
commit 8a75eaa23e
13 changed files with 963 additions and 64 deletions

View File

@@ -15,6 +15,7 @@ public class SimulationModel : MonoBehaviour, IClickable
}
private void OnEnable()
{
Debug.Log(this.name);
DataManager.I.AddModel(this);
StartCoroutine(RunSimulationCoroutine());
}

View File

@@ -0,0 +1,617 @@
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using Newtonsoft.Json.Linq;
using System.Collections.Concurrent;
using RTG;
using Unity.VisualScripting;
using Best.HTTP.JSON;
public class SimulationModelConveyor : SimulationModel
{
Vector3 startPosition;
Vector3 endPosition;
ConcurrentQueue<JObject> dataQueue;
GameObject targetCargo;
int transportTime;
float elapseTime;
string inputQueueID;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
startPosition = this.transform.Find(nameof(startPosition)).transform.position;
endPosition = this.transform.Find(nameof(endPosition)).transform.position;
}
// Update is called once per frame
void Update()
{
if ( targetCargo != null)
{
startPosition = this.transform.Find(nameof(startPosition)).transform.position;
endPosition = this.transform.Find(nameof(endPosition)).transform.position;
elapseTime += Time.deltaTime;
if (elapseTime < transportTime)
{
targetCargo.transform.position = Vector3.Lerp(startPosition, endPosition, elapseTime / transportTime);
}
else if ( elapseTime > transportTime )
{
//targetCargo = null;
elapseTime = 0;
}
}
}
protected override IEnumerator RunSimulationCoroutine()
{
Debug.Log("Start");
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.TryDequeue(out JObject currentData))
{
0string rawData = currentData.ToString();
Debug.Log(rawData);
if ( rawData.Contains("conveyor_loading"))
{
//targetCargo = Instantiate(this.gameObject);
//var resourcePath = "Models";
//var tmpModel = Resources.Load<GameObject>(string.Format("{0}/{1}", resourcePath, "Box_Pallet"));
//targetCargo = Instantiate(tmpModel);
SimulationModel model = DataManager.I.GetModel(inputQueueID);
SimulationModelStore storeModel = (SimulationModelStore)model;
if ( targetCargo == null)
{
yield return new WaitUntil(() =>
{
targetCargo = storeModel.GetProduct();
return targetCargo != null;
});
targetCargo.transform.position = startPosition;
targetCargo.transform.rotation = Quaternion.identity;
}
ConveyorLoadingEventMessage message
= JsonConvert.DeserializeObject<ConveyorLoadingEventMessage>(rawData);
Debug.Log(message);
}
if ( rawData.Contains("conveyor_started"))
{
string[] transportTimeparseKey = { "data", "move_time" };
ConveyorStartedEventMessage startDataMessage
= JsonConvert.DeserializeObject<ConveyorStartedEventMessage>(rawData);
transportTime = (int)startDataMessage.Data.MoveTime;
inputQueueID = startDataMessage.Data.InputQueues[0].Queue;
}
if ( rawData.Contains("conveyor_unloading"))
{
if ( rawData.Contains("_to_queue"))
{
ConveyorUnloadingToQueueEventMessage message
= JsonConvert.DeserializeObject<ConveyorUnloadingToQueueEventMessage>(rawData);
string destinationID = message.Data.QueueName;
SimulationModel model = DataManager.I.GetModel(destinationID);
SimulationModelStore storemodel = (SimulationModelStore)model;
storemodel.StoreProduct(targetCargo);
targetCargo = null;
}
else if ( rawData.Contains("_to_store"))
{
ConveyorUnloadingToStoreEventMessage message
= JsonConvert.DeserializeObject<ConveyorUnloadingToStoreEventMessage>(rawData);
string destinationID = message.Data.StoreName;
SimulationModel model = DataManager.I.GetModel(destinationID);
SimulationModelStore storemodel = (SimulationModelStore)model;
storemodel.StoreProduct(targetCargo);
targetCargo = null;
}
}
}
yield return null;
}
}
}
[Serializable]
public class ConveyorUnloadingToStoreEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("simulation_time")]
public double? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorUnloadingToStoreData? Data { get; set; }
[JsonProperty("timestamp")]
public string? Timestamp { get; set; }
}
[Serializable]
public class ConveyorUnloadingToStoreData
{
[JsonProperty("product_id")]
public string? ProductId { get; set; }
[JsonProperty("store_name")]
public string? StoreName { get; set; }
[JsonProperty("store_length")]
public int? StoreLength { get; set; }
[JsonProperty("destination_type")]
public string? DestinationType { get; set; }
[JsonProperty("is_defective")]
public bool? IsDefective { get; set; }
[JsonProperty("items_count")]
public int? ItemsCount { get; set; }
[JsonProperty("remaining_capacity")]
public int? RemainingCapacity { get; set; }
[JsonProperty("transported_count")]
public int? TransportedCount { get; set; }
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("belt_speed")]
public double? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public double? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class ConveyorUnloadingToQueueEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("simulation_time")]
public double? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorUnloadingToQueueData? Data { get; set; }
[JsonProperty("timestamp")]
public string? Timestamp { get; set; }
}
[Serializable]
public class ConveyorUnloadingToQueueData
{
[JsonProperty("product_id")]
public string? ProductId { get; set; }
[JsonProperty("queue_name")]
public string? QueueName { get; set; }
[JsonProperty("queue_length")]
public int? QueueLength { get; set; }
[JsonProperty("destination_type")]
public string? DestinationType { get; set; }
[JsonProperty("is_defective")]
public bool? IsDefective { get; set; }
[JsonProperty("items_count")]
public int? ItemsCount { get; set; }
[JsonProperty("remaining_capacity")]
public int? RemainingCapacity { get; set; }
[JsonProperty("transported_count")]
public int? TransportedCount { get; set; }
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("belt_speed")]
public double? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public double? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class ConveyorUnloadingEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("component_label")]
public string? ComponentLabel { get; set; }
[JsonProperty("simulation_time")]
public float? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorUnloadingData? Data { get; set; }
}
[Serializable]
public class ConveyorUnloadingData
{
[JsonProperty("product_id")]
public string? ProductId { get; set; }
[JsonProperty("product_position")]
public float? ProductPosition { get; set; }
[JsonProperty("travel_time")]
public float? TravelTime { get; set; }
[JsonProperty("is_defective")]
public bool? IsDefective { get; set; }
[JsonProperty("destination_queue")]
public string? DestinationQueue { get; set; }
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("belt_speed")]
public float? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public float? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class ConveyorStartedEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("component_label")]
public string? ComponentLabel { get; set; }
[JsonProperty("simulation_time")]
public float? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorStartedData? Data { get; set; }
}
[Serializable]
public class ConveyorStartedData
{
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("move_time")]
public int? MoveTime { get; set; }
[JsonProperty("belt_speed")]
public float? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public float? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("min_gap")]
public float? MinGap { get; set; }
[JsonProperty("input_queues")]
public List<InputQueue>? InputQueues { get; set; }
[JsonProperty("output_queue")]
public string? OutputQueue { get; set; }
[JsonProperty("defect_queue")]
public string? DefectQueue { get; set; }
[JsonProperty("required_resource")]
public string? RequiredResource { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class InputQueue
{
[JsonProperty("queue")]
public string? Queue { get; set; }
[JsonProperty("required_items")]
public int? RequiredItems { get; set; }
}
[Serializable]
public class ConveyorLoadingEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("component_label")]
public string? ComponentLabel { get; set; }
[JsonProperty("simulation_time")]
public float? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorLoadingData? Data { get; set; }
}
[Serializable]
public class ConveyorLoadingData
{
[JsonProperty("product_id")]
public string? ProductId { get; set; }
[JsonProperty("belt_load_percentage")]
public float? BeltLoadPercentage { get; set; }
[JsonProperty("first_item_position")]
public float? FirstItemPosition { get; set; }
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("belt_speed")]
public float? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public float? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class ConveyorEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("component_label")]
public string? ComponentLabel { get; set; }
[JsonProperty("simulation_time")]
public float? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorEventData? Data { get; set; }
}
[Serializable]
public class ConveyorEventData
{
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("move_time")]
public int? MoveTime { get; set; }
[JsonProperty("belt_speed")]
public float? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public float? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("min_gap")]
public float? MinGap { get; set; }
[JsonProperty("input_queues")]
public List<InputQueue>? InputQueues { get; set; }
[JsonProperty("output_queue")]
public string? OutputQueue { get; set; }
[JsonProperty("defect_queue")]
public string? DefectQueue { get; set; }
[JsonProperty("required_resource")]
public string? RequiredResource { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}
[Serializable]
public class ConveyorMovingEventMessage
{
[JsonProperty("type")]
public string? Type { get; set; }
[JsonProperty("_event")]
public string? Event { get; set; }
[JsonProperty("component_type")]
public string? ComponentType { get; set; }
[JsonProperty("component_id")]
public string? ComponentId { get; set; }
[JsonProperty("component_label")]
public string? ComponentLabel { get; set; }
[JsonProperty("simulation_time")]
public float? SimulationTime { get; set; }
[JsonProperty("data")]
public ConveyorMovingData? Data { get; set; }
}
[Serializable]
public class ConveyorMovingData
{
[JsonProperty("item_positions")]
public List<float>? ItemPositions { get; set; }
[JsonProperty("average_position")]
public float? AveragePosition { get; set; }
[JsonProperty("current_speed")]
public float? CurrentSpeed { get; set; }
[JsonProperty("conveyor_id")]
public string? ConveyorId { get; set; }
[JsonProperty("conveyor_label")]
public string? ConveyorLabel { get; set; }
[JsonProperty("belt_speed")]
public float? BeltSpeed { get; set; }
[JsonProperty("belt_length")]
public float? BeltLength { get; set; }
[JsonProperty("capacity")]
public int? Capacity { get; set; }
[JsonProperty("items_on_belt")]
public int? ItemsOnBelt { get; set; }
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3c1c4b267f3ba2a4db0bf182de028232

View File

@@ -25,6 +25,9 @@ public class SimulationModelMove : SimulationModel
float elapsedTime = 0;
Vector3 originalPos;
Vector3 prevPos;
bool isRewind = false;
void Update()
{
int interval = 0;
@@ -41,6 +44,7 @@ public class SimulationModelMove : SimulationModel
if (destination != null)
{
elapsedTime += Time.deltaTime;
if (elapsedTime / arrivalTime > 1.0f)
{
transform.position = destination.position;
@@ -55,7 +59,8 @@ public class SimulationModelMove : SimulationModel
dir.Normalize();
transform.forward = dir;
prevPos = transform.position;
}
}
}
}
protected override IEnumerator RunSimulationCoroutine()
@@ -85,6 +90,7 @@ public class SimulationModelMove : SimulationModel
string[] adjustedTimeKey = { "data", "adjusted" };
string[] inputQueueKey = { "data", "input", "queues" };
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" };
@@ -93,11 +99,46 @@ public class SimulationModelMove : SimulationModel
if (currentData.ContainsKey(eventKey))
{
string value = currentData[eventKey].ToString();
//if (value.Contains(eventMove))
//{
// Debug.Log("MoveeventIncomming");
// elapsedTime = 0;
// arrivalTime = GetJsonFloatValue(currentData, arrivalTimeKey);
// originalPos = transform.position;
// prevPos = transform.position;
// JArray originQueues = GetJsonArray(currentData, inputQueueKey);
// string arrivalQueue = GetJsonValue(currentData, outputQueueKey)?.ToString();
// if ( string.IsNullOrEmpty(arrivalQueue))
// {
// arrivalQueue = 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();
// }
// if (arrivalQueue != null)
// {
// SimulationModel model = DataManager.I.GetModel(arrivalQueue);
// SimulationModelStore storeModel = (SimulationModelStore)model;
// destination = storeModel.GetTransporterPosition();
// }
//}
if (value.Contains(eventLoading))
{
JArray sourceQueues = GetJsonArray(currentData, sourceQueueKey);
JArray sourceStores = GetJsonArray(currentData, sourceStoreKey);
int loadCount = GetJsonIntValue(currentData, loadCountKey);
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();
@@ -106,9 +147,11 @@ public class SimulationModelMove : SimulationModel
for (int i = 0; i < loadCount; i++)
{
GameObject product = null;
yield return new WaitUntil(() => {
yield return new WaitUntil(() =>
{
product = storeModel.GetProduct();
return product != null; });
return product != null;
});
product.transform.parent = productPos;
product.transform.localPosition = Vector3.zero;
product.transform.localRotation = Quaternion.identity;
@@ -123,7 +166,8 @@ public class SimulationModelMove : SimulationModel
for (int i = 0; i < loadCount; i++)
{
GameObject product = null;
yield return new WaitUntil(() => {
yield return new WaitUntil(() =>
{
product = storeModel.GetProduct();
return product != null;
});
@@ -135,8 +179,10 @@ public class SimulationModelMove : SimulationModel
}
else if (value.Contains(eventMove))
{
Debug.Log("MoveeventIncomming");
elapsedTime = 0;
arrivalTime = GetJsonFloatValue(currentData, arrivalTimeKey);
arrivalTime = GetJsonFloatValue(currentData, arrivalTimeKey);
originalPos = transform.position;
prevPos = transform.position;
JArray originQueues = GetJsonArray(currentData, inputQueueKey);
@@ -146,17 +192,19 @@ public class SimulationModelMove : SimulationModel
string queueID = originQueues[0].ToString();
SimulationModel model = DataManager.I.GetModel(queueID);
SimulationModelStore storeModel = (SimulationModelStore)model;
origin = storeModel.GetTransporterPosition();
origin = storeModel.GetTransporterPosition();
}
if (arrivalQueue != null)
{
{
SimulationModel model = DataManager.I.GetModel(arrivalQueue);
SimulationModelStore storeModel = (SimulationModelStore)model;
destination = storeModel.GetTransporterPosition();
destination = storeModel.GetTransporterPosition();
}
}
else if (value.Contains(eventUnloading))
{
{
yield return null;
string queueID = GetJsonValue(currentData, unloadQueueKey)?.ToString();
SimulationModel model = DataManager.I.GetModel(queueID);
SimulationModelStore storeModel = (SimulationModelStore)model;
@@ -176,7 +224,7 @@ public class SimulationModelMove : SimulationModel
float originalTime = GetJsonFloatValue(currentData, originalTimeKey);
float adjustedTime = GetJsonFloatValue(currentData, adjustedTimeKey);
arrivalTime += adjustedTime - originalTime;
elapsedTime = 0;
elapsedTime = 0;
originalPos = transform.position;
prevPos = transform.position;
}