using System; using System.Collections; using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; using UnityEngine.UI; // 新风机组数据 public class FreshAirVentilatorController : MonoBehaviour { public TextMeshProUGUI location; // 新风机位置 private Transform dataView; private HttpRequestHandler httpRequestHandler; private List freshAirVentilatorDatas; private List lastData; private string httpUrl=HttpRequestHandler.httpIp+"/situation/deviceData/deviceDataApi"; private string encryptText; // 加密文本 public static string deviceId="AHU_5_03"; public static string deviceName=""; public Sprite normalSprite1; // 正常状态图片 public Sprite normalSprite2; // 正常状态图片 public Sprite alarmSprite1; // 报警状态图片 public Sprite alarmSprite2; // 报警状态图片 private void Awake() { dataView = transform; httpRequestHandler = gameObject.AddComponent(); } void OnEnable() { StartCoroutine(UpdateDataSource()); } public IEnumerator UpdateDataSource() { while (true) { string AESValue = string.Format( "{{\n \"deviceId\": \"{0}\"\n}}", deviceId); 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 returnInfo = JsonUtility.FromJson>(response); freshAirVentilatorDatas = returnInfo.data.Tags; UpdateDataView(); } // 请求失败的回调 private void OnError(string error) { Debug.LogError("Request Error: " + error); } // 更新填充数据 private void UpdateDataView() { // 当数据未更新时,不刷新数据 if (lastData!=null && freshAirVentilatorDatas.SequenceEqual(lastData)) { print("新风机组数据未更新"+DateTime.Now); return; } print("新风机组数据更新了"+DateTime.Now); lastData = freshAirVentilatorDatas; // 通风柜位置 location.text = freshAirVentilatorDatas[0].Location; // 风机状态 FumeHoodTagsData FJ = freshAirVentilatorDatas.Find(data => data.ID == "tag0001"); dataView.GetChild(0).GetChild(3).GetComponent().text = FJ.Value=="1"?"开启":"关闭"; dataView.GetChild(0).GetChild(0).GetComponent().sprite=FJ.Value=="1"?normalSprite1:alarmSprite1; dataView.GetChild(0).GetChild(1).GetComponent().sprite=FJ.Value=="1"?normalSprite2:alarmSprite2; // 回风温度 FumeHoodTagsData HF1 = freshAirVentilatorDatas.Find(data => data.ID == "tag0030"); dataView.GetChild(1).GetChild(3).GetComponent().text = HF1.Value==""?"无":HF1.Value; // 回风湿度 FumeHoodTagsData HF2 = freshAirVentilatorDatas.Find(data => data.ID == "tag0031"); dataView.GetChild(2).GetChild(3).GetComponent().text = HF2.Value==""?"无":HF2.Value; // 送风温度 FumeHoodTagsData SF1 = freshAirVentilatorDatas.Find(data => data.ID == "tag0036"); dataView.GetChild(3).GetChild(3).GetComponent().text = SF1.Value==""?"无":SF1.Value; // 送风湿度 FumeHoodTagsData SF2 = freshAirVentilatorDatas.Find(data => data.ID == "tag0037"); dataView.GetChild(4).GetChild(3).GetComponent().text = SF2.Value==""?"无":SF2.Value; } }