From 83525b721b180dc59f17d0d317376faefd4bfe5d Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Mon, 26 Jul 2021 17:57:35 -0600 Subject: [PATCH] feat(SigCommands): #759 Adds SigCommand concept for each data type --- .../Feedbacks/BoolFeedback.cs | 1 + .../Feedbacks/FeedbackBase.cs | 16 +-- .../PepperDash_Essentials_Core.csproj | 4 + .../SigCommands/BoolSigCommand.cs | 105 ++++++++++++++++++ .../SigCommands/IntSigCommand.cs | 80 +++++++++++++ .../SigCommands/SigCommandBase.cs | 47 ++++++++ .../SigCommands/StringSigCommand.cs | 80 +++++++++++++ 7 files changed, 321 insertions(+), 12 deletions(-) create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/BoolSigCommand.cs create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/IntSigCommand.cs create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/SigCommandBase.cs create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/StringSigCommand.cs diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Feedbacks/BoolFeedback.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Feedbacks/BoolFeedback.cs index f46b4767..5b72b370 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Feedbacks/BoolFeedback.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Feedbacks/BoolFeedback.cs @@ -62,6 +62,7 @@ namespace PepperDash.Essentials.Core ValueFunc = valueFunc; } + public override void FireUpdate() { bool newValue = InTestMode ? TestValue : ValueFunc.Invoke(); diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Feedbacks/FeedbackBase.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Feedbacks/FeedbackBase.cs index c3aac50b..4e1f73a9 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Feedbacks/FeedbackBase.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Feedbacks/FeedbackBase.cs @@ -42,8 +42,6 @@ namespace PepperDash.Essentials.Core Key = key; } - - /// /// Clears test mode and fires update. /// @@ -53,9 +51,9 @@ namespace PepperDash.Essentials.Core FireUpdate(); } - /// - /// Fires an update synchronously - /// + /// + /// Computes the value by running the ValueFunc and if the value has changed, updates the linked sigs and fires the OnOutputChange event + /// public abstract void FireUpdate(); /// @@ -66,13 +64,7 @@ namespace PepperDash.Essentials.Core CrestronInvoke.BeginInvoke(o => FireUpdate()); } - ///// - ///// Helper method that fires event. Use this intstead of calling OutputChange - ///// - //protected void OnOutputChange() - //{ - // if (OutputChange != null) OutputChange(this, EventArgs.Empty); - //} + // Helper Methods for firing event protected void OnOutputChange(bool value) { diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj b/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj index 36992629..036090c5 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj @@ -157,6 +157,9 @@ + + + @@ -332,6 +335,7 @@ + diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/BoolSigCommand.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/BoolSigCommand.cs new file mode 100644 index 00000000..952a965c --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/BoolSigCommand.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Crestron.SimplSharpPro; + +using PepperDash.Core; + +namespace PepperDash.Essentials.Core +{ + /// + /// A helper class to make it easier to work with Crestron Sigs + /// + public class BoolSigCommand : SigCommandBase + { + /// + /// Func that evaluates on FireUpdate and updates the linked sigs + /// + public Func ValueFunc { get; private set; } + + List LinkedInputSigs = new List(); + List LinkedComplementInputSigs = new List(); + + public BoolSigCommand(Func valueFunc) + : this(null, valueFunc) + { + } + + /// + /// Creates the SigCommand with the Func as described. + /// + /// + /// When linking to a sig, the sig value func will be run and that sigs value updated. + /// To update all the linked sigs, must been called + /// + /// Delegate to invoke when this SigCommand gets fired + public BoolSigCommand(string key, Func valueFunc) + : base(key) + { + ValueFunc = valueFunc; + } + + public override void FireUpdate() + { + var value = InvokeValueFunc(); + + LinkedInputSigs.ForEach(s => UpdateSig(value, s)); + LinkedComplementInputSigs.ForEach(s => UpdateComplementSig(value, s)); + } + + bool InvokeValueFunc() + { + return ValueFunc.Invoke(); + } + + /// + /// Links an input sig + /// + /// + public void LinkInputSig(BoolInputSig sig) + { + LinkedInputSigs.Add(sig); + UpdateSig(InvokeValueFunc(), sig); + } + + /// + /// Unlinks an inputs sig + /// + /// + public void UnlinkInputSig(BoolInputSig sig) + { + LinkedInputSigs.Remove(sig); + } + + /// + /// Links an input sig to the complement value + /// + /// + public void LinkComplementInputSig(BoolInputSig sig) + { + LinkedComplementInputSigs.Add(sig); + UpdateComplementSig(InvokeValueFunc(), sig); + } + + /// + /// Unlinks an input sig to the complement value + /// + /// + public void UnlinkComplementInputSig(BoolInputSig sig) + { + LinkedComplementInputSigs.Remove(sig); + } + + void UpdateSig(bool value, BoolInputSig sig) + { + sig.BoolValue = value; + } + + void UpdateComplementSig(bool value, BoolInputSig sig) + { + sig.BoolValue = !value; + } + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/IntSigCommand.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/IntSigCommand.cs new file mode 100644 index 00000000..3c26b338 --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/IntSigCommand.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Crestron.SimplSharpPro; + +using PepperDash.Core; + +namespace PepperDash.Essentials.Core +{ + /// + /// A helper class to make it easier to work with Crestron Sigs + /// + public class IntSigCommand : SigCommandBase + { + /// + /// Func that evaluates on FireUpdate and updates the linked sigs + /// + public Func ValueFunc { get; private set; } + + List LinkedInputSigs = new List(); + + public IntSigCommand(Func valueFunc) + : this(null, valueFunc) + { + } + + /// + /// Creates the SigCommand with the Func as described. + /// + /// + /// When linking to a sig, the sig value func will be run and that sigs value updated. + /// To update all the linked sigs, must been called + /// + /// Delegate to invoke when this SigCommand gets fired + public IntSigCommand(string key, Func valueFunc) + : base(key) + { + ValueFunc = valueFunc; + } + + public override void FireUpdate() + { + var value = InvokeValueFunc(); + + LinkedInputSigs.ForEach(s => UpdateSig(value, s)); + } + + ushort InvokeValueFunc() + { + return (ushort)ValueFunc.Invoke(); + } + + /// + /// Links an input sig + /// + /// + public void LinkInputSig(UShortInputSig sig) + { + LinkedInputSigs.Add(sig); + UpdateSig(InvokeValueFunc(), sig); + } + + /// + /// Unlinks an inputs sig + /// + /// + public void UnlinkInputSig(UShortInputSig sig) + { + LinkedInputSigs.Remove(sig); + } + + void UpdateSig(ushort value, UShortInputSig sig) + { + sig.UShortValue = value; + } + + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/SigCommandBase.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/SigCommandBase.cs new file mode 100644 index 00000000..0fdbf92f --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/SigCommandBase.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +using PepperDash.Core; + +namespace PepperDash.Essentials.Core +{ + /// + /// A helper class to make it easier to work with Crestron Sigs + /// + public abstract class SigCommandBase : IKeyed + { + + public string Key { get; private set; } + + /// + /// Base Constructor - empty + /// + protected SigCommandBase() + { + } + + protected SigCommandBase(string key) + { + if (key == null) + Key = ""; + else + Key = key; + } + + /// + /// Computes the value by running the ValueFunc and if the value has changed, updates the linked sigs and fires the OnOutputChange event + /// + public abstract void FireUpdate(); + + /// + /// Fires the update asynchronously within a CrestronInvoke + /// + public void InvokeFireUpdate() + { + CrestronInvoke.BeginInvoke(o => FireUpdate()); + } + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/StringSigCommand.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/StringSigCommand.cs new file mode 100644 index 00000000..f76080c3 --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/SigCommands/StringSigCommand.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Crestron.SimplSharpPro; + +using PepperDash.Core; + +namespace PepperDash.Essentials.Core +{ + /// + /// A helper class to make it easier to work with Crestron Sigs + /// + public class StringSigCommand : SigCommandBase + { + /// + /// Func that evaluates on FireUpdate and updates the linked sigs + /// + public Func ValueFunc { get; private set; } + + List LinkedInputSigs = new List(); + + public StringSigCommand(Func valueFunc) + : this(null, valueFunc) + { + } + + /// + /// Creates the SigCommand with the Func as described. + /// + /// + /// When linking to a sig, the sig value func will be run and that sigs value updated. + /// To update all the linked sigs, must been called + /// + /// Delegate to invoke when this SigCommand gets fired + public StringSigCommand(string key, Func valueFunc) + : base(key) + { + ValueFunc = valueFunc; + } + + public override void FireUpdate() + { + var value = InvokeValueFunc(); + + LinkedInputSigs.ForEach(s => UpdateSig(value, s)); + } + + string InvokeValueFunc() + { + return ValueFunc.Invoke(); + } + + /// + /// Links an input sig + /// + /// + public void LinkInputSig(StringInputSig sig) + { + LinkedInputSigs.Add(sig); + UpdateSig(InvokeValueFunc(), sig); + } + + /// + /// Unlinks an inputs sig + /// + /// + public void UnlinkInputSig(StringInputSig sig) + { + LinkedInputSigs.Remove(sig); + } + + void UpdateSig(string value, StringInputSig sig) + { + sig.StringValue = value; + } + + } +} \ No newline at end of file