NG_/Assets/Scripts/Data/Controller/DeviceListController.cs
2024-12-13 19:40:05 +08:00

137 lines
5.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using TMPro;
using UnityEngine;
// 设备列表控制器
public class DeviceListController : MonoBehaviour,HttpController
{
public Transform dataView; // 显示对象
private HttpRequestHandler httpRequestHandler;
private int index = 0; // 数据下标
private List<DeviceData> deviceDatas;
private List<DeviceData> lastDatas; // 上一次的数据源
private bool firstLoad = true;
private string httpUrl=HttpRequestHandler.httpIp+"/device/ledger/listAllApi";
private string encryptText; // 加密文本
void OnEnable()
{
firstLoad = true;
httpRequestHandler = gameObject.AddComponent<HttpRequestHandler>();
encryptText = AESHelper.Encrypt("/device/ledger/listAllApi");
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<DeviceData>> returnInfo = JsonUtility.FromJson<ReturnInfo<List<DeviceData>>>(response);
deviceDatas = returnInfo.data;
if (firstLoad)
{
firstLoad = false;
UpdateDataView();
}
}
// 请求失败的回调
private void OnError(string error)
{
Debug.LogError("Request Error: " + error);
}
// 更新填充数据
private void UpdateDataView()
{
int count = deviceDatas.Count;
print("资产数量:"+count);
for (int i=0;i < dataView.childCount; i++)
{
// (index+i)%userDatas.Count()
// 当轮播图轮动多少次时列表的第一个数据就应该是数据源里的第多少个用i递增过去当超出数据源数量时使用%将多余的拿出去以获得正确的下标
// 序号
dataView.GetChild(i).GetChild(0).GetChild(0).GetChild(0).GetComponent<TextMeshProUGUI>().text =
((index+i)%count+1).ToString();
// 设备类型
dataView.GetChild(i).GetChild(0).GetChild(1).GetComponent<TextMeshProUGUI>().text =
deviceDatas[(index+i)%count].deviceType == "" ? "无":deviceDatas[(index+i)%count].deviceType;
// 设备名称
dataView.GetChild(i).GetChild(0).GetChild(2).GetComponent<TextMeshProUGUI>().text =
deviceDatas[(index+i)%count].deviceName == "" ? "无":deviceDatas[(index+i)%count].deviceName;
// 设备型号
dataView.GetChild(i).GetChild(0).GetChild(3).GetComponent<TextMeshProUGUI>().text =
deviceDatas[(index+i)%count].deviceModel == "" ? "无":deviceDatas[(index+i)%count].deviceModel;
// 设备状态
switch (deviceDatas[(index+i)%count].deviceStatus)
{
case "1": dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().text = "正常";
// 设备状态为正常时,显示浅蓝色
dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().color = new Color(0f, 0.8f, 1f);
break;
case "2": dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().text = "故障";
dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().color = new Color(1f, 0.7f, 0.2f);
break;
case "3": dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().text = "维修中";
dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().color = new Color(0.1f, 1f, 0f);
break;
case "4": dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().text = "报废";
dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().color = new Color(1f, 0.2f, 0.2f);
break;
case "5": dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().text = "未启用";
dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().color = new Color(0.4f, 0.5f, 0.5f);
break;
default: dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().text = "无";
dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().color = new Color(1f, 1f, 1f);
break;
}
/*if (index == deviceDatas.Count-1)
{
return;
}*/
}
}
// 更新下标
public void UpdateIndex()
{
// 轮播图每次轮动下标增加,下标达到数据源数量最大值时清零
if (deviceDatas == null)
{
return;
}
if (index<deviceDatas.Count-1)
{
index++;
}
else
{
index = 0;
}
UpdateDataView();
}
}