123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- 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<Text> 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<string> 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<string, float> dicTemperature = new Dictionary<string, float>();
- Dictionary<string, float> dicHumidity = new Dictionary<string, float>();
- dicTemperature = JsonConvert.DeserializeObject<Dictionary<string, float>>(data["data"]["point"]["ζÈ"].ToJson());
- dicHumidity = JsonConvert.DeserializeObject<Dictionary<string, float>>(data["data"]["point"]["ʪ¶È"].ToJson());
- List<Vector3> listTemperature = new List<Vector3>();
- List<Vector3> listhumidity = new List<Vector3>();
- List<string> listTimerStr = new List<string>();
- 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<Vector3> listTempeatureData = new List<Vector3>();
- 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<string>();
- 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<Vector3> listHumidityData = new List<Vector3>();
- 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<string> GetListTime()
- {
- List<string> listTimer = new List<string>();
- 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;
- }
- }
|