NoiseControl.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using XCharts.Runtime;
  9. public class NoiseControl : MonoBehaviour
  10. {
  11. public LineChart noiseLine;
  12. public List<Text> listTimer;
  13. private void Start()
  14. {
  15. StartCoroutine(Timer(0.3f));
  16. }
  17. private IEnumerator Timer(float timers)
  18. {
  19. yield return new WaitForSeconds(timers);
  20. GetData();
  21. }
  22. private IEnumerable Timer2(float timers)
  23. {
  24. yield return new WaitForSeconds(timers);
  25. GetData();
  26. while (true)
  27. {
  28. yield return new WaitForSeconds(1800);
  29. GetData();
  30. }
  31. }
  32. private void GetData()
  33. {
  34. List<string> listTimerData = GetListTime();
  35. JsonData data = new JsonData();
  36. data["deviceClassification"] = "VOICE";
  37. data["ago"] = 24;
  38. PanelController.Instance.Post("https://api-fat1.ghz-tech.com" + "/feature-tester/v1/AIot/device/getDatapoint", data.ToJson(), (msg) =>
  39. {
  40. Debug.Log(msg);
  41. JsonData data = JsonMapper.ToObject(msg);
  42. if (data["code"].ToString() == "200")
  43. {
  44. Dictionary<string, float> dicNoise = new Dictionary<string, float>();
  45. dicNoise = JsonConvert.DeserializeObject<Dictionary<string, float>>(data["data"]["point"]["NOISEÖµ"].ToJson());
  46. List<Vector3> listNoise = new List<Vector3>();
  47. List<string> listTimerStr = new List<string>();
  48. foreach (var item in dicNoise)
  49. {
  50. string[] str1 = item.Key.Split(' ');
  51. string[] str2 = str1[1].Split(':');
  52. if (str2[1] == "00")
  53. {
  54. int timers = int.Parse(str2[0]);
  55. timers++;
  56. timers = timers % 24;
  57. string timer;
  58. if (timers < 10)
  59. {
  60. timer = "0" + timers.ToString() + ":00";
  61. }
  62. else
  63. {
  64. timer = timers.ToString() + ":00";
  65. }
  66. listTimerStr.Add(timer);
  67. listNoise.Add(new Vector3(0, item.Value, 0));
  68. }
  69. }
  70. List<Vector3> listNoiseData = new List<Vector3>();
  71. for (int i = 0; i < listTimerData.Count - 1; i++)
  72. {
  73. bool finish = false;
  74. for (int j = 0; j < listTimerStr.Count; j++)
  75. {
  76. if (listTimerData[i] == listTimerStr[j])
  77. {
  78. finish = true;
  79. listNoiseData.Add(listNoise[i]);
  80. break;
  81. }
  82. }
  83. if (finish == false)
  84. {
  85. listNoiseData.Add(Vector3.up);
  86. }
  87. }
  88. if (listTimerData[listNoiseData.Count - 1] == listTimerStr[listTimerStr.Count - 1])
  89. {
  90. listNoiseData.Add(listNoise[listTimerStr.Count - 1]);
  91. }
  92. else
  93. {
  94. listNoiseData.Add(Vector3.up);
  95. }
  96. for (int i = 0; i < listTimerData.Count; i++)
  97. {
  98. listTimer[i].text = listTimerData[i];
  99. }
  100. for (int i = 0; i < listNoiseData.Count; i++)
  101. {
  102. noiseLine.UpdateData("ÔëÉù", i, listNoiseData[i].y);
  103. }
  104. }
  105. });
  106. }
  107. private List<string> GetListTime()
  108. {
  109. List<string> listTimer = new List<string>();
  110. int hour = DateTime.Now.Hour;
  111. for (int i = 0; i < 7; i++)
  112. {
  113. int hTimer = hour - 24 + i * 4 - 1;
  114. if (hTimer < 0)
  115. hTimer = 24 + hTimer;
  116. if (hTimer < 10)
  117. {
  118. listTimer.Add("0" + hTimer.ToString() + ":00");
  119. }
  120. else
  121. {
  122. listTimer.Add(hTimer.ToString() + ":00");
  123. }
  124. Debug.Log(listTimer[i]);
  125. }
  126. return listTimer;
  127. }
  128. }