| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using TeamAAS.Communication.Interfaces;
- namespace TeamAAS.Communication.LightSources
- {
- /// <summary>
- /// 光源控制器 - 整合串口通信与光源协议,提供 KCS/TSD 光源的统一控制接口
- /// </summary>
- public class LightSourceController : IDisposable
- {
- private ICommunication _comm;
- private readonly LightModel _model;
- private readonly int _channelCount;
- public string Name { get; set; } = "光源";
- public bool IsConnected => _comm?.IsConnected ?? false;
- public LightModel Model => _model;
- public int ChannelCount => _channelCount;
- /// <summary>
- /// 当前各通道亮度值 (0-255)
- /// </summary>
- public Dictionary<int, int> ChannelBrightness { get; } = new Dictionary<int, int>();
- public event Action<string> LogMessage;
- public LightSourceController(LightModel model, int channelCount)
- {
- _model = model;
- _channelCount = channelCount;
- for (int i = 0; i < channelCount; i++)
- ChannelBrightness[i] = 0;
- }
- public void AttachCommunication(ICommunication comm)
- {
- _comm = comm;
- }
- public async Task<bool> ConnectAsync()
- {
- if (_comm == null) { Log("未绑定通讯设备"); return false; }
- try
- {
- if (!_comm.IsConnected)
- await _comm.ConnectAsync();
- Log($"已连接 ({_comm.Name})");
- return true;
- }
- catch (Exception ex)
- {
- Log($"连接失败: {ex.Message}");
- return false;
- }
- }
- public void Disconnect()
- {
- try { _comm?.Disconnect(); } catch { }
- Log("已断开");
- }
- public async Task<bool> SetBrightnessAsync(int channel, int brightness, int timeoutMs = 1000)
- {
- if (channel < 1 || channel > _channelCount) { Log($"CH口超出范围: {channel}"); return false; }
- if (brightness < 0) brightness = 0;
- if (brightness > 255) brightness = 255;
- int idx = channel - 1;
- bool ok;
- switch (_model)
- {
- case LightModel.TSD_DPA12024V_4T_3_0:
- {
- string tsCmd = TSDLightProtocol.BuildSetBrightnessCommand(idx, brightness);
- var resp1 = await SendAndWaitAsync(tsCmd, timeoutMs);
- ok = TSDLightProtocol.VerifyResponse(resp1);
- }
- break;
- default:
- {
- string kcsCmd = KCSLightProtocol.BuildSetBrightnessCommand(idx, brightness);
- var resp2 = await SendAndWaitAsync(kcsCmd, timeoutMs);
- ok = KCSLightProtocol.VerifySetResponse(resp2, idx);
- }
- break;
- }
- if (ok)
- {
- ChannelBrightness[idx] = brightness;
- Log($"CH{channel} → {brightness} ✓");
- }
- else
- {
- Log($"CH{channel} → {brightness} ✗ (超时或无响应)");
- }
- return ok;
- }
- public async Task<bool> TurnOnAllAsync(int timeoutMs = 1000)
- {
- bool allOk = true;
- switch (_model)
- {
- case LightModel.TSD_DPA12024V_4T_3_0:
- foreach (string tsCmd in TSDLightProtocol.BuildTurnOnAllCommands(_channelCount))
- {
- var resp = await SendAndWaitAsync(tsCmd, timeoutMs);
- if (!TSDLightProtocol.VerifyResponse(resp)) allOk = false;
- }
- break;
- default:
- int br = ChannelBrightness.Values.FirstOrDefault(v => v > 0);
- if (br == 0) br = 120;
- string kcsCmd = KCSLightProtocol.BuildTurnOnAllCommand(_channelCount, br);
- var resp2 = await SendAndWaitAsync(kcsCmd, timeoutMs);
- allOk = KCSLightProtocol.VerifyAllResponse(resp2);
- break;
- }
- Log(allOk ? "全开 ✓" : "全开 ✗");
- return allOk;
- }
- public async Task<bool> TurnOffAllAsync(int timeoutMs = 1000)
- {
- bool allOk = true;
- switch (_model)
- {
- case LightModel.TSD_DPA12024V_4T_3_0:
- foreach (string tsCmd in TSDLightProtocol.BuildTurnOffAllCommands(_channelCount))
- {
- var resp = await SendAndWaitAsync(tsCmd, timeoutMs);
- if (!TSDLightProtocol.VerifyResponse(resp)) allOk = false;
- }
- break;
- default:
- string kcsCmd = KCSLightProtocol.BuildTurnOffAllCommand(_channelCount);
- var resp2 = await SendAndWaitAsync(kcsCmd, timeoutMs);
- allOk = KCSLightProtocol.VerifyAllResponse(resp2);
- break;
- }
- if (allOk)
- {
- for (int i = 0; i < _channelCount; i++)
- ChannelBrightness[i] = 0;
- }
- Log(allOk ? "全关 ✓" : "全关 ✗");
- return allOk;
- }
- public async Task<int> ReadBrightnessAsync(int channel, int timeoutMs = 1000)
- {
- if (channel < 1 || channel > _channelCount) return 0;
- int idx = channel - 1;
- int brightness;
- switch (_model)
- {
- case LightModel.TSD_DPA12024V_4T_3_0:
- {
- string tsCmd = TSDLightProtocol.BuildReadCommand(idx);
- var resp1 = await SendAndWaitAsync(tsCmd, timeoutMs);
- brightness = TSDLightProtocol.ParseReadResponse(resp1);
- }
- break;
- default:
- {
- bool useSR = _model == LightModel.KCS_KDC3_24V300W_8T;
- string kcsCmd = KCSLightProtocol.BuildReadCommand(idx, useSR);
- var resp2 = await SendAndWaitAsync(kcsCmd, timeoutMs);
- brightness = KCSLightProtocol.ParseReadResponse(resp2);
- }
- break;
- }
- ChannelBrightness[idx] = brightness;
- Log($"读CH{channel}: {brightness}");
- return brightness;
- }
- private async Task<string> SendAndWaitAsync(string command, int timeoutMs)
- {
- if (_comm == null || !_comm.IsConnected) return null;
- string response = null;
- var mre = new ManualResetEventSlim(false);
- Action<object, string> handler = (s, msg) =>
- {
- response = msg;
- mre.Set();
- };
- _comm.DataReceivedEvent += handler;
- try
- {
- _comm.WriteValue("", command);
- if (await Task.Run(() => mre.Wait(timeoutMs)))
- return response;
- }
- finally
- {
- _comm.DataReceivedEvent -= handler;
- }
- return null;
- }
- private void Log(string msg)
- {
- LogMessage?.Invoke($"[{_model}] {msg}");
- }
- public void Dispose()
- {
- try { _comm?.Disconnect(); } catch { }
- }
- }
- }
|