using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
///
/// 挂载设备
///
public class Device : MonoBehaviour
{
public DeviceInfor mydata;
public bool isPut = false;
[ContextMenu("添加端口信息脚本")]
public void addscript()
{
//添加端口信息脚本
for (int i = 0; i < transform.childCount; i++)
{
var go = transform.GetChild(i);
mydata.ports.Add(go);
if (!go.GetComponent()) go.AddComponent();
if (!go.GetComponent()) go.AddComponent();
}
}
// Start is called before the first frame update
private void Awake()
{
}
void Start()
{
}
// Update is called once per frame
void Update()
{
if (isPut)
{
isPut = false;
refreshMyID();
LineInforChecker();
//自己的设备JSON
var myjson = refreshJson();
//更新设备JSON
string machineID = string.Format("{0}-{1}-{2}", mydata.area, mydata.row, mydata.tier);
lock (CombineJSON.jsonsDic)
{
if (CombineJSON.jsonsDic.ContainsKey(machineID))
{
CombineJSON.jsonsDic.Remove(machineID);
CombineJSON.jsonsDic.Add(machineID, myjson);
}
else CombineJSON.jsonsDic.Add(machineID, myjson);
}
}
}
///
/// 生成所有设备JSON
///
[ContextMenu("生成所有设备JSON")]
public void AllDeviceJson()
{
string AllDeviceJson = CombineJSON.GetCombineJSON();
Debug.Log(AllDeviceJson);
}
///
/// 刷新自己名字
///
void refreshMyID()
{
for (int i = 0; i < mydata.ports.Count; i++)
{
var po = mydata.ports[i].GetComponent();
var li = mydata.ports[i].GetComponent();
li.root.myID = string.Format("{0}-{1}-{2}-{3}", mydata.area, mydata.row, mydata.tier, i + 1);
li.transform.name = li.root.myID;
}
}
///
/// 更新该设备下所有端口信息(JSON)
///
string refreshJson()
{
List> strings = new List>();
var _line = GetComponentsInChildren().ToList();
for (int i = 0; i < _line.Count; i++)
{
var line = new List();
line.Add(_line[i].root.myID);
line.Add(_line[i].root.otherID);
line.Add(_line[i].root.lineUID);
strings.Add(line);
}
DeviceInfor person = new DeviceInfor();
person.devtype = mydata.devtype;
person.ports = null;
person.area = mydata.area;
person.row = mydata.row;
person.tier = mydata.tier;
person.lineInfors = new List>(strings);
string json = JsonConvert.SerializeObject(person);
//Debug.Log(json);
return json;
}
///
/// 遍历相同lineUID的物体
///
/// 寻找具体某一UID
void LineInforChecker(string uid = null)
{
bool isfind = false;
LineInfor[] lineInfors = FindObjectsOfType();
//记录相同lineUID
Dictionary> lineUIDDict = new Dictionary>();
foreach (var item in lineInfors)
{
if (string.IsNullOrEmpty(item.root.lineUID)) continue;
// 判断lineUID是否存在于字典中
if (lineUIDDict.ContainsKey(item.root.lineUID))
{
lineUIDDict[item.root.lineUID].Add(item);
}
else
{
List newList = new List();
newList.Add(item);
lineUIDDict.Add(item.root.lineUID, newList);
}
}
// 输出相同lineUID
foreach (var item in lineUIDDict)
{
if (item.Value.Count > 1)
{
//查找特定的GUID
if (uid != null)
{
if (item.Key == uid)
{
isfind = true;
Debug.Log(string.Format
("lineUID: {0},GameA Name: {1},GameB Name: {2}", item.Key, item.Value[0].root.myID, item.Value[1].root.myID));
refreshLineInfor(item.Key, item.Value[0], item.Value[1]);
break;
}
}
//遍历
else
{
isfind = true;
Debug.Log(string.Format
("lineUID: {0},GameA Name: {1},GameB Name: {2}", item.Key, item.Value[0].root.myID, item.Value[1].root.myID));
refreshLineInfor(item.Key, item.Value[0], item.Value[1]);
}
}
}
if (!isfind) Debug.Log("无匹配端口UID");
}
///
/// 刷新端口线条信息
///
public void refreshLineInfor(string GUID, LineInfor A, LineInfor B)
{
A.root.lineUID = GUID;
B.root.lineUID = GUID;
A.root.otherID = B.root.myID;
if (string.IsNullOrEmpty(B.root.otherID))
{
B.root.otherID = A.root.myID;
//更新B端口的设备下所有端口信息
var DeviceB = B.transform.parent.GetComponent();
if (DeviceB != null)
{
DeviceB.isPut = true;
}
}
}
}