BladeServerManager.cs 17 KB

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