using System; using System.Collections.Generic; using System.Threading; using Plugins.Motion.Models; using TeamAAS.FlowEditor.Models; using TeamAAS.FlowEditor.Plugins; using TeamAAS.Motion; namespace Plugins.Motion { [Serializable] [Plugin("轴使能", PluginCategory.硬件模块, typeof(MotionEnableModel), "Power", Description = "对指定轴或全部轴上使能")] public class MotionEnablePlugin : BasePlugin { public override List DeclareOutputs() => new List { new OutputField("结果", typeof(bool)), new OutputField("使能", typeof(bool)), new OutputField("最终位置", typeof(double)), }; public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary results) { var res = new Dictionary(); results = res; NodeRunStatus Fail(string m) { res["Error"] = m; res["结果"] = false; Log(1, $"轴使能失败: {m}", TeamAAS.LogLevel.Error); return NodeRunStatus.Failed; } try { var card = MotionPluginHelper.GetCard(Service(), Model.MotionCardName, out var err); if (card == null) return Fail(err); err = MotionPluginHelper.EnsureConnected(card, Model.AutoConnect); if (err != null) return Fail(err); Log(3, $"轴使能 卡={card.Name} 轴={Model.AxisNo}"); if (!card.SetEnable(Model.AxisNo, true)) return Fail("下发使能失败"); if (Model.AxisNo >= 0) MotionPluginHelper.FillStatus(res, card.GetAxisStatus(Model.AxisNo)); else res["使能"] = true; res["结果"] = true; return NodeRunStatus.Success; } catch (OperationCanceledException) { throw; } catch (Exception ex) { return Fail(ex.Message); } } } [Serializable] [Plugin("轴下使能", PluginCategory.硬件模块, typeof(MotionDisableModel), "PowerOff", Description = "对指定轴或全部轴下使能")] public class MotionDisablePlugin : BasePlugin { public override List DeclareOutputs() => new List { new OutputField("结果", typeof(bool)), new OutputField("使能", typeof(bool)), new OutputField("最终位置", typeof(double)), }; public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary results) { var res = new Dictionary(); results = res; NodeRunStatus Fail(string m) { res["Error"] = m; res["结果"] = false; Log(1, $"轴下使能失败: {m}", TeamAAS.LogLevel.Error); return NodeRunStatus.Failed; } try { var card = MotionPluginHelper.GetCard(Service(), Model.MotionCardName, out var err); if (card == null) return Fail(err); err = MotionPluginHelper.EnsureConnected(card, Model.AutoConnect); if (err != null) return Fail(err); Log(3, $"轴下使能 卡={card.Name} 轴={Model.AxisNo}"); card.Stop(Model.AxisNo); if (!card.SetEnable(Model.AxisNo, false)) return Fail("下发下使能失败"); if (Model.AxisNo >= 0) MotionPluginHelper.FillStatus(res, card.GetAxisStatus(Model.AxisNo)); else res["使能"] = false; res["结果"] = true; return NodeRunStatus.Success; } catch (OperationCanceledException) { throw; } catch (Exception ex) { return Fail(ex.Message); } } } }