using LitJson; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using XCharts.Runtime; public class HumitureLineControl : MonoBehaviour { public LineChart humitLine; public List listTimer; private void Start() { StartCoroutine(Timer(0.3f)); } private IEnumerator Timer(float timers) { yield return new WaitForSeconds(timers); GetData(); while (true) { yield return new WaitForSeconds(1800); GetData(); } } private void GetData() { List listTimerData = GetListTime(); JsonData data = new JsonData(); data["deviceClassification"] = "TEMPERATURE_HUMIDITY"; data["ago"] = 26; PanelController.Instance.Post("https://api-fat1.ghz-tech.com" + "/feature-tester/v1/AIot/device/getDatapoint", data.ToJson(), (msg) => { Debug.Log(msg); JsonData data = JsonMapper.ToObject(msg); if (data["code"].ToString() == "200") { Dictionary dicTemperature = new Dictionary(); Dictionary dicHumidity = new Dictionary(); dicTemperature = JsonConvert.DeserializeObject>(data["data"]["point"]["梁業"].ToJson()); dicHumidity = JsonConvert.DeserializeObject>(data["data"]["point"]["物業"].ToJson()); List listTemperature = new List(); List listhumidity = new List(); List listTimerStr = new List(); foreach (var item in dicTemperature) { string[] str1 = item.Key.Split(' '); string[] str2 = str1[1].Split(':'); if (str2[1] == "00") { int timers = int.Parse(str2[0]); timers++; timers = timers % 24; string timer; if (timers < 10) { timer = "0" + timers.ToString() + ":00"; } else { timer = timers.ToString() + ":00"; } listTimerStr.Add(timer); listTemperature.Add(new Vector3(0, item.Value, 0)); } } List listTempeatureData = new List(); for (int i = 0; i < listTimerData.Count - 1; i++) { bool finish = false; for (int j = 0; j < listTimerStr.Count; j++) { if (listTimerData[i] == listTimerStr[j]) { finish = true; listTempeatureData.Add(listTemperature[i]); break; } } if (finish == false) { listTempeatureData.Add(Vector3.up); } } if (listTimerData[listTempeatureData.Count - 1] == listTimerStr[listTimerStr.Count - 1]) { listTempeatureData.Add(listTemperature[listTimerStr.Count - 1]); } else { listTempeatureData.Add(Vector3.up); } listTimerStr.Clear(); listTimerStr = new List(); foreach (var item in dicHumidity) { string[] str1 = item.Key.Split(' '); string[] str2 = str1[1].Split(':'); if (str2[1] == "00") { int timers = int.Parse(str2[0]); timers++; timers = timers % 24; string timer; if (timers < 10) { timer = "0" + timers.ToString() + ":00"; } else { timer = timers.ToString() + ":00"; } listTimerStr.Add(timer); listhumidity.Add(new Vector3(0, item.Value, 0)); } } List listHumidityData = new List(); for (int i = 0; i < listTimerData.Count - 1; i++) { bool finish = false; for (int j = 0; j < listTimerStr.Count; j++) { if (listTimerData[i] == listTimerStr[j]) { finish = true; listHumidityData.Add(listhumidity[i]); break; } } if (finish == false) { listHumidityData.Add(Vector3.up); } } if (listTimerData[listTempeatureData.Count - 1] == listTimerStr[listTimerStr.Count - 1]) { listHumidityData.Add(listhumidity[listTimerStr.Count - 1]); } else { listHumidityData.Add(Vector3.up); } for (int i = 0; i < listTimerData.Count; i++) { listTimer[i].text = listTimerData[i]; } for (int i = 0; i < listTempeatureData.Count; i++) { humitLine.UpdateData("梁業", i, listTempeatureData[i].y); humitLine.UpdateData("物業", i, listHumidityData[i].y); } //int num = 0; //for (int i = 0; i < listTimerStr.Count; i=i+4) //{ // string[] str = listTimerStr[i].Split(' '); // string[] str2 = str[1].Split(':'); // int timers = int.Parse(str2[0]); // timers++; // Debug.Log("DGJ ===> timers "+ num+" "+ + timers); // if (timers < 10) // { // listTimer[num].text = "0" + timers.ToString() + ":00"; // } // else // { // listTimer[num].text = timers.ToString() + ":00"; // } // humitLine.UpdateData("梁業", num, listTemperature[i].y); // humitLine.UpdateData("物業", num, listhumidity[i].y); // num++; //} //humitLine.UpdateData("梁業", num, listTemperature[listTemperature.Count - 1].y); //humitLine.UpdateData("物業", num, listhumidity[listTemperature.Count - 1].y); } }); } private List GetListTime() { List listTimer = new List(); int hour = DateTime.Now.Hour; for (int i = 0; i < 7; i++) { int hTimer = hour - 24 + i*4-1; if (hTimer < 0) hTimer = 24 + hTimer; if (hTimer < 10) { listTimer.Add("0" + hTimer.ToString() + ":00"); } else { listTimer.Add(hTimer.ToString() + ":00"); } Debug.Log(listTimer[i]); } return listTimer; } }