using AutoMapper;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using DG.Tweening;
using static DeviceQuery;
using static ENVQuery;
using static PortQuery;
using static TmsAlarmQuery;
public class GameManager : MonoBehaviour
{
public static GameManager Inst;
///
/// 接口地址
///
public webURL Jk_URL;
///
/// 全局变量
///
public GlobalParameter arguments = new GlobalParameter();
///
/// 是否加载完成
///
public bool isLoading = false;
string ct = null;
//[Header("机柜点位集合")]
//public List Cabinets_POS;
[Header("机柜区域集合")]
public List Cabinet_quYu;
[Header("当前设备ID")] public string nowDeviceID;
[Header("当前点击设备")] public GameObject nowDevice;
[Header("设备------------------------查询接口")] public DeviceQuery.Root root_AllDevice;
/////
///// 场景所有机柜
/////
// public List Cabinets_go;
///
/// 场景所有机柜
///
[Header("场景所有机柜")] public List Cabinets_go;
///
/// 场景所有机柜信息
///
public List Cabinets;
///
/// 场景所有机框
///
[Header("场景所有机框信息")] public List Racks_go;
///
/// 场景所有机框信息
///
public List Racks;
///
/// 场景所有机槽
///
[Header("场景所有机槽信息")] public List MachineSlots_go;
///
/// 场景所有机槽信息
///
public List MachineSlots;
///
/// 场景所有板卡
///
[Header("场景所有板卡信息")] public List TmsCards_go;
///
/// 场景所有板卡信息
///
public List TmsCards;
[Header("端口------------------------查询接口")] public PortQuery.Root root_AllPort;
///
/// 场景所有端口
///
[Header("场景所有端口信息")] public List TmsPorts_go;
///
/// 场景所有端口信息
///
public List TmsPorts;
[HideInInspector] public int nowLayerMask = 6;
[Header("环境------------------------查询接口")] public ENVQuery.Root root_AllENV;
public List ENVs;
[Header("告警------------------------查询接口")] public TmsAlarmQuery.Root root_AllAlarm;
public List tmsAlarms;
///
/// 搜索框
///
[Header("--------------------------------")]
public GameObject search_box;
[Header("----------弹窗--------------")]
public List pop_ups;
///
/// 场景方大状态
///
[Header("----------场景方大状态--------------")]
public bool magnifyState = false;
[Header("----------是否在循环读取信息--------------")]
public bool isgonging = false;
private void Awake()
{
if (Inst != this && Inst != null)
{
Destroy(this.gameObject);
}
else
{
Inst = this;
DontDestroyOnLoad(this.gameObject);
}
//StartCoroutine(LoadAddress());
}
private void Start()
{
}
private void OnEnable()
{
//StartCoroutine(LoadJSON(Application.streamingAssetsPath + "/设备查询.json"));
//StartCoroutine(LoadJSON(Application.streamingAssetsPath + "/端口查询.json"));
//StartCoroutine(LoadJSON(Application.streamingAssetsPath + "/告警查询.json"));
//StartCoroutine(LoadJSON(Application.streamingAssetsPath + "/环境查询.json"));
//return;
#region 正式
//TransparentGlowManage.Inst.addScript();
//await init_Device();
//await init_Port();
//await init_TmsAlarm();
//await init_ENV();
//if (root_AllDevice.data == null) return;
//#region 加载场景
//Load_Device();
//Load_Port();
//Load_TmsAlarm();
//Load_ENV();
//#endregion
#endregion
}
///
/// 加载环境
///
public void Load_ENV()
{
if (root_AllENV == null) return;
/***********************************************************/
//环境信息-保存
for (int i = 0; i < root_AllENV.data.Count; i++)
ENVs.Add(root_AllENV.data[i]);
for (int i = 0; i < ENVs.Count; i++)
{
bool isFind = false;
var rackId = ENVs[i].rackId;
foreach (var item in Cabinets_go)
{
var D = item.GetComponent().deviceList;
if (D.id == rackId)
{
//温度
if (ENVs[i].pointType == "1")
{
item.GetComponent().eNVList_W = ENVs[i];
isFind = true;
break;
}
//湿度
else if (ENVs[i].pointType == "2")
{
item.GetComponent().eNVList_S = ENVs[i];
isFind = true;
break;
}
}
}
if (isFind) continue;
var machine_room = GameObject.Find("机房");
if (machine_room && rackId == machine_room.GetComponent().deviceList.id)
{
//温度
if (ENVs[i].pointType == "1")
{
machine_room.GetComponent().eNVList_W = ENVs[i];
isFind = true;
}
//湿度
else if (ENVs[i].pointType == "2")
{
machine_room.GetComponent().eNVList_S = ENVs[i];
isFind = true;
}
}
if (isFind) continue;
var accumulator_room = GameObject.Find("蓄电池室");
if (accumulator_room && rackId == accumulator_room.GetComponent().deviceList.id)
{
//温度
if (ENVs[i].pointType == "1")
{
accumulator_room.GetComponent().eNVList_W = ENVs[i];
isFind = true;
}
//湿度
else if (ENVs[i].pointType == "2")
{
accumulator_room.GetComponent().eNVList_S = ENVs[i];
isFind = true;
}
}
if (isFind) continue;
}
Debug.Log("环境加载完成");
}
///
/// 加载告警
///
public void Load_TmsAlarm()
{
if (root_AllAlarm == null) return;
/***********************************************************/
//告警信息-保存
for (int i = 0; i < root_AllAlarm.data.Count; i++)
tmsAlarms.Add(root_AllAlarm.data[i]);
for (int i = 0; i < tmsAlarms.Count; i++)
{
bool isFind = false;
var deviceId = tmsAlarms[i].deviceId;
///设备id
foreach (var item in Cabinets_go)
{
var D = item.GetComponent().deviceList;
if (D.id == deviceId)
{
item.GetComponent().tmsAlarmLists.Add(tmsAlarms[i]);
isFind = true;
break;
}
}
if (isFind) continue;
foreach (var item in Racks_go)
{
var D = item.GetComponent().deviceList;
if (D.id == deviceId)
{
item.GetComponent().tmsAlarmLists.Add(tmsAlarms[i]);
isFind = true;
break;
}
}
if (isFind) continue;
foreach (var item in MachineSlots_go)
{
var D = item.GetComponent().deviceList;
if (D.id == deviceId)
{
item.GetComponent().tmsAlarmLists.Add(tmsAlarms[i]);
isFind = true;
break;
}
}
if (isFind) continue;
foreach (var item in TmsCards_go)
{
var D = item.GetComponent().deviceList;
if (D.id == deviceId)
{
item.GetComponent().tmsAlarmLists.Add(tmsAlarms[i]);
isFind = true;
break;
}
}
}
Debug.Log("告警加载完成");
}
///
/// 加载端口
///
private void Load_Port()
{
if (root_AllPort == null) return;
/***********************************************************/
//端口信息-保存
foreach (var item in root_AllPort.data)
TmsPorts.Add(item);
//动态加载设备(端口)
for (int i = 0; i < TmsCards_go.Count; i++)//遍历板卡
{
//板卡下所有设备
var dk = TmsCards_go[i].transform.GetComponentsInChildren().ToList();
if (dk == null) continue;
for (int j = 0; j < dk.Count; j++)
{
TmsPorts_go.Add(dk[j].gameObject);
}
}
for (int i = 0; i < Racks_go.Count; i++)//遍历机框
{
//机框下所有设备
var dk = Racks_go[i].transform.GetComponentsInChildren().ToList();
if (dk == null) continue;
for (int j = 0; j < dk.Count; j++)
{
TmsPorts_go.Add(dk[j].gameObject);
}
}
for (int i = 0; i < MachineSlots_go.Count; i++)//遍历机槽
{
//机槽下所有设备
var dk = MachineSlots_go[i].transform.GetComponentsInChildren().ToList();
if (dk == null) continue;
for (int j = 0; j < dk.Count; j++)
{
TmsPorts_go.Add(dk[j].gameObject);
}
}
//读取端口配置
for (int i = 0; i < TmsPorts_go.Count; i++)
{
PortQuery P;
DeviceQuery DP;
P = TmsPorts_go[i].GetComponent();
DP = TmsPorts_go[i].transform.parent.GetComponent();
if (!DP) continue;
foreach (var item in TmsPorts)
{
if (item.deviceId == DP.deviceList.id && item.portPosition == P.portList.portPosition)
{
TmsPorts_go[i].GetComponent().portList = item;
break;
}
}
}
Array.ForEach(GameObject.FindObjectsOfType(), item =>
{
item.gameObject.AddComponent();
});
Debug.Log("端口加载完成");
}
///
/// 加载设备
///
private void Load_Device()
{
if (root_AllDevice == null) return;
//信息分类-保存
foreach (var item in root_AllDevice.data)
{
//(机柜)
if (item.type == "1")
Cabinets.Add(item);
//(机框)
if (item.type == "2" && item.deviceType == "1")
Racks.Add(item);
//(机槽)
else if (item.type == "2" && item.deviceType == "2")
MachineSlots.Add(item);
//(板卡)
else if (item.type == "2" && item.deviceType == "3")
TmsCards.Add(item);
}
//读取机柜配置
#region 单独机柜
//if (Cabinets_POS != null)
//{
// for (int i = 0; i < Cabinets_POS.Count; i++)
// {
// for (int j = 0; j < Cabinets.Count; j++)
// {
// if (Cabinets_POS[i].name == Cabinets[j].devicePosition && Cabinets[j].type == "1")
// {
// GameObject go;
// try
// {
// go = Instantiate(Resources.Load("古泉站换流站机房/" + ""));//??
// }
// catch (Exception e)
// {
// Debug.Log("模型生成错误:" + e.Message);
// break;
// }
// var n = Cabinets_POS[i].name.Split('-');//拆分区域-编号
// go.name = Cabinets_POS[i].name;
// go.transform.SetParent(Cabinet_quYu[CengJi(n[0])]);
// go.transform.position = Cabinets_POS[i].position;
// go.transform.rotation = Cabinets_POS[i].rotation;
// go.GetComponent().deviceList = root_AllDevice.data[j];
// Cabinets_go.Add(go);
// break;
// }
// }
// }
//}
#endregion
//读取机柜配置
for (int i = 0; i < Cabinets_go.Count; i++)
{
var n = Cabinets_go[i].GetComponent().deviceList;
for (int j = 0; j < Cabinets.Count; j++)
{
if (n.devicePosition == Cabinets[j].devicePosition)
{
n = Cabinets[j];
Cabinets_go[i].GetComponent().deviceList = n;
//AssignFields(n, Cabinets[j]);
break;
}
}
}
//读取机柜环境配置
for (int j = 0; j < ENVs.Count;)
{
for (int i = 0; i < Cabinets_go.Count; i++)
{
var n = Cabinets_go[i].GetComponent().deviceList;
var e = Cabinets_go[i].GetComponent();
if (ENVs[j].pointDeviceId == n.id)
{
if (ENVs[j].pointType == "1")
{
e.eNVList_W = ENVs[j];
break;
}
else if (ENVs[j].pointType == "2")
{
e.eNVList_S = ENVs[j];
break;
}
else if (ENVs[j].pointType == "3")
{
}
else if (ENVs[j].pointType == "4")
{
}
}
}
}
//动态加载设备(机框)
for (int i = 0; i < Racks.Count; i++)
{
foreach (var item in Cabinets_go)
{
if (item.GetComponent().deviceList.id == Racks[i].rackId)
{
//获取模型编号
string modelNum = Racks[i].modelNum;
//获取U位
var U = Racks[i].devicePosition;
GameObject go;
try
{
go = Instantiate(Resources.Load("古泉站机房模型90个型号/" + modelNum));
//写入机框配置
go.GetComponent().deviceList = Racks[i];
Racks_go.Add(go);
Renderer renderer = go.GetComponentInChildren();
if (renderer)
{
var initrot = go.transform.rotation;
go.transform.rotation = Quaternion.identity;
var bounds = renderer.bounds;
var a = go.AddComponent();
a.isTrigger = false;
a.center = bounds.center - go.transform.position;
a.size = bounds.size;
//a.transform.rotation = Quaternion.identity; //重置其旋转为默认值
go.transform.rotation = initrot;
}
Vector3 center = renderer.bounds.center;
go.transform.position = item.transform.Find("U位/" + U).position;
var anchorPos = go.transform.Find("锚点").gameObject;
var collider = item.transform.Find("U位/" + U).GetComponent();
go.transform.position = new Vector3(anchorPos.transform.position.x, anchorPos.transform.position.y + collider.bounds.size.y / 2, anchorPos.transform.position.z);
//go.transform.rotation = item.transform.Find("U位/" + U).rotation;
go.name = Racks[i].id;
if (!go.GetComponent())
go.AddComponent();
go.GetComponent().isplace = true;
go.transform.SetParent(item.transform.Find("U位/" + U));
go.GetComponent().isplace = true;
Array.ForEach(go.transform.GetComponentsInChildren(), (item) =>
{
item.enabled = false;
});
}
catch (Exception e)
{
Debug.Log("模型生成错误:" + item.name + "\n" + e.Message);
break;
}
}
}
}
//动态加载设备(机槽)
for (int i = 0; i < Racks_go.Count; i++)//遍历机框
{
//机框下所有设备
var jc = Racks_go[i].transform.GetComponentsInChildren().ToList();
for (int j = 0; j < jc.Count; j++)
{
//如果是机槽
if (jc[j].deviceList.deviceType == "2")
{
MachineSlots_go.Add(jc[j].gameObject);
}
}
}
//读取机槽配置
for (int i = 0; i < MachineSlots_go.Count; i++)
{
DeviceQuery D;
DeviceQuery DP;
D = MachineSlots_go[i].transform.GetComponent();//机槽
DP = MachineSlots_go[i].transform.parent.GetComponent();//机槽父物体
if (!DP) continue;
foreach (var item in MachineSlots)
{
//机槽在机框里
/*if (item.deviceType == "2")
{
}*/
if (DP.deviceList.deviceType == "1" && item.shelfId == DP.deviceList.id && item.devicePosition == D.deviceList.devicePosition)
{
MachineSlots_go[i].GetComponent().deviceList = item;
break;
}
}
}
//动态加载设备(板卡)
for (int i = 0; i < MachineSlots_go.Count; i++)//遍历机槽
{
//机槽下所有设备
var bk = MachineSlots_go[i].transform.GetComponentsInChildren().ToList();
for (int j = 0; j < bk.Count; j++)
{
//如果是板卡
if (bk[j].deviceList.deviceType == "3")
{
TmsCards_go.Add(bk[j].gameObject);
}
}
}
for (int i = 0; i < Racks_go.Count; i++)//遍历机框
{
//机框下所有设备
var bk = Racks_go[i].transform.GetComponentsInChildren().ToList();
for (int j = 0; j < bk.Count; j++)
{
//如果是板卡
if (bk[j].deviceList.deviceType == "3")
{
TmsCards_go.Add(bk[j].gameObject);
}
}
}
//读取板卡配置
for (int i = 0; i < TmsCards_go.Count; i++)
{
DeviceQuery DP;
DeviceQuery D;
DP = TmsCards_go[i].transform.parent.GetComponent();//板卡父物体
D = TmsCards_go[i].transform.GetComponent();//板卡
if (!DP) continue;
foreach (var item in TmsCards)
{
/*if (item.deviceType == "3")
{
}*/
//板卡在机槽里
if (DP.deviceList.deviceType == "2" && item.slotId == DP.deviceList.id && item.devicePosition == D.deviceList.devicePosition)
{
TmsCards_go[i].GetComponent().deviceList = item;
break;
}
//板卡在机框里
else if (DP.deviceList.deviceType == "1" && item.shelfId == DP.deviceList.id && item.devicePosition == D.deviceList.devicePosition)
{
TmsCards_go[i].GetComponent().deviceList = item;
break;
}
}
}
Debug.Log("设备加载完成");
}
///
/// 查询所有设备
///
///
///
public IEnumerator init_Device()
{
for (int i = 0; i < Racks_go.Count; i++)
{
DestroyImmediate(Racks_go[i]);
}
for (int i = 0; i < MachineSlots_go.Count; i++)
{
DestroyImmediate(MachineSlots_go[i]);
}
for (int i = 0; i < TmsCards_go.Count; i++)
{
DestroyImmediate(TmsCards_go[i]);
}
Cabinets.Clear();
Racks_go.Clear();
Racks.Clear();
MachineSlots_go.Clear();
MachineSlots.Clear();
TmsCards_go.Clear();
TmsCards.Clear();
//var jsonResult = await CombineJSON.GetJson_POST(Jk_URL.sb_cx, token);
//root_AllDevice = JsonConvert.DeserializeObject(jsonResult);
//StartCoroutine(
// CombineJSON.GetJson_POST(Jk_URL.sb_cx, token, (jsonResult) =>
// {
// root_AllDevice = JsonConvert.DeserializeObject(jsonResult);
// Debug.Log("设备查询完成" + "URL:" + Jk_URL.sb_cx + "Token:" + token + "返回内容" + jsonResult);
// })
//);
yield return StartCoroutine(
CombineJSON.GetJson_POST(Jk_URL.sb_cx, arguments.token, (jsonResult) =>
{
if (string.IsNullOrEmpty(jsonResult))
{
return;
}
try
{
root_AllDevice = JsonConvert.DeserializeObject(jsonResult);
Debug.Log("设备查询完成" /*+ "URL:" + Jk_URL.sb_cx + "Token:" + arguments.token + "返回内容" + jsonResult*/);
}
catch (Exception e)
{
Debug.Log("序列化-设备查询错误:" + e.Message);
}
})
); ;
}
///
/// 查询所有端口
///
///
public IEnumerator init_Port()
{
for (int i = 0; i < TmsPorts_go.Count; i++)
{
DestroyImmediate(TmsPorts_go[i]);
}
TmsPorts_go.Clear();
TmsPorts.Clear();
//var jsonResult1 = await CombineJSON.GetJson_POST(Jk_URL.dk_cx, token);
//root_AllPort = JsonConvert.DeserializeObject(jsonResult1);
//StartCoroutine(
//CombineJSON.GetJson_POST(Jk_URL.dk_cx, token, (jsonResult1) =>
//{
// root_AllPort = JsonConvert.DeserializeObject(jsonResult1);
// Debug.Log("端口查询完成" + "URL:" + Jk_URL.dk_cx + "Token:" + token + "返回内容" + jsonResult1);
//})
//);
yield return StartCoroutine(
CombineJSON.GetJson_POST(Jk_URL.dk_cx, arguments.token, (jsonResult1) =>
{
if (string.IsNullOrEmpty(jsonResult1))
{
return;
}
try
{
root_AllPort = JsonConvert.DeserializeObject(jsonResult1);
Debug.Log("端口查询完成" /*+ "URL:" + Jk_URL.dk_cx + "Token:" + arguments.token + "返回内容" + jsonResult1*/);
}
catch (Exception e)
{
Debug.Log("序列化-端口查询错误:" + e.Message);
}
})
); ;
}
///
/// 查询所有环境
///
///
public IEnumerator init_ENV()
{
ENVs.Clear();
//var jsonResult2 = await CombineJSON.GetJson_POST(Jk_URL.hj_cx, token);
//root_AllENV = JsonConvert.DeserializeObject(jsonResult2);
//StartCoroutine(
//CombineJSON.GetJson_POST(Jk_URL.hj_cx, token, (jsonResult2) =>
//{
// root_AllENV = JsonConvert.DeserializeObject(jsonResult2);
// Debug.Log("环境查询完成" + "URL:" + Jk_URL.hj_cx + "Token:" + token + "返回内容" + jsonResult2);
//})
//);
yield return StartCoroutine(
CombineJSON.GetJson_POST(Jk_URL.hj_cx, arguments.token, (jsonResult2) =>
{
if (string.IsNullOrEmpty(jsonResult2))
{
return;
}
try
{
root_AllENV = JsonConvert.DeserializeObject(jsonResult2);
ENVs.Clear();
Debug.Log("环境查询完成" /*+ "URL:" + Jk_URL.hj_cx + "Token:" + arguments.token + "返回内容" + jsonResult2*/);
}
catch (Exception e)
{
Debug.Log("序列化-环境查询错误:" + e.Message);
}
})
);
}
///
/// 查询所有告警
///
///
public IEnumerator init_TmsAlarm()
{
Array.ForEach(GameObject.FindObjectsOfType(), itme =>
{
itme.tmsAlarmLists.Clear();
});
tmsAlarms.Clear();
//var jsonResult3 = await CombineJSON.GetJson_POST(Jk_URL.gj_cx, token);
//root_AllAlarm = JsonConvert.DeserializeObject(jsonResult3);
//StartCoroutine(
//CombineJSON.GetJson_POST(Jk_URL.gj_cx, token, (jsonResult3) =>
//{
// root_AllAlarm = JsonConvert.DeserializeObject(jsonResult3);
// Debug.Log("告警查询完成" + "URL:" + Jk_URL.gj_cx + "Token:" + token + "返回内容" + jsonResult3);
//})
//);
yield return StartCoroutine(
CombineJSON.GetJson_POST(Jk_URL.gj_cx, arguments.token, (jsonResult3) =>
{
if (string.IsNullOrEmpty(jsonResult3))
{
return;
}
try
{
root_AllAlarm = JsonConvert.DeserializeObject(jsonResult3);
Array.ForEach(GameObject.FindObjectsOfType(), itme =>
{
itme.tmsAlarmLists.Clear();
});
tmsAlarms.Clear();
Debug.Log("告警查询完成" /*+ "URL:" + Jk_URL.gj_cx + "Token:" + arguments.token + "返回内容" + jsonResult3*/);
}
catch (Exception e)
{
Debug.Log("序列化-告警查询错误:" + e.Message);
}
})
);
}
///
/// 场景模型数据初始化
///
public async Task deleteInit()
{
}
IEnumerator LoadJSON(string url)
{
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
string jsonData = www.downloadHandler.text;
// 解析JSON数据
ParseJSON(jsonData);
}
}
public void getTmsAlam()
{
TmsAlarmQuery.Root root = new TmsAlarmQuery.Root();
}
void ParseJSON(string jsonString)
{
try
{
root_AllDevice = JsonConvert.DeserializeObject(jsonString);
}
catch
{
try
{
root_AllPort = JsonConvert.DeserializeObject(jsonString);
}
catch
{
try
{
root_AllAlarm = JsonConvert.DeserializeObject(jsonString);
}
catch
{
try
{
root_AllENV = JsonConvert.DeserializeObject(jsonString);
}
catch
{
}
}
}
}
}
///
/// 获取层级
///
///
///
public int CengJi(string s)
{
int cj = 0;
switch (s)
{
case "A":
cj = 0;
break;
case "B":
cj = 1;
break;
case "C":
cj = 2;
break;
case "D":
cj = 3;
break;
case "E":
cj = 4;
break;
case "F":
cj = 5;
break;
case "G":
cj = 6;
break;
case "H":
cj = 7;
break;
case "I":
cj = 8;
break;
default:
break;
}
return cj;
}
///
/// 根据配置生成线缆
///
[ContextMenu("根据配置生成线缆")]
public void FindPortPos()
{
var pqs = Array.FindAll(GameObject.FindObjectsOfType(), itme =>
{
PortQuery pq = itme.GetComponent();
if (!string.IsNullOrEmpty(pq.portList.id))
return true;
else
return false;
//return true;
})/*.Select(item => item.gameObject).ToArray()*/;
var P_list = FindObjectGroups(pqs.ToList());
CreateLine createLine = PatternChoose.Inst.transform.Find("画线").GetComponent();
createLine.list7.Clear();
foreach (var item in P_list)
{
createLine.list7.Add(item[0].transform);
createLine.list7.Add(item[1].transform);
}
createLine.F7();
}
public List> FindObjectGroups(List objects)
{
List> objectGroups = new List>();
for (int i = 0; i < objects.Count; i++)
{
PortQuery objectA = objects[i];
for (int j = i + 1; j < objects.Count; j++)
{
PortQuery objectB = objects[j];
if (objectA.portList.conDevice == objectB.portList.deviceId && objectA.portList.port == objectB.portList.conPort &&
objectB.portList.conDevice == objectA.portList.deviceId && objectB.portList.port == objectA.portList.conPort)
{
bool isDuplicate = false;
foreach (List group in objectGroups)
{
if (group.Contains(objectA) || group.Contains(objectB))
{
isDuplicate = true;
break;
}
}
if (!isDuplicate)
{
List newGroup = new List();
newGroup.Add(objectA);
newGroup.Add(objectB);
objectGroups.Add(newGroup);
}
}
}
}
return objectGroups;
}
public void AssignFields(T target, U source)
{
Type typeTarget = typeof(T);
Type typeSource = typeof(U);
PropertyInfo[] propertiesTarget = typeTarget.GetProperties();
PropertyInfo[] propertiesSource = typeSource.GetProperties();
foreach (PropertyInfo propertyTarget in propertiesTarget)
{
PropertyInfo propertySource = Array.Find(propertiesSource, p => p.Name == propertyTarget.Name);
if (propertySource != null)
{
object propertyValue = propertySource.GetValue(source);
propertyTarget.SetValue(target, propertyValue);
}
}
}
#region MyRegion
[ContextMenu("改位置")]
public IEnumerator dsa()
{
foreach (var item in Cabinets)
{
RedactDevice redactDevice = new RedactDevice();
int a = int.Parse(item.deviceCode.Split('R')[1]);
string b = "";
string nub = "";
if (a <= 8) { b = "I-"; nub = (a % 8).ToString().PadLeft(2, '0'); }
else if (a <= 16) { b = "H-"; nub = ((a - 8) % 8).ToString().PadLeft(2, '0'); }
else if (a <= 24) { b = "G-"; nub = ((a - 16) % 8).ToString().PadLeft(2, '0'); }
else if (a <= 32) { b = "F-"; nub = ((a - 24) % 8).ToString().PadLeft(2, '0'); }
else if (a <= 40) { b = "E-"; nub = ((a - 32) % 8).ToString().PadLeft(2, '0'); }
else if (a <= 48) { b = "D-"; nub = ((a - 40) % 8).ToString().PadLeft(2, '0'); }
else if (a <= 56) { b = "C-"; nub = ((a - 48) % 8).ToString().PadLeft(2, '0'); }
else if (a <= 64) { b = "B-"; nub = ((a - 56) % 8).ToString().PadLeft(2, '0'); }
else if (a <= 72) { b = "A-"; nub = ((a - 64) % 8).ToString().PadLeft(2, '0'); }
if (nub == "00") nub = "08";
item.devicePosition = b + nub;
MapperConfiguration config = new MapperConfiguration(cfg =>
{
cfg.CreateMap();
cfg.CreateMap();
});
IMapper mapper = config.CreateMapper();
var objB = mapper.Map(item);
//redactDevice.mybody = ObjectConverter.Convert(item);
redactDevice.mybody = objB;
yield return StartCoroutine(redactDevice.saveJson());
yield return new WaitForSeconds(0.5f);
//await Task.Delay(500); // 等待5秒钟
}
}
#endregion
public static class ObjectConverter
{
public static TTarget Convert(TSource source)
{
Type targetType = typeof(TTarget);
Type sourceType = typeof(TSource);
PropertyInfo[] targetProperties = targetType.GetProperties();
PropertyInfo[] sourceProperties = sourceType.GetProperties();
var targetInstance = Activator.CreateInstance(targetType);
foreach (var targetProperty in targetProperties)
{
var sourceProperty = Array.Find(sourceProperties, p => p.Name == targetProperty.Name);
if (sourceProperty != null && sourceProperty.PropertyType == targetProperty.PropertyType)
{
var value = sourceProperty.GetValue(source);
var targetSetMethod = targetProperty.GetSetMethod();
if (targetSetMethod != null)
{
targetSetMethod.Invoke(targetInstance, new object[] { value });
}
}
}
return (TTarget)targetInstance;
}
}
///
/// 显示机柜容量
///
[ContextMenu("显示机柜容量")]
public void DisplayCapacity(Toggle t)
{
GameObject go;
if (!t.isOn)
{
CloseHighlight();
return;
}
//CloseHighlight();
Color c1 = new Color(1, 109f / 255, 0, 1);//小
Color c2 = new Color(1, 240f / 255, 0, 1);
Color c3 = new Color(1, 168f / 255, 0, 1);
Color c4 = new Color(112f / 255, 240f / 255, 73f / 255, 1);// 大
for (int i = 0; i < Cabinets_go.Count; i++)
{
var D = Cabinets_go[i].GetComponent().deviceList;
//var H = Cabinets_go[i].GetComponent();
//try
//{
// go = Cabinets_go[i].transform.Find(Cabinets_go[i].name).gameObject;
//}
//catch (Exception)
//{
// go = Cabinets_go[i];
//}
//var M = go.GetComponent().materials[0];
//var M_old = Cabinets_go[i].GetComponent().empty[0];
//var M_new = new Material(M_old);
var hight = Cabinets_go[i].transform.Find("高亮").gameObject;
if (string.IsNullOrEmpty(D.residueNum)) continue;
else if (Convert.ToSingle(D.residueNum) <= (42 * 0.25f))
{
//M_new.SetColor(c1);
//ChangeMaterialColor(go, 0, c1);
hight.SetActive(true);
hight.GetComponent().material = Resources.Load("Materials/1Tou");
}
else if (Convert.ToSingle(D.residueNum) <= (42 * 0.5f))
{
// M_new.color = c2;
//ChangeMaterialColor(go, 0, c2);
hight.SetActive(true);
hight.GetComponent().material = Resources.Load("Materials/2Tou");
}
else if (Convert.ToSingle(D.residueNum) <= (42 * 0.75f))
{
//M_new.color = c3;
//ChangeMaterialColor(go, 0, c3);
hight.SetActive(true);
hight.GetComponent().material = Resources.Load("Materials/3Tou");
}
else if (Convert.ToSingle(D.residueNum) <= (42))
{
//M_new.color = c4;
//ChangeMaterialColor(go, 0, c4);
hight.SetActive(true);
hight.GetComponent().material = Resources.Load("Materials/4Tou");
}
}
}
///
/// 显示机柜容量
///
///
public void DisplayCapacity(bool t)
{
GameObject go;
if (!t)
{
CloseHighlight();
return;
}
//CloseHighlight();
Color c1 = new Color(1, 109f / 255, 0, 1);//小
Color c2 = new Color(1, 240f / 255, 0, 1);
Color c3 = new Color(1, 168f / 255, 0, 1);
Color c4 = new Color(112f / 255, 240f / 255, 73f / 255, 1);// 大
for (int i = 0; i < Cabinets_go.Count; i++)
{
var D = Cabinets_go[i].GetComponent().deviceList;
//var H = Cabinets_go[i].GetComponent();
try
{
go = Cabinets_go[i].transform.Find(Cabinets_go[i].name).gameObject;
}
catch (Exception)
{
go = Cabinets_go[i];
}
//var M = go.GetComponent().materials[0];
//var M_old = Cabinets_go[i].GetComponent().empty[0];
//var M_new = new Material(M_old);
var hight = Cabinets_go[i].transform.Find("高亮").gameObject;
if (string.IsNullOrEmpty(D.residueNum)) continue;
else if (Convert.ToSingle(D.residueNum) <= (42 * 0.25f))
{
//M_new.SetColor(c1);
//ChangeMaterialColor(go, 0, c1);
hight.SetActive(true);
hight.GetComponent().material = Resources.Load("Materials/1Tou");
}
else if (Convert.ToSingle(D.residueNum) <= (42 * 0.5f))
{
// M_new.color = c2;
//ChangeMaterialColor(go, 0, c2);
hight.SetActive(true);
hight.GetComponent().material = Resources.Load("Materials/2Tou");
}
else if (Convert.ToSingle(D.residueNum) <= (42 * 0.75f))
{
//M_new.color = c3;
//ChangeMaterialColor(go, 0, c3);
hight.SetActive(true);
hight.GetComponent().material = Resources.Load("Materials/3Tou");
}
else if (Convert.ToSingle(D.residueNum) <= (42))
{
//M_new.color = c4;
//ChangeMaterialColor(go, 0, c4);
hight.SetActive(true);
hight.GetComponent().material = Resources.Load("Materials/4Tou");
}
}
}
///
/// 关闭高亮
///
[ContextMenu("关闭高亮")]
public void CloseHighlight()
{
//GameObject go;
for (int i = 0; i < Cabinets_go.Count; i++)
{
// try
// {
// go = Cabinets_go[i].transform.Find(Cabinets_go[i].name).gameObject;
// }
// catch (Exception)
// {
// go = Cabinets_go[i];
// }
// ChangeMaterialColor(go, 0, new Color(1, 1, 1, 1));
var hight = Cabinets_go[i].transform.Find("高亮").gameObject;
hight.SetActive(false);
}
}
///
/// 显示柜门状态
///
[ContextMenu("显示柜门状态")]
public void DoorCondition(Toggle t)
{
if (!t.isOn)
{
for (int i = 0; i < Cabinets_go.Count; i++)
{
var door = Array.Find(Cabinets_go[i].transform.GetComponentsInChildren(), item =>
{
return item.name.Contains("Object");
});
if (door)
door.transform.localRotation = Quaternion.Euler(Vector3.zero);
}
CloseHighlight();
return;
}
CloseHighlight();
Color c = new Color(108f / 255, 221f / 255, 239f / 255, 1);
for (int i = 0; i < Cabinets_go.Count; i++)
{
var D = Cabinets_go[i].GetComponent().deviceList;
var hight = Cabinets_go[i].transform.Find("高亮").gameObject;
//var H = Cabinets_go[i].GetComponent();
if (string.IsNullOrEmpty(D.openFlag)) continue;
else if (D.openFlag == "0")
{
//关
hight.SetActive(false);
}
else if (D.openFlag == "1")
{
//开
GameObject go;
try
{
go = Cabinets_go[i].transform.Find(Cabinets_go[i].name).gameObject;
}
catch (Exception)
{
go = Cabinets_go[i];
}
if (go)
{
//ChangeMaterialColor(go, 0, c);
hight.SetActive(true);
hight.GetComponent().material = Resources.Load("Materials/3Tou");
}
var door = Array.Find(Cabinets_go[i].transform.GetComponentsInChildren(), item =>
{
return item.name.Contains("Object");
});
if (door)
door.transform.localRotation = Quaternion.Euler(Vector3.forward * -90);
}
}
}
///
/// 显示柜门状态
///
public void DoorCondition()
{
CloseHighlight();
//Color c = new Color(108f / 255, 221f / 255, 239f / 255, 1);
for (int i = 0; i < Cabinets_go.Count; i++)
{
var D = Cabinets_go[i].GetComponent().deviceList;
var hight = Cabinets_go[i].transform.Find("高亮").gameObject;
//var H = Cabinets_go[i].GetComponent();
if (string.IsNullOrEmpty(D.openFlag)) continue;
else if (D.openFlag == "0")
{
//关
hight.SetActive(false);
power_close(Cabinets_go[i]);
}
else if (D.openFlag == "1")
{
//开
GameObject go = null;
try
{
go = Cabinets_go[i].transform.Find(Cabinets_go[i].name).gameObject;//有门的
}
catch (Exception)
{
go = Cabinets_go[i];//无门的
}
if (go)
{
//ChangeMaterialColor(go, 0, c);
hight.SetActive(true);
hight.GetComponent().material = Resources.Load("Materials/3Tou");
}
else
{
hight.SetActive(false);
}
//var door = Array.Find(Cabinets_go[i].transform.GetComponentsInChildren(), item =>
//{
// return item.name.Contains("Object");
//});
//if (door)
// door.transform.localRotation = Quaternion.Euler(Vector3.forward * -90);
power_open(Cabinets_go[i]);
}
}
}
///
/// 关门
///
public void power_close()
{
for (int i = 0; i < Cabinets_go.Count; i++)
{
var door = Array.Find(Cabinets_go[i].transform.GetComponentsInChildren(), item =>
{
return item.name.Contains("Object");
});
if (door && Cabinets_go[i].name == "R71")
{
//Cabinets_go[i].transform.GetChild(0).localRotation = Quaternion.Euler(Vector3.zero);
//Cabinets_go[i].transform.GetChild(1).localRotation = Quaternion.Euler(Vector3.zero);
Cabinets_go[i].transform.GetChild(0).DOLocalRotate(Vector3.zero, 2);
Cabinets_go[i].transform.GetChild(1).DOLocalRotate(Vector3.zero, 2);
}
else if (door)
//door.transform.localRotation = Quaternion.Euler(Vector3.zero);
door.transform.DOLocalRotate(Vector3.zero, 2);
}
CloseHighlight();
}
///
/// 开门
///
public void power_open()
{
for (int i = 0; i < Cabinets_go.Count; i++)
{
var door = Array.Find(Cabinets_go[i].transform.GetComponentsInChildren(), item =>
{
return item.name.Contains("Object");
});
if (door && Cabinets_go[i].name == "R71")
{
//Cabinets_go[i].transform.GetChild(0).localRotation = Quaternion.Euler(Vector3.forward * -90);
//Cabinets_go[i].transform.GetChild(1).localRotation = Quaternion.Euler(Vector3.forward * 90);
Cabinets_go[i].transform.GetChild(0).DOLocalRotate(Vector3.forward * -90, 2f);
Cabinets_go[i].transform.GetChild(1).DOLocalRotate(Vector3.forward * 90, 2f);
}
else if (door)
//door.transform.localRotation = Quaternion.Euler(Vector3.forward * -90);
door.transform.DOLocalRotate(Vector3.forward * -90, 2f);
}
CloseHighlight();
}
///
/// 开门(单个)
///
///
public void power_open(GameObject cabinet)
{
var door = Array.Find(cabinet.GetComponentsInChildren(), item =>
{
return item.name.Contains("Object");
});
if (door && door.name == "R71")
{
//cabinet.transform.GetChild(0).localRotation = Quaternion.Euler(Vector3.forward * -90);
//cabinet.transform.GetChild(1).localRotation = Quaternion.Euler(Vector3.forward * 90);
cabinet.transform.GetChild(0).DOLocalRotate(Vector3.forward * -90, 2f);
cabinet.transform.GetChild(1).DOLocalRotate(Vector3.forward * 90, 2f);
}
else if (door)
//door.transform.localRotation = Quaternion.Euler(Vector3.forward * -90);
door.transform.DOLocalRotate(Vector3.forward * -90, 2f);
}
///
/// 关门(单个)
///
///
public void power_close(GameObject cabinet)
{
var door = Array.Find(cabinet.GetComponentsInChildren(), item =>
{
return item.name.Contains("Object");
});
if (door && door.name == "R71")
{
//cabinet.transform.GetChild(0).localRotation = Quaternion.Euler(Vector3.zero);
//cabinet.transform.GetChild(1).localRotation = Quaternion.Euler(Vector3.zero);
cabinet.transform.GetChild(0).DOLocalRotate(Vector3.zero, 2);
cabinet.transform.GetChild(1).DOLocalRotate(Vector3.zero, 2);
}
else if (door)
//door.transform.localRotation = Quaternion.Euler(Vector3.zero);
door.transform.DOLocalRotate(Vector3.zero, 2);
}
///
/// 变色
///
///
///
///
public void ChangeMaterialColor(GameObject go, int materialIndex, Color newColor)
{
MeshRenderer renderer = go.GetComponent();
// 获取当前材质球数组
Material[] materials = renderer.materials;
// 确保 materialIndex 在有效范围内
if (materialIndex >= 0 && materialIndex < materials.Length)
{
// 创建新的材质球数组
Material[] newMaterials = materials.Clone() as Material[];
// 更改特定索引位置的材质球颜色
newMaterials[materialIndex].color = newColor;
// 将新的材质球数组赋值给 MeshRenderer
renderer.materials = newMaterials;
}
}
///
/// 变色
///
///
///
///
public void ChangeMaterialColor(GameObject go, int index)
{
var hight = go.transform.Find("高亮").gameObject;
Material material = null;
switch (index)
{
case 1:
material = Resources.Load("Materials/1Tou");
break;
case 2:
material = Resources.Load("Materials/2Tou");
break;
case 3:
material = Resources.Load("Materials/3Tou");
break;
case 4:
material = Resources.Load("Materials/4Tou");
break;
default:
break;
}
hight.GetComponent().material = material;
hight.SetActive(true);
}
private void OnDisable()
{
StopTimer();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.End))
{
//string s = ("{\"data\":{\"token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2OTI5MTQzNzAsInVzZXJuYW1lIjoiMTgzMTIzNDU2NzgifQ.w30WlzIF-n9_iGJHdlBva65iKwrx8jONLe-XTxQpJWI\",\"heatThresholdValue\":\"36\",\"humidityThresholdValue\":\"85\"}}");
//StartCoroutine(WebInteraction.Inst.unity_token_value(s));
Array.ForEach(pop_ups.ToArray(), (itme) =>
{
itme.gameObject.SetActive(false);
});
StartCoroutine(LoadAddress((ct) =>
{
if (ct != null)
{
Jk_URL = new webURL(ct);
StartCoroutine(Initialize());
}
else
{
Debug.Log("获取穿透错误!");
}
}));
}
}
///
///
///
[Header("--------------------------------")]
public GameObject objectToShow;
public float idleTime = 5f; // 空闲时间阈值,单位为秒
[HideInInspector] public Coroutine timerCoroutine;
public void StartTimer()
{
// 开始计时器
timerCoroutine = StartCoroutine(ShowObjectAfterIdleTime());
}
private void StopTimer()
{
// 停止计时器
if (timerCoroutine != null)
{
StopCoroutine(timerCoroutine);
timerCoroutine = null;
}
}
public void StopTimer(Toggle t)
{
// 停止计时器
if (timerCoroutine != null && !t.isOn)
{
StopCoroutine(timerCoroutine);
timerCoroutine = null;
}
}
public void ResetTimer()
{
// 重置计时器
StopTimer();
StartTimer();
}
public IEnumerator ShowObjectAfterIdleTime()
{
// 等待一段时间
yield return new WaitForSeconds(idleTime);
// 显示物体
objectToShow.SetActive(true);
}
///
/// 递增寻找父物体
///
///
///
///
public GameObject FindParent(GameObject gameObject, System.Predicate condition)
{
if (gameObject == null)
return null;
Transform parent = gameObject.transform.parent;
while (parent != null)
{
if (condition(parent.gameObject))
{
return parent.gameObject;
}
parent = parent.parent;
}
return null;
}
///
/// 条件
///
///
///
public bool IsDesiredParent(GameObject go)
{
// 在此处添加自定义条件
// 例如:判断是否具有特定组件、特定标签等
DeviceQuery d = go.GetComponent();
return (d != null && d.deviceList.type == "1");
}
// 读取地址
public IEnumerator LoadAddress(Action callback)
{
string filePath = Application.streamingAssetsPath + "/dz.txt";
UnityWebRequest www = UnityWebRequest.Get(filePath);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
string fileContent = www.downloadHandler.text;
Debug.Log("获取穿透成功" /*+ fileContent + "Token:" + Inst.arguments.token*/);
if (string.IsNullOrEmpty(fileContent))
{
callback(null);
yield break;
}
callback(fileContent);
}
else
{
callback(null);
Debug.LogError("Error: " + www.error);
}
}
// 获取接口信息
public IEnumerator Initialize()
{
isLoading = false;
TransparentGlowManage.Inst.addScript();
yield return StartCoroutine(init_Device());
yield return StartCoroutine(init_Port());
yield return StartCoroutine(init_TmsAlarm());
yield return StartCoroutine(init_ENV());
yield return StartCoroutine(LineQuery.Inst.getJsonCoroutine());
LoadScene();
}
// 加载场景
private void LoadScene()
{
Array.ForEach(GameManager.Inst.pop_ups.ToArray(), (itme) =>
{
itme.gameObject.SetActive(false);
});
// 加载场景
Load_Device();
Load_Port();
Load_TmsAlarm();
Load_ENV();
Application.ExternalCall("OnSceneLoaded", "三维加载完成");
//三维场景加载时显示地图
isLoading = true;
}
///
/// 接口地址
///
[System.Serializable]
public class webURL
{
///
/// 设备-查询URL
///
[Header("设备-查询URL")] public string sb_cx;
///
/// 设备-新增URL
///
[Header("设备-新增URL")] public string sb_xz;
///
/// 设备-删除URL
///
[Header("设备-删除URL")] public string sb_sc;
///
/// 设备-编辑URL
///
[Header("设备-编辑URL")] public string sb_bj;
///
/// 端口-查询URL
///
[Header("端口-查询URL")] public string dk_cx;
///
/// 端口-新增URL
///
[Header("端口-新增URL")] public string dk_xz;
///
/// 端口-删除URL
///
[Header("端口-删除URL")] public string dk_sc;
///
/// 端口-编辑URL
///
[Header("端口-编辑URL")] public string dk_bj;
///
/// 环境-查询URL
///
[Header("环境-查询URL")] public string hj_cx;
///
/// 告警-查询URL
///
[Header("告警-查询URL")] public string gj_cx;
///
/// 工作票-查询URL
///
[Header("工作票-查询URL")] public string gzp_cx;
///
/// 检修票-查询URL
///
[Header("检修票-查询URL")] public string jxp_cx;
///
/// 线缆组-查询URL
///
[Header("线缆组-查询URL")] public string xlz_cx;
///
/// 线缆组-新增URL
///
[Header("线缆组-查询URL")] public string xlz_xz;
///
/// 线缆组-删除URL
///
[Header("线缆组-查询URL")] public string xlz_sc;
public webURL(string ct)
{
//string ct = "http://fpt9gx.natappfree.cc/";
sb_cx = ct + "machineRoom/device/queryList";
sb_xz = ct + "machineRoom/device/save";
sb_sc = ct + "machineRoom/device/delete";
sb_bj = ct + "machineRoom/device/updateById";
dk_cx = ct + "machineRoom/port/queryList";
dk_xz = ct + "machineRoom/port/save";
dk_sc = ct + "machineRoom/port/delete";
dk_bj = ct + "machineRoom/port/updateById";
hj_cx = ct + "machineRoom/tmsPoint/queryList";
gj_cx = ct + "machineRoom/tmsAlarm/queryList";
gzp_cx = ct + "machineRoom/tmsWorkTicket/queryList";
jxp_cx = ct + "machineRoom/tmsOverhaulTicket/queryList";
xlz_cx = ct + "machineRoom/deviceLine/queryList";
xlz_xz = ct + "machineRoom/deviceLine/save";
xlz_sc = ct + "machineRoom/deviceLine/delete";
}
}
///
/// 全局参数
///
[System.Serializable]
public class GlobalParameter
{
///
/// 登录后返回
///
public string token;
///
/// 温度阈值
///
public string heatThresholdValue;
///
/// 湿度阈值
///
public string humidityThresholdValue;
}
}