using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Kill.Bluetooth;
using Kill.Bluetooth.Protocol;
using Kill.Core;
using Kill.Managers;
using Kill.UI.Components;
using UnityEngine;
using UnityEngine.UI;
namespace Kill.UI.Pages
{
public class SafetySettingPage : MonoBehaviour
{
public Switch visualDetectionSwitch;
public Text visualDetectionText;
///
/// 灵敏度选择器 视觉检测和毫米波雷达通用
///
public MutiSelectPage sensitivitySelect;
public Text MillimeterWaveRadarText;
public Button backBtn;
///
/// 0视觉检测灵敏度选择 1毫米波雷达灵敏度选择 2试射测试
///
public Button[] settingButtons;
// 灵敏度语言key
private List sensitivityKeyList = new List { "100114", "100115", "100116" };
private List sensitivityStrList = new List();
// 当前设置值
private bool currentVisualEnabled = false;
private bool selectedVisualEnabled = false;
private SensitivityLevel currentVisualSensitivity = SensitivityLevel.Medium;
private SensitivityLevel selectedVisualSensitivity = SensitivityLevel.Medium;
private SensitivityLevel currentWaveSensitivity = SensitivityLevel.Medium;
private SensitivityLevel selectedWaveSensitivity = SensitivityLevel.Medium;
// 进入设置前的原始值
private bool originalVisualEnabled = false;
private SensitivityLevel originalVisualSensitivity = SensitivityLevel.Medium;
private SensitivityLevel originalWaveSensitivity = SensitivityLevel.Medium;
// 静态标志:是否有设置页面正在处理回调
private static bool _isSettingPageActive = false;
void Start()
{
// 初始化语言文本
InitLanguageText();
// 绑定按钮事件
BindButtonEvents();
InitSafetySettingPage();
}
///
/// 初始化语言文本
///
private void InitLanguageText()
{
sensitivityStrList.Clear();
foreach (var key in sensitivityKeyList)
{
sensitivityStrList.Add(LanguageManager.Instance.GetLanguage(key));
}
}
///
/// 绑定按钮事件
///
private void BindButtonEvents()
{
// 绑定返回按钮
if (backBtn != null)
{
backBtn.onClick.RemoveAllListeners();
backBtn.onClick.AddListener(OnBackButtonClick);
}
// 绑定设置按钮
if (settingButtons != null && settingButtons.Length >= 3)
{
// 视觉检测灵敏度选择
if (settingButtons[0] != null)
{
settingButtons[0].onClick.RemoveAllListeners();
settingButtons[0].onClick.AddListener(OnClickVisualSensitivitySelect);
}
// 毫米波雷达灵敏度选择
if (settingButtons[1] != null)
{
settingButtons[1].onClick.RemoveAllListeners();
settingButtons[1].onClick.AddListener(OnClickWaveSensitivitySelect);
}
}
}
///
/// 初始化安全设置页面
///
public void InitSafetySettingPage()
{
Debug.Log("[SafetySettingPage] 初始化安全设置页面");
// 注册蓝牙回调(顶替主页)
RegisterBluetoothCallbacks();
ReadSafetySettings();
}
///
/// 注册蓝牙回调(顶替主页)
///
private void RegisterBluetoothCallbacks()
{
if (BLECommunicationManager.Instance != null)
{
// 设置标志位,表示设置页面正在处理
_isSettingPageActive = true;
// 订阅事件
BLECommunicationManager.Instance.OnVisualDetectionSettingReceived += OnVisualDetectionSettingReceived;
BLECommunicationManager.Instance.OnMillimeterWaveSettingReceived += OnMillimeterWaveSettingReceived;
Debug.Log("[SafetySettingPage] 蓝牙回调已注册");
}
}
///
/// 注销蓝牙回调(恢复主页)
///
private void UnregisterBluetoothCallbacks()
{
if (BLECommunicationManager.Instance != null)
{
// 取消订阅事件
BLECommunicationManager.Instance.OnVisualDetectionSettingReceived -= OnVisualDetectionSettingReceived;
BLECommunicationManager.Instance.OnMillimeterWaveSettingReceived -= OnMillimeterWaveSettingReceived;
// 清除标志位
_isSettingPageActive = false;
Debug.Log("[SafetySettingPage] 蓝牙回调已注销");
}
}
///
/// 视觉检测设置接收回调
///
private void OnVisualDetectionSettingReceived(VisualDetectionSetting setting)
{
Loom.QueueOnMainThread(() =>
{
// 只有当前设置页面激活时才处理
if (!_isSettingPageActive) return;
currentVisualEnabled = setting.Enable;
selectedVisualEnabled = currentVisualEnabled;
originalVisualEnabled = currentVisualEnabled;
currentVisualSensitivity = setting.Sensitivity;
selectedVisualSensitivity = currentVisualSensitivity;
originalVisualSensitivity = currentVisualSensitivity;
Debug.Log($"[SafetySettingPage] 读取视觉检测设置: 开关={currentVisualEnabled}, 灵敏度={currentVisualSensitivity}");
// 然后读取毫米波雷达设置
BLECommunicationManager.Instance?.ReadMillimeterWaveSetting();
});
}
///
/// 毫米波雷达设置接收回调
///
private void OnMillimeterWaveSettingReceived(MillimeterWaveSetting setting)
{
Loom.QueueOnMainThread(() =>
{
// 只有当前设置页面激活时才处理
if (!_isSettingPageActive) return;
LoadingUI.Hide();
// 毫米波雷达保持开启,只读取灵敏度
currentWaveSensitivity = setting.Sensitivity;
selectedWaveSensitivity = currentWaveSensitivity;
originalWaveSensitivity = currentWaveSensitivity;
Debug.Log($"[SafetySettingPage] 读取毫米波雷达设置: 灵敏度={currentWaveSensitivity}");
UpdateUI();
});
}
///
/// 检查是否有设置页面正在处理回调
///
public static bool IsSettingPageActive()
{
return _isSettingPageActive;
}
///
/// 读取安全设置
///
private void ReadSafetySettings()
{
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
LoadSettingsFromConfig();
return;
}
LoadingUI.Show();
// 先读取视觉检测设置
BLECommunicationManager.Instance?.ReadVisualDetectionSetting();
}
private void LoadSettingsFromConfig()
{
var config = DataManager.Instance.deviceConfig;
currentVisualEnabled = config?.visual_detect_enable ?? false;
selectedVisualEnabled = currentVisualEnabled;
currentVisualSensitivity = DeviceConfig.ParseServerEnum(config?.visual_sensitivity, SensitivityLevel.Medium);
selectedVisualSensitivity = currentVisualSensitivity;
currentWaveSensitivity = DeviceConfig.ParseServerEnum(config?.radar_sensitivity, SensitivityLevel.Medium);
selectedWaveSensitivity = currentWaveSensitivity;
UpdateUI();
}
///
/// 更新UI显示
///
private void UpdateUI()
{
// 初始化视觉检测开关
if (visualDetectionSwitch != null)
{
visualDetectionSwitch.Init(selectedVisualEnabled, OnVisualSwitchChanged);
}
// 更新视觉检测灵敏度文本
UpdateVisualDetectionSensitivityText();
// 更新毫米波雷达灵敏度文本
UpdateMillimeterWaveSensitivityText();
}
///
/// 更新视觉检测灵敏度文本
///
private void UpdateVisualDetectionSensitivityText()
{
if (visualDetectionText == null) return;
visualDetectionText.text = GetSensitivityText(selectedVisualSensitivity);
}
///
/// 更新毫米波雷达灵敏度文本
///
private void UpdateMillimeterWaveSensitivityText()
{
if (MillimeterWaveRadarText == null) return;
MillimeterWaveRadarText.text = GetSensitivityText(selectedWaveSensitivity);
}
///
/// 获取灵敏度显示文本
///
private string GetSensitivityText(SensitivityLevel level)
{
int index = (int)level;
if (index >= 0 && index < sensitivityStrList.Count)
{
return sensitivityStrList[index];
}
return level.ToString();
}
///
/// 视觉检测开关状态改变回调
///
private async void OnVisualSwitchChanged(bool isOn)
{
Debug.Log($"[SafetySettingPage] 视觉检测开关: {(isOn ? "开" : "关")}");
selectedVisualEnabled = isOn;
// 发送设置到设备
await SendVisualDetectionSettingToDeviceAsync();
}
///
/// 打开视觉检测灵敏度选择页面
///
public void OnClickVisualSensitivitySelect()
{
if (sensitivitySelect == null) return;
MutiSelectPage selectPage = Instantiate(sensitivitySelect, transform);
int currentIndex = (int)selectedVisualSensitivity;
// 设置选项文本
for (int i = 0; i < selectPage.options.Count && i < sensitivityStrList.Count; i++)
{
Text optionText = selectPage.options[i].GetComponentInChildren();
if (optionText != null)
{
optionText.text = sensitivityStrList[i];
}
}
selectPage.Init(currentIndex, async (selectedIndex) =>
{
Debug.Log($"[SafetySettingPage] 选择视觉检测灵敏度: {selectedIndex}");
selectedVisualSensitivity = (SensitivityLevel)selectedIndex;
UpdateVisualDetectionSensitivityText();
await SendVisualDetectionSettingToDeviceAsync();
});
selectPage.gameObject.SetActive(true);
}
///
/// 打开毫米波雷达灵敏度选择页面
///
public void OnClickWaveSensitivitySelect()
{
if (sensitivitySelect == null) return;
MutiSelectPage selectPage = Instantiate(sensitivitySelect, transform);
int currentIndex = (int)selectedWaveSensitivity;
// 设置选项文本
for (int i = 0; i < selectPage.options.Count && i < sensitivityStrList.Count; i++)
{
Text optionText = selectPage.options[i].GetComponentInChildren();
if (optionText != null)
{
optionText.text = sensitivityStrList[i];
}
}
selectPage.Init(currentIndex, async (selectedIndex) =>
{
Debug.Log($"[SafetySettingPage] 选择毫米波雷达灵敏度: {selectedIndex}");
selectedWaveSensitivity = (SensitivityLevel)selectedIndex;
UpdateMillimeterWaveSensitivityText();
await SendMillimeterWaveSettingToDeviceAsync();
});
selectPage.gameObject.SetActive(true);
}
///
/// 试射测试按钮点击
///
public void OnClickFireTest()
{
Debug.Log("[SafetySettingPage] 点击试射测试按钮");
LoadingUI.Show();
BLECommunicationManager.Instance?.TriggerLaserTest((success) =>
{
LoadingUI.Hide();
if (success)
{
Debug.Log("[SafetySettingPage] 试射测试已触发");
}
else
{
Debug.LogError("[SafetySettingPage] 试射测试触发失败");
}
});
}
///
/// 发送视觉检测设置到设备
///
private async Task SendVisualDetectionSettingToDeviceAsync()
{
VisualDetectionSetting setting = new VisualDetectionSetting
{
Enable = selectedVisualEnabled,
Sensitivity = selectedVisualSensitivity
};
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
bool success = await HomePageCtrl.Instance.SendBleCommandByWifiAsync(
BLEConstants.CMD_VISUAL_DETECTION_SETTING, setting.ToBytes());
if (success)
{
SyncVisualConfig();
}
else
{
Debug.LogError("[SafetySettingPage] 视觉检测设置失败");
selectedVisualEnabled = currentVisualEnabled;
selectedVisualSensitivity = currentVisualSensitivity;
UpdateVisualDetectionSensitivityText();
}
}
else
{
BLECommunicationManager.Instance?.WriteVisualDetectionSetting(setting, (success) =>
{
Loom.QueueOnMainThread(() =>
{
if (success)
{
Debug.Log($"[SafetySettingPage] 视觉检测设置成功: 开关={selectedVisualEnabled}, 灵敏度={selectedVisualSensitivity}");
SyncVisualConfig();
}
else
{
Debug.LogError("[SafetySettingPage] 视觉检测设置失败");
selectedVisualEnabled = currentVisualEnabled;
selectedVisualSensitivity = currentVisualSensitivity;
UpdateVisualDetectionSensitivityText();
}
});
});
}
}
private void SyncVisualConfig()
{
var config = DataManager.Instance.deviceConfig;
config.visual_detect_enable = selectedVisualEnabled;
config.visual_sensitivity = DeviceConfig.ToServerString(selectedVisualSensitivity);
}
///
/// 发送毫米波雷达设置到设备(保持开启,只设置灵敏度)
///
private async Task SendMillimeterWaveSettingToDeviceAsync()
{
// 毫米波雷达保持开启,只修改灵敏度
MillimeterWaveSetting setting = new MillimeterWaveSetting
{
Enable = true, // 保持开启
Sensitivity = selectedWaveSensitivity,
SafeDistance = 10 // 默认安全距离1米(单位0.1米)
};
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
bool success = await HomePageCtrl.Instance.SendBleCommandByWifiAsync(
BLEConstants.CMD_MILLIMETER_WAVE_SETTING, setting.ToBytes());
if (success)
{
SyncRadarConfig();
}
else
{
Debug.LogError("[SafetySettingPage] 毫米波雷达设置失败");
selectedWaveSensitivity = currentWaveSensitivity;
UpdateMillimeterWaveSensitivityText();
}
}
else
{
BLECommunicationManager.Instance?.WriteMillimeterWaveSetting(setting, (success) =>
{
Loom.QueueOnMainThread(() =>
{
if (success)
{
Debug.Log($"[SafetySettingPage] 毫米波雷达设置成功: 灵敏度={selectedWaveSensitivity}");
SyncRadarConfig();
}
else
{
Debug.LogError("[SafetySettingPage] 毫米波雷达设置失败");
selectedWaveSensitivity = currentWaveSensitivity;
UpdateMillimeterWaveSensitivityText();
}
});
});
}
}
private void SyncRadarConfig()
{
var config = DataManager.Instance.deviceConfig;
config.radar_sensitivity = DeviceConfig.ToServerString(selectedWaveSensitivity);
}
///
/// 返回按钮点击事件 - 直接关闭页面
///
private void OnBackButtonClick()
{
Debug.Log("[SafetySettingPage] 返回按钮点击,关闭页面");
ClosePage();
}
///
/// 关闭页面
///
public void ClosePage()
{
LoadingUI.Hide();
// 注销蓝牙回调(恢复主页)
UnregisterBluetoothCallbacks();
Destroy(gameObject);
NotifyHomePageRefresh();
// 清除UIManager返回事件
UIManager.Instance?.ClearBackAction();
}
///
/// 通知主页刷新
///
private void NotifyHomePageRefresh()
{
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
HomePageCtrl.Instance?.RefreshSafetyUI();
return;
}
// 通知主页刷新安全设置显示
BLECommunicationManager.Instance?.ReadVisualDetectionSetting();
BLECommunicationManager.Instance?.ReadMillimeterWaveSetting();
}
}
}