BladeServerManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. using SC.XR.Unity.Module_InputSystem;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using Ximmerse.XR.Tag;
  8. /// <summary>
  9. /// 机柜Tag管理
  10. /// </summary>
  11. public class BladeServerManager : MonoSingleton<BladeServerManager>
  12. {
  13. /// <summary>
  14. /// 所有的 UIItem
  15. /// </summary>
  16. public List<DataItem> listTagTracking = new List<DataItem>();
  17. // public List<TagTracking> nowTagTracking = new List<TagTracking>();
  18. /// <summary>
  19. /// 当前识别到的Tag
  20. /// </summary>
  21. public Dictionary<int, TagTracking> dicNowTagTracking = new Dictionary<int, TagTracking>();
  22. /// <summary>
  23. /// 当前使用的Tag
  24. /// </summary>
  25. public TagTracking foundTag;
  26. public string foundID;
  27. /// <summary>
  28. /// 当前Tag对应的UIItem
  29. /// </summary>
  30. public DataItem foundItem;
  31. /// <summary>
  32. /// 当前Tag 对应的机柜
  33. /// </summary>
  34. public MachineObj foundMachineObj;
  35. /// <summary>
  36. /// 检测角度
  37. /// </summary>
  38. public float foundAngle;
  39. /// <summary>
  40. /// 显示移动的距离
  41. /// </summary>
  42. public float showDis;
  43. /// <summary>
  44. /// 隐藏的距离
  45. /// </summary>
  46. public float fadeDis;
  47. /// <summary>
  48. /// 编辑Tag的移动框
  49. /// </summary>
  50. // public Transform selectPanel;
  51. /// <summary>
  52. /// 模拟测试用的
  53. /// </summary>
  54. public TagTracking TestTracking;
  55. public GameObject deviceDetails;
  56. /// <summary>
  57. /// 同步位置的Obj
  58. /// </summary>
  59. private Transform tagObj;
  60. /// <summary>
  61. /// 是否是编辑状态
  62. /// </summary>
  63. private bool state = false;
  64. //public delegate void TrackingFound(DataItem tagobj);
  65. ///// <summary>
  66. ///// 物体识别到显示
  67. ///// </summary>
  68. //public TrackingFound OnTrackFound;
  69. //public delegate void TrackingLast(DataItem tagobj);
  70. ///// <summary>
  71. ///// 物体识别到隐藏
  72. ///// </summary>
  73. //public TrackingLast OnTrackLast;
  74. private bool isSync;
  75. public bool isDaoHang;
  76. private void Start()
  77. {
  78. tagObj = new GameObject("TagObj").transform;
  79. tagObj.position = Vector3.zero;
  80. tagObj.eulerAngles = Vector3.zero;
  81. isSync = false;
  82. showDis = 0.55f;
  83. fadeDis = 1.3f;
  84. //ManipulationHandler manipulation = selectPanel.GetComponent<ManipulationHandler>();
  85. //manipulation.PointerDown.AddListener(( msg )=> { SelectPointerDown(); });
  86. //manipulation.PointerUp.AddListener((msg) => { SelectPointerUp(); });
  87. }
  88. float dis;
  89. float angle;
  90. Quaternion rot;
  91. float roty;
  92. Transform point;
  93. private void Update()
  94. {
  95. #if UNITY_EDITOR
  96. if(Input.GetKeyDown(KeyCode.K))
  97. {
  98. OnTrackingFound(TestTracking);
  99. }
  100. #endif
  101. // Debug.Log(state);
  102. // if(isDaoHang)
  103. TagSynchronization();
  104. }
  105. /// <summary>
  106. /// 同步Tag
  107. /// </summary>
  108. private void TagSynchronization()
  109. {
  110. if (foundItem != null && foundTag != null && !state)
  111. {
  112. dis = Vector3.Distance(new Vector3(foundTag.transform.position.x,0, foundTag.transform.position.z) ,new Vector3(OpenXRCamera.Instance.head.transform.position.x,0, OpenXRCamera.Instance.head.transform.position.z) );
  113. angle = Vector3.Angle(OpenXRCamera.Instance.head.GetComponent<Camera>().transform.forward, foundTag.transform.position - OpenXRCamera.Instance.head.transform.position);
  114. // Debug.Log("Dis " + dis + " Angle" + angle + " showDis " + showDis + " fadeDis " + fadeDis);
  115. if (Mathf.Abs(dis) < showDis && isDaoHang)
  116. {
  117. if (!foundItem.gameObject.activeSelf&& (BD_ShowDevicecDetailsObj.Instance == null||(BD_ShowDevicecDetailsObj.Instance != null && (!BD_ShowDevicecDetailsObj.Instance.gameObject.activeSelf||
  118. !BD_ShowDevicecDetailsObj.Instance.transform.parent.gameObject.activeSelf))))
  119. {
  120. Debug.Log("DGJ ==== >> ShowFoundItem ");
  121. foundItem.gameObject.SetActive(true);
  122. if (BD_ShowDevicecDetailsObj.Instance != null && BD_ShowDevicecDetailsObj.Instance.gameObject.activeSelf)
  123. BD_ShowDevicecDetailsObj.Instance.gameObject.SetActive(false);
  124. // ScenesManager.Instance.showOffice(ScenesManager.SceneType.DeviceDetails);
  125. // OnTrackFound(foundItem);
  126. }
  127. point = foundTag.transform.GetChild(0);
  128. point.localPosition = foundItem.offsetPos;
  129. switch (Mathf.Abs(foundItem.eulerAxle))
  130. {
  131. case 1:
  132. point.localEulerAngles = new Vector3(0, 0, 90 * (foundItem.eulerAxle / Mathf.Abs(foundItem.eulerAxle)));
  133. break;
  134. case 2:
  135. point.localEulerAngles = new Vector3(0, foundItem.eulerAxle > 0 ? 0 : 180, 0);
  136. break;
  137. case 3:
  138. point.localEulerAngles = new Vector3(90 * (foundItem.eulerAxle / Mathf.Abs(foundItem.eulerAxle)), 0, 0);
  139. break;
  140. default:
  141. break;
  142. }
  143. if (isSync)
  144. {
  145. Debug.Log("Dis " + dis + " Angle" + angle + " showDis " + showDis + " fadeDis " + fadeDis);
  146. Debug.Log("FoundItem " + foundItem.transform.position + " point" + point.transform.position + " CA " + new Vector3(point.transform.position.x - foundItem.transform.position.x, 0, point.position.z - foundItem.transform.position.z));
  147. //foundItem.transform.Translate(new Vector3(0, point.eulerAngles.y, 0), Time.deltaTime * 10);
  148. foundItem.transform.eulerAngles = new Vector3(0, point.eulerAngles.y, 0);
  149. foundItem.transform.position = point.transform.position;
  150. UICallManager.Instance.OnTrackfound(foundItem);
  151. isSync = false;
  152. //MachineRoomManager.Instance.transform.eulerAngles = new Vector3(MachineRoomManager.Instance.transform.eulerAngles.x, point.eulerAngles.y+180, MachineRoomManager.Instance.transform.eulerAngles.z);
  153. //MachineRoomManager.Instance.transform.position += new Vector3(point.transform.position.x - foundItem.transform.position.x, point.position.y - foundItem.transform.position.y, point.position.z - foundItem.transform.position.z);
  154. //float offy = OpenXRCamera.Instance.transform.position.y - foundItem.transform.position.y;
  155. //offy = offy * 0.05f;
  156. //MachineRoomManager.Instance.transform.position += new Vector3(0, offy, 0);
  157. }
  158. }
  159. // 隐藏的距离判断改由每个DataItem 自己监测
  160. //else if (Mathf.Abs(dis) > fadeDis)
  161. //{
  162. // //OnTrackLast(foundItem);
  163. // // foundItem.gameObject.SetActive(false);
  164. // foundItem.Hide();
  165. // UICallManager.Instance.OnTracklast(foundItem);
  166. // foundItem = null;
  167. // // foundTag = null;
  168. // dicNowTagTracking.Clear();
  169. // dicNowTagTracking = new Dictionary<int, TagTracking>();
  170. //}
  171. }
  172. else
  173. {
  174. isSync = true;
  175. }
  176. }
  177. public void OnTrackingFound(TagTracking tracking)
  178. {
  179. #region 组合Tag识别
  180. //if (!dicNowTagTracking.ContainsKey(tracking.TrackId))
  181. //{
  182. // //if(dicNowTagTracking.Count==1)
  183. // //{
  184. // // TagTracking firstTracking = dicNowTagTracking.FirstOrDefault().Value;
  185. // // Debug.Log(firstTracking.TrackId +" "+tracking.TrackId+" "+ Vector3.Distance(firstTracking.transform.position, tracking.transform.position) + " Distance");
  186. // // //if (Vector3.Distance(firstTracking.transform.position, tracking.transform.position) < 0.1f) // 判断两个Tag是否为并列使用
  187. // // //{
  188. // // // dicNowTagTracking.Add(tracking.TrackId, tracking);
  189. // // // tagObj.SetParent(tracking.transform);
  190. // // // tagObj.localEulerAngles = Vector3.zero;
  191. // // // tagObj.localPosition = Vector3.zero;
  192. // // // tagObj.SetParent(firstTracking.transform);
  193. // // // if (tagObj.localPosition.x > 0)
  194. // // // {
  195. // // // foundTag = firstTracking;
  196. // // // foundID = tracking.TrackId.ToString() + firstTracking.TrackId.ToString();
  197. // // // }
  198. // // // else
  199. // // // {
  200. // // // foundTag = tracking;
  201. // // // foundID = firstTracking.TrackId.ToString() + tracking.TrackId.ToString();
  202. // // // }
  203. // // //}
  204. // // //else
  205. // // //{
  206. // // // // 初始化
  207. // // // StartTracking(tracking);
  208. // // //}
  209. // //}
  210. // //else
  211. // //{
  212. // // // 初始化
  213. // // StartTracking(tracking);
  214. // //}
  215. // #endregion
  216. // if (dicNowTagTracking == null || dicNowTagTracking.Count == 0)
  217. // {
  218. // // 初始化
  219. // StartTracking(tracking);
  220. // }
  221. //}
  222. #endregion
  223. foundTag = tracking;
  224. foundID = tracking.TrackId.ToString();
  225. Debug.Log("FoundID "+ foundID);
  226. // UILogManager.Instance.SendLogText(2, foundID);
  227. if (foundItem==null|| foundItem.tagId!= int.Parse( foundID))
  228. {
  229. QueryFoundItem(int.Parse(foundID));
  230. isSync = true;
  231. WayFindingManager.Instance.CloseWayFind();
  232. // ScenesManager.Instance.showOffice(ScenesManager.SceneType.DeviceDetails);
  233. }
  234. Debug.Log(" FoundItem " + foundItem.ID);
  235. UILogManager.Instance.SendLogText(1, tracking.TrackId.ToString() + " " + foundID);
  236. }
  237. public void OnTrackingLost(TagTracking tracking)
  238. {
  239. //if (tracking.TrackId == foundTag.TrackId)
  240. // foundItem = null;
  241. }
  242. private void StartTracking(TagTracking tracking)
  243. {
  244. if(MachineRoomManager.Instance.isDataInit)
  245. {
  246. dicNowTagTracking.Clear();
  247. dicNowTagTracking = new Dictionary<int, TagTracking>();
  248. dicNowTagTracking.Add(tracking.TrackId, tracking);
  249. foundTag = tracking;
  250. foundID = tracking.TrackId.ToString();
  251. }
  252. }
  253. private void QueryFoundItem(int foundID)
  254. {
  255. Debug.Log("DGJ ===> QueryFoundItem " + listTagTracking.Count);
  256. if (MachineRoomManager.Instance.isDataInit)
  257. {
  258. int row = SynPlayerTransform.Instance.GetRowCabinets();
  259. bool orientation = SynPlayerTransform.Instance.GetOrientation();
  260. if(orientation)
  261. {
  262. row += 2;
  263. }
  264. else
  265. {
  266. row += 1;
  267. }
  268. Debug.Log(row * MachineRoomManager.Instance.data.list + " " + (row + 1) * MachineRoomManager.Instance.data.list);
  269. for (int i = row*MachineRoomManager.Instance.data.list; i < (row+1)*MachineRoomManager.Instance.data.list; i++)
  270. {
  271. Debug.Log("DGJ ===> " + listTagTracking[i].tagId);
  272. if (listTagTracking[i].tagId == foundID)
  273. {
  274. if (foundItem != null)
  275. {
  276. //foundItem.gameObject.SetActive(false);
  277. // foundItem.Hide();
  278. if (state)
  279. {
  280. foundItem.isSelect = false;
  281. foundItem.SetSelect(false);
  282. // foundItem.SaveEdiotrData(selectPanel);
  283. }
  284. }
  285. foundItem = listTagTracking[i];
  286. // foundItem.gameObject.SetActive(true);
  287. Debug.Log(" QueryFoundItem " + foundID);
  288. if (state)
  289. {
  290. //selectPanel.eulerAngles = foundItem.transform.eulerAngles;
  291. //selectPanel.position = foundItem.transform.position;
  292. //selectPanel.gameObject.SetActive(true);
  293. foundItem.isSelect = true;
  294. foundItem.SetSelect(true);
  295. }
  296. break;
  297. }
  298. }
  299. #region old
  300. //for (int i = 0; i < listTagTracking.Count; i++)
  301. //{
  302. // Debug.Log("DGJ ===> " + listTagTracking[i].tagId);
  303. // if (listTagTracking[i].tagId == foundID)
  304. // {
  305. // if (foundItem != null)
  306. // {
  307. // //foundItem.gameObject.SetActive(false);
  308. // // foundItem.Hide();
  309. // if (state)
  310. // {
  311. // foundItem.isSelect = false;
  312. // foundItem.SetSelect(false);
  313. // // foundItem.SaveEdiotrData(selectPanel);
  314. // }
  315. // }
  316. // foundItem = listTagTracking[i];
  317. // // foundItem.gameObject.SetActive(true);
  318. // Debug.Log(" QueryFoundItem " + foundID);
  319. // if (state)
  320. // {
  321. // //selectPanel.eulerAngles = foundItem.transform.eulerAngles;
  322. // //selectPanel.position = foundItem.transform.position;
  323. // //selectPanel.gameObject.SetActive(true);
  324. // foundItem.isSelect = true;
  325. // foundItem.SetSelect(true);
  326. // }
  327. // break;
  328. // }
  329. //}
  330. // 找到x-tag 对应的机柜
  331. //for (int i = 0; i < MachineRoomManager.Instance.listObjes.Count; i++)
  332. //{
  333. // if(MachineRoomManager.Instance.listObjes[i].deviceParameters!=null&&MachineRoomManager.Instance.listObjes[i].deviceParameters.deviceId == foundItem.ID.ToString())
  334. // {
  335. // // 找到后,
  336. // foundMachineObj = MachineRoomManager.Instance.listObjes[i];
  337. // }
  338. //}
  339. #endregion
  340. if (foundItem != null && foundItem.tagId != foundID)
  341. {
  342. //foundItem.gameObject.SetActive(false);
  343. // foundItem.Hide();
  344. foundItem.isSelect = false;
  345. foundItem.SetSelect(false);
  346. foundItem = null;
  347. }
  348. }
  349. }
  350. public void EditorTag( Text text)
  351. {
  352. if(foundItem!=null)
  353. {
  354. text.text = state ? "打开" : "关闭";
  355. text.text += "机柜Tag编辑";
  356. state = !state;
  357. if(state)
  358. {
  359. //selectPanel.eulerAngles = foundItem.transform.eulerAngles;
  360. //selectPanel.position = foundItem.transform.position;
  361. //selectPanel.gameObject.SetActive(true);
  362. foundItem.isSelect = true;
  363. foundItem.SetSelect(true);
  364. }
  365. else
  366. {
  367. foundItem.SaveEdiotrData(foundTag.transform);
  368. //selectPanel.gameObject.SetActive(false);
  369. foundItem.isSelect = false;
  370. foundItem.SetSelect(false);
  371. }
  372. }
  373. if(!state)
  374. {
  375. //for (int i = 0; i < listTagTracking.Count; i++)
  376. //{
  377. // listTagTracking[i].SaveData();
  378. //}
  379. foundItem.SaveData();
  380. // Servermanager.Instance.Save();
  381. MachineRoomManager.Instance.SaveAllData();
  382. }
  383. }
  384. public void SelectPointerEnter()
  385. {
  386. //if(foundItem!=null)
  387. // foundItem.SelectObj(selectPanel);
  388. }
  389. public void SelectPointerDown()
  390. {
  391. if (foundItem != null&& foundTag!=null)
  392. {
  393. foundItem.isSelect = true;
  394. foundItem.SetSelect( true);
  395. }
  396. }
  397. public void SelectPointerUp()
  398. {
  399. if (foundItem != null && foundTag != null)
  400. {
  401. foundItem.isSelect = false;
  402. foundItem.SetSelect(false);
  403. foundItem.SaveEdiotrData(foundTag.transform);
  404. }
  405. }
  406. private void SaveData()
  407. {
  408. /*
  409. *
  410. *
  411. */
  412. }
  413. }