LightSourceController.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using TeamAAS.Communication.Interfaces;
  7. namespace TeamAAS.Communication.LightSources
  8. {
  9. /// <summary>
  10. /// 光源控制器 - 整合串口通信与光源协议,提供 KCS/TSD 光源的统一控制接口
  11. /// </summary>
  12. public class LightSourceController : IDisposable
  13. {
  14. private ICommunication _comm;
  15. private readonly LightModel _model;
  16. private readonly int _channelCount;
  17. public string Name { get; set; } = "光源";
  18. public bool IsConnected => _comm?.IsConnected ?? false;
  19. public LightModel Model => _model;
  20. public int ChannelCount => _channelCount;
  21. /// <summary>
  22. /// 当前各通道亮度值 (0-255)
  23. /// </summary>
  24. public Dictionary<int, int> ChannelBrightness { get; } = new Dictionary<int, int>();
  25. public event Action<string> LogMessage;
  26. public LightSourceController(LightModel model, int channelCount)
  27. {
  28. _model = model;
  29. _channelCount = channelCount;
  30. for (int i = 0; i < channelCount; i++)
  31. ChannelBrightness[i] = 0;
  32. }
  33. public void AttachCommunication(ICommunication comm)
  34. {
  35. _comm = comm;
  36. }
  37. public async Task<bool> ConnectAsync()
  38. {
  39. if (_comm == null) { Log("未绑定通讯设备"); return false; }
  40. try
  41. {
  42. if (!_comm.IsConnected)
  43. await _comm.ConnectAsync();
  44. Log($"已连接 ({_comm.Name})");
  45. return true;
  46. }
  47. catch (Exception ex)
  48. {
  49. Log($"连接失败: {ex.Message}");
  50. return false;
  51. }
  52. }
  53. public void Disconnect()
  54. {
  55. try { _comm?.Disconnect(); } catch { }
  56. Log("已断开");
  57. }
  58. public async Task<bool> SetBrightnessAsync(int channel, int brightness, int timeoutMs = 1000)
  59. {
  60. if (channel < 1 || channel > _channelCount) { Log($"CH口超出范围: {channel}"); return false; }
  61. if (brightness < 0) brightness = 0;
  62. if (brightness > 255) brightness = 255;
  63. int idx = channel - 1;
  64. bool ok;
  65. switch (_model)
  66. {
  67. case LightModel.TSD_DPA12024V_4T_3_0:
  68. {
  69. string tsCmd = TSDLightProtocol.BuildSetBrightnessCommand(idx, brightness);
  70. var resp1 = await SendAndWaitAsync(tsCmd, timeoutMs);
  71. ok = TSDLightProtocol.VerifyResponse(resp1);
  72. }
  73. break;
  74. default:
  75. {
  76. string kcsCmd = KCSLightProtocol.BuildSetBrightnessCommand(idx, brightness);
  77. var resp2 = await SendAndWaitAsync(kcsCmd, timeoutMs);
  78. ok = KCSLightProtocol.VerifySetResponse(resp2, idx);
  79. }
  80. break;
  81. }
  82. if (ok)
  83. {
  84. ChannelBrightness[idx] = brightness;
  85. Log($"CH{channel} → {brightness} ✓");
  86. }
  87. else
  88. {
  89. Log($"CH{channel} → {brightness} ✗ (超时或无响应)");
  90. }
  91. return ok;
  92. }
  93. public async Task<bool> TurnOnAllAsync(int timeoutMs = 1000)
  94. {
  95. bool allOk = true;
  96. switch (_model)
  97. {
  98. case LightModel.TSD_DPA12024V_4T_3_0:
  99. foreach (string tsCmd in TSDLightProtocol.BuildTurnOnAllCommands(_channelCount))
  100. {
  101. var resp = await SendAndWaitAsync(tsCmd, timeoutMs);
  102. if (!TSDLightProtocol.VerifyResponse(resp)) allOk = false;
  103. }
  104. break;
  105. default:
  106. int br = ChannelBrightness.Values.FirstOrDefault(v => v > 0);
  107. if (br == 0) br = 120;
  108. string kcsCmd = KCSLightProtocol.BuildTurnOnAllCommand(_channelCount, br);
  109. var resp2 = await SendAndWaitAsync(kcsCmd, timeoutMs);
  110. allOk = KCSLightProtocol.VerifyAllResponse(resp2);
  111. break;
  112. }
  113. Log(allOk ? "全开 ✓" : "全开 ✗");
  114. return allOk;
  115. }
  116. public async Task<bool> TurnOffAllAsync(int timeoutMs = 1000)
  117. {
  118. bool allOk = true;
  119. switch (_model)
  120. {
  121. case LightModel.TSD_DPA12024V_4T_3_0:
  122. foreach (string tsCmd in TSDLightProtocol.BuildTurnOffAllCommands(_channelCount))
  123. {
  124. var resp = await SendAndWaitAsync(tsCmd, timeoutMs);
  125. if (!TSDLightProtocol.VerifyResponse(resp)) allOk = false;
  126. }
  127. break;
  128. default:
  129. string kcsCmd = KCSLightProtocol.BuildTurnOffAllCommand(_channelCount);
  130. var resp2 = await SendAndWaitAsync(kcsCmd, timeoutMs);
  131. allOk = KCSLightProtocol.VerifyAllResponse(resp2);
  132. break;
  133. }
  134. if (allOk)
  135. {
  136. for (int i = 0; i < _channelCount; i++)
  137. ChannelBrightness[i] = 0;
  138. }
  139. Log(allOk ? "全关 ✓" : "全关 ✗");
  140. return allOk;
  141. }
  142. public async Task<int> ReadBrightnessAsync(int channel, int timeoutMs = 1000)
  143. {
  144. if (channel < 1 || channel > _channelCount) return 0;
  145. int idx = channel - 1;
  146. int brightness;
  147. switch (_model)
  148. {
  149. case LightModel.TSD_DPA12024V_4T_3_0:
  150. {
  151. string tsCmd = TSDLightProtocol.BuildReadCommand(idx);
  152. var resp1 = await SendAndWaitAsync(tsCmd, timeoutMs);
  153. brightness = TSDLightProtocol.ParseReadResponse(resp1);
  154. }
  155. break;
  156. default:
  157. {
  158. bool useSR = _model == LightModel.KCS_KDC3_24V300W_8T;
  159. string kcsCmd = KCSLightProtocol.BuildReadCommand(idx, useSR);
  160. var resp2 = await SendAndWaitAsync(kcsCmd, timeoutMs);
  161. brightness = KCSLightProtocol.ParseReadResponse(resp2);
  162. }
  163. break;
  164. }
  165. ChannelBrightness[idx] = brightness;
  166. Log($"读CH{channel}: {brightness}");
  167. return brightness;
  168. }
  169. private async Task<string> SendAndWaitAsync(string command, int timeoutMs)
  170. {
  171. if (_comm == null || !_comm.IsConnected) return null;
  172. string response = null;
  173. var mre = new ManualResetEventSlim(false);
  174. Action<object, string> handler = (s, msg) =>
  175. {
  176. response = msg;
  177. mre.Set();
  178. };
  179. _comm.DataReceivedEvent += handler;
  180. try
  181. {
  182. _comm.WriteValue("", command);
  183. if (await Task.Run(() => mre.Wait(timeoutMs)))
  184. return response;
  185. }
  186. finally
  187. {
  188. _comm.DataReceivedEvent -= handler;
  189. }
  190. return null;
  191. }
  192. private void Log(string msg)
  193. {
  194. LogMessage?.Invoke($"[{_model}] {msg}");
  195. }
  196. public void Dispose()
  197. {
  198. try { _comm?.Disconnect(); } catch { }
  199. }
  200. }
  201. }