| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- 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
- {
- /// <summary>设置页 - 光源管理(属性 + 操作)</summary>
- public partial class SettingViewModel
- {
- #region 光源管理
- private LightSourceController _lightController;
- public ObservableCollection<LightModelDisplay> LightModelDisplayOptions { get; }
- = new ObservableCollection<LightModelDisplay>();
- 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<string> LightSerialDeviceNames { get; } = new ObservableCollection<string>();
- 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<ChannelBrightnessInfo> _lightChannels = new ObservableCollection<ChannelBrightnessInfo>();
- public ObservableCollection<ChannelBrightnessInfo> 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
- }
- }
|