SettingViewModel.Light.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using TeamAAS.Communication;
  7. using TeamAAS.Communication.LightSources;
  8. namespace TeamAAS.ViewModels
  9. {
  10. /// <summary>设置页 - 光源管理(属性 + 操作)</summary>
  11. public partial class SettingViewModel
  12. {
  13. #region 光源管理
  14. private LightSourceController _lightController;
  15. public ObservableCollection<LightModelDisplay> LightModelDisplayOptions { get; }
  16. = new ObservableCollection<LightModelDisplay>();
  17. private LightModelDisplay _selectedLightModelItem;
  18. public LightModelDisplay SelectedLightModelItem
  19. {
  20. get { return _selectedLightModelItem; }
  21. set
  22. {
  23. if (SetProperty(ref _selectedLightModelItem, value))
  24. {
  25. if (value != null)
  26. {
  27. _selectedLightModel = value.Model;
  28. RaisePropertyChanged(nameof(SelectedLightModel));
  29. RaisePropertyChanged(nameof(LightChannelCount));
  30. RaisePropertyChanged(nameof(LightProtocolName));
  31. DisconnectLight();
  32. }
  33. }
  34. }
  35. }
  36. private LightModel _selectedLightModel = LightModel.KCS_KDC_12V60W_4T;
  37. public LightModel SelectedLightModel
  38. {
  39. get { return _selectedLightModel; }
  40. set
  41. {
  42. if (SetProperty(ref _selectedLightModel, value))
  43. {
  44. RaisePropertyChanged(nameof(LightChannelCount));
  45. RaisePropertyChanged(nameof(LightProtocolName));
  46. DisconnectLight();
  47. }
  48. }
  49. }
  50. public int LightChannelCount => LightSourceManager.GetChannelCount(_selectedLightModel);
  51. public string LightProtocolName => LightSourceManager.GetProtocolName(_selectedLightModel);
  52. public ObservableCollection<string> LightSerialDeviceNames { get; } = new ObservableCollection<string>();
  53. private string _selectedLightSerialDevice = "";
  54. public string SelectedLightSerialDevice
  55. {
  56. get { return _selectedLightSerialDevice; }
  57. set { SetProperty(ref _selectedLightSerialDevice, value); }
  58. }
  59. public bool IsLightConnected => _lightController?.IsConnected ?? false;
  60. private string _lightConnectionStatus = "未连接";
  61. public string LightConnectionStatus
  62. {
  63. get { return _lightConnectionStatus; }
  64. set { SetProperty(ref _lightConnectionStatus, value); }
  65. }
  66. private int _lightChannel = 1;
  67. public int LightChannel
  68. {
  69. get { return _lightChannel; }
  70. set { SetProperty(ref _lightChannel, value); }
  71. }
  72. private int _lightBrightness = 120;
  73. public int LightBrightness
  74. {
  75. get { return _lightBrightness; }
  76. set { SetProperty(ref _lightBrightness, value); }
  77. }
  78. private int _lightTimeoutMs = 1000;
  79. public int LightTimeoutMs
  80. {
  81. get { return _lightTimeoutMs; }
  82. set { SetProperty(ref _lightTimeoutMs, value); }
  83. }
  84. private string _lightMessage = "";
  85. public string LightMessage
  86. {
  87. get { return _lightMessage; }
  88. set { SetProperty(ref _lightMessage, value); }
  89. }
  90. private ObservableCollection<ChannelBrightnessInfo> _lightChannels = new ObservableCollection<ChannelBrightnessInfo>();
  91. public ObservableCollection<ChannelBrightnessInfo> LightChannels
  92. {
  93. get { return _lightChannels; }
  94. set { SetProperty(ref _lightChannels, value); }
  95. }
  96. #endregion
  97. #region 光源操作
  98. private void RefreshLightSerialDevices()
  99. {
  100. LightSerialDeviceNames.Clear();
  101. try
  102. {
  103. foreach (var comm in _communicationManager.Devices)
  104. {
  105. if (comm is TeamAAS.Communication.Devices.SerialCommunication)
  106. {
  107. LightSerialDeviceNames.Add(comm.Name);
  108. }
  109. }
  110. if (LightSerialDeviceNames.Count > 0 && string.IsNullOrEmpty(SelectedLightSerialDevice))
  111. SelectedLightSerialDevice = LightSerialDeviceNames.First();
  112. }
  113. catch { }
  114. }
  115. private async Task LightConnectAsync()
  116. {
  117. if (string.IsNullOrEmpty(SelectedLightSerialDevice))
  118. {
  119. LightMessage = "请先选择串口设备";
  120. return;
  121. }
  122. try
  123. {
  124. var comm = _communicationManager.GetByName(SelectedLightSerialDevice);
  125. if (comm == null)
  126. {
  127. LightMessage = $"未找到设备: {SelectedLightSerialDevice}";
  128. return;
  129. }
  130. if (!comm.IsConnected)
  131. {
  132. await comm.ConnectAsync();
  133. }
  134. _lightController = new LightSourceController(SelectedLightModel, LightChannelCount)
  135. {
  136. Name = SelectedLightSerialDevice
  137. };
  138. _lightController.AttachCommunication(comm);
  139. _lightController.LogMessage += OnLightLogMessage;
  140. LightConnectionStatus = _lightController.IsConnected ? "已连接" : "连接失败";
  141. RaisePropertyChanged(nameof(IsLightConnected));
  142. if (_lightController.IsConnected)
  143. {
  144. LightMessage = $"已连接 {LightProtocolName} ({SelectedLightModel})";
  145. await LightReadAllBrightnessAsync();
  146. }
  147. else
  148. {
  149. LightMessage = "连接失败,请检查串口配置";
  150. }
  151. }
  152. catch (Exception ex)
  153. {
  154. LightMessage = $"连接异常: {ex.Message}";
  155. }
  156. }
  157. private void LightDisconnect()
  158. {
  159. try
  160. {
  161. if (_lightController != null)
  162. {
  163. _lightController.LogMessage -= OnLightLogMessage;
  164. _lightController.Disconnect();
  165. _lightController.Dispose();
  166. _lightController = null;
  167. }
  168. var comm = _communicationManager.GetByName(SelectedLightSerialDevice);
  169. if (comm?.IsConnected == true)
  170. {
  171. comm.Disconnect();
  172. }
  173. LightConnectionStatus = "未连接";
  174. RaisePropertyChanged(nameof(IsLightConnected));
  175. LightMessage = "已断开";
  176. LightChannels.Clear();
  177. }
  178. catch (Exception ex)
  179. {
  180. LightMessage = $"断开异常: {ex.Message}";
  181. }
  182. }
  183. private void DisconnectLight()
  184. {
  185. if (_lightController != null)
  186. {
  187. try
  188. {
  189. _lightController.LogMessage -= OnLightLogMessage;
  190. _lightController.Disconnect();
  191. _lightController.Dispose();
  192. }
  193. catch { }
  194. _lightController = null;
  195. }
  196. LightConnectionStatus = "未连接";
  197. RaisePropertyChanged(nameof(IsLightConnected));
  198. LightChannels.Clear();
  199. }
  200. private async Task LightTurnOnAllAsync()
  201. {
  202. if (_lightController == null || !_lightController.IsConnected)
  203. {
  204. LightMessage = "请先连接光源";
  205. return;
  206. }
  207. try
  208. {
  209. bool ok = await _lightController.TurnOnAllAsync(LightTimeoutMs);
  210. LightMessage = ok ? "全开成功" : "全开失败";
  211. if (ok) await LightReadAllBrightnessAsync();
  212. }
  213. catch (Exception ex)
  214. {
  215. LightMessage = $"全开异常: {ex.Message}";
  216. }
  217. }
  218. private async Task LightTurnOffAllAsync()
  219. {
  220. if (_lightController == null || !_lightController.IsConnected)
  221. {
  222. LightMessage = "请先连接光源";
  223. return;
  224. }
  225. try
  226. {
  227. bool ok = await _lightController.TurnOffAllAsync(LightTimeoutMs);
  228. LightMessage = ok ? "全关成功" : "全关失败";
  229. if (ok) await LightReadAllBrightnessAsync();
  230. }
  231. catch (Exception ex)
  232. {
  233. LightMessage = $"全关异常: {ex.Message}";
  234. }
  235. }
  236. private async Task LightSetBrightnessAsync()
  237. {
  238. if (_lightController == null || !_lightController.IsConnected)
  239. {
  240. LightMessage = "请先连接光源";
  241. return;
  242. }
  243. try
  244. {
  245. bool ok = await _lightController.SetBrightnessAsync(LightChannel, LightBrightness, LightTimeoutMs);
  246. LightMessage = ok ? $"CH{LightChannel} → {LightBrightness} 成功" : $"CH{LightChannel} 设置失败";
  247. if (ok)
  248. {
  249. var item = LightChannels.FirstOrDefault(c => c.Channel == LightChannel);
  250. if (item != null)
  251. {
  252. item.Brightness = LightBrightness;
  253. item.IsOn = LightBrightness > 0;
  254. }
  255. }
  256. }
  257. catch (Exception ex)
  258. {
  259. LightMessage = $"设置异常: {ex.Message}";
  260. }
  261. }
  262. private async Task LightReadBrightnessAsync()
  263. {
  264. if (_lightController == null || !_lightController.IsConnected)
  265. {
  266. LightMessage = "请先连接光源";
  267. return;
  268. }
  269. try
  270. {
  271. int br = await _lightController.ReadBrightnessAsync(LightChannel, LightTimeoutMs);
  272. LightBrightness = br;
  273. var item = LightChannels.FirstOrDefault(c => c.Channel == LightChannel);
  274. if (item != null)
  275. {
  276. item.Brightness = br;
  277. item.IsOn = br > 0;
  278. }
  279. LightMessage = $"CH{LightChannel} 亮度: {br}";
  280. }
  281. catch (Exception ex)
  282. {
  283. LightMessage = $"读取异常: {ex.Message}";
  284. }
  285. }
  286. private async Task LightReadAllBrightnessAsync()
  287. {
  288. if (_lightController == null) return;
  289. LightChannels.Clear();
  290. int count = LightChannelCount;
  291. for (int i = 1; i <= count; i++)
  292. {
  293. int br = await _lightController.ReadBrightnessAsync(i, LightTimeoutMs);
  294. LightChannels.Add(new ChannelBrightnessInfo
  295. {
  296. Channel = i,
  297. ChannelName = $"CH{i}",
  298. Brightness = br,
  299. IsOn = br > 0
  300. });
  301. }
  302. }
  303. private void OnLightLogMessage(string msg)
  304. {
  305. Application.Current?.Dispatcher?.Invoke(() =>
  306. {
  307. LightMessage = msg;
  308. });
  309. }
  310. #endregion
  311. }
  312. }