| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TeamAAS.Communication.Interfaces;
- using TeamAAS.Communication.LightSources;
- namespace TeamAAS.Communication
- {
- /// <summary>
- /// 光源管理器。懒汉单例:<c>LightSourceManager.Instance</c>。
- /// 提供两种使用模式:
- /// 1) 直接调用:传入 serialDeviceName + LightModel,内部 new 临时 controller 执行完即释放;
- /// 2) 长期持有:通过 Register/Get/Remove 管理命名 controller,适合 UI 长期绑定场景。
- /// 同时保留静态工具方法(GetChannelCount/GetProtocolName)供反射或下拉框使用。
- /// </summary>
- public class LightSourceManager
- {
- #region 单例
- private static readonly Lazy<LightSourceManager> _instance =
- new Lazy<LightSourceManager>(() => new LightSourceManager(), isThreadSafe: true);
- /// <summary>
- /// 懒汉单例入口。首次访问时初始化,线程安全。
- /// </summary>
- public static LightSourceManager Instance => _instance.Value;
- #endregion
- #region 长期持有的控制器池
- private readonly object _poolLock = new object();
- private readonly Dictionary<string, LightSourceController> _pool =
- new Dictionary<string, LightSourceController>();
- /// <summary>
- /// 注册一个长期持有的光源控制器(按 name 索引)。
- /// 已存在同名 controller 会先 Dispose 旧的再替换。
- /// </summary>
- public LightSourceController Register(string name, LightModel model, ICommunication comm)
- {
- if (string.IsNullOrWhiteSpace(name))
- throw new ArgumentException("name 不能为空", nameof(name));
- if (comm == null)
- throw new ArgumentNullException(nameof(comm));
- var controller = new LightSourceController(model, GetChannelCount(model))
- {
- Name = name
- };
- controller.AttachCommunication(comm);
- LightSourceController old = null;
- lock (_poolLock)
- {
- if (_pool.TryGetValue(name, out old))
- _pool[name] = controller;
- else
- _pool.Add(name, controller);
- }
- if (old != null && !ReferenceEquals(old, controller))
- {
- try { old.Disconnect(); } catch { }
- try { old.Dispose(); } catch { }
- }
- return controller;
- }
- /// <summary>
- /// 获取已注册的光源控制器(按 name)。不存在返回 null。
- /// </summary>
- public LightSourceController Get(string name)
- {
- if (string.IsNullOrWhiteSpace(name)) return null;
- lock (_poolLock)
- {
- return _pool.TryGetValue(name, out var c) ? c : null;
- }
- }
- /// <summary>
- /// 移除并释放已注册的光源控制器。
- /// </summary>
- public bool Remove(string name)
- {
- if (string.IsNullOrWhiteSpace(name)) return false;
- LightSourceController controller;
- lock (_poolLock)
- {
- if (!_pool.TryGetValue(name, out controller))
- return false;
- _pool.Remove(name);
- }
- if (controller != null)
- {
- try { controller.Disconnect(); } catch { }
- try { controller.Dispose(); } catch { }
- }
- return true;
- }
- /// <summary>
- /// 获取所有已注册控制器的快照。
- /// </summary>
- public IReadOnlyList<LightSourceController> GetAll()
- {
- lock (_poolLock)
- {
- return _pool.Values.ToList().AsReadOnly();
- }
- }
- /// <summary>
- /// 清空并释放所有已注册控制器。
- /// </summary>
- public void Clear()
- {
- List<LightSourceController> snapshot;
- lock (_poolLock)
- {
- snapshot = _pool.Values.ToList();
- _pool.Clear();
- }
- foreach (var c in snapshot)
- {
- try { c.Disconnect(); } catch { }
- try { c.Dispose(); } catch { }
- }
- }
- #endregion
- #region 直接操作模式(按需 new 临时 controller)
- /// <summary>
- /// 直接设置亮度(不长期持有 controller)。
- /// </summary>
- public async Task<bool> SetBrightnessAsync(string serialDeviceName, LightModel model,
- int channel, int brightness, int timeoutMs = 1000)
- {
- var comm = ResolveCommunication(serialDeviceName);
- if (comm == null) return false;
- int channelCount = GetChannelCount(model);
- var controller = new LightSourceController(model, channelCount);
- try
- {
- controller.AttachCommunication(comm);
- return await controller.SetBrightnessAsync(channel, brightness, timeoutMs);
- }
- finally { controller.Dispose(); }
- }
- /// <summary>
- /// 直接全开(不长期持有 controller)。
- /// </summary>
- public async Task<bool> TurnOnAllAsync(string serialDeviceName, LightModel model,
- int timeoutMs = 1000)
- {
- var comm = ResolveCommunication(serialDeviceName);
- if (comm == null) return false;
- int channelCount = GetChannelCount(model);
- var controller = new LightSourceController(model, channelCount);
- try
- {
- controller.AttachCommunication(comm);
- return await controller.TurnOnAllAsync(timeoutMs);
- }
- finally { controller.Dispose(); }
- }
- /// <summary>
- /// 直接全关(不长期持有 controller)。
- /// </summary>
- public async Task<bool> TurnOffAllAsync(string serialDeviceName, LightModel model,
- int timeoutMs = 1000)
- {
- var comm = ResolveCommunication(serialDeviceName);
- if (comm == null) return false;
- int channelCount = GetChannelCount(model);
- var controller = new LightSourceController(model, channelCount);
- try
- {
- controller.AttachCommunication(comm);
- return await controller.TurnOffAllAsync(timeoutMs);
- }
- finally { controller.Dispose(); }
- }
- /// <summary>
- /// 直接读取某通道亮度(不长期持有 controller)。
- /// </summary>
- public async Task<int> ReadBrightnessAsync(string serialDeviceName, LightModel model,
- int channel, int timeoutMs = 1000)
- {
- var comm = ResolveCommunication(serialDeviceName);
- if (comm == null) return 0;
- int channelCount = GetChannelCount(model);
- var controller = new LightSourceController(model, channelCount);
- try
- {
- controller.AttachCommunication(comm);
- return await controller.ReadBrightnessAsync(channel, timeoutMs);
- }
- finally { controller.Dispose(); }
- }
- private ICommunication ResolveCommunication(string serialDeviceName)
- {
- if (string.IsNullOrWhiteSpace(serialDeviceName)) return null;
- var comm = CommunicationManager.Instance.GetByName(serialDeviceName);
- return comm?.IsConnected == true ? comm : null;
- }
- #endregion
- #region 静态工具方法
- /// <summary>
- /// 根据型号获取通道数量
- /// </summary>
- public static int GetChannelCount(LightModel model)
- {
- switch (model)
- {
- case LightModel.KCS_KDC_12V60W_4T: return 4;
- case LightModel.KCS_KDC3_24V300W_8T: return 8;
- case LightModel.TSD_DPA12024V_4T_3_0: return 4;
- default: return 4;
- }
- }
- /// <summary>
- /// 根据型号获取协议名称
- /// </summary>
- public static string GetProtocolName(LightModel model)
- {
- switch (model)
- {
- case LightModel.KCS_KDC_12V60W_4T:
- case LightModel.KCS_KDC3_24V300W_8T: return "KCS";
- case LightModel.TSD_DPA12024V_4T_3_0: return "TSD";
- default: return "Unknown";
- }
- }
- #endregion
- }
- }
|