86 lines
2.4 KiB
C#
86 lines
2.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
// 房间的环境数据
|
|
public class FootEnvironmentController : MonoBehaviour
|
|
{
|
|
public List<GameObject> dataView;
|
|
public string devicesId;
|
|
public List<string> ids;
|
|
public List<string> suffixs;
|
|
|
|
private HttpRequestHandler httpRequestHandler;
|
|
private int index = 0; // 数据下标
|
|
private EMIMS_Float_Data _emimsFloatDatas;
|
|
private string httpUrl=HttpRequestHandler.httpIp+"/situation/deviceData/deviceDataApi";
|
|
private string encryptText; // 加密文本
|
|
|
|
private void Awake()
|
|
{
|
|
if (gameObject.GetComponent<HttpRequestHandler>()==null)
|
|
{
|
|
httpRequestHandler = gameObject.AddComponent<HttpRequestHandler>();
|
|
}
|
|
else
|
|
{
|
|
httpRequestHandler = gameObject.GetComponent<HttpRequestHandler>();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void OnEnable()
|
|
{
|
|
string AESValue = string.Format(
|
|
"{{\n \"deviceId\": \"{0}\"\n}}",
|
|
devicesId);
|
|
encryptText = AESHelper.Encrypt(AESValue);
|
|
StartCoroutine(UpdateDataSource());
|
|
}
|
|
|
|
public IEnumerator UpdateDataSource()
|
|
{
|
|
while (true)
|
|
{
|
|
// 发起一个 POST 请求
|
|
string jsonData = "{\"encryptText\":\"" + encryptText + "\"}";
|
|
StartCoroutine(httpRequestHandler.PostRequest(httpUrl, jsonData, OnPostSuccess, OnError));
|
|
yield return new WaitForSeconds(10f);
|
|
}
|
|
}
|
|
|
|
// POST 请求成功回调
|
|
private void OnPostSuccess(string response)
|
|
{
|
|
ReturnInfo<EMIMS_Float_Data> returnInfo = JsonUtility.FromJson<ReturnInfo<EMIMS_Float_Data>>(response);
|
|
_emimsFloatDatas = returnInfo.data;
|
|
UpdateDataView();
|
|
}
|
|
|
|
// 请求失败的回调
|
|
private void OnError(string error)
|
|
{
|
|
Debug.LogError("Request Error: " + error);
|
|
}
|
|
|
|
|
|
// 更新填充数据
|
|
private void UpdateDataView()
|
|
{
|
|
if (_emimsFloatDatas==null || _emimsFloatDatas.Tags==null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < dataView.Count; i++)
|
|
{
|
|
EM_Float_Data emFloatData=_emimsFloatDatas.Tags.Find(item => item.ID == ids[i]);
|
|
dataView[i].GetComponent<TextMeshProUGUI>().text = emFloatData==null?"无信号": emFloatData.Value.ToString() + suffixs[i];
|
|
}
|
|
|
|
|
|
}
|
|
}
|