“虞渠成” 412184ef1b wifi适配
2026-06-17 15:42:55 +08:00

485 lines
17 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 Kill.Bluetooth;
using Kill.Bluetooth.Protocol;
using Kill.Core;
using Kill.Managers;
using Kill.UI.Components;
using UnityEngine;
using UnityEngine.UI;
using Kill.Network;
using System.Threading.Tasks;
namespace Kill.UI.Pages
{
public class DeviceInfoPage : MonoBehaviour
{
/// <summary>
/// 0所有者页面 1分享者页面
/// </summary>
public GameObject[] pages;
bool isOwner = false;
List<Action> initActions;
Managers.DeviceInfo deviceInfo;
public Switch[] fingerprintSwitchs;
Switch fingerprintSwitch;
public Text[] deviceNames;
Text deviceName;
public RGBlightSetting rGBlightSetting;
public Text[] rgbValues;
Text rgbValue;
RGBControl rGBControl;
public LCDSetting lcdSetting;
public ShareDevicePage shareDevicePage;
public DeviceSharedUserPage deviceSharedUserPage;
public AboutDevicePage aboutDevicePage;
string nowUserid;
private void Start()
{
Debug.Log("DeviceInfoPage初始化");
UIManager.Instance.RegisterBackAction(Back);
initActions = new List<Action>();
deviceInfo = DataManager.Instance.selectedDevice;
nowUserid = DataManager.Instance.userInfo.id;
if (nowUserid == deviceInfo.owner_id)
isOwner = true;
else
isOwner = false;
if (isOwner)
{
Debug.Log("打开所有者设置");
pages[0].SetActive(true);
pages[1].SetActive(false);
fingerprintSwitch = fingerprintSwitchs[0];
rgbValue = rgbValues[0];
deviceName = deviceNames[0];
}
else
{
Debug.Log("打开分享者设置");
pages[1].SetActive(true);
pages[0].SetActive(false);
fingerprintSwitch = fingerprintSwitchs[1];
deviceName = deviceNames[1];
rgbValue = rgbValues[1];
}
deviceName.text = deviceInfo.device_name;
initActions.Add(ReadFingerprint);
initActions.Add(ReadLightSettings);
LoadingUI.Show();
InitData();
}
bool isfingerOn = true;
public async void ReadFingerprint()
{
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
// WiFi模式从服务器指纹列表读取
await DataManager.Instance.GetFingerprints(DataManager.Instance.selectedDeviceMac);
bool hasFp = false;
foreach (var user in DataManager.Instance.fingerprintUserDatas)
{
if (user.username == nowUserid)
{
hasFp = user.has_fingerprint;
break;
}
}
isfingerOn = hasFp;
initActions.Remove(ReadFingerprint);
fingerprintSwitch.Init(isfingerOn, SetFingerprintSwicth);
InitData();
return;
}
BLECommunicationManager.Instance.QueryUserList((UserListResponse) =>
{
Loom.QueueOnMainThread(() =>
{
foreach (var user in UserListResponse.Users)
{
if (user.Username == nowUserid)
{
bool isfingerOn = user.HasFingerprint;
break;
}
}
initActions.Remove(ReadFingerprint);
fingerprintSwitch.Init(isfingerOn, SetFingerprintSwicth);
InitData();
});
});
}
public void SetFingerprintSwicth(bool isOn)
{
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
// WiFi模式下不能通过WiFi控制指纹开关仅展示状态
ToastUI.Show("100293");
fingerprintSwitch.isOn = !isOn;
return;
}
LoadingUI.Show();
BLECommunicationManager.Instance.SetFingerprintEnable(nowUserid, isOn, (response) =>
{
Loom.QueueOnMainThread(() =>
{
if (response.IsSuccess)
{
if (isOn)
SetFingerprint();
else
LoadingUI.Hide();
}
else
{
fingerprintSwitch.isOn = !isOn;
LoadingUI.Hide();
}
});
});
}
public GameObject fingerTip;
public void SetFingerprint()
{
BLECommunicationManager.Instance.StartFingerprintRecord(nowUserid, (response) =>
{
Loom.QueueOnMainThread(() =>
{
if (response.IsSuccess)
{
fingerTip.SetActive(true);
LoadingUI.Hide();
}
else
{
SetFingerprint();
}
});
});
}
/// <summary>
/// 读取RGB设置
/// </summary>
private void ReadLightSettings()
{
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
var config = DataManager.Instance.deviceConfig;
this.rGBControl = new RGBControl();
rGBControl.Enable = config?.rgb_enable ?? false;
rGBControl.SetColor(new Color((config?.rgb_red ?? 0) / 255f, (config?.rgb_green ?? 0) / 255f, (config?.rgb_blue ?? 0) / 255f));
rGBControl.Effect = DeviceConfig.ParseServerEnum(config?.rgb_effect, RGBEffectMode.Blinking);
rgbValue.text = GetOnOffValue(rGBControl.Enable);
initActions.Remove(ReadLightSettings);
InitData();
return;
}
// 读取rgb
BLECommunicationManager.Instance.ReadRGBControl((setting) =>
{
Loom.QueueOnMainThread(() =>
{
rGBControl = setting;
rgbValue.text = GetOnOffValue(rGBControl.Enable);
initActions.Remove(ReadLightSettings);
InitData();
});
});
}
public void InitData()
{
if (initActions != null && initActions.Count > 0)
{
initActions[0].Invoke();
}
else
{
LoadingUI.Hide();
}
}
public string GetOnOffValue(bool ison)
{
string value = ison ? LanguageManager.Instance.GetLanguage("100099") : LanguageManager.Instance.GetLanguage("100100");
return value;
}
public void OpenRgbSetting()
{
RGBlightSetting settingPage = Instantiate(rGBlightSetting.gameObject, transform).GetComponent<RGBlightSetting>();
settingPage.gameObject.SetActive(true);
settingPage.Init(CloseRgbSetting, rGBControl);
}
public void CloseRgbSetting(RGBControl ct)
{
rGBControl = ct;
rgbValue.text = GetOnOffValue(rGBControl.Enable);
UIManager.Instance.RegisterBackAction(Back);
}
public void OpenLCDSetting()
{
LCDSetting settingPage = Instantiate(lcdSetting.gameObject, transform).GetComponent<LCDSetting>();
settingPage.gameObject.SetActive(true);
settingPage.Init(CloseLCDSetting);
}
public void CloseLCDSetting()
{
UIManager.Instance.RegisterBackAction(Back);
}
public RenamePage renamePage;
public void OpenRenamePage()
{
RenamePage settingPage = Instantiate(renamePage.gameObject, transform).GetComponent<RenamePage>();
settingPage.gameObject.SetActive(true);
settingPage.Init(deviceInfo.device_name, CloseRenamePage);
}
public void CloseRenamePage(string newName)
{
UIManager.Instance.RegisterBackAction(Back);
deviceInfo.device_name = newName;
deviceName.text = newName;
DataManager.Instance.selectedDevice.device_name = newName;
}
public VideoAndSoundSetting videoAndSoundSetting;
public void OpenVideoAndSoundSetting()
{
VideoAndSoundSetting settingPage = Instantiate(videoAndSoundSetting.gameObject, transform).GetComponent<VideoAndSoundSetting>();
settingPage.gameObject.SetActive(true);
settingPage.Init(CloseVideoAndSoundSetting);
}
public void CloseVideoAndSoundSetting()
{
UIManager.Instance.RegisterBackAction(Back);
}
public void Back()
{
UIManager.Instance.OpenMainPage(UIManager.PageName.homePage);
}
public GameObject[] restoreSettingsTips;
public void RestoreSettings0()
{
restoreSettingsTips[0].gameObject.SetActive(true);
restoreSettingsTips[1].gameObject.SetActive(false);
restoreSettingsTips[2].gameObject.SetActive(false);
restoreSettingsTips[0].GetComponent<WindowTipCtrl>().Init(RestoreSettings1, null);
}
string password = "";
public void RestoreSettings1()
{
restoreSettingsTips[1].gameObject.SetActive(true);
restoreSettingsTips[0].gameObject.SetActive(false);
restoreSettingsTips[2].gameObject.SetActive(false);
restoreSettingsTips[1].GetComponent<WindowTipCtrl>().Init(CheckPassWord, null);
}
public void CheckPassWord()
{
password = restoreSettingsTips[1].transform.Find("框").GetComponentInChildren<InputField>().text;
if (password == "")
{
return;
}
LoadingUI.Show();
CheckPasswordAsync(password);
}
public void RestoreSettings2()
{
restoreSettingsTips[2].gameObject.SetActive(true);
restoreSettingsTips[0].gameObject.SetActive(false);
restoreSettingsTips[1].gameObject.SetActive(false);
restoreSettingsTips[2].GetComponent<WindowTipCtrl>().Init(ResetDeviceAsync, null);
}
/// <summary>
/// 验证密码
/// </summary>
private async void CheckPasswordAsync(string password)
{
try
{
// 构建请求数据
var requestData = new PasswordRequest
{
password = password
};
var response = await NetworkCtrl.Instance.Post<NoDataResponse>("/api/v1/auth/password/verify", requestData);
if (response.Data.code == 200)
{
Debug.Log("密码正确");
RestoreSettings2();
}
else
{
ToastUI.Show("100040");
}
LoadingUI.Hide();
}
catch (Exception ex)
{
ToastUI.Show("100040");
}
}
private async void ResetDeviceAsync()
{
LoadingUI.Show();
restoreSettingsTips[2].gameObject.SetActive(false);
try
{
// 构建请求数据
var requestData = new ResetDeviceRequest
{
device_sn = deviceInfo.ble_mac
};
var response = await NetworkCtrl.Instance.Post<NoDataResponse>("/api/v1/device/reset", requestData);
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
Debug.Log(data.code);
if (data.code == 200)
{
ResetDeviceBluetooth();
}
else
{
ToastUI.Show("100036");
}
LoadingUI.Hide();
},
onError: (code, message) =>
{
LoadingUI.Hide();
ToastUI.Show("100036");
}
);
}
catch (Exception ex)
{
ToastUI.Show("100036");
}
}
private void ResetDeviceBluetooth()
{
LoadingUI.Show();
BLECommunicationManager.Instance.FactoryReset((success) =>
{
Loom.QueueOnMainThread(() =>
{
if (success)
{
BluetoothManager.Instance.ChangeAimMac("");
BLECommunicationManager.Instance.Disconnect();
LoadingUI.Hide();
UIManager.Instance.OpenMainPage(UIManager.PageName.homePage);
}
else
{
LoadingUI.Hide();
ToastUI.Show("100068");
}
});
});
}
public void OpenShareDevicePage()
{
ShareDevicePage settingPage = Instantiate(shareDevicePage.gameObject, transform).GetComponent<ShareDevicePage>();
settingPage.gameObject.SetActive(true);
}
public void OpenDeviceSharedUserPage()
{
DeviceSharedUserPage settingPage = Instantiate(deviceSharedUserPage.gameObject, transform).GetComponent<DeviceSharedUserPage>();
settingPage.gameObject.SetActive(true);
}
public void CancelShareBySharedUser()
{
CancelShareAsyncBySharedUser();
}
public async Task CancelShareAsyncBySharedUser()
{
string targetUserId = DataManager.Instance.userInfo.id;
// 调用后端接口共享设备
LoadingUI.Show();
var requestData = new ShareDeviceRequest
{
device_sn = DataManager.Instance.selectedDevice.ble_mac,
target_user_id = targetUserId,
owner_id = DataManager.Instance.selectedDevice.owner_id
};
try
{
var response = await NetworkCtrl.Instance.Post<NoDataResponse>("/api/v1/device/unshare", requestData);
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
Debug.Log("取消设备共享成功");
ToastUI.Show("100206");
if (BluetoothManager.Instance.IsConnected)
{
BLECommunicationManager.Instance.UnregisterUser(targetUserId, (unregisterSuccess) =>
{
Loom.QueueOnMainThread(() =>
{
LoadingUI.Hide();
if (unregisterSuccess.IsSuccess)
{
BLECommunicationManager.Instance.Disconnect();
UIManager.Instance.OpenMainPage(UIManager.PageName.homePage);
}
else
{
Debug.LogError("取消设备共享成功,但蓝牙用户注销失败");
UIManager.Instance.OpenMainPage(UIManager.PageName.homePage);
}
});
});
}
else
{
LoadingUI.Hide();
UIManager.Instance.OpenMainPage(UIManager.PageName.homePage);
}
},
onError: (code, msg) =>
{
Debug.LogError($"取消设备共享失败: {code} - {msg}");
ToastUI.ShowText(code.ToString());
}
);
}
catch (Exception ex)
{
LoadingUI.Hide();
Debug.LogError("取消设备共享失败: " + ex.Message);
}
}
public void OpenAboutDevicePage()
{
AboutDevicePage settingPage = Instantiate(aboutDevicePage.gameObject, transform).GetComponent<AboutDevicePage>();
settingPage.gameObject.SetActive(true);
}
}
}