HumitureLineControl.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using XCharts.Runtime;
  10. public class HumitureLineControl : MonoBehaviour
  11. {
  12. public LineChart humitLine;
  13. public List<Text> listTimer;
  14. private void Start()
  15. {
  16. StartCoroutine(Timer(0.3f));
  17. }
  18. private IEnumerator Timer(float timers)
  19. {
  20. yield return new WaitForSeconds(timers);
  21. GetData();
  22. while (true)
  23. {
  24. yield return new WaitForSeconds(1800);
  25. GetData();
  26. }
  27. }
  28. private void GetData()
  29. {
  30. List<string> listTimerData = GetListTime();
  31. JsonData data = new JsonData();
  32. data["deviceClassification"] = "TEMPERATURE_HUMIDITY";
  33. data["ago"] = 26;
  34. PanelController.Instance.Post("https://api-fat1.ghz-tech.com" + "/feature-tester/v1/AIot/device/getDatapoint", data.ToJson(), (msg) =>
  35. {
  36. Debug.Log(msg);
  37. JsonData data = JsonMapper.ToObject(msg);
  38. if (data["code"].ToString() == "200")
  39. {
  40. Dictionary<string, float> dicTemperature = new Dictionary<string, float>();
  41. Dictionary<string, float> dicHumidity = new Dictionary<string, float>();
  42. dicTemperature = JsonConvert.DeserializeObject<Dictionary<string, float>>(data["data"]["point"]["ζÈ"].ToJson());
  43. dicHumidity = JsonConvert.DeserializeObject<Dictionary<string, float>>(data["data"]["point"]["ʪ¶È"].ToJson());
  44. List<Vector3> listTemperature = new List<Vector3>();
  45. List<Vector3> listhumidity = new List<Vector3>();
  46. List<string> listTimerStr = new List<string>();
  47. foreach (var item in dicTemperature)
  48. {
  49. string[] str1 = item.Key.Split(' ');
  50. string[] str2 = str1[1].Split(':');
  51. if (str2[1] == "00")
  52. {
  53. int timers = int.Parse(str2[0]);
  54. timers++;
  55. timers = timers % 24;
  56. string timer;
  57. if (timers < 10)
  58. {
  59. timer = "0" + timers.ToString() + ":00";
  60. }
  61. else
  62. {
  63. timer = timers.ToString() + ":00";
  64. }
  65. listTimerStr.Add(timer);
  66. listTemperature.Add(new Vector3(0, item.Value, 0));
  67. }
  68. }
  69. List<Vector3> listTempeatureData = new List<Vector3>();
  70. for (int i = 0; i < listTimerData.Count - 1; i++)
  71. {
  72. bool finish = false;
  73. for (int j = 0; j < listTimerStr.Count; j++)
  74. {
  75. if (listTimerData[i] == listTimerStr[j])
  76. {
  77. finish = true;
  78. listTempeatureData.Add(listTemperature[i]);
  79. break;
  80. }
  81. }
  82. if (finish == false)
  83. {
  84. listTempeatureData.Add(Vector3.up);
  85. }
  86. }
  87. if (listTimerData[listTempeatureData.Count - 1] == listTimerStr[listTimerStr.Count - 1])
  88. {
  89. listTempeatureData.Add(listTemperature[listTimerStr.Count - 1]);
  90. }
  91. else
  92. {
  93. listTempeatureData.Add(Vector3.up);
  94. }
  95. listTimerStr.Clear();
  96. listTimerStr = new List<string>();
  97. foreach (var item in dicHumidity)
  98. {
  99. string[] str1 = item.Key.Split(' ');
  100. string[] str2 = str1[1].Split(':');
  101. if (str2[1] == "00")
  102. {
  103. int timers = int.Parse(str2[0]);
  104. timers++;
  105. timers = timers % 24;
  106. string timer;
  107. if (timers < 10)
  108. {
  109. timer = "0" + timers.ToString() + ":00";
  110. }
  111. else
  112. {
  113. timer = timers.ToString() + ":00";
  114. }
  115. listTimerStr.Add(timer);
  116. listhumidity.Add(new Vector3(0, item.Value, 0));
  117. }
  118. }
  119. List<Vector3> listHumidityData = new List<Vector3>();
  120. for (int i = 0; i < listTimerData.Count - 1; i++)
  121. {
  122. bool finish = false;
  123. for (int j = 0; j < listTimerStr.Count; j++)
  124. {
  125. if (listTimerData[i] == listTimerStr[j])
  126. {
  127. finish = true;
  128. listHumidityData.Add(listhumidity[i]);
  129. break;
  130. }
  131. }
  132. if (finish == false)
  133. {
  134. listHumidityData.Add(Vector3.up);
  135. }
  136. }
  137. if (listTimerData[listTempeatureData.Count - 1] == listTimerStr[listTimerStr.Count - 1])
  138. {
  139. listHumidityData.Add(listhumidity[listTimerStr.Count - 1]);
  140. }
  141. else
  142. {
  143. listHumidityData.Add(Vector3.up);
  144. }
  145. for (int i = 0; i < listTimerData.Count; i++)
  146. {
  147. listTimer[i].text = listTimerData[i];
  148. }
  149. for (int i = 0; i < listTempeatureData.Count; i++)
  150. {
  151. humitLine.UpdateData("ζÈ", i, listTempeatureData[i].y);
  152. humitLine.UpdateData("ʪ¶È", i, listHumidityData[i].y);
  153. }
  154. //int num = 0;
  155. //for (int i = 0; i < listTimerStr.Count; i=i+4)
  156. //{
  157. // string[] str = listTimerStr[i].Split(' ');
  158. // string[] str2 = str[1].Split(':');
  159. // int timers = int.Parse(str2[0]);
  160. // timers++;
  161. // Debug.Log("DGJ ===> timers "+ num+" "+ + timers);
  162. // if (timers < 10)
  163. // {
  164. // listTimer[num].text = "0" + timers.ToString() + ":00";
  165. // }
  166. // else
  167. // {
  168. // listTimer[num].text = timers.ToString() + ":00";
  169. // }
  170. // humitLine.UpdateData("ζÈ", num, listTemperature[i].y);
  171. // humitLine.UpdateData("ʪ¶È", num, listhumidity[i].y);
  172. // num++;
  173. //}
  174. //humitLine.UpdateData("ζÈ", num, listTemperature[listTemperature.Count - 1].y);
  175. //humitLine.UpdateData("ʪ¶È", num, listhumidity[listTemperature.Count - 1].y);
  176. }
  177. });
  178. }
  179. private List<string> GetListTime()
  180. {
  181. List<string> listTimer = new List<string>();
  182. int hour = DateTime.Now.Hour;
  183. for (int i = 0; i < 7; i++)
  184. {
  185. int hTimer = hour - 24 + i*4-1;
  186. if (hTimer < 0)
  187. hTimer = 24 + hTimer;
  188. if (hTimer < 10)
  189. {
  190. listTimer.Add("0" + hTimer.ToString() + ":00");
  191. }
  192. else
  193. {
  194. listTimer.Add(hTimer.ToString() + ":00");
  195. }
  196. Debug.Log(listTimer[i]);
  197. }
  198. return listTimer;
  199. }
  200. }