2026-05-11 08:39:33 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using Kill.Managers;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Kill.Network;
|
|
|
|
|
|
using Kill.UI.Components;
|
|
|
|
|
|
using Kill.Bluetooth;
|
|
|
|
|
|
using Kill.Bluetooth.Protocol;
|
|
|
|
|
|
using Kill.Core;
|
2026-06-08 08:55:10 +08:00
|
|
|
|
using Unity.VisualScripting;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Kill.UI.Pages
|
|
|
|
|
|
{
|
|
|
|
|
|
public class HomePageCtrl : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
public static HomePageCtrl Instance { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public GameObject noDevicePage;
|
|
|
|
|
|
public GameObject normalPage;
|
|
|
|
|
|
// 存储待执行的UI更新操作
|
|
|
|
|
|
private List<Action> pendingUIUpdates = new List<Action>();
|
|
|
|
|
|
public Button selectDeviceButton;
|
|
|
|
|
|
public GameObject selectDevicePage;
|
|
|
|
|
|
|
|
|
|
|
|
// 设备列表数据 (使用 Kill.Managers.DeviceInfo)
|
|
|
|
|
|
public List<Kill.Managers.DeviceInfo> OwnedDevices { get; private set; } = new List<Kill.Managers.DeviceInfo>();
|
|
|
|
|
|
public List<Kill.Managers.DeviceInfo> SharedDevices { get; private set; } = new List<Kill.Managers.DeviceInfo>();
|
|
|
|
|
|
public Kill.Managers.DeviceInfo selectedDevice;
|
|
|
|
|
|
bool hasBluetooth = false;
|
|
|
|
|
|
bool hasWifi = false;
|
|
|
|
|
|
|
|
|
|
|
|
// UI 组件引用
|
|
|
|
|
|
public HomePageDeviceState deviceState;
|
|
|
|
|
|
public HomePageDeviceCtrl deviceCtrl;
|
|
|
|
|
|
public HomePageSchedule deviceSchedule;
|
|
|
|
|
|
public Button scheduleSettingButton;
|
|
|
|
|
|
public GameObject scheduleSettingPrefab;
|
2026-06-08 08:55:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 触发密码验证按钮
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public GameObject LockButtonPlane;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
// 状态初始化相关
|
|
|
|
|
|
private bool isDeviceStateInitializing = false; // 是否正在初始化设备状态
|
|
|
|
|
|
private int pendingStatusCount = 0; // 待接收的状态数量
|
2026-06-08 08:55:10 +08:00
|
|
|
|
private int TOTAL_STATUS_COUNT = 8; // 总共需要查询的状态数量
|
2026-05-11 08:39:33 +08:00
|
|
|
|
private float statusQueryTimeout = 15f; // 状态查询超时时间(秒)
|
|
|
|
|
|
private Coroutine statusQueryTimeoutCoroutine; // 超时协程
|
|
|
|
|
|
|
|
|
|
|
|
// 串行查询相关
|
|
|
|
|
|
private Queue<Action> statusQueryQueue = new Queue<Action>(); // 状态查询队列
|
|
|
|
|
|
private bool isQueryingStatus = false; // 是否正在查询状态
|
|
|
|
|
|
|
|
|
|
|
|
// 工作模式设置后查询相关
|
|
|
|
|
|
private bool isWaitingForWorkModeQuery = false; // 是否正在等待工作模式查询响应
|
|
|
|
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
Instance = this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async Task Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 订阅蓝牙事件
|
|
|
|
|
|
SubscribeBluetoothEvents();
|
|
|
|
|
|
|
|
|
|
|
|
// 获取设备列表
|
|
|
|
|
|
await FetchDeviceList();
|
|
|
|
|
|
Init();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-08 08:55:10 +08:00
|
|
|
|
float waitLockTime=0;
|
|
|
|
|
|
void FixedUpdate()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!LockButtonPlane.activeSelf)
|
|
|
|
|
|
{
|
|
|
|
|
|
waitLockTime+=Time.deltaTime;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
waitLockTime=0;
|
|
|
|
|
|
}
|
|
|
|
|
|
if(waitLockTime>=60)
|
|
|
|
|
|
LockButtonPlane.SetActive(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
void OnApplicationPause(bool pause)
|
|
|
|
|
|
{
|
|
|
|
|
|
LockButtonPlane.SetActive(true);
|
|
|
|
|
|
}
|
2026-05-11 08:39:33 +08:00
|
|
|
|
void OnDestroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
UnsubscribeBluetoothEvents();
|
2026-06-08 08:55:10 +08:00
|
|
|
|
StopAllCoroutines();
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region 蓝牙事件订阅
|
|
|
|
|
|
|
|
|
|
|
|
private void SubscribeBluetoothEvents()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (BluetoothManager.Instance != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
BluetoothManager.Instance.OnConnectedSuccess += OnBluetoothConnected;
|
|
|
|
|
|
BluetoothManager.Instance.OnDisconnected += OnBluetoothDisconnected;
|
|
|
|
|
|
BluetoothManager.Instance.OnError += OnBluetoothError;
|
2026-06-10 15:04:14 +08:00
|
|
|
|
BluetoothManager.Instance.OnStopScan+=deviceState.StopScan;
|
|
|
|
|
|
BluetoothManager.Instance.OnStartScan+=deviceState.OnStartScan;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (BLECommunicationManager.Instance != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 订阅用户登录结果事件
|
|
|
|
|
|
BLECommunicationManager.Instance.OnUserLoginResult += OnUserLoginResult;
|
|
|
|
|
|
|
|
|
|
|
|
// 订阅设备状态事件
|
|
|
|
|
|
BLECommunicationManager.Instance.OnWorkModeSettingReceived += OnWorkModeReceived;
|
2026-06-08 08:55:10 +08:00
|
|
|
|
BLECommunicationManager.Instance.OnDeviceLockControlReceived += OnLockReceived;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
BLECommunicationManager.Instance.OnScheduleTaskListReceived += OnScheduleTaskListReceived;
|
|
|
|
|
|
BLECommunicationManager.Instance.OnAngleControlReceived += OnAngleControlReceived;
|
|
|
|
|
|
BLECommunicationManager.Instance.OnDistanceControlReceived += OnDistanceControlReceived;
|
|
|
|
|
|
BLECommunicationManager.Instance.OnFillLightControlReceived += OnFillLightControlReceived;
|
|
|
|
|
|
//BLECommunicationManager.Instance.OnMillimeterWaveSettingReceived += OnMillimeterWaveReceived;
|
|
|
|
|
|
BLECommunicationManager.Instance.OnVisualDetectionSettingReceived += OnVisualDetectionReceived;
|
2026-06-08 08:55:10 +08:00
|
|
|
|
// 订阅蚊虫数据事件
|
|
|
|
|
|
BLECommunicationManager.Instance.OnMosquitoDataReceived += OnMosquitoDataReceived;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UnsubscribeBluetoothEvents()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (BluetoothManager.Instance != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
BluetoothManager.Instance.OnConnectedSuccess -= OnBluetoothConnected;
|
|
|
|
|
|
BluetoothManager.Instance.OnDisconnected -= OnBluetoothDisconnected;
|
|
|
|
|
|
BluetoothManager.Instance.OnError -= OnBluetoothError;
|
2026-06-10 15:04:14 +08:00
|
|
|
|
BluetoothManager.Instance.OnStopScan-=deviceState.StopScan;
|
|
|
|
|
|
BluetoothManager.Instance.OnStartScan-=deviceState.OnStartScan;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (BLECommunicationManager.Instance != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 取消订阅用户登录结果事件
|
|
|
|
|
|
BLECommunicationManager.Instance.OnUserLoginResult -= OnUserLoginResult;
|
2026-06-08 08:55:10 +08:00
|
|
|
|
BLECommunicationManager.Instance.OnDeviceLockControlReceived -= OnLockReceived;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
BLECommunicationManager.Instance.OnWorkModeSettingReceived -= OnWorkModeReceived;
|
|
|
|
|
|
BLECommunicationManager.Instance.OnScheduleTaskListReceived -= OnScheduleTaskListReceived;
|
|
|
|
|
|
BLECommunicationManager.Instance.OnAngleControlReceived -= OnAngleControlReceived;
|
|
|
|
|
|
BLECommunicationManager.Instance.OnDistanceControlReceived -= OnDistanceControlReceived;
|
|
|
|
|
|
BLECommunicationManager.Instance.OnFillLightControlReceived -= OnFillLightControlReceived;
|
|
|
|
|
|
// BLECommunicationManager.Instance.OnMillimeterWaveSettingReceived -= OnMillimeterWaveReceived;
|
|
|
|
|
|
BLECommunicationManager.Instance.OnVisualDetectionSettingReceived -= OnVisualDetectionReceived;
|
2026-06-08 08:55:10 +08:00
|
|
|
|
// 取消订阅蚊虫数据事件
|
|
|
|
|
|
BLECommunicationManager.Instance.OnMosquitoDataReceived -= OnMosquitoDataReceived;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 蓝牙事件处理
|
|
|
|
|
|
|
|
|
|
|
|
// 标记是否已完成蓝牙用户登录
|
|
|
|
|
|
private bool isBluetoothUserLoggedIn = false;
|
|
|
|
|
|
|
|
|
|
|
|
private void OnBluetoothConnected()
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 蓝牙连接成功");
|
|
|
|
|
|
hasBluetooth = true;
|
|
|
|
|
|
|
|
|
|
|
|
// 更新连接状态UI
|
|
|
|
|
|
pendingUIUpdates.Add(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceState?.UpdateDeviceState(hasBluetooth, hasWifi);
|
2026-05-18 08:42:33 +08:00
|
|
|
|
// 先进行蓝牙用户登录,登录成功后再获取设备状态
|
|
|
|
|
|
StartCoroutine(BluetoothUserLoginThenQueryStatus());
|
2026-05-11 08:39:33 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-06-08 08:55:10 +08:00
|
|
|
|
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
2026-06-12 09:42:44 +08:00
|
|
|
|
private void OnWifiConnected()
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 设备已在线");
|
|
|
|
|
|
hasWifi = true;
|
|
|
|
|
|
|
|
|
|
|
|
// 更新连接状态UI
|
|
|
|
|
|
pendingUIUpdates.Add(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-05-11 08:39:33 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 蓝牙用户登录,登录成功后查询设备状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private IEnumerator BluetoothUserLoginThenQueryStatus()
|
|
|
|
|
|
{
|
2026-05-18 08:42:33 +08:00
|
|
|
|
LoadingUI.Show();
|
|
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取用户ID
|
|
|
|
|
|
string username = DataManager.Instance?.userInfo?.id;
|
|
|
|
|
|
if (string.IsNullOrEmpty(username))
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("[HomePageCtrl] 蓝牙用户登录失败:用户未登录");
|
|
|
|
|
|
yield break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 开始进行蓝牙用户登录,用户名: {username}");
|
|
|
|
|
|
isBluetoothUserLoggedIn = false;
|
|
|
|
|
|
|
|
|
|
|
|
// 调用蓝牙用户登录
|
|
|
|
|
|
BLECommunicationManager.Instance?.UserLogin(username, (response) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (response.IsSuccess)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 蓝牙用户登录成功");
|
|
|
|
|
|
isBluetoothUserLoggedIn = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError($"[HomePageCtrl] 蓝牙用户登录失败: {response.Status}");
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 等待登录结果(最多3秒)
|
|
|
|
|
|
float timeout = 3f;
|
|
|
|
|
|
float elapsed = 0f;
|
|
|
|
|
|
while (!isBluetoothUserLoggedIn && elapsed < timeout)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new WaitForSeconds(0.1f);
|
|
|
|
|
|
elapsed += 0.1f;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (isBluetoothUserLoggedIn)
|
|
|
|
|
|
{
|
2026-05-18 08:42:33 +08:00
|
|
|
|
Debug.Log("[HomePageCtrl] 蓝牙用户登录成功,获取锁定状态");
|
|
|
|
|
|
BLECommunicationManager.Instance.ReadDeviceLockControl();
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2026-05-18 08:42:33 +08:00
|
|
|
|
Debug.LogWarning("[HomePageCtrl] 蓝牙用户登录超时,3s重试");
|
|
|
|
|
|
yield return new WaitForSeconds(3);
|
|
|
|
|
|
StartCoroutine(BluetoothUserLoginThenQueryStatus());
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 用户登录结果回调
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void OnUserLoginResult(UserLoginResponse response)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 收到用户登录结果: {response.Status}");
|
|
|
|
|
|
// 登录结果在 UserLogin 回调中处理,这里仅作日志记录
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 开始设备状态初始化
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void StartDeviceStateInitialization()
|
|
|
|
|
|
{
|
|
|
|
|
|
isDeviceStateInitializing = true;
|
|
|
|
|
|
pendingStatusCount = TOTAL_STATUS_COUNT;
|
|
|
|
|
|
|
|
|
|
|
|
// 显示Loading,阻止操作
|
|
|
|
|
|
LoadingUI.Show();
|
|
|
|
|
|
|
|
|
|
|
|
// 启动超时检测
|
|
|
|
|
|
if (statusQueryTimeoutCoroutine != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
StopCoroutine(statusQueryTimeoutCoroutine);
|
|
|
|
|
|
}
|
|
|
|
|
|
statusQueryTimeoutCoroutine = StartCoroutine(StatusQueryTimeoutCoroutine());
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 开始设备状态初始化,待接收状态数: {pendingStatusCount}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 状态查询超时协程
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private IEnumerator StatusQueryTimeoutCoroutine()
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new WaitForSeconds(statusQueryTimeout);
|
|
|
|
|
|
|
|
|
|
|
|
if (isDeviceStateInitializing)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning($"[HomePageCtrl] 设备状态查询超时,已接收 {TOTAL_STATUS_COUNT - pendingStatusCount}/{TOTAL_STATUS_COUNT} 个状态");
|
|
|
|
|
|
FinishDeviceStateInitialization();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-08 08:55:10 +08:00
|
|
|
|
Coroutine bluetoothDeviceInfoCoroutine = null;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 完成设备状态初始化
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void FinishDeviceStateInitialization()
|
|
|
|
|
|
{
|
|
|
|
|
|
isDeviceStateInitializing = false;
|
|
|
|
|
|
pendingStatusCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// 隐藏Loading
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
|
|
|
|
|
|
// 停止超时协程
|
|
|
|
|
|
if (statusQueryTimeoutCoroutine != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
StopCoroutine(statusQueryTimeoutCoroutine);
|
|
|
|
|
|
statusQueryTimeoutCoroutine = null;
|
|
|
|
|
|
}
|
2026-06-08 08:55:10 +08:00
|
|
|
|
bluetoothDeviceInfoCoroutine=StartCoroutine(CheckDeviceError());
|
2026-05-11 08:39:33 +08:00
|
|
|
|
Debug.Log("[HomePageCtrl] 设备状态初始化完成");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 接收一个状态,减少待接收计数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void ReceiveStatus()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!isDeviceStateInitializing) return;
|
|
|
|
|
|
|
|
|
|
|
|
pendingStatusCount--;
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 接收到状态,剩余: {pendingStatusCount}/{TOTAL_STATUS_COUNT}");
|
|
|
|
|
|
|
|
|
|
|
|
// 所有状态都接收完成
|
|
|
|
|
|
if (pendingStatusCount <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
FinishDeviceStateInitialization();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnBluetoothDisconnected(string address)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 蓝牙断开连接");
|
|
|
|
|
|
hasBluetooth = false;
|
|
|
|
|
|
isBluetoothUserLoggedIn = false;
|
|
|
|
|
|
isQueryingStatus = false;
|
|
|
|
|
|
statusQueryQueue?.Clear();
|
|
|
|
|
|
UIManager.Instance?.ClearBackAction();
|
|
|
|
|
|
UIManager.Instance.OpenPage(UIManager.PageName.homePage, null, true);
|
2026-06-08 08:55:10 +08:00
|
|
|
|
if(bluetoothDeviceInfoCoroutine!=null)
|
|
|
|
|
|
{
|
|
|
|
|
|
StopCoroutine(bluetoothDeviceInfoCoroutine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnBluetoothError(string error)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError($"[HomePageCtrl] 蓝牙错误: {error}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 延迟查询设备状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private IEnumerator QueryDeviceStatusDelayed()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 等待 500ms 确保连接稳定
|
2026-05-18 08:42:33 +08:00
|
|
|
|
yield return new WaitForSeconds(0.1f);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
|
|
|
|
|
|
// 开始设备状态初始化(显示Loading)
|
|
|
|
|
|
StartDeviceStateInitialization();
|
|
|
|
|
|
|
|
|
|
|
|
// 串行查询设备状态
|
|
|
|
|
|
StartCoroutine(QueryAllDeviceStatusSequential());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 串行查询所有设备状态(收到返回后再发送下一个)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private IEnumerator QueryAllDeviceStatusSequential()
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 开始串行查询设备状态...");
|
|
|
|
|
|
|
|
|
|
|
|
// 清空队列
|
|
|
|
|
|
statusQueryQueue.Clear();
|
|
|
|
|
|
isQueryingStatus = true;
|
|
|
|
|
|
|
|
|
|
|
|
// 将查询请求加入队列
|
|
|
|
|
|
statusQueryQueue.Enqueue(() => { Debug.Log("[HomePageCtrl] 查询: 工作模式"); BLECommunicationManager.Instance?.ReadWorkMode(); });
|
|
|
|
|
|
statusQueryQueue.Enqueue(() => { Debug.Log("[HomePageCtrl] 查询: 定时任务"); BLECommunicationManager.Instance?.ReadScheduleTasks(); });
|
|
|
|
|
|
statusQueryQueue.Enqueue(() => { Debug.Log("[HomePageCtrl] 查询: 角度控制"); BLECommunicationManager.Instance?.ReadAngleControl(); });
|
|
|
|
|
|
statusQueryQueue.Enqueue(() => { Debug.Log("[HomePageCtrl] 查询: 距离控制"); BLECommunicationManager.Instance?.ReadDistanceControl(); });
|
|
|
|
|
|
statusQueryQueue.Enqueue(() => { Debug.Log("[HomePageCtrl] 查询: 补光灯控制"); BLECommunicationManager.Instance?.ReadFillLightControl(); });
|
|
|
|
|
|
statusQueryQueue.Enqueue(() => { Debug.Log("[HomePageCtrl] 查询: 视觉检测"); BLECommunicationManager.Instance?.ReadVisualDetectionSetting(); });
|
2026-06-08 08:55:10 +08:00
|
|
|
|
statusQueryQueue.Enqueue(OnWriteLanguageClick);
|
|
|
|
|
|
statusQueryQueue.Enqueue(OnWriteTimeClick);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
// 依次执行查询
|
|
|
|
|
|
while (statusQueryQueue.Count > 0 && isQueryingStatus)
|
|
|
|
|
|
{
|
|
|
|
|
|
int pendingCountBefore = pendingStatusCount;
|
|
|
|
|
|
|
|
|
|
|
|
var query = statusQueryQueue.Dequeue();
|
|
|
|
|
|
query?.Invoke();
|
|
|
|
|
|
|
|
|
|
|
|
// 等待 200ms 确保请求发送完成
|
|
|
|
|
|
yield return new WaitForSeconds(0.2f);
|
|
|
|
|
|
|
|
|
|
|
|
// 等待响应(最多2秒)
|
|
|
|
|
|
float waitTime = 0f;
|
|
|
|
|
|
float maxWaitTime = 2f;
|
|
|
|
|
|
while (waitTime < maxWaitTime)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 检查是否已接收到状态(通过 pendingStatusCount 减少来判断)
|
|
|
|
|
|
if (pendingStatusCount < pendingCountBefore)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 收到响应,继续下一个查询");
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
yield return new WaitForSeconds(0.05f);
|
|
|
|
|
|
waitTime += 0.05f;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
isQueryingStatus = false;
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 串行查询设备状态完成");
|
2026-06-08 08:55:10 +08:00
|
|
|
|
OnGetAllMosquitoDataClick();
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 查询所有设备状态(旧方法,已废弃)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void QueryAllDeviceStatus()
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 开始查询设备状态...");
|
|
|
|
|
|
|
|
|
|
|
|
// 查询工作模式
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadWorkMode();
|
|
|
|
|
|
|
|
|
|
|
|
// 查询硬件状态
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadHardwareStatus();
|
|
|
|
|
|
|
|
|
|
|
|
// 查询定时任务
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadScheduleTasks();
|
|
|
|
|
|
|
|
|
|
|
|
// 查询角度控制
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadAngleControl();
|
|
|
|
|
|
|
|
|
|
|
|
// 查询距离控制
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadDistanceControl();
|
|
|
|
|
|
|
|
|
|
|
|
// 查询补光灯控制
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadFillLightControl();
|
|
|
|
|
|
|
|
|
|
|
|
// 查询毫米波雷达设置
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadMillimeterWaveSetting();
|
|
|
|
|
|
|
|
|
|
|
|
// 查询视觉检测设置
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadVisualDetectionSetting();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 设备状态回调
|
|
|
|
|
|
|
|
|
|
|
|
// 当前工作模式
|
|
|
|
|
|
private WorkMode currentWorkMode = WorkMode.Standby;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置工作模式
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="mode">0:待机 1:扫描 2:消杀</param>
|
|
|
|
|
|
public void SetWorkMode(int mode)
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
waitLockTime=0;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
// 检查是否与当前模式相同
|
|
|
|
|
|
if ((int)currentWorkMode == mode)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 目标模式 {mode} 与当前模式相同,不执行切换");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查蓝牙连接
|
|
|
|
|
|
if (!BluetoothManager.Instance.IsConnected)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning("[HomePageCtrl] 蓝牙未连接,无法切换工作模式");
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 显示 Loading
|
|
|
|
|
|
LoadingUI.Show();
|
|
|
|
|
|
|
|
|
|
|
|
// 构建工作模式设置
|
|
|
|
|
|
WorkModeSetting setting = new WorkModeSetting
|
|
|
|
|
|
{
|
|
|
|
|
|
Mode = (WorkMode)mode
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 正在设置工作模式: {setting.Mode}");
|
|
|
|
|
|
|
|
|
|
|
|
// 发送设置命令
|
|
|
|
|
|
BLECommunicationManager.Instance?.WriteWorkMode(setting, (success) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Loom.QueueOnMainThread(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (success)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 工作模式设置成功: {setting.Mode}");
|
|
|
|
|
|
// 更新当前模式
|
|
|
|
|
|
currentWorkMode = setting.Mode;
|
|
|
|
|
|
|
|
|
|
|
|
// 更新 UI
|
|
|
|
|
|
pendingUIUpdates.Add(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceCtrl?.InitWrokMode(mode);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("[HomePageCtrl] 工作模式设置失败");
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-06-08 08:55:10 +08:00
|
|
|
|
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
2026-06-11 11:17:27 +08:00
|
|
|
|
public void SetWorkModeByWifi(int mode)
|
2026-05-11 08:39:33 +08:00
|
|
|
|
{
|
2026-06-11 11:17:27 +08:00
|
|
|
|
SetWorkModeByWifiAsync(mode);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
2026-06-11 11:17:27 +08:00
|
|
|
|
public async Task SetWorkModeByWifiAsync(int mode)
|
2026-05-11 08:39:33 +08:00
|
|
|
|
{
|
2026-06-11 11:17:27 +08:00
|
|
|
|
// 发送设置工作模式为消杀的指令
|
|
|
|
|
|
var setting = new WorkModeSetting { Mode = (WorkMode)mode };
|
|
|
|
|
|
byte[] commandBytes = BLECommunicationManager.Instance.GetCommandBytes(
|
|
|
|
|
|
BLEConstants.CMD_WORK_MODE_SETTING,
|
|
|
|
|
|
BLEConstants.RW_WRITE,
|
|
|
|
|
|
setting.ToBytes()
|
|
|
|
|
|
);
|
|
|
|
|
|
string commandHex = BitConverter.ToString(commandBytes).Replace("-", "");
|
|
|
|
|
|
Debug.Log(commandHex);
|
|
|
|
|
|
var requestData = new DeviceControlRequest
|
|
|
|
|
|
{
|
|
|
|
|
|
device_sn =selectedDevice.ble_mac,
|
|
|
|
|
|
command = commandHex
|
|
|
|
|
|
};
|
2026-05-11 08:39:33 +08:00
|
|
|
|
|
2026-06-11 11:17:27 +08:00
|
|
|
|
var response = await NetworkCtrl.Instance.Post<DeviceControlResponse>("/api/v1/device/command",requestData);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
|
2026-06-11 11:17:27 +08:00
|
|
|
|
// 处理结果
|
|
|
|
|
|
if (response.IsSuccess)
|
2026-05-11 08:39:33 +08:00
|
|
|
|
{
|
2026-06-11 11:17:27 +08:00
|
|
|
|
ToastUI.Show(response.Data.message);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-05-18 08:42:33 +08:00
|
|
|
|
private void OnLockReceived(DeviceLockControl setting)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 锁定状态: {setting.IsLocked}");
|
|
|
|
|
|
pendingUIUpdates.Add(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadingUI.Hide();
|
2026-06-08 08:55:10 +08:00
|
|
|
|
if (setting.IsLocked)
|
2026-05-18 08:42:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
deviceCtrl.InitDeviceControl(false);
|
|
|
|
|
|
BLECommunicationManager.Instance.ReadScheduleTasks();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceCtrl.InitDeviceControl(true);
|
|
|
|
|
|
StartCoroutine(QueryDeviceStatusDelayed());
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-05-11 08:39:33 +08:00
|
|
|
|
private void OnWorkModeReceived(WorkModeSetting setting)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 收到工作模式: {setting.Mode}");
|
|
|
|
|
|
ReceiveStatus();
|
|
|
|
|
|
|
|
|
|
|
|
// 更新当前工作模式
|
|
|
|
|
|
currentWorkMode = setting.Mode;
|
|
|
|
|
|
|
|
|
|
|
|
// 如果是工作模式设置后的查询响应,重置等待标志
|
|
|
|
|
|
if (isWaitingForWorkModeQuery)
|
|
|
|
|
|
{
|
|
|
|
|
|
isWaitingForWorkModeQuery = false;
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 工作模式查询响应已处理");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pendingUIUpdates.Add(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceCtrl?.InitWrokMode((int)setting.Mode);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void OnScheduleTaskListReceived(ScheduleTaskListResponse response)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果设置页面正在处理,跳过主页处理
|
|
|
|
|
|
if (ScheduleSettingPage.IsSettingPageActive())
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 设置页面正在处理定时任务列表,跳过主页更新");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (response.IsSuccess)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 收到定时任务列表: {response.Tasks.Length} 个任务");
|
|
|
|
|
|
ReceiveStatus();
|
|
|
|
|
|
pendingUIUpdates.Add(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceSchedule?.Init(response.Tasks);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ReceiveStatus();
|
|
|
|
|
|
}
|
2026-06-08 08:55:10 +08:00
|
|
|
|
scheduleSettingButton.interactable = true;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnAngleControlReceived(AngleControl control)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果设置页面正在处理,跳过主页处理
|
|
|
|
|
|
if (FovSettingPage.IsSettingPageActive())
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 设置页面正在处理角度控制,跳过主页更新");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 收到角度控制: {control.ActualAngle}°");
|
|
|
|
|
|
ReceiveStatus();
|
|
|
|
|
|
pendingUIUpdates.Add(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 使用实际角度值
|
|
|
|
|
|
int fov = (int)control.ActualAngle;
|
|
|
|
|
|
deviceCtrl?.InitFovText(fov);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 刷新角度控制设置(供FovSettingPage调用)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void RefreshAngleControl()
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 刷新角度控制设置");
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadAngleControl();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnDistanceControlReceived(DistanceControl control)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果设置页面正在处理,跳过主页处理
|
|
|
|
|
|
if (LensSettingPage.IsSettingPageActive())
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 设置页面正在处理距离控制,跳过主页更新");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 单位是 0.1m,需要除以 10 转换为实际米数
|
|
|
|
|
|
float detectionDistance = control.DetectionDistance / 10f;
|
|
|
|
|
|
float aimDistance = control.AimDistance / 10f;
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 收到距离控制: 检测={detectionDistance}m, 瞄准={aimDistance}m");
|
|
|
|
|
|
ReceiveStatus();
|
|
|
|
|
|
pendingUIUpdates.Add(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 使用检测距离,单位 0.1m 转实际米数,保留1位小数
|
|
|
|
|
|
deviceCtrl?.InitLensText(detectionDistance, DataManager.Instance.userInfo.unit_system); // 0 = 米
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 刷新lens设置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void RefreshLensControl()
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 刷新距离控制设置");
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadDistanceControl();
|
|
|
|
|
|
}
|
|
|
|
|
|
private void OnFillLightControlReceived(FillLightControl control)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果设置页面正在处理,跳过主页处理
|
|
|
|
|
|
if (LightSettingPage.IsSettingPageActive())
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 设置页面正在处理补光灯控制,跳过主页更新");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 收到补光灯控制: {control.Enable}");
|
|
|
|
|
|
ReceiveStatus();
|
|
|
|
|
|
pendingUIUpdates.Add(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceCtrl?.InitLightText(control.Enable);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
public void RefreshLightControl()
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 刷新补光灯控制设置");
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadFillLightControl();
|
|
|
|
|
|
}
|
|
|
|
|
|
// private void OnMillimeterWaveReceived(MillimeterWaveSetting setting)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Debug.Log($"[HomePageCtrl] 收到毫米波设置: {setting.Enable}");
|
|
|
|
|
|
// ReceiveStatus();
|
|
|
|
|
|
// pendingUIUpdates.Add(() =>
|
|
|
|
|
|
// {
|
|
|
|
|
|
// // 更新安全状态 - 毫米波
|
|
|
|
|
|
// // 注意:需要结合 VisualDetectionSetting 一起判断
|
|
|
|
|
|
// });
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
private void OnVisualDetectionReceived(VisualDetectionSetting setting)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果设置页面正在处理,跳过主页处理
|
|
|
|
|
|
if (SafetySettingPage.IsSettingPageActive())
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 设置页面正在处理视觉检测设置,跳过主页更新");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 收到视觉检测设置: {setting.Enable}");
|
|
|
|
|
|
ReceiveStatus();
|
|
|
|
|
|
pendingUIUpdates.Add(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceCtrl?.InitSafeText(setting.Enable);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2026-06-12 09:42:44 +08:00
|
|
|
|
public async Task Init()
|
2026-05-11 08:39:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (DataManager.Instance.userInfo.device_count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
noDevicePage.SetActive(true);
|
|
|
|
|
|
normalPage.SetActive(false);
|
|
|
|
|
|
selectedDevice = null;
|
|
|
|
|
|
selectDeviceButton.gameObject.SetActive(false);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
if (DataManager.Instance.userInfo.exam_completed == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
UIManager.Instance.OpenMainPage(UIManager.PageName.safetylearningPage);
|
|
|
|
|
|
}
|
2026-05-11 08:39:33 +08:00
|
|
|
|
noDevicePage.SetActive(false);
|
|
|
|
|
|
normalPage.SetActive(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (DataManager.Instance.selectedDeviceMac == "")
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
if(OwnedDevices.Count>0)
|
|
|
|
|
|
{
|
|
|
|
|
|
selectedDevice = OwnedDevices[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
selectedDevice=SharedDevices[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
DataManager.Instance.selectedDeviceMac = selectedDevice.ble_mac;
|
|
|
|
|
|
DataManager.Instance.SavaSelectedDeviceMac(selectedDevice.ble_mac);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
selectedDevice = OwnedDevices.Find(x => x.ble_mac == DataManager.Instance.selectedDeviceMac);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
if (selectedDevice == null)
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
selectedDevice = SharedDevices.Find(x => x.ble_mac == DataManager.Instance.selectedDeviceMac);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
if (selectedDevice == null)
|
|
|
|
|
|
{
|
2026-06-10 15:04:14 +08:00
|
|
|
|
if (OwnedDevices.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
selectedDevice = OwnedDevices[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
selectedDevice = SharedDevices[0];
|
|
|
|
|
|
}
|
2026-06-08 08:55:10 +08:00
|
|
|
|
DataManager.Instance.selectedDeviceMac = selectedDevice.ble_mac;
|
|
|
|
|
|
DataManager.Instance.SavaSelectedDeviceMac(selectedDevice.ble_mac);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
2026-06-10 15:04:14 +08:00
|
|
|
|
Debug.Log("当前选中设备:" + DataManager.Instance.selectedDeviceMac);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-12 09:42:44 +08:00
|
|
|
|
hasWifi=await GetDeviceOnlineStatus();
|
2026-06-08 08:55:10 +08:00
|
|
|
|
hasBluetooth = BluetoothManager.Instance.ChangeAimMac(selectedDevice.ble_mac);
|
2026-06-10 15:04:14 +08:00
|
|
|
|
|
2026-05-11 08:39:33 +08:00
|
|
|
|
selectDeviceButton.GetComponentInChildren<Text>().text = selectedDevice.device_name;
|
|
|
|
|
|
selectDeviceButton.gameObject.SetActive(true);
|
|
|
|
|
|
|
|
|
|
|
|
// 先初始化所有 UI 为未连接状态
|
|
|
|
|
|
ResetAllDeviceUI();
|
|
|
|
|
|
|
|
|
|
|
|
InitDeviceState();
|
2026-06-12 09:42:44 +08:00
|
|
|
|
deviceState?.UpdateDeviceState(hasBluetooth, hasWifi);
|
2026-05-11 08:39:33 +08:00
|
|
|
|
// 如果蓝牙已连接,立即查询状态
|
|
|
|
|
|
if (hasBluetooth)
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceCtrl?.InitDeviceControl(true);
|
|
|
|
|
|
// 开始设备状态初始化(显示Loading)
|
|
|
|
|
|
StartDeviceStateInitialization();
|
|
|
|
|
|
StartCoroutine(QueryAllDeviceStatusSequential());
|
|
|
|
|
|
scheduleSettingButton.interactable = true;
|
2026-06-12 09:42:44 +08:00
|
|
|
|
moreSettingButton.interactable = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(hasWifi)
|
|
|
|
|
|
{
|
|
|
|
|
|
await DataManager.Instance.GetDeviceConfig(selectedDevice.ble_mac);
|
|
|
|
|
|
deviceCtrl?.InitDeviceControl(true);
|
|
|
|
|
|
scheduleSettingButton.interactable = true;
|
|
|
|
|
|
moreSettingButton.interactable = true;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceCtrl?.InitDeviceControl(false);
|
|
|
|
|
|
scheduleSettingButton.interactable = false;
|
2026-06-12 09:42:44 +08:00
|
|
|
|
moreSettingButton.interactable = false;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
2026-06-08 08:55:10 +08:00
|
|
|
|
GetLastRecords();
|
|
|
|
|
|
ShowCharts();
|
|
|
|
|
|
GetMessageTypeList();
|
2026-06-10 15:04:14 +08:00
|
|
|
|
// StartCoroutine(GetLastMosquitoData());
|
2026-06-08 08:55:10 +08:00
|
|
|
|
DataManager.Instance.selectedDevice = selectedDevice;
|
2026-06-10 15:04:14 +08:00
|
|
|
|
DataManager.Instance.OwnedDevices=OwnedDevices;
|
|
|
|
|
|
DataManager.Instance.SharedDevices=SharedDevices;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重置所有设备 UI 为未连接状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void ResetAllDeviceUI()
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 重置所有 UI 为未连接状态");
|
|
|
|
|
|
|
|
|
|
|
|
// 重置工作模式为未连接
|
|
|
|
|
|
deviceCtrl?.InitWrokMode(-1);
|
|
|
|
|
|
|
|
|
|
|
|
// 重置 FOV
|
|
|
|
|
|
deviceCtrl?.InitFovText(0);
|
|
|
|
|
|
|
|
|
|
|
|
// 重置距离
|
|
|
|
|
|
deviceCtrl?.InitLensText(0f, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// 重置补光灯
|
|
|
|
|
|
deviceCtrl?.InitLightText(false);
|
|
|
|
|
|
|
|
|
|
|
|
// 重置安全状态
|
|
|
|
|
|
deviceCtrl?.InitSafeText(false);
|
|
|
|
|
|
|
|
|
|
|
|
// 重置定时任务显示
|
|
|
|
|
|
deviceSchedule?.DisplayNoSchedule();
|
|
|
|
|
|
scheduleSettingButton.onClick.RemoveAllListeners();
|
|
|
|
|
|
scheduleSettingButton.onClick.AddListener(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ShowScheduleSettingPage();
|
|
|
|
|
|
});
|
|
|
|
|
|
scheduleSettingButton.interactable = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取用户设备列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task FetchDeviceList()
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadingUI.Show();
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[HomePageCtrl] 开始获取设备列表...");
|
|
|
|
|
|
|
|
|
|
|
|
// 构建带 user_id 参数的 URL
|
|
|
|
|
|
string userId = DataManager.Instance?.userInfo?.id;
|
|
|
|
|
|
if (string.IsNullOrEmpty(userId))
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("[HomePageCtrl] 获取设备列表失败: 用户未登录");
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string url = $"/api/v1/device/user/list?user_id={userId}";
|
|
|
|
|
|
var response = await NetworkCtrl.Instance.Get<DeviceListResponse>(url);
|
|
|
|
|
|
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
|
|
|
|
|
|
ResponseCodeHandler.HandleResponse(response,
|
|
|
|
|
|
onSuccess: (data) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 更新设备列表
|
|
|
|
|
|
OwnedDevices = data.data.owned_devices ?? new List<Kill.Managers.DeviceInfo>();
|
2026-06-08 08:55:10 +08:00
|
|
|
|
foreach (var device in OwnedDevices)
|
2026-05-18 08:42:33 +08:00
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
device.owner_id = DataManager.Instance.userInfo.id;
|
|
|
|
|
|
if(string.IsNullOrEmpty(device.ble_mac))
|
|
|
|
|
|
{
|
|
|
|
|
|
device.ble_mac = device.device_sn;
|
|
|
|
|
|
}
|
2026-05-18 08:42:33 +08:00
|
|
|
|
}
|
2026-05-11 08:39:33 +08:00
|
|
|
|
SharedDevices = data.data.shared_devices ?? new List<Kill.Managers.DeviceInfo>();
|
2026-06-08 08:55:10 +08:00
|
|
|
|
foreach (var device in SharedDevices)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(string.IsNullOrEmpty(device.ble_mac))
|
|
|
|
|
|
{
|
|
|
|
|
|
device.ble_mac = device.device_sn;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-05-11 08:39:33 +08:00
|
|
|
|
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 获取设备列表成功: 自有设备={OwnedDevices.Count}, 共享设备={SharedDevices.Count}");
|
|
|
|
|
|
DataManager.Instance.userInfo.device_count = OwnedDevices.Count + SharedDevices.Count;
|
|
|
|
|
|
},
|
|
|
|
|
|
onError: null
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
Debug.LogError($"[HomePageCtrl] 获取设备列表异常: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 执行所有挂起的UI更新
|
|
|
|
|
|
if (pendingUIUpdates.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var update in pendingUIUpdates)
|
|
|
|
|
|
{
|
|
|
|
|
|
update?.Invoke();
|
|
|
|
|
|
}
|
|
|
|
|
|
pendingUIUpdates.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ConnectDevice()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!CheckSafetylearning())
|
|
|
|
|
|
{
|
|
|
|
|
|
UIManager.Instance.OpenPage(UIManager.PageName.safetylearningPage);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
UIManager.Instance.OpenPage(UIManager.PageName.connectDevicePage);
|
|
|
|
|
|
}
|
|
|
|
|
|
public bool CheckSafetylearning()
|
|
|
|
|
|
{
|
|
|
|
|
|
bool isPassed = false;
|
|
|
|
|
|
isPassed = DataManager.Instance.userInfo.exam_completed;
|
|
|
|
|
|
return isPassed;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void InitDeviceState()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 初始化设备状态UI
|
|
|
|
|
|
deviceState?.UpdateDeviceState(hasBluetooth, hasWifi);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ShowDeviceList()
|
|
|
|
|
|
{
|
|
|
|
|
|
selectDevicePage.SetActive(true);
|
|
|
|
|
|
selectDevicePage.GetComponent<HomePageDevicePage>().InitDeviceList(OwnedDevices, SharedDevices, selectedDevice);
|
|
|
|
|
|
UIManager.Instance.RegisterBackAction(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
selectDevicePage.GetComponent<HomePageDevicePage>().ClosePage();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
public GameObject fovSettingPagePrefab;
|
|
|
|
|
|
public void ShowFovSettingPage()
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
waitLockTime=0;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
if (fovSettingPagePrefab == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("[HomePageCtrl] 未设置FOV设置页面预制体");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
GameObject fovSettingPage = Instantiate(fovSettingPagePrefab, transform);
|
|
|
|
|
|
fovSettingPage.SetActive(true);
|
|
|
|
|
|
UIManager.Instance.RegisterBackAction(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
fovSettingPage.GetComponent<FovSettingPage>().OnCancelButtonClick();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
public GameObject lensSettingPagePrefab;
|
|
|
|
|
|
public void ShowLensSettingPage()
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
waitLockTime=0;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
if (lensSettingPagePrefab == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("[HomePageCtrl] 未设置lens设置页面预制体");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
GameObject lensSettingPage = Instantiate(lensSettingPagePrefab, transform);
|
|
|
|
|
|
lensSettingPage.SetActive(true);
|
|
|
|
|
|
UIManager.Instance.RegisterBackAction(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
lensSettingPage.GetComponent<LensSettingPage>().OnCancelButtonClick();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
public GameObject lightSettingPagePrefab;
|
|
|
|
|
|
public void ShowLightSettingPage()
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
waitLockTime=0;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
if (lightSettingPagePrefab == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("[HomePageCtrl] 未设置light设置页面预制体");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
GameObject lightSettingPage = Instantiate(lightSettingPagePrefab, transform);
|
|
|
|
|
|
lightSettingPage.SetActive(true);
|
|
|
|
|
|
UIManager.Instance.RegisterBackAction(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
lightSettingPage.GetComponent<LightSettingPage>().ClosePage();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
public GameObject safetySettingPagePrefab;
|
|
|
|
|
|
public void ShowSafetySettingPage()
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
waitLockTime=0;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
if (safetySettingPagePrefab == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("[HomePageCtrl] 未设置safetySettingPage预制体");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
GameObject safetySettingPage = Instantiate(safetySettingPagePrefab, transform);
|
|
|
|
|
|
safetySettingPage.SetActive(true);
|
|
|
|
|
|
UIManager.Instance.RegisterBackAction(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
safetySettingPage.GetComponent<SafetySettingPage>().ClosePage();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ShowScheduleSettingPage()
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
waitLockTime=0;
|
2026-05-11 08:39:33 +08:00
|
|
|
|
if (scheduleSettingPrefab == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("[HomePageCtrl] 未设置scheduleSettingPage预制体");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
GameObject scheduleSettingPage = Instantiate(scheduleSettingPrefab, transform);
|
|
|
|
|
|
scheduleSettingPage.SetActive(true);
|
|
|
|
|
|
|
|
|
|
|
|
// 注册页面关闭回调,更新主页定时任务显示
|
|
|
|
|
|
var schedulePage = scheduleSettingPage.GetComponent<ScheduleSettingPage>();
|
|
|
|
|
|
if (schedulePage != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
schedulePage.OnPageClosed = () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 重新读取定时任务更新主页显示
|
|
|
|
|
|
BLECommunicationManager.Instance?.ReadScheduleTasks();
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UIManager.Instance.RegisterBackAction(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
scheduleSettingPage.GetComponent<ScheduleSettingPage>().ClosePage();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-05-18 08:42:33 +08:00
|
|
|
|
public Button moreSettingButton;
|
|
|
|
|
|
public void EnterMoreSettingPage()
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
waitLockTime=0;
|
|
|
|
|
|
DataManager.Instance.selectedDevice = selectedDevice;
|
2026-05-18 08:42:33 +08:00
|
|
|
|
UIManager.Instance.OpenPage(UIManager.PageName.deviceInfoPage);
|
|
|
|
|
|
}
|
2026-06-08 08:55:10 +08:00
|
|
|
|
public void EnterRankingPage()
|
|
|
|
|
|
{
|
|
|
|
|
|
UIManager.Instance.OpenPage(UIManager.PageName.rankPage);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void EnterSelfPage()
|
|
|
|
|
|
{
|
|
|
|
|
|
UIManager.Instance.OpenPage(UIManager.PageName.selfPage);
|
|
|
|
|
|
}
|
|
|
|
|
|
public MessageTypeListPage messagePagePrefab;
|
|
|
|
|
|
public void EnterMessagePage()
|
|
|
|
|
|
{
|
2026-06-10 15:04:14 +08:00
|
|
|
|
waitLockTime=0;
|
2026-06-08 08:55:10 +08:00
|
|
|
|
Instantiate(messagePagePrefab,transform);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void EnterVideoPage()
|
|
|
|
|
|
{
|
|
|
|
|
|
waitLockTime=0;
|
|
|
|
|
|
UIManager.Instance.OpenPage(UIManager.PageName.videoPage);
|
|
|
|
|
|
}
|
2026-05-18 08:42:33 +08:00
|
|
|
|
public void SetLock(bool ison)
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
waitLockTime=0;
|
|
|
|
|
|
if (hasBluetooth)
|
2026-05-18 08:42:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
BLECommunicationManager.Instance.WriteDeviceLockControl(!ison, (success) =>
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
|
2026-05-18 08:42:33 +08:00
|
|
|
|
Loom.QueueOnMainThread(() =>
|
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
if (ison)
|
2026-05-18 08:42:33 +08:00
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
|
DeviceLockControl deviceLockControl = new DeviceLockControl();
|
|
|
|
|
|
deviceLockControl.IsLocked = false;
|
2026-05-18 08:42:33 +08:00
|
|
|
|
OnLockReceived(deviceLockControl);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceCtrl.InitDeviceControl(false);
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
}
|
2026-06-08 08:55:10 +08:00
|
|
|
|
|
2026-05-18 08:42:33 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
ToastUI.Show("100081");
|
|
|
|
|
|
}
|
2026-06-08 08:55:10 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
IEnumerator CheckDeviceError()
|
|
|
|
|
|
{
|
|
|
|
|
|
while(hasBluetooth)
|
|
|
|
|
|
{
|
|
|
|
|
|
GetDeviceStatus();
|
|
|
|
|
|
yield return new WaitForSeconds(30);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
int waitTime = 0;
|
|
|
|
|
|
IEnumerator GetLastMosquitoData()
|
|
|
|
|
|
{
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new WaitForSeconds(1);
|
|
|
|
|
|
waitTime++;
|
|
|
|
|
|
if (waitTime==50)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnGetAllMosquitoDataClick();
|
|
|
|
|
|
}
|
|
|
|
|
|
if(waitTime==100)
|
|
|
|
|
|
{
|
|
|
|
|
|
waitTime=0;
|
|
|
|
|
|
GetLastRecords();
|
|
|
|
|
|
ShowCharts();
|
|
|
|
|
|
GetMessageTypeList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public Button errorTipButton;
|
|
|
|
|
|
public GameObject deiviceErrorTip;
|
|
|
|
|
|
public void GetDeviceStatus()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> errorlist = new List<string>();
|
|
|
|
|
|
BLECommunicationManager.Instance.ReadHardwareStatus((status) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Loom.QueueOnMainThread(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
bool hasError = false;
|
|
|
|
|
|
if (status.TemperatureError != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
hasError = true;
|
|
|
|
|
|
string error = LanguageManager.Instance.GetLanguage("100228");
|
|
|
|
|
|
if (status.TemperatureError == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
error += LanguageManager.Instance.GetLanguage("100229");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
error += LanguageManager.Instance.GetLanguage("100230");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status.MotorError != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
hasError = true;
|
|
|
|
|
|
string error = LanguageManager.Instance.GetLanguage("100231");
|
|
|
|
|
|
if (status.MotorError == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
error += LanguageManager.Instance.GetLanguage("100232");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
error += LanguageManager.Instance.GetLanguage("100233");
|
|
|
|
|
|
}
|
|
|
|
|
|
errorlist.Add(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status.LaserError != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
hasError = true;
|
|
|
|
|
|
string error = LanguageManager.Instance.GetLanguage("100234");
|
|
|
|
|
|
if (status.LaserError == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
error += LanguageManager.Instance.GetLanguage("100232");
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (status.LaserError == 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
error += LanguageManager.Instance.GetLanguage("100236");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
error += LanguageManager.Instance.GetLanguage("100235");
|
|
|
|
|
|
}
|
|
|
|
|
|
errorlist.Add(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status.VisionError != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
hasError = true;
|
|
|
|
|
|
string error = LanguageManager.Instance.GetLanguage("100237");
|
|
|
|
|
|
error += LanguageManager.Instance.GetLanguage("100232");
|
|
|
|
|
|
errorlist.Add(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status.AccelerometerError != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
hasError = true;
|
|
|
|
|
|
string error = LanguageManager.Instance.GetLanguage("100239");
|
|
|
|
|
|
error += LanguageManager.Instance.GetLanguage("100232");
|
|
|
|
|
|
errorlist.Add(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status.MillimeterWaveError != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
hasError = true;
|
|
|
|
|
|
string error = LanguageManager.Instance.GetLanguage("100240");
|
|
|
|
|
|
error += LanguageManager.Instance.GetLanguage("100232");
|
|
|
|
|
|
errorlist.Add(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status.LidarError != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
hasError = true;
|
|
|
|
|
|
string error = LanguageManager.Instance.GetLanguage("100241");
|
|
|
|
|
|
error += LanguageManager.Instance.GetLanguage("100232");
|
|
|
|
|
|
errorlist.Add(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
errorTipButton.gameObject.SetActive(hasError);
|
|
|
|
|
|
string fullerror = "";
|
|
|
|
|
|
int index = 1;
|
|
|
|
|
|
foreach (string e in errorlist)
|
|
|
|
|
|
{
|
|
|
|
|
|
fullerror += $"{index}.{e}/n";
|
|
|
|
|
|
}
|
|
|
|
|
|
deiviceErrorTip.GetComponentInChildren<Text>().text=fullerror;
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Text lastRecordTimeTexts;
|
|
|
|
|
|
public async void GetLastRecords()
|
|
|
|
|
|
{
|
|
|
|
|
|
waitTime=0;
|
|
|
|
|
|
LoadingUI.Show();
|
|
|
|
|
|
string url = $"/api/v1/stats/device/records?device_sn={selectedDevice.ble_mac}&type=1&page=1&page_size=1";
|
|
|
|
|
|
var response = await NetworkCtrl.Instance.Get<RecordDataListResponse>(url);
|
|
|
|
|
|
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
|
|
|
|
|
|
ResponseCodeHandler.HandleResponse(response,
|
|
|
|
|
|
onSuccess: (data) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (data.data.records != null && data.data.records.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
RecordData record = data.data.records[0];
|
|
|
|
|
|
lastRecordTimeTexts.text=$"<b>{record.time:HH:mm}</b> <size=30>{record.time:yyyy/MM/dd}</size>";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
lastRecordTimeTexts.text = "--";
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
onError: null
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
public HomePageChartsCtrl homePageChartsCtrl;
|
|
|
|
|
|
public void ShowCharts()
|
|
|
|
|
|
{
|
|
|
|
|
|
waitTime = 0;
|
|
|
|
|
|
homePageChartsCtrl.Init();
|
|
|
|
|
|
}
|
|
|
|
|
|
List<MosquitoData> mosquitoDatas;
|
|
|
|
|
|
int aimMosquitoDataCount=0;
|
|
|
|
|
|
Coroutine waitToPostMosquitoDatas=null;
|
|
|
|
|
|
private void OnMosquitoDataReceived(MosquitoData mosquitoData)
|
|
|
|
|
|
{
|
|
|
|
|
|
DateTime now=DateTime.Now;
|
|
|
|
|
|
mosquitoDatas.Add(mosquitoData);
|
|
|
|
|
|
if(waitToPostMosquitoDatas!=null)
|
|
|
|
|
|
StopCoroutine(waitToPostMosquitoDatas);
|
|
|
|
|
|
if(mosquitoDatas.Count>=aimMosquitoDataCount&&aimMosquitoDataCount>0)
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
PostMosquitoDatas();
|
|
|
|
|
|
aimMosquitoDataCount=0;
|
|
|
|
|
|
waitToPostMosquitoDatas=null;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(mosquitoDatas.Count>0)
|
|
|
|
|
|
{
|
|
|
|
|
|
waitToPostMosquitoDatas=StartCoroutine(WaitToPostMosquitoDatas());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
IEnumerator WaitToPostMosquitoDatas()
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new WaitForSeconds(2);
|
|
|
|
|
|
PostMosquitoDatas();
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
}
|
|
|
|
|
|
async void PostMosquitoDatas()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<PostRecordData> postRecordDatas=new List<PostRecordData>();
|
|
|
|
|
|
foreach(var a in mosquitoDatas)
|
|
|
|
|
|
{
|
|
|
|
|
|
var b=new PostRecordData
|
|
|
|
|
|
{
|
|
|
|
|
|
device_sn=selectedDevice.ble_mac,
|
|
|
|
|
|
record_time=a.GetTimeString(),
|
|
|
|
|
|
angle=a.GetActualAngle(),
|
|
|
|
|
|
distance=a.GetActualDis()
|
|
|
|
|
|
};
|
|
|
|
|
|
postRecordDatas.Add(b);
|
|
|
|
|
|
}
|
|
|
|
|
|
Debug.Log("发送灭蚊数据:"+JsonUtility.ToJson(postRecordDatas));
|
|
|
|
|
|
var response = await NetworkCtrl.Instance.Post<NoDataResponse>("/api/v1/stats/device/records", postRecordDatas);
|
|
|
|
|
|
ResponseCodeHandler.HandleResponse(response, null, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取所有蚊虫数据 (0xA4)
|
|
|
|
|
|
/// 流程:1.查询数量 -> 2.请求数据 -> 3.接收通知
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void OnGetAllMosquitoDataClick()
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadingUI.Show();
|
|
|
|
|
|
// 步骤1: 查询蚊虫数据数量
|
|
|
|
|
|
BLECommunicationManager.Instance.ReadMosquitoDataCount((count) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Loom.QueueOnMainThread(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (count.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("设备中没有蚊虫数据");
|
2026-06-10 15:04:14 +08:00
|
|
|
|
LoadingUI.Hide();
|
2026-06-08 08:55:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log($"检测到 {count.Count} 条蚊虫数据,正在请求...");
|
|
|
|
|
|
|
|
|
|
|
|
// 步骤2: 请求所有蚊虫数据
|
|
|
|
|
|
BLECommunicationManager.Instance.RequestMosquitoData(count.Count, (success) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Loom.QueueOnMainThread(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (success)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"已请求 {count.Count} 条蚊虫数据,等待接收...");
|
|
|
|
|
|
mosquitoDatas=new List<MosquitoData>();
|
|
|
|
|
|
aimMosquitoDataCount=(int)count.Count;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("蚊虫数据请求失败");
|
2026-06-10 15:04:14 +08:00
|
|
|
|
LoadingUI.Hide();
|
2026-06-08 08:55:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 写入语言设置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void OnWriteLanguageClick()
|
|
|
|
|
|
{
|
|
|
|
|
|
bool isChinese = LanguageManager.Instance.languageType == LanguageManager.LanguageType.Chinese;
|
|
|
|
|
|
BLECommunicationManager.Instance.WriteLanguageSetting(isChinese, (success) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Loom.QueueOnMainThread(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
ReceiveStatus();
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 写入时间设置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void OnWriteTimeClick()
|
|
|
|
|
|
{
|
|
|
|
|
|
TimeSetting timeSetting = new TimeSetting();
|
|
|
|
|
|
timeSetting.SetTime(DateTime.Now);
|
|
|
|
|
|
BLECommunicationManager.Instance.WriteTimeSetting(timeSetting, (success) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Loom.QueueOnMainThread(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
ReceiveStatus();
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
public GameObject tipPoint;
|
|
|
|
|
|
public async Task GetMessageTypeList()
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadingUI.Show();
|
|
|
|
|
|
var response = await NetworkCtrl.Instance.Get<MessageTypeDataListResponse>("/api/v1/notifications/unread-count");
|
|
|
|
|
|
ResponseCodeHandler.HandleResponse(response,
|
|
|
|
|
|
onSuccess: (data) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if(data.data.announcement>0||data.data.device>0||data.data.warning>0)
|
|
|
|
|
|
{
|
|
|
|
|
|
tipPoint.SetActive(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
tipPoint.SetActive(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
},(code,message)=>{tipPoint.SetActive(false);}
|
|
|
|
|
|
);
|
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
|
2026-05-18 08:42:33 +08:00
|
|
|
|
}
|
2026-05-11 08:39:33 +08:00
|
|
|
|
|
2026-06-12 09:42:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 查询设备在线状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<bool> GetDeviceOnlineStatus()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (selectedDevice == null || string.IsNullOrEmpty(selectedDevice.ble_mac))
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning("[HomePageCtrl] 未选中设备");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var tcs = new TaskCompletionSource<bool>();
|
|
|
|
|
|
|
|
|
|
|
|
var response = await NetworkCtrl.Instance.Get<DeviceOnlineResponse>(
|
|
|
|
|
|
"/api/v1/device/online",
|
|
|
|
|
|
new Dictionary<string, string> { { "mac", selectedDevice.ble_mac } }
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
ResponseCodeHandler.HandleResponse(response,
|
|
|
|
|
|
onSuccess: (data) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[HomePageCtrl] 设备在线状态: {data.data}");
|
|
|
|
|
|
tcs.TrySetResult(data.data);
|
|
|
|
|
|
DataManager.Instance.hasWifi=data.data;
|
|
|
|
|
|
},
|
|
|
|
|
|
onError: (code, msg) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError($"[HomePageCtrl] 查询设备在线状态失败: {msg}");
|
|
|
|
|
|
tcs.TrySetResult(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return await tcs.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-12 09:42:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-06-08 08:55:10 +08:00
|
|
|
|
|
2026-05-11 08:39:33 +08:00
|
|
|
|
}
|