| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- 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<LightControlModel>
- {
- public override List<OutputField> DeclareOutputs() => new List<OutputField>
- {
- new OutputField("响应", typeof(string)),
- new OutputField("结果", typeof(bool)),
- new OutputField("指令", typeof(string)),
- };
- public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary<string, object> results)
- {
- var res = new Dictionary<string, object>();
- 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<CommunicationManager>()?.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<int>(Model.Channel);
- int br = GetValue<int>(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();
- }
- }
- }
|