فهرست منبع

支持光源控制器及通道自定义名称与配置

重构控制器与通道的构造和初始化流程,贯穿 LightControllerConfig 配置信息,支持自定义通道名称。调整相关方法签名,确保配置信息在各层正确传递,提升系统灵活性和可扩展性。
孝锋 徐 8 ماه پیش
والد
کامیت
4d58500bb1

+ 3 - 2
TeamAAS-VM/Core/Lights/KCSLightChannel.cs

@@ -60,13 +60,14 @@ namespace TeamAAS_VP.Core.Lights
         /// <param name="index">通道索引(从 0 开始)。</param>
         /// <param name="controller">用于与硬件交互的 <see cref="KCSLightController"/> 实例,不能为空。</param>
         /// <exception cref="ArgumentNullException">当 <paramref name="controller"/> 为 null 时抛出。</exception>
-        public KCSLightChannel(int index, KCSLightController controller)
+        /// <param name="channelName">通道名称。</param>
+        public KCSLightChannel(int index, KCSLightController controller,string channelName)
         {
             if (controller == null)
                 throw new ArgumentNullException(nameof(controller));
 
             ChannelIndex = index;
-            ChannelName = $"Channel {index + 1}";
+            ChannelName = channelName;
             _controller = controller;
         }
 

+ 10 - 4
TeamAAS-VM/Core/Lights/KCSLightController.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using TeamAAS_VP.Models.Lights;
 
 namespace TeamAAS_VP.Core.Lights
 {
@@ -17,6 +18,8 @@ namespace TeamAAS_VP.Core.Lights
         private readonly ICommunicationProtocol _protocol;
         // 使用 ASCII 编码将字符串转换为字节流
         private readonly Encoding _encoding;
+        //光源配置
+        private readonly LightControllerConfig _config;
 
         /// <summary>
         /// 创建一个新的 <see cref="KCSLightController"/> 实例。
@@ -24,21 +27,24 @@ namespace TeamAAS_VP.Core.Lights
         /// <param name="id">控制器的唯一标识符。</param>
         /// <param name="protocol">用于与设备通信的协议实现(必须已实现连接/发送/接收等)。</param>
         /// <param name="channelCount">该控制器管理的通道数量。</param>
-        public KCSLightController(int id, ICommunicationProtocol protocol,int channelCount)
-            : base(id, channelCount)
+        /// <param name="config">控制器的配置对象。</param>
+        public KCSLightController(int id, ICommunicationProtocol protocol,int channelCount, LightControllerConfig config)
+            : base(id, channelCount,config)
         {
             _protocol = protocol;
             _encoding = Encoding.ASCII;
+            _config = config;
         }
 
         /// <summary>
         /// 为指定索引创建通道实例。
         /// </summary>
         /// <param name="index">通道索引(从 0 开始)。</param>
+        /// <param name="channelName">通道名称。</param>
         /// <returns>返回对应的 <see cref="KCSLightChannel"/> 实例。</returns>
-        protected override ILightChannel CreateChannel(int index)
+        protected override ILightChannel CreateChannel(int index,string channelName)
         {
-            return new KCSLightChannel(index, this);
+            return new KCSLightChannel(index, this, channelName);
         }
 
         /// <summary>

+ 12 - 3
TeamAAS-VM/Core/Lights/LightControllerBase.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using TeamAAS_VP.Models.Lights;
 
 namespace TeamAAS_VP.Core.Lights
 {
@@ -32,6 +33,11 @@ namespace TeamAAS_VP.Core.Lights
         /// </summary>
         public int ChannelCount { get; }
 
+        /// <summary>
+        /// 控制器配置(只读)。
+        /// </summary>
+        public LightControllerConfig Config { get; }
+
         /// <summary>
         /// 指示控制器当前是否与设备连接。派生类应在连接/断开时维护此状态。
         /// </summary>
@@ -52,11 +58,13 @@ namespace TeamAAS_VP.Core.Lights
         /// </summary>
         /// <param name="id">控制器标识符。</param>
         /// <param name="channelCount">通道数量,必须为非负整数。</param>
-        protected LightControllerBase(int id,int channelCount)
+        /// <param name="config">控制器配置。</param>
+        protected LightControllerBase(int id,int channelCount, LightControllerConfig config)
         {
             Id = id;
             ChannelCount = channelCount;
             ChannelsInternal = new List<ILightChannel>();
+            Config = config;
             InitializeChannels();
         }
 
@@ -68,7 +76,7 @@ namespace TeamAAS_VP.Core.Lights
         {
             for (int i = 0; i < ChannelCount; i++)
             {
-                ChannelsInternal.Add(CreateChannel(i));
+                ChannelsInternal.Add(CreateChannel(i, Config.ChannelConfigs[i].Name));
             }
         }
 
@@ -77,8 +85,9 @@ namespace TeamAAS_VP.Core.Lights
         /// 派生类必须实现此方法以返回实际的 <see cref="ILightChannel"/> 对象。
         /// </summary>
         /// <param name="index">通道索引(从 0 开始)。</param>
+        /// <param name="channelName">通道名称。</param>
         /// <returns>新创建的 <see cref="ILightChannel"/> 实例。</returns>
-        protected abstract ILightChannel CreateChannel(int index);
+        protected abstract ILightChannel CreateChannel(int index,string channelName);
 
         /// <summary>
         /// 异步连接到控制器对应的硬件或服务。

+ 1 - 1
TeamAAS-VM/Services/LightManagerService.cs

@@ -86,7 +86,7 @@ namespace TeamAAS_VP.Services
                 if (config == null) throw new ArgumentNullException(nameof(config));
                 // 使用配置里的串口配置创建协议实现,再创建控制器实例
                 var protocol = new SerialPortProtocol(config.SerialPortConfig);
-                return new KCSLightController(id, protocol, config.ChannelCount);
+                return new KCSLightController(id, protocol, config.ChannelCount,config);
             });
         }