446 lines
12 KiB
C#
446 lines
12 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using UnityEngine.UIElements;
|
|
using System.Reflection.Emit;
|
|
|
|
namespace Octopus.Simulator
|
|
{
|
|
[Serializable]
|
|
public class SimulationInfo
|
|
{
|
|
public int status;
|
|
public string code;
|
|
public string message;
|
|
public SimulationData data = new SimulationData();
|
|
public Meta meta;
|
|
public string timestamp;
|
|
|
|
}
|
|
|
|
[Serializable]
|
|
public class SimulationData
|
|
{
|
|
public int id;
|
|
public int projectId;
|
|
//public int logicId;
|
|
public int userId;
|
|
public string name;
|
|
public string simulationCode;
|
|
public logicData data;
|
|
public LogicWebConfig webConfig;
|
|
//public SimulationParameters parameters;
|
|
public string status;
|
|
public string resultData;
|
|
public string createdAt;
|
|
public string updatedAt;
|
|
public string deletedAt;
|
|
//public SimulationProject project;
|
|
//public SimulationComponent component;
|
|
//public SimulationUser user;
|
|
}
|
|
|
|
[Serializable]
|
|
public class logicData
|
|
{
|
|
public string name;
|
|
public bool trace;
|
|
public List<LogicQueue> queues;
|
|
public List<LogicResource> resources;
|
|
public List<LogicStore> stores;
|
|
[SerializeReference]
|
|
[JsonConverter(typeof(ComponentsConverter))]
|
|
public List<ILogicComponent> components;
|
|
public int simulation_time;
|
|
}
|
|
|
|
[Serializable]
|
|
public class SimulationParameters
|
|
{
|
|
public int speed;
|
|
public int duration;
|
|
public bool realtime;
|
|
}
|
|
public enum LogicItemType
|
|
{
|
|
Queue,
|
|
Resource,
|
|
Store,
|
|
Component
|
|
}
|
|
|
|
public enum ComponentType
|
|
{
|
|
Product_Generator,
|
|
Processor,
|
|
Transporter,
|
|
Shipment,
|
|
Conveyor
|
|
}
|
|
|
|
public interface ILogicItem
|
|
{
|
|
string Name { get; }
|
|
string Description { get; }
|
|
string Label { get; }
|
|
LogicItemType ItemType { get; }
|
|
}
|
|
[Serializable]
|
|
public class LogicQueue : ILogicItem
|
|
{
|
|
public string name = "";
|
|
public string description = "";
|
|
public string label;
|
|
public int capacity=0;
|
|
public int quantity;
|
|
public int priority_order;
|
|
public string queue_type;
|
|
public string Name => name;
|
|
public string Description => description;
|
|
public string Label => label;
|
|
public LogicItemType ItemType => LogicItemType.Queue;
|
|
}
|
|
|
|
[Serializable]
|
|
public class LogicResource : ILogicItem
|
|
{
|
|
public string name = "";
|
|
public int capacity = 0;
|
|
public string description = "";
|
|
public string label;
|
|
public int? repair_time = 0;
|
|
public double speed_factor = 0.0;
|
|
public int? breakdown_interval = 0;
|
|
public string Name => name;
|
|
public string Description => description;
|
|
public string Label => label;
|
|
public LogicItemType ItemType => LogicItemType.Resource;
|
|
}
|
|
|
|
[Serializable]
|
|
public class LogicStore : ILogicItem
|
|
{
|
|
public string name;
|
|
public string description;
|
|
public string label;
|
|
public int capacity;
|
|
public int quantity;
|
|
public int priority_order;
|
|
public string Name => name;
|
|
public string Description => description;
|
|
public string Label => label;
|
|
public LogicItemType ItemType => LogicItemType.Store;
|
|
}
|
|
|
|
[Serializable]
|
|
public abstract class ILogicComponent : ILogicItem
|
|
{
|
|
public abstract ComponentType Type { get; }
|
|
public abstract string Name { get; }
|
|
public abstract string Description { get; }
|
|
public abstract string Label { get; }
|
|
public LogicItemType ItemType => LogicItemType.Component;
|
|
}
|
|
|
|
[Serializable]
|
|
public class ProductGenerator : ILogicComponent
|
|
{
|
|
public string name;
|
|
public int rate;
|
|
public string description;
|
|
public string label;
|
|
public string output_queue;
|
|
public string defect_queue;
|
|
public string output_resource;
|
|
public int prod_priority;
|
|
public int batch_size;
|
|
public int defect_rate;
|
|
public int period;
|
|
public int? capacity=0;
|
|
public override string Name => name;
|
|
public override string Description => description;
|
|
public override string Label => label;
|
|
public override ComponentType Type => ComponentType.Product_Generator;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Processor : ILogicComponent
|
|
{
|
|
public string name;
|
|
public string description;
|
|
public string label;
|
|
public int processing_time;
|
|
public int batch_size;
|
|
public List<InputQueue> input_queues;
|
|
public string output_queue;
|
|
public string defect_queue;
|
|
public string required_resource;
|
|
public int process_amount;
|
|
public float defect_rate;
|
|
public override string Name => name;
|
|
public override string Description => description;
|
|
public override string Label => label;
|
|
public override ComponentType Type => ComponentType.Processor;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Transporter : ILogicComponent
|
|
{
|
|
public string name;
|
|
public string description;
|
|
public string label;
|
|
public int? transport_time=0;
|
|
public float? transport_distance = 0f;
|
|
public float? movement_speed = 0f;
|
|
public int? capacity=0;
|
|
public string input_queue;
|
|
public string output_queue;
|
|
public string defect_queue;
|
|
public string required_resource;
|
|
public string transporter_type;
|
|
public float return_time;
|
|
public int min_batch_size;
|
|
public override string Name => name;
|
|
public override string Description => description;
|
|
public override string Label => label;
|
|
public override ComponentType Type => ComponentType.Transporter;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Shipment : ILogicComponent
|
|
{
|
|
public string name;
|
|
public string description;
|
|
public string label;
|
|
public int? shipping_interval=0;
|
|
public int? shipping_amount=0;
|
|
public List<InputQueue> input_queues;
|
|
public override string Name => name;
|
|
public override string Description => description;
|
|
public override string Label => label;
|
|
public override ComponentType Type => ComponentType.Shipment;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Conveyor : ILogicComponent
|
|
{
|
|
public string name;
|
|
public string description;
|
|
public string label;
|
|
public int move_time;
|
|
public float belt_speed;
|
|
public int? capacity=0;
|
|
public string input_queue;
|
|
public string output_queue;
|
|
public string defect_queue;
|
|
public string required_resource;
|
|
public override string Name => name;
|
|
public override string Description => description;
|
|
public override string Label => label;
|
|
public override ComponentType Type => ComponentType.Conveyor;
|
|
}
|
|
|
|
[Serializable]
|
|
public class InputQueue
|
|
{
|
|
public string name;
|
|
public int required_items;
|
|
}
|
|
|
|
[Serializable]
|
|
public class SimulationProject
|
|
{
|
|
public int id;
|
|
public int userId;
|
|
public string name;
|
|
public string description;
|
|
public string createdAt;
|
|
public string updatedAt;
|
|
}
|
|
|
|
[Serializable]
|
|
public class SimulationComponent
|
|
{
|
|
public int id;
|
|
public int userId;
|
|
public string name;
|
|
public string description;
|
|
public string createdAt;
|
|
public string updatedAt;
|
|
}
|
|
|
|
[Serializable]
|
|
public class SimulationUser
|
|
{
|
|
public int id;
|
|
public string ccPositionId;
|
|
public string userid;
|
|
public string name;
|
|
public string auth;
|
|
public string email;
|
|
public bool active;
|
|
public string joinDate;
|
|
public bool activeClassifyRule;
|
|
public string signatureId;
|
|
public string employeeNumber;
|
|
public string resignationDate;
|
|
public string profileId;
|
|
public string createdAt;
|
|
public string updatedAt;
|
|
}
|
|
|
|
[Serializable]
|
|
public class LogicWebConfig
|
|
{
|
|
public List<Node> nodes;
|
|
public List<Edge> edges;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Edge
|
|
{
|
|
public string id;
|
|
public EdgeData data;
|
|
public string type;
|
|
public string label;
|
|
public string source;
|
|
public string target;
|
|
public float sourceX;
|
|
public float sourceY;
|
|
public float targetX;
|
|
public float targetY;
|
|
}
|
|
|
|
[Serializable]
|
|
public class EdgeData
|
|
{
|
|
public string name;
|
|
public string type;
|
|
public string label;
|
|
public bool visible;
|
|
public bool isIgnore;
|
|
public bool isShowName;
|
|
public string description;
|
|
public Node sourceNode;
|
|
public Node targetNode;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Node
|
|
{
|
|
public string id;
|
|
public NodeData data;
|
|
public string type;
|
|
public bool dragging;
|
|
public bool isParent;
|
|
public Position position;
|
|
public bool resizing;
|
|
public bool selected;
|
|
public Dimension dimensions;
|
|
public bool initialized;
|
|
public HandleBounds handleBounds;
|
|
public ComputedPosition computedPosition;
|
|
}
|
|
|
|
[Serializable]
|
|
public class NodeData
|
|
{
|
|
public string name;
|
|
public string type;
|
|
public string label;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Position
|
|
{
|
|
public float x;
|
|
public float y;
|
|
}
|
|
|
|
|
|
[Serializable]
|
|
public class Dimension
|
|
{
|
|
public float width;
|
|
public float height;
|
|
}
|
|
[Serializable]
|
|
public class HandleBounds
|
|
{
|
|
public List<Handle> source;
|
|
public List<Handle> target;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Handle
|
|
{
|
|
public float x;
|
|
public float y;
|
|
public string id;
|
|
public float width;
|
|
public float height;
|
|
public string position;
|
|
}
|
|
|
|
[Serializable]
|
|
public class ComputedPosition : Position
|
|
{
|
|
public float z;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Meta
|
|
{
|
|
public Param param;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Param
|
|
{
|
|
public int id;
|
|
}
|
|
|
|
public class ComponentsConverter : JsonConverter<List<ILogicComponent>>
|
|
{
|
|
public override bool CanWrite => false;
|
|
|
|
public override List<ILogicComponent> ReadJson(JsonReader reader, Type objectType, List<ILogicComponent> existingValue, bool hasExistingValue, JsonSerializer serializer)
|
|
{
|
|
// 배열 로드
|
|
var array = JArray.Load(reader);
|
|
var list = new List<ILogicComponent>();
|
|
|
|
foreach (var token in array)
|
|
{
|
|
var jo = (JObject)token;
|
|
var type = jo["type"]?.Value<string>()
|
|
?? throw new JsonSerializationException("components 요소에 'type' 필드가 없습니다.");
|
|
|
|
// 타입별 인스턴스 생성
|
|
ILogicComponent comp = type switch
|
|
{
|
|
"product_generator" => new ProductGenerator(),
|
|
"processor" => new Processor(),
|
|
"transporter" => new Transporter(),
|
|
"shipment" => new Shipment(),
|
|
"conveyor" => new Conveyor(),
|
|
_ => throw new NotSupportedException($"지원하지 않는 component type: {type}")
|
|
};
|
|
|
|
// JSON → 인스턴스 채우기
|
|
serializer.Populate(jo.CreateReader(), comp);
|
|
list.Add(comp);
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
public override void WriteJson(JsonWriter writer, List<ILogicComponent> value, JsonSerializer serializer)
|
|
{
|
|
// 쓰기가 필요 없으면 예외 또는 기본 직렬화
|
|
serializer.Serialize(writer, value);
|
|
}
|
|
}
|
|
} |