using System.Linq;
using Newtonsoft.Json;
using SC.XR.Unity.Module_InputSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR;
using Ximmerse.XR.Tag;
using Newtonsoft.Json.Linq;
///
/// 大场景生成和Tag管理
///
public class MachineRoomManager : MonoSingleton
{
public MachineRoomData data;
public GameObject cabinetObj;
public GameObject bladeServer;
public bool isInit = false;
public MachineRoomTagData tagData;
public List listEditorTag;
GameObject tagObjPoint;
private List listBSItem;
public TagTracking foundTracking;
public GameObject cubeY;
public Transform leftLine;
public Transform rightLine;
bool isSelect = false;
private Transform selectTrigger;
private Vector3 selectTOldPos;
private Vector3 selectTOldRot;
public bool isDataInit = false;
public Transform startShowDevice;
///
/// 临时创建 的tagid
///
private List listTagid;
///
/// 临时创建 刀片机信息
///
private DeviceParameters dp1;
private DeviceParameters dp2;
///
/// 机房列表
///
public List listRoomName;
public void Init()
{
tagObjPoint = new GameObject();
listBSItem = new List();
listRoomName = new List();
listRoomName.Add("4A-23");
listRoomName.Add("4A-24");
listRoomName.Add("4A-25");
listRoomName.Add("4A-26");
listRoomName.Add("4A-27");
listTagid = new List();
listTagid.Add(6);
listTagid.Add(15);
listTagid.Add(26);
listTagid.Add(27);
listTagid.Add(28);
listTagid.Add(30);
listTagid.Add(31);
listTagid.Add(32);
listTagid.Add(35);
listTagid.Add(36);
listTagid.Add(38);
listTagid.Add(41);
//listTagid.Add(42);
//listTagid.Add(43);
//listTagid.Add(44);
//listTagid.Add(50);
//listTagid.Add(53);
//listTagid.Add(55);
//listTagid.Add(58);
//listTagid.Add(61);
//listTagid.Add(62);
//listTagid.Add(64);
//listTagid.Add(31);
//listTagid.Add(61);
//listTagid.Add(64);
//listTagid.Add(43);
//listTagid.Add(3161);
//listTagid.Add(3164);
//listTagid.Add(4361);
//listTagid.Add(4364);
#region 原初始化
// //PlayerPrefs.DeleteAll();
// if (PlayerPrefs.HasKey("MachineRoomData"))
// {
// string msg = PlayerPrefs.GetString("MachineRoomData");
// UILogManager.Instance.text1.text = msg;
// Debug.Log("MachineRoomData " + msg);
// data = JsonConvert.DeserializeObject(msg);
// // Debug.Log("PlayerPrefs.HasKey(MachineRoomData) ");
// //data = new MachineRoomData();
// //data.line = 4;
// //data.list = 3;
// //data.intervalW = 1;
// //data.intervalL = 2;
// //data.cabinetW = 1.2f;
// //data.cabinetH = 2f;
// //data.bladeSeverNumber = 4;
// //data.bIntervalH = 0.2f;
// //data.firstBladeSeverH = 0.4f;
// //data.TagOffestPosY = 0;
// //data.CameraFar = 3;
// //data.roomName = "4A-23";
// //data.totalCapacity = 8520;
// //data.usedCapacity = 4420;
// //data.totalPower = 1278;
// //data.usedPower = 846.24f;
// //data.totalLoadBearing = 85.2f;
// //data.usedBearing = 46.83f;
// }
// else
// {
// data = new MachineRoomData();
// data.line = 4;
// data.list = 3;
// data.intervalW = 0.1f;
// data.intervalL = 1f;
// data.cabinetW = 0.6f;
// data.cabinetH = 2f;
// data.bladeSeverNumber = 4;
// data.bIntervalH = 0.2f;
// data.firstBladeSeverH = 0.4f;
// data.tagOffestPosY = 1f;
// data.cameraFar = 10;
// data.roomName = "4A-23";
// data.totalCapacity = 8520;
// data.usedCapacity = 4420;
// data.totalPower = 1278;
// data.usedPower = 846.24f;
// data.totalLoadBearing = 85.2f;
// data.usedBearing = 46.83f;
// }
// // data.listCabinetData = null;
// data.listEquipmentData = null;
// data.listTagData = null;
//// PlayerPrefs.DeleteAll();
// if (PlayerPrefs.HasKey("MachineRoomTagData"))
// {
// string msg = PlayerPrefs.GetString("MachineRoomTagData");
// Debug.Log("MachineRoomTagData " + msg);
// data.listTagData = JsonConvert.DeserializeObject>(msg);
// }
// else
// {
// data.listTagData = new List();
// MachineRoomTagData data1 = new MachineRoomTagData();
// data1.id = 65;
// //data1.pos = new Vector3(4.5f, 0, 0);
// data1.pos = Vector3.zero;
// data1.rot = new Vector3(0, 180, 0);
// data1.offestPos = Vector3.zero;
// data1.offestrot = Vector3.zero;
// MachineRoomTagData data4 = new MachineRoomTagData();
// data4.id = 141;
// // data4.pos = new Vector3(4.5f, 2.5f, 3.7f);
// data4.pos = Vector3.zero;
// data4.rot = new Vector3(0, 180, 0);
// data4.offestPos = Vector3.zero;
// data4.offestrot = Vector3.zero;
// MachineRoomTagData data5 = new MachineRoomTagData();
// data5.id = 142;
// // data5.pos = new Vector3(4.5f, 2.5f, 3.7f);
// data5.pos = Vector3.zero;
// data5.rot = new Vector3(0, 180, 0);
// data5.offestPos = Vector3.zero;
// data5.offestrot = Vector3.zero;
// data.listTagData.Add(data1);
// data.listTagData.Add(data4);
// data.listTagData.Add(data5);
// }
// if (PlayerPrefs.HasKey("MachineRoomCabinetData"))
// {
// string msg = PlayerPrefs.GetString("MachineRoomCabinetData");
// // data.listCabinetData = JsonConvert.DeserializeObject>(msg);
// data.listEquipmentData = JsonConvert.DeserializeObject>(msg);
// }
#endregion
}
private void Start()
{
Init();
//Init();
// Init();
#if UNITY_EDITOR
// Init();
#endif
}
public void SceneInitCallBack(string msg)
{
Debug.Log(msg);
JObject json = JObject.Parse(msg);
UILogManager.Instance.text1.text = msg;
if (json["code"].ToString() == "200")
{
string data = json["data"]["room"].ToString();
this.data = JsonConvert.DeserializeObject(data);
UICallManager.Instance.MachineRoomInit();
}
else
{
Debug.LogError(" 请求场景参数出错 !!! " + HttpActionLang.sceneInit);
}
}
public void SceneInit()
{
SettingMachineRoomData.Instance.Init(data);
OpenXRCamera.Instance.head.GetComponent().farClipPlane = data.cameraFar;
MachineInit();
// ServerInit();
UICallManager.Instance.ShowDeviceTransform(startShowDevice);
isDataInit = true;
}
private void Update()
{
if (state && tagData != null)
{
for (int i = 0; i < listEditorTag.Count; i++)
{
listEditorTag[i].gameObject.SetActive(!state);
if (listEditorTag[i].id == tagData.id)
listEditorTag[i].gameObject.SetActive(true);
}
}
if (tagData != null && foundTracking != null && DragManager.Instance.triggerState == TriggerState.No && !state)
{
tagObjPoint.transform.SetParent(foundTracking.transform);
tagObjPoint.transform.localEulerAngles = new Vector3(0, tagData.offestrot == Vector3.zero ? tagData.rot.y : tagData.offestrot.y, 0);
tagObjPoint.transform.localPosition = tagData.offestPos == Vector3.zero ? tagData.pos : tagData.offestPos;
// Debug.Log(" MachineRoom T " + tagData.offestPos);
transform.eulerAngles = new Vector3(0, tagObjPoint.transform.eulerAngles.y, 0);
transform.position = new Vector3(tagObjPoint.transform.position.x, foundTracking.transform.position.y + data.tagOffestPosY, tagObjPoint.transform.position.z);
}
if (Input.GetKeyDown(KeyCode.L))
{
OnTrackingFound(foundTracking);
}
}
public List listObjes = new List();
public void MachineInit()
{
//listObjes.Clear();
//listObjes = new List();
//// 重新生成机柜组
//if(data.listEquipmentData == null||data.listEquipmentData.Count!=(data.line*data.list))
//{
// data.listEquipmentData = new List();
// for (int i = 0; i < data.line; i++)
// {
// for (int j = 0; j < data.list; j++)
// {
// MachineParameters mp1 = new MachineParameters();
// mp1.deviceId = (i * data.list + j + 1).ToString();
// mp1.tagID = -1000;
// // mp1.offsetPos = new Vector3(-0.4f, 0, -0.3f);
// mp1.offsetPos = Vector3.zero;
// mp1.eulerAxle = 3;
// mp1.deviceName = "设备" + (i * data.list + j + 1).ToString();
// mp1.carryingSystem = "承载系统1";
// mp1.deviceLocation = "4A-23机房";
// mp1.equipmentType = "机柜";
// mp1.equipmentModel = null;
// mp1.factory = null;
// mp1.specification = null;
// mp1.listDevicesParameters = new List();
// if (int.Parse( mp1.deviceId) % 2 == 0)
// mp1.listDevicesParameters.Add(dp1);
// else
// mp1.listDevicesParameters.Add(dp2);
// data.listEquipmentData.Add(mp1);
// }
// }
// for (int i = 0; i < listTagid.Count; i++)
// {
// if ((i + 1) < data.listEquipmentData.Count)
// {
// data.listEquipmentData[i].tagID = listTagid[i];
// }
// else
// break;
// }
//}
GameObject rDataItem = Resources.Load("DataItem");
for (int i = 0; i < data.listEquipmentData.Count; i++)
{
GameObject obj = GameObject.Instantiate(cabinetObj, transform);
obj.transform.localScale = new Vector3(data.cabinetW, data.cabinetH, obj.transform.localScale.z);
Debug.Log(data.cabinetH + " " + data.cabinetW);
obj.name =i.ToString();
// Debug.Log(obj.name + " " + new Vector3(j * (data.cabinetW + data.intervalW), 0, i * data.intervalL));
obj.transform.localPosition = new Vector3((i%data.list)*(data.cabinetW + data.intervalW), 0, (i/data.list) * data.intervalL);
obj.GetComponent().Setting((i / data.list) % 2 == 0);
listObjes.Add(obj.GetComponent());
listObjes[i].deviceParameters = data.listEquipmentData[i];
if(data.listEquipmentData[i]!=null)
{
GameObject item = GameObject.Instantiate(rDataItem);
DataItem dataItem = item.GetComponent();
dataItem.Init(data.listEquipmentData[i]);
// item.SetActive(true);
for (int j = 0; j < data.listEquipmentData[i].listDevicesParameters.Count; j++)
{
dataItem.SetDeviceParametersData(data.listEquipmentData[i].listDevicesParameters);
}
listObjes[i].dataItem = dataItem;
dataItem.transform.SetParent(listObjes[i].transform);
dataItem.transform.localPosition = listObjes[i].xTag.localPosition;
dataItem.transform.localEulerAngles = new Vector3(0, listObjes[i].isSingle ? 180 : 0, 0);
}
}
//Debug.Log(data.line);
Vector3 leftPos = new Vector3(-1 * (data.cabinetW + data.intervalW), 0, 0);
Vector3 rightPos = new Vector3((data.cabinetW * data.list + data.intervalW * data.list + data.cabinetW + data.intervalW), 0, 0);
WayFindingManager.Instance.SetLine(leftPos, rightPos);
SaveAllData();
isInit = true;
}
public IEnumerator RandomRange()
{
yield return 2f;
int randomNum = UnityEngine.Random.Range(0, listObjes.Count-1);
// WayFindingManager.Instance.SetServerItem(listObjes[randomNum].transform);
List listMachineObj = new List();
for (int i = 0; i < 3; i++)
{
if(i("DataItem");
// for (int i = 0; i < data.listCabinetData.Count; i++)
// {
// if (data.listCabinetData[i].listBladeServerData == null)
// continue;
// for (int j = 0; j < data.listCabinetData[i].listBladeServerData.Count; j++)
// {
// GameObject item = GameObject.Instantiate(rDataItem);
// DataItem dataItem = item.GetComponent();
// dataItem.Init(data.listCabinetData[i].listBladeServerData[j].tagID, data.listCabinetData[i].listBladeServerData[j].pos, data.listCabinetData[i].listBladeServerData[j].eulerAxle , data.listCabinetData[i].listBladeServerData[j], data.listCabinetData[i].listBladeServerData[j].width, data.listCabinetData[i].listBladeServerData[j].heigth);
// }
// }
if (data.listEquipmentData == null)
return;
GameObject rDataItem = Resources.Load("DataItem");
for (int i = 0; i < data.listEquipmentData.Count; i++)
{
GameObject item = GameObject.Instantiate(rDataItem);
DataItem dataItem = item.GetComponent();
dataItem.Init(data.listEquipmentData[i]);
// item.SetActive(true);
for (int j = 0; j < data.listEquipmentData[i].listDevicesParameters.Count;j++)
{
dataItem.SetDeviceParametersData(data.listEquipmentData[i].listDevicesParameters);
}
}
}
public void SettingMachine( )
{
if (listObjes != null)
{
for (int i = 0; i < listObjes.Count; i++)
{
listObjes[i].gameObject.SetActive(false);
}
listObjes.Clear();
listObjes = new List();
}
if(BladeServerManager.Instance.listTagTracking!=null)
{
for (int i = 0; i < BladeServerManager.Instance.listTagTracking.Count; i++)
{
BladeServerManager.Instance.listTagTracking[i].gameObject.SetActive(false);
}
BladeServerManager.Instance.listTagTracking.Clear();
}
//data.listEquipmentData = new List();
//for (int i = 0; i < data.line; i++)
//{
// for (int j = 0; j < data.list; j++)
// {
// MachineParameters mp1 = new MachineParameters();
// mp1.deviceId = (i * data.list + j + 1).ToString();
// mp1.tagID = -1000;
// mp1.offsetPos = new Vector3(0f, 0, 0f);
// mp1.eulerAxle = 3;
// mp1.deviceName = "设备" + (i * data.list + j + 1).ToString();
// mp1.carryingSystem = "承载系统1";
// mp1.deviceLocation = "4A-23机房";
// mp1.equipmentType = "机柜";
// mp1.equipmentModel = null;
// mp1.factory = null;
// mp1.specification = null;
// mp1.listDevicesParameters = new List();
// if (int.Parse( mp1.deviceId) % 2 == 0)
// mp1.listDevicesParameters.Add(dp1);
// else
// mp1.listDevicesParameters.Add(dp2);
// data.listEquipmentData.Add(mp1);
// }
//}
//Debug.Log(data.line);
//for (int i = 0; i < listTagid.Count; i++)
//{
// if (data.listEquipmentData.Count < (i + 1))
// {
// Debug.Log("a "+listTagid);
// Debug.Log(data.listEquipmentData[i].tagID);
// data.listEquipmentData[i].tagID = listTagid[i];
// }
//}
//for (int i = 0; i < data.listEquipmentData.Count; i++)
//{
// GameObject obj = GameObject.Instantiate(cabinetObj, transform);
// obj.transform.localScale = new Vector3(data.cabinetW, data.cabinetH, obj.transform.localScale.z);
// Debug.Log(data.cabinetH + " " + data.cabinetW);
// obj.name = data.listEquipmentData[i].deviceName;
// // Debug.Log(obj.name + " " + new Vector3(j * (data.cabinetW + data.intervalW), 0, i * data.intervalL));
// obj.transform.localPosition = new Vector3((i % data.line) * (data.cabinetW + data.intervalW), 0, i * data.intervalL);
// obj.GetComponent().Setting((i % data.line) % 2 == 0);
// listObjes.Add(obj.GetComponent());
// listObjes[i].deviceParameters = data.listEquipmentData[i];
//}
//Vector3 leftPos = new Vector3(-1 * (data.cabinetW + data.intervalW), 0, 0);
//Vector3 rightPos = new Vector3((data.cabinetW * data.list + data.intervalW * data.list), 0, 0);
//WayFindingManager.Instance.SetLine(leftPos, rightPos);
GameObject rDataItem = Resources.Load("DataItem");
for (int i = 0; i < data.listEquipmentData.Count; i++)
{
GameObject obj = GameObject.Instantiate(cabinetObj, transform);
obj.transform.localScale = new Vector3(data.cabinetW, data.cabinetH, obj.transform.localScale.z);
Debug.Log(data.cabinetH + " " + data.cabinetW);
obj.name = data.listEquipmentData[i].deviceName;
// Debug.Log(obj.name + " " + new Vector3(j * (data.cabinetW + data.intervalW), 0, i * data.intervalL));
obj.transform.localPosition = new Vector3((i % data.line) * (data.cabinetW + data.intervalW), 0, (i / data.line) * data.intervalL);
obj.GetComponent().Setting((i / data.line) % 2 == 0);
listObjes.Add(obj.GetComponent());
listObjes[i].deviceParameters = data.listEquipmentData[i];
GameObject item = GameObject.Instantiate(rDataItem);
DataItem dataItem = item.GetComponent();
dataItem.Init(data.listEquipmentData[i]);
// item.SetActive(true);
for (int j = 0; j < data.listEquipmentData[i].listDevicesParameters.Count; j++)
{
dataItem.SetDeviceParametersData(data.listEquipmentData[i].listDevicesParameters);
}
listObjes[i].dataItem = dataItem;
dataItem.transform.SetParent(listObjes[i].transform);
dataItem.transform.localPosition = listObjes[i].xTag.localPosition;
dataItem.transform.localEulerAngles = new Vector3(0, listObjes[i].isSingle ? 180 : 0, 0);
}
//Debug.Log(data.line);
Vector3 leftPos = new Vector3(-1 * (data.cabinetW + data.intervalW), 0, 0);
Vector3 rightPos = new Vector3((data.cabinetW * data.list + data.intervalW * data.list + data.cabinetW + data.intervalW), 0, 0);
WayFindingManager.Instance.SetLine(leftPos, rightPos);
SaveAllData();
isInit = true;
}
public void SettingCameraFar()
{
OpenXRCamera.Instance.head.GetComponent().farClipPlane = data.cameraFar;
}
public void OnTrackingFirst(TagTracking tracking)
{
if(isDataInit)
QueryTagData(tracking);
}
public void OnTrackingStay(TagTracking tracking)
{
}
public void OnTrackingFound(TagTracking tracking)
{
// UILogManager.Instance.SendLogText(2, tracking.TrackId.ToString());
if (isDataInit)
QueryTagData(tracking);
}
public void OnTrackingLost(TagTracking tracking)
{
if (isDataInit)
{
if (foundTracking == tracking)
foundTracking = null;
}
}
private void QueryTagData( TagTracking tracking)
{
if (isDataInit)
{
foundTracking = tracking;
tagData = null;
Debug.Log("DGJ ===> QueryTagData" + data.listTagData.Count);
for (int i = 0; i < data.listTagData.Count; i++)
{
if (data.listTagData[i].id == tracking.TrackId)
{
tagData = data.listTagData[i];
SynPlayerTransform.Instance.transform.SetParent(tracking.transform);
break;
}
}
}
//if (state)
//{
// for (int i = 0; i < listEditorTag.Count; i++)
// {
// listEditorTag[i].gameObject.SetActive(!state);
// if (listEditorTag[i].id == tracking.TrackId)
// listEditorTag[i].gameObject.SetActive(true);
// }
//}
}
bool state = false;
public void EditorTag(Text text)
{
text.text = state ? "打开" : "关闭";
text.text += "场景编辑";
state = !state;
DragManager.Instance.state = state;
cubeY.SetActive(state);
for (int i = 0; i < listObjes.Count; i++)
{
listObjes[i].MeshState(state);
}
if (!state && foundTracking!=null)
{
for (int i = 0; i < listEditorTag.Count; i++)
{
listEditorTag[i].gameObject.SetActive(state);
}
for (int i = 0; i < data.listTagData.Count; i++)
{
//data.listTagData[i].offestPos = data.listTagData[i].tagModel.localPosition;
//data.listTagData[i].offestrot = data.listTagData[i].tagModel.localEulerAngles;
for (int j = 0; j < listEditorTag.Count; j++)
{
if (listEditorTag[j].id == data.listTagData[i].id)
{
//data.listTagData[i].offestPos+= listEditorTag[j].transform.localPosition;
//data.listTagData[i].offestrot+= listEditorTag[j].transform.localEulerAngles;
transform.SetParent(listEditorTag[j].transform.parent);
data.listTagData[i].offestPos = transform.localPosition;
data.listTagData[i].offestrot = transform.localEulerAngles;
transform.parent = null;
Debug.Log("DGJ ===> 保存 Tag " + listEditorTag[j].id);
}
}
}
UpdataTagData tagData = new UpdataTagData();
tagData.listTagData = data.listTagData;
string jsonDate = JsonConvert.SerializeObject(tagData);
Debug.Log(jsonDate);
HttpTool.Instance.Post(HttpActionLang.updateTag, jsonDate, UpdateTagCallBack);
selectTOldPos = Vector3.zero;
selectTOldRot = Vector3.zero;
DragManager.Instance.triggerState = TriggerState.No;
selectTrigger = null;
}
if(!state)
{
SaveAllData();
}
}
private void UpdateTagCallBack(string msg)
{
Debug.Log(msg);
JObject json = JObject.Parse(msg);
if (json["code"].ToString() == "200")
{
Debug.Log(" 更新场景Tag参数成功 ");
}
else
{
Debug.LogError(" 更新场景Tag参数出错 !!! " + HttpActionLang.updateCabinet);
}
}
public void SaveAllData()
{
PlayerPrefs.SetString("MachineRoomTagData", JsonConvert.SerializeObject(data.listTagData));
PlayerPrefs.SetString("MachineRoomCabinetData", JsonConvert.SerializeObject(data.listEquipmentData));
PlayerPrefs.SetString("MachineRoomData", JsonConvert.SerializeObject(data));
Debug.Log(" 保存成功 " + JsonConvert.SerializeObject(data));
}
private void SaveData()
{
if(data!=null)
{
string _str = JsonConvert.SerializeObject(data);
System.IO.Directory.CreateDirectory(Application.persistentDataPath+"/Data");
string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_").Replace("/", "_").Replace(":", "_");
Debug.Log(Application.persistentDataPath + "/Data");
using (System.IO.StreamWriter writer = System.IO.File.CreateText(Application.persistentDataPath + "/Data" + "/"+ NowTime + "_data.txt"))
{
writer.Write(_str);
//writer.
}
//UnityEditor.AssetDatabase.Refresh();
}
}
private void OnDisable()
{
//PlayerPrefs.SetString("MachineRoomTagData", JsonConvert.SerializeObject(data.listTagData));
//PlayerPrefs.SetString("MachineRoomCabinetData", JsonConvert.SerializeObject(data.listEquipmentData));
//PlayerPrefs.SetString("MachineRoomData", JsonConvert.SerializeObject(data));
// SaveData();
}
}
public class UpdataTagData
{
public UpdataTagData()
{
listTagData = new List();
}
public List listTagData { get; set; }
}