LightControlPlugin.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using TeamAAS.Core;
  6. using TeamAAS.Communication;
  7. using TeamAAS.Communication.Devices;
  8. using TeamAAS.Communication.Interfaces;
  9. using TeamAAS.Communication.LightSources;
  10. using TeamAAS.FlowEditor.Models;
  11. using TeamAAS.FlowEditor.Plugins;
  12. using TeamAAS.Communication.Enums;
  13. using Plugins.Standard.Models;
  14. namespace Plugins.Standard
  15. {
  16. [PluginAttribute("光源控制", PluginCategory.硬件模块, typeof(LightControlModel),
  17. "Lightbulb")]
  18. [System.Serializable]
  19. public class LightControlPlugin : BasePlugin<LightControlModel>
  20. {
  21. public override List<OutputField> DeclareOutputs() => new List<OutputField>
  22. {
  23. new OutputField("响应", typeof(string)),
  24. new OutputField("结果", typeof(bool)),
  25. new OutputField("指令", typeof(string)),
  26. };
  27. public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary<string, object> results)
  28. {
  29. var res = new Dictionary<string, object>();
  30. results = res;
  31. // 统一失败出口:写 Error + 结果=false + 记 1 级错误日志(任何阈值都落盘)
  32. NodeRunStatus Fail(string message)
  33. {
  34. res["Error"] = message;
  35. res["结果"] = false;
  36. Log(1, $"光源控制失败: {message}", TeamAAS.LogLevel.Error);
  37. return NodeRunStatus.Failed;
  38. }
  39. try
  40. {
  41. var serialName = Model.SerialDeviceName;
  42. Log(3, $"开始光源控制,设备={(string.IsNullOrEmpty(serialName) ? "(未选择)" : serialName)}, 模式={Model.Mode}");
  43. if (string.IsNullOrEmpty(serialName))
  44. return Fail("未选择光源设备");
  45. var comm = Service<CommunicationManager>()?.GetByName(serialName);
  46. if (comm == null)
  47. return Fail($"光源设备未找到: {serialName}");
  48. if (!comm.IsConnected)
  49. return Fail($"光源设备未连接: {serialName}");
  50. var serial = comm as SerialCommunication;
  51. if (serial != null && serial.Terminator != Terminator.None)
  52. return Fail("光源设备结束符需设为 None");
  53. int channelCount = LightSourceManager.GetChannelCount(Model.LightModel);
  54. var manager = LightSourceManager.Instance;
  55. bool ok;
  56. int timeoutMs = Model.TimeoutMs;
  57. try
  58. {
  59. switch (Model.Mode)
  60. {
  61. case LightControlMode.TurnOnAll:
  62. ok = manager.TurnOnAllAsync(serialName, Model.LightModel, timeoutMs).GetAwaiter().GetResult();
  63. res["响应"] = ok ? "全开成功" : "全开失败";
  64. break;
  65. case LightControlMode.TurnOffAll:
  66. ok = manager.TurnOffAllAsync(serialName, Model.LightModel, timeoutMs).GetAwaiter().GetResult();
  67. res["响应"] = ok ? "全关成功" : "全关失败";
  68. break;
  69. default:
  70. {
  71. int ch = GetValue<int>(Model.Channel);
  72. int br = GetValue<int>(Model.Brightness);
  73. if (ch < 1 || ch > channelCount) ch = 1;
  74. if (br < 0) br = 0;
  75. if (br > 999) br = 999;
  76. Log(4, $"设置亮度: CH={ch}(有效1-{channelCount}), 亮度={br}(0-999), 超时={timeoutMs}ms");
  77. ok = manager.SetBrightnessAsync(serialName, Model.LightModel, ch, br, timeoutMs).GetAwaiter().GetResult();
  78. res["响应"] = ok ? $"CH{ch} → {br} 成功" : $"CH{ch} → {br} 失败";
  79. break;
  80. }
  81. }
  82. res["结果"] = ok;
  83. res["指令"] = manager.ToString();
  84. string resp = res["响应"]?.ToString() ?? "";
  85. if (ok)
  86. Log(2, $"光源控制成功: {resp}");
  87. else
  88. Log(1, $"光源控制失败: {resp}", TeamAAS.LogLevel.Error);
  89. return ok ? NodeRunStatus.Success : NodeRunStatus.Failed;
  90. }
  91. finally { }
  92. }
  93. catch (Exception ex)
  94. {
  95. return Fail(ex.Message);
  96. }
  97. }
  98. public static bool SetBrightness(string serialDeviceName, LightModel model, int channel, int brightness, int timeoutMs = 1000)
  99. {
  100. return LightSourceManager.Instance.SetBrightnessAsync(serialDeviceName, model, channel, brightness, timeoutMs)
  101. .GetAwaiter().GetResult();
  102. }
  103. public static bool TurnOnAll(string serialDeviceName, LightModel model, int timeoutMs = 1000)
  104. {
  105. return LightSourceManager.Instance.TurnOnAllAsync(serialDeviceName, model, timeoutMs)
  106. .GetAwaiter().GetResult();
  107. }
  108. public static bool TurnOffAll(string serialDeviceName, LightModel model, int timeoutMs = 1000)
  109. {
  110. return LightSourceManager.Instance.TurnOffAllAsync(serialDeviceName, model, timeoutMs)
  111. .GetAwaiter().GetResult();
  112. }
  113. }
  114. }