using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using TeamAAS.Core; using TeamAAS.Communication; using TeamAAS.Communication.Devices; using TeamAAS.Communication.Interfaces; using TeamAAS.Communication.LightSources; using TeamAAS.FlowEditor.Models; using TeamAAS.FlowEditor.Plugins; using TeamAAS.Communication.Enums; using Plugins.Standard.Models; namespace Plugins.Standard { [PluginAttribute("光源控制", PluginCategory.硬件模块, typeof(LightControlModel), "Lightbulb")] [System.Serializable] public class LightControlPlugin : BasePlugin { public override List DeclareOutputs() => new List { new OutputField("响应", typeof(string)), new OutputField("结果", typeof(bool)), new OutputField("指令", typeof(string)), }; public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary results) { var res = new Dictionary(); results = res; // 统一失败出口:写 Error + 结果=false + 记 1 级错误日志(任何阈值都落盘) NodeRunStatus Fail(string message) { res["Error"] = message; res["结果"] = false; Log(1, $"光源控制失败: {message}", TeamAAS.LogLevel.Error); return NodeRunStatus.Failed; } try { var serialName = Model.SerialDeviceName; Log(3, $"开始光源控制,设备={(string.IsNullOrEmpty(serialName) ? "(未选择)" : serialName)}, 模式={Model.Mode}"); if (string.IsNullOrEmpty(serialName)) return Fail("未选择光源设备"); var comm = Service()?.GetByName(serialName); if (comm == null) return Fail($"光源设备未找到: {serialName}"); if (!comm.IsConnected) return Fail($"光源设备未连接: {serialName}"); var serial = comm as SerialCommunication; if (serial != null && serial.Terminator != Terminator.None) return Fail("光源设备结束符需设为 None"); int channelCount = LightSourceManager.GetChannelCount(Model.LightModel); var manager = LightSourceManager.Instance; bool ok; int timeoutMs = Model.TimeoutMs; try { switch (Model.Mode) { case LightControlMode.TurnOnAll: ok = manager.TurnOnAllAsync(serialName, Model.LightModel, timeoutMs).GetAwaiter().GetResult(); res["响应"] = ok ? "全开成功" : "全开失败"; break; case LightControlMode.TurnOffAll: ok = manager.TurnOffAllAsync(serialName, Model.LightModel, timeoutMs).GetAwaiter().GetResult(); res["响应"] = ok ? "全关成功" : "全关失败"; break; default: { int ch = GetValue(Model.Channel); int br = GetValue(Model.Brightness); if (ch < 1 || ch > channelCount) ch = 1; if (br < 0) br = 0; if (br > 999) br = 999; Log(4, $"设置亮度: CH={ch}(有效1-{channelCount}), 亮度={br}(0-999), 超时={timeoutMs}ms"); ok = manager.SetBrightnessAsync(serialName, Model.LightModel, ch, br, timeoutMs).GetAwaiter().GetResult(); res["响应"] = ok ? $"CH{ch} → {br} 成功" : $"CH{ch} → {br} 失败"; break; } } res["结果"] = ok; res["指令"] = manager.ToString(); string resp = res["响应"]?.ToString() ?? ""; if (ok) Log(2, $"光源控制成功: {resp}"); else Log(1, $"光源控制失败: {resp}", TeamAAS.LogLevel.Error); return ok ? NodeRunStatus.Success : NodeRunStatus.Failed; } finally { } } catch (Exception ex) { return Fail(ex.Message); } } public static bool SetBrightness(string serialDeviceName, LightModel model, int channel, int brightness, int timeoutMs = 1000) { return LightSourceManager.Instance.SetBrightnessAsync(serialDeviceName, model, channel, brightness, timeoutMs) .GetAwaiter().GetResult(); } public static bool TurnOnAll(string serialDeviceName, LightModel model, int timeoutMs = 1000) { return LightSourceManager.Instance.TurnOnAllAsync(serialDeviceName, model, timeoutMs) .GetAwaiter().GetResult(); } public static bool TurnOffAll(string serialDeviceName, LightModel model, int timeoutMs = 1000) { return LightSourceManager.Instance.TurnOffAllAsync(serialDeviceName, model, timeoutMs) .GetAwaiter().GetResult(); } } }