“虞渠成” ed4a57b29d 0611
2026-06-11 11:17:27 +08:00

464 lines
16 KiB
C#

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 void ReadFingerprint()
{
#if UNITY_EDITOR
Debug.Log("初始化指纹");
initActions.Remove(ReadFingerprint);
fingerprintSwitch.Init(true, (ison) => { Debug.Log($"指纹开关:{ison}"); });
InitData();
return;
#endif
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 UNITY_EDITOR
return;
#endif
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 UNITY_EDITOR
this.rGBControl = new RGBControl();
rGBControl.SetColor(new Color(0.1f, 0.2f, 0.3f));
rGBControl.Enable = true;
rGBControl.Effect = 0;
rgbValue.text = GetOnOffValue(rGBControl.Enable);
initActions.Remove(ReadLightSettings);
InitData();
return;
#endif
// 读取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);
}
}
}