using System; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Windows; using TeamAAS.Communication; using TeamAAS.Communication.LightSources; namespace TeamAAS.ViewModels { /// 设置页 - 光源管理(属性 + 操作) public partial class SettingViewModel { #region 光源管理 private LightSourceController _lightController; public ObservableCollection LightModelDisplayOptions { get; } = new ObservableCollection(); private LightModelDisplay _selectedLightModelItem; public LightModelDisplay SelectedLightModelItem { get { return _selectedLightModelItem; } set { if (SetProperty(ref _selectedLightModelItem, value)) { if (value != null) { _selectedLightModel = value.Model; RaisePropertyChanged(nameof(SelectedLightModel)); RaisePropertyChanged(nameof(LightChannelCount)); RaisePropertyChanged(nameof(LightProtocolName)); DisconnectLight(); } } } } private LightModel _selectedLightModel = LightModel.KCS_KDC_12V60W_4T; public LightModel SelectedLightModel { get { return _selectedLightModel; } set { if (SetProperty(ref _selectedLightModel, value)) { RaisePropertyChanged(nameof(LightChannelCount)); RaisePropertyChanged(nameof(LightProtocolName)); DisconnectLight(); } } } public int LightChannelCount => LightSourceManager.GetChannelCount(_selectedLightModel); public string LightProtocolName => LightSourceManager.GetProtocolName(_selectedLightModel); public ObservableCollection LightSerialDeviceNames { get; } = new ObservableCollection(); private string _selectedLightSerialDevice = ""; public string SelectedLightSerialDevice { get { return _selectedLightSerialDevice; } set { SetProperty(ref _selectedLightSerialDevice, value); } } public bool IsLightConnected => _lightController?.IsConnected ?? false; private string _lightConnectionStatus = "未连接"; public string LightConnectionStatus { get { return _lightConnectionStatus; } set { SetProperty(ref _lightConnectionStatus, value); } } private int _lightChannel = 1; public int LightChannel { get { return _lightChannel; } set { SetProperty(ref _lightChannel, value); } } private int _lightBrightness = 120; public int LightBrightness { get { return _lightBrightness; } set { SetProperty(ref _lightBrightness, value); } } private int _lightTimeoutMs = 1000; public int LightTimeoutMs { get { return _lightTimeoutMs; } set { SetProperty(ref _lightTimeoutMs, value); } } private string _lightMessage = ""; public string LightMessage { get { return _lightMessage; } set { SetProperty(ref _lightMessage, value); } } private ObservableCollection _lightChannels = new ObservableCollection(); public ObservableCollection LightChannels { get { return _lightChannels; } set { SetProperty(ref _lightChannels, value); } } #endregion #region 光源操作 private void RefreshLightSerialDevices() { LightSerialDeviceNames.Clear(); try { foreach (var comm in _communicationManager.Devices) { if (comm is TeamAAS.Communication.Devices.SerialCommunication) { LightSerialDeviceNames.Add(comm.Name); } } if (LightSerialDeviceNames.Count > 0 && string.IsNullOrEmpty(SelectedLightSerialDevice)) SelectedLightSerialDevice = LightSerialDeviceNames.First(); } catch { } } private async Task LightConnectAsync() { if (string.IsNullOrEmpty(SelectedLightSerialDevice)) { LightMessage = "请先选择串口设备"; return; } try { var comm = _communicationManager.GetByName(SelectedLightSerialDevice); if (comm == null) { LightMessage = $"未找到设备: {SelectedLightSerialDevice}"; return; } if (!comm.IsConnected) { await comm.ConnectAsync(); } _lightController = new LightSourceController(SelectedLightModel, LightChannelCount) { Name = SelectedLightSerialDevice }; _lightController.AttachCommunication(comm); _lightController.LogMessage += OnLightLogMessage; LightConnectionStatus = _lightController.IsConnected ? "已连接" : "连接失败"; RaisePropertyChanged(nameof(IsLightConnected)); if (_lightController.IsConnected) { LightMessage = $"已连接 {LightProtocolName} ({SelectedLightModel})"; await LightReadAllBrightnessAsync(); } else { LightMessage = "连接失败,请检查串口配置"; } } catch (Exception ex) { LightMessage = $"连接异常: {ex.Message}"; } } private void LightDisconnect() { try { if (_lightController != null) { _lightController.LogMessage -= OnLightLogMessage; _lightController.Disconnect(); _lightController.Dispose(); _lightController = null; } var comm = _communicationManager.GetByName(SelectedLightSerialDevice); if (comm?.IsConnected == true) { comm.Disconnect(); } LightConnectionStatus = "未连接"; RaisePropertyChanged(nameof(IsLightConnected)); LightMessage = "已断开"; LightChannels.Clear(); } catch (Exception ex) { LightMessage = $"断开异常: {ex.Message}"; } } private void DisconnectLight() { if (_lightController != null) { try { _lightController.LogMessage -= OnLightLogMessage; _lightController.Disconnect(); _lightController.Dispose(); } catch { } _lightController = null; } LightConnectionStatus = "未连接"; RaisePropertyChanged(nameof(IsLightConnected)); LightChannels.Clear(); } private async Task LightTurnOnAllAsync() { if (_lightController == null || !_lightController.IsConnected) { LightMessage = "请先连接光源"; return; } try { bool ok = await _lightController.TurnOnAllAsync(LightTimeoutMs); LightMessage = ok ? "全开成功" : "全开失败"; if (ok) await LightReadAllBrightnessAsync(); } catch (Exception ex) { LightMessage = $"全开异常: {ex.Message}"; } } private async Task LightTurnOffAllAsync() { if (_lightController == null || !_lightController.IsConnected) { LightMessage = "请先连接光源"; return; } try { bool ok = await _lightController.TurnOffAllAsync(LightTimeoutMs); LightMessage = ok ? "全关成功" : "全关失败"; if (ok) await LightReadAllBrightnessAsync(); } catch (Exception ex) { LightMessage = $"全关异常: {ex.Message}"; } } private async Task LightSetBrightnessAsync() { if (_lightController == null || !_lightController.IsConnected) { LightMessage = "请先连接光源"; return; } try { bool ok = await _lightController.SetBrightnessAsync(LightChannel, LightBrightness, LightTimeoutMs); LightMessage = ok ? $"CH{LightChannel} → {LightBrightness} 成功" : $"CH{LightChannel} 设置失败"; if (ok) { var item = LightChannels.FirstOrDefault(c => c.Channel == LightChannel); if (item != null) { item.Brightness = LightBrightness; item.IsOn = LightBrightness > 0; } } } catch (Exception ex) { LightMessage = $"设置异常: {ex.Message}"; } } private async Task LightReadBrightnessAsync() { if (_lightController == null || !_lightController.IsConnected) { LightMessage = "请先连接光源"; return; } try { int br = await _lightController.ReadBrightnessAsync(LightChannel, LightTimeoutMs); LightBrightness = br; var item = LightChannels.FirstOrDefault(c => c.Channel == LightChannel); if (item != null) { item.Brightness = br; item.IsOn = br > 0; } LightMessage = $"CH{LightChannel} 亮度: {br}"; } catch (Exception ex) { LightMessage = $"读取异常: {ex.Message}"; } } private async Task LightReadAllBrightnessAsync() { if (_lightController == null) return; LightChannels.Clear(); int count = LightChannelCount; for (int i = 1; i <= count; i++) { int br = await _lightController.ReadBrightnessAsync(i, LightTimeoutMs); LightChannels.Add(new ChannelBrightnessInfo { Channel = i, ChannelName = $"CH{i}", Brightness = br, IsOn = br > 0 }); } } private void OnLightLogMessage(string msg) { Application.Current?.Dispatcher?.Invoke(() => { LightMessage = msg; }); } #endregion } }