79 lines
2.7 KiB
C#
79 lines
2.7 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
using TMPro;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
public class DeviceStateController : MonoBehaviour
|
||
|
|
{
|
||
|
|
private Transform dataView; // 显示对象
|
||
|
|
|
||
|
|
private HttpRequestHandler httpRequestHandler;
|
||
|
|
private List<DeviceStateData> deviceStateDatas;
|
||
|
|
private List<DeviceStateData> lastDatas; // 上一次的数据源
|
||
|
|
private string httpUrl=HttpRequestHandler.httpIp+"/device/ledger/countDeviceStatus";
|
||
|
|
private string encryptText; // 加密文本
|
||
|
|
|
||
|
|
void OnEnable()
|
||
|
|
{
|
||
|
|
dataView = transform;
|
||
|
|
httpRequestHandler = gameObject.AddComponent<HttpRequestHandler>();
|
||
|
|
|
||
|
|
encryptText = AESHelper.Encrypt("/device/ledger/countDeviceStatus");
|
||
|
|
|
||
|
|
StartCoroutine(UpdateDataSource());
|
||
|
|
}
|
||
|
|
|
||
|
|
public IEnumerator UpdateDataSource()
|
||
|
|
{
|
||
|
|
while (true)
|
||
|
|
{
|
||
|
|
// 发起一个 POST 请求
|
||
|
|
string jsonData = "{\"encryptText\":\"" + encryptText + "\"}";
|
||
|
|
StartCoroutine(httpRequestHandler.PostRequest(httpUrl, jsonData, OnPostSuccess, OnError));
|
||
|
|
yield return new WaitForSeconds(4f);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// POST 请求成功回调
|
||
|
|
private void OnPostSuccess(string response)
|
||
|
|
{
|
||
|
|
Debug.Log("POST Success 设备状态: " + DateTime.Now);
|
||
|
|
ReturnInfo<List<DeviceStateData>> returnInfo = JsonUtility.FromJson<ReturnInfo<List<DeviceStateData>>>(response);
|
||
|
|
deviceStateDatas = returnInfo.data;
|
||
|
|
|
||
|
|
UpdateDataView();
|
||
|
|
}
|
||
|
|
|
||
|
|
// 请求失败的回调
|
||
|
|
private void OnError(string error)
|
||
|
|
{
|
||
|
|
Debug.LogError("Request Error: " + error);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 更新填充数据
|
||
|
|
private void UpdateDataView()
|
||
|
|
{
|
||
|
|
int count = deviceStateDatas.Count;
|
||
|
|
print("设备状态数量:"+count);
|
||
|
|
|
||
|
|
for (int i = 0; i < deviceStateDatas.Count; i++)
|
||
|
|
{
|
||
|
|
switch (deviceStateDatas[i].deviceStatus)
|
||
|
|
{
|
||
|
|
case "1": dataView.GetChild(0).GetComponent<TextMeshProUGUI>().text =
|
||
|
|
deviceStateDatas[i].statusCount.ToString(); break;
|
||
|
|
case "2": dataView.GetChild(1).GetComponent<TextMeshProUGUI>().text =
|
||
|
|
deviceStateDatas[i].statusCount.ToString(); break;
|
||
|
|
case "3": dataView.GetChild(2).GetComponent<TextMeshProUGUI>().text =
|
||
|
|
deviceStateDatas[i].statusCount.ToString(); break;
|
||
|
|
case "4": dataView.GetChild(3).GetComponent<TextMeshProUGUI>().text =
|
||
|
|
deviceStateDatas[i].statusCount.ToString(); break;
|
||
|
|
case "5": dataView.GetChild(4).GetComponent<TextMeshProUGUI>().text =
|
||
|
|
deviceStateDatas[i].statusCount.ToString(); break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|