DeviceAlarmControl.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class DeviceAlarmControl : MonoBehaviour
  8. {
  9. public int ListCount = 10000;
  10. public RecycleView VerticalScroll;
  11. public ScrollRect scrollRect;
  12. public string deviceName;
  13. public List<AIDeviceAlarm> listAlarm;
  14. public Dictionary<GameObject, AIDeviceAlarmItem> dicAlarmItem;
  15. private bool isGetData = false;
  16. private void Start()
  17. {
  18. listAlarm = new List<AIDeviceAlarm>();
  19. dicAlarmItem = new Dictionary<GameObject, AIDeviceAlarmItem>();
  20. scrollRect = VerticalScroll.GetComponent<ScrollRect>();
  21. //for (int i = 0; i < 20; i++)
  22. //{
  23. // AIDeviceAlarm testData = new AIDeviceAlarm();
  24. // testData.level = "中";
  25. // testData.times = "2024-05-23 16:13";
  26. // testData.deviceName = "温湿度传感器" + i.ToString();
  27. // testData.describe = "设备离线";
  28. // testData.duarationTimer = i.ToString() + "天0时59分56秒";
  29. // listAlarm.Add(testData);
  30. //}
  31. //Debug.Log("DGJ==>TestData " +JsonConvert.SerializeObject(listAlarm));
  32. JsonData data = new JsonData();
  33. data["deviceClassification"] = "";
  34. data["page"] = 1;
  35. data["limit"] = 20;
  36. PanelController.Instance.Post("https://api-fat1.ghz-tech.com" + "/feature-tester/v1/AIot/alarm/getalarmrecord", data.ToJson(), (msg) =>
  37. {
  38. Debug.Log("DGJ ==> " + "/feature-tester/v1/AIot/alarm/getalarmrecord" + " " + msg);
  39. JsonData data = JsonMapper.ToObject(msg);
  40. if (data["code"].ToString() == "200")
  41. {
  42. string msgData = data["data"].ToString();
  43. msgData = JsonConvert.SerializeObject(data["data"]["list"].ToJson());
  44. Debug.Log(JsonConvert.SerializeObject(msgData));
  45. // JsonData data2 = JsonMapper.ToObject(msgData);
  46. listAlarm = JsonConvert.DeserializeObject<List<AIDeviceAlarm>>(data["data"]["list"].ToJson());
  47. ListCount = int.Parse(data["data"]["total"].ToString());
  48. DataUpdate(listAlarm);
  49. }
  50. StartCoroutine(GetListAlarm());
  51. });
  52. }
  53. private IEnumerator GetListAlarm()
  54. {
  55. bool isHttp = false;
  56. while (listAlarm.Count < ListCount)
  57. {
  58. yield return new WaitForFixedUpdate();
  59. if (!isHttp)
  60. {
  61. isHttp = true;
  62. JsonData data = new JsonData();
  63. data["deviceClassification"] = "";
  64. data["page"] = listAlarm.Count / 20 + 1;
  65. data["limit"] = 20;
  66. PanelController.Instance.Post("https://api-fat1.ghz-tech.com" + "/feature-tester/v1/AIot/alarm/getalarmrecord", data.ToJson(), (msg) =>
  67. {
  68. Debug.Log("DGJ ==> " + "/feature-tester/v1/AIot/alarm/getalarmrecord" + " " + msg);
  69. JsonData data = JsonMapper.ToObject(msg);
  70. if (data["code"].ToString() == "200")
  71. {
  72. string msgData = data["data"]["list"].ToString();
  73. List<AIDeviceAlarm> listAlarmdata = new List<AIDeviceAlarm>();
  74. listAlarmdata = JsonConvert.DeserializeObject<List<AIDeviceAlarm>>(data["data"]["list"].ToJson());
  75. if (listAlarmdata.Count > 0)
  76. {
  77. listAlarm.AddRange(listAlarmdata);
  78. }
  79. }
  80. isHttp = false;
  81. });
  82. }
  83. }
  84. }
  85. public void DataUpdate(List<AIDeviceAlarm> listdata)
  86. {
  87. this.listAlarm = listdata;
  88. StartScrollView();
  89. }
  90. public void StartScrollView()
  91. {
  92. VerticalScroll.Init(NormalCallBack);
  93. VerticalScroll.ShowList(ListCount);
  94. }
  95. private void NormalCallBack(GameObject cell, int index)
  96. {
  97. // cell.transform.Find("Text1").GetComponent<Text>().text = index.ToString();
  98. Debug.Log("DGJ ==> Index" + index);
  99. if (isGetData)
  100. return;
  101. if (ListCount != listAlarm.Count && ((index + 1) >= listAlarm.Count))
  102. {
  103. isGetData = true;
  104. // 请求添加
  105. JsonData data = new JsonData();
  106. data["deviceClassification"] = "";
  107. data["page"] = listAlarm.Count / 20 + 1;
  108. data["limit"] = 20;
  109. PanelController.Instance.Post("https://api-fat1.ghz-tech.com" + "/feature-tester/v1/AIot/alarm/getalarmrecord", data.ToJson(), (msg) =>
  110. {
  111. Debug.Log("DGJ ==> " + "/feature-tester/v1/AIot/alarm/getalarmrecord" + " " + msg);
  112. JsonData data = JsonMapper.ToObject(msg);
  113. if (data["code"].ToString() == "200")
  114. {
  115. string msgData = data["data"]["list"].ToString();
  116. List<AIDeviceAlarm> listAlarmdata = new List<AIDeviceAlarm>();
  117. listAlarmdata = JsonConvert.DeserializeObject<List<AIDeviceAlarm>>(data["data"]["list"].ToJson());
  118. if (listAlarmdata.Count > 0)
  119. {
  120. listAlarm.AddRange(listAlarmdata);
  121. }
  122. }
  123. scrollRect.inertia = true;
  124. scrollRect.decelerationRate = 0.135f;
  125. });
  126. scrollRect.inertia = false;
  127. scrollRect.decelerationRate = 0;
  128. return;
  129. }
  130. if (dicAlarmItem.ContainsKey(cell))
  131. {
  132. dicAlarmItem[cell].DataUpdate(listAlarm[index]);
  133. }
  134. else
  135. {
  136. dicAlarmItem.Add(cell, cell.GetComponent<AIDeviceAlarmItem>());
  137. dicAlarmItem[cell].DataUpdate(listAlarm[index]);
  138. }
  139. }
  140. }