112 lines
3.8 KiB
C#
112 lines
3.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using XCharts.Runtime;
|
|
|
|
// 温湿度趋势图
|
|
public class HumitureTrend_Controller : MonoBehaviour
|
|
{
|
|
private LineChart lineChart;
|
|
private HttpRequestHandler httpRequestHandler;
|
|
private List<HumitureStatisticsData> _humitureStatisticsData;
|
|
private List<HumitureStatisticsData> lastData;
|
|
private string httpUrl=HttpRequestHandler.httpIp+"/situation/deviceData/deviceHumitureStatisticsApi";
|
|
private string encryptText; // 加密文本
|
|
private int day=15; // 天数
|
|
|
|
public string deviceId="DDC04_2";
|
|
public string tagId1="tag0012";
|
|
public string tagId2="tag0013";
|
|
|
|
void OnEnable()
|
|
{
|
|
lineChart=gameObject.GetComponent<LineChart>();
|
|
httpRequestHandler = gameObject.AddComponent<HttpRequestHandler>();
|
|
|
|
StartCoroutine(UpdateDataSource());
|
|
}
|
|
|
|
public IEnumerator UpdateDataSource()
|
|
{
|
|
while (true)
|
|
{
|
|
string nowTime = DateTime.Now.ToString("yyyy-MM-dd");
|
|
string startTime = DateTime.Now.AddDays(-day).ToString("yyyy-MM-dd");
|
|
string AESValue = string.Format(
|
|
"{{\n \"deviceId\": \"{0}\",\n \"endTime\": \"{1}\",\n \"startTime\": \"{2}\",\n \"statisticsType\": \"1\"\n}}",
|
|
deviceId,nowTime, startTime);
|
|
|
|
encryptText = AESHelper.Encrypt(AESValue);
|
|
|
|
// 发起一个 POST 请求
|
|
string jsonData = "{\"encryptText\":\"" + encryptText + "\"}";
|
|
StartCoroutine(httpRequestHandler.PostRequest(httpUrl, jsonData, OnPostSuccess, OnError));
|
|
yield return new WaitForSeconds(4f);
|
|
}
|
|
}
|
|
|
|
// POST 请求成功回调
|
|
private void OnPostSuccess(string response)
|
|
{
|
|
ReturnInfo<List<HumitureStatisticsData>> returnInfo = JsonUtility.FromJson<ReturnInfo<List<HumitureStatisticsData>>>(response);
|
|
_humitureStatisticsData = returnInfo.data;
|
|
UpdateDataView();
|
|
}
|
|
|
|
// 请求失败的回调
|
|
private void OnError(string error)
|
|
{
|
|
Debug.LogError("Request Error: " + error);
|
|
}
|
|
|
|
|
|
// 更新填充数据
|
|
private void UpdateDataView()
|
|
{
|
|
// 当数据未更新时,不刷新数据
|
|
if (lastData!=null && _humitureStatisticsData.SequenceEqual(lastData))
|
|
{
|
|
// print("温湿度数据未更新"+DateTime.Now);
|
|
return;
|
|
}
|
|
lastData = _humitureStatisticsData;
|
|
|
|
print("温湿度趋势:"+_humitureStatisticsData.Count);
|
|
// 温度
|
|
List<HumitureStatisticsData> TDataList=_humitureStatisticsData.Where(x=>x.TagId==tagId1).ToList();
|
|
// 湿度
|
|
List<HumitureStatisticsData> HDataList=_humitureStatisticsData.Where(x=>x.TagId==tagId2).ToList();
|
|
// 趋势图
|
|
XAxis xAxis = lineChart.GetChartComponent<XAxis>();
|
|
// x轴清空
|
|
xAxis.data.Clear();
|
|
// 清空数据
|
|
lineChart.ClearSerieData();
|
|
|
|
// X轴添加
|
|
for (int i = 0; i < 7; i++)
|
|
{
|
|
DateTime dateTime = DateTime.Now.AddDays(+i-day);
|
|
string date = dateTime.ToString("dd")+"日";
|
|
xAxis.data.Add(date);
|
|
}
|
|
|
|
// 温湿度
|
|
for (int xIndex = 0; xIndex < xAxis.data.Count; xIndex++)
|
|
{
|
|
HumitureStatisticsData find1 = TDataList.Find(x=>x.Identification+"日"==xAxis.data[xIndex]);
|
|
lineChart.series[0].AddData(find1==null?0:find1.StatisticsTotal/find1.StatisticsCount);
|
|
|
|
HumitureStatisticsData find2 = HDataList.Find(x=>x.Identification+"日"==xAxis.data[xIndex]);
|
|
lineChart.series[1].AddData(find2==null?0:find2.StatisticsTotal/find2.StatisticsCount);
|
|
|
|
}
|
|
|
|
lineChart.RefreshChart();
|
|
}
|
|
|
|
}
|