Compare commits

...

1 Commits

Author SHA1 Message Date
Neil Dorin
83525b721b feat(SigCommands): #759 Adds SigCommand concept for each data type 2021-07-26 17:57:35 -06:00
7 changed files with 321 additions and 12 deletions

View File

@@ -62,6 +62,7 @@ namespace PepperDash.Essentials.Core
ValueFunc = valueFunc;
}
public override void FireUpdate()
{
bool newValue = InTestMode ? TestValue : ValueFunc.Invoke();

View File

@@ -42,8 +42,6 @@ namespace PepperDash.Essentials.Core
Key = key;
}
/// <summary>
/// Clears test mode and fires update.
/// </summary>
@@ -53,9 +51,9 @@ namespace PepperDash.Essentials.Core
FireUpdate();
}
/// <summary>
/// Fires an update synchronously
/// </summary>
/// <summary>
/// Computes the value by running the ValueFunc and if the value has changed, updates the linked sigs and fires the OnOutputChange event
/// </summary>
public abstract void FireUpdate();
/// <summary>
@@ -66,13 +64,7 @@ namespace PepperDash.Essentials.Core
CrestronInvoke.BeginInvoke(o => FireUpdate());
}
///// <summary>
///// Helper method that fires event. Use this intstead of calling OutputChange
///// </summary>
//protected void OnOutputChange()
//{
// if (OutputChange != null) OutputChange(this, EventArgs.Empty);
//}
// Helper Methods for firing event
protected void OnOutputChange(bool value)
{

View File

@@ -157,6 +157,9 @@
<Compile Include="Comm and IR\GenericComm.cs" />
<Compile Include="Comm and IR\GenericHttpClient.cs" />
<Compile Include="Comm and IR\IRPortHelper.cs" />
<Compile Include="SigCommands\BoolSigCommand.cs" />
<Compile Include="SigCommands\IntSigCommand.cs" />
<Compile Include="SigCommands\SigCommandBase.cs" />
<Compile Include="Config\Essentials\ConfigUpdater.cs" />
<Compile Include="Config\Essentials\ConfigReader.cs" />
<Compile Include="Config\Essentials\ConfigWriter.cs" />
@@ -332,6 +335,7 @@
<Compile Include="Shades\Shade Interfaces.cs" />
<Compile Include="Shades\ShadeBase.cs" />
<Compile Include="Shades\ShadeController.cs" />
<Compile Include="SigCommands\StringSigCommand.cs" />
<Compile Include="SmartObjects\SmartObjectNumeric.cs" />
<Compile Include="SmartObjects\SmartObjectDynamicList.cs" />
<Compile Include="SmartObjects\SmartObjectDPad.cs" />

View File

@@ -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
{
/// <summary>
/// A helper class to make it easier to work with Crestron Sigs
/// </summary>
public class BoolSigCommand : SigCommandBase
{
/// <summary>
/// Func that evaluates on FireUpdate and updates the linked sigs
/// </summary>
public Func<bool> ValueFunc { get; private set; }
List<BoolInputSig> LinkedInputSigs = new List<BoolInputSig>();
List<BoolInputSig> LinkedComplementInputSigs = new List<BoolInputSig>();
public BoolSigCommand(Func<bool> valueFunc)
: this(null, valueFunc)
{
}
/// <summary>
/// Creates the SigCommand with the Func as described.
/// </summary>
/// <remarks>
/// When linking to a sig, the sig value func will be run and that sigs value updated.
/// To update all the linked sigs, <seealso cref="FireUpdate"/> must been called
/// </remarks>
/// <param name="valueFunc">Delegate to invoke when this SigCommand gets fired</param>
public BoolSigCommand(string key, Func<bool> 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();
}
/// <summary>
/// Links an input sig
/// </summary>
/// <param name="sig"></param>
public void LinkInputSig(BoolInputSig sig)
{
LinkedInputSigs.Add(sig);
UpdateSig(InvokeValueFunc(), sig);
}
/// <summary>
/// Unlinks an inputs sig
/// </summary>
/// <param name="sig"></param>
public void UnlinkInputSig(BoolInputSig sig)
{
LinkedInputSigs.Remove(sig);
}
/// <summary>
/// Links an input sig to the complement value
/// </summary>
/// <param name="sig"></param>
public void LinkComplementInputSig(BoolInputSig sig)
{
LinkedComplementInputSigs.Add(sig);
UpdateComplementSig(InvokeValueFunc(), sig);
}
/// <summary>
/// Unlinks an input sig to the complement value
/// </summary>
/// <param name="sig"></param>
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;
}
}
}

View File

@@ -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
{
/// <summary>
/// A helper class to make it easier to work with Crestron Sigs
/// </summary>
public class IntSigCommand : SigCommandBase
{
/// <summary>
/// Func that evaluates on FireUpdate and updates the linked sigs
/// </summary>
public Func<int> ValueFunc { get; private set; }
List<UShortInputSig> LinkedInputSigs = new List<UShortInputSig>();
public IntSigCommand(Func<int> valueFunc)
: this(null, valueFunc)
{
}
/// <summary>
/// Creates the SigCommand with the Func as described.
/// </summary>
/// <remarks>
/// When linking to a sig, the sig value func will be run and that sigs value updated.
/// To update all the linked sigs, <seealso cref="FireUpdate"/> must been called
/// </remarks>
/// <param name="valueFunc">Delegate to invoke when this SigCommand gets fired</param>
public IntSigCommand(string key, Func<int> valueFunc)
: base(key)
{
ValueFunc = valueFunc;
}
public override void FireUpdate()
{
var value = InvokeValueFunc();
LinkedInputSigs.ForEach(s => UpdateSig(value, s));
}
ushort InvokeValueFunc()
{
return (ushort)ValueFunc.Invoke();
}
/// <summary>
/// Links an input sig
/// </summary>
/// <param name="sig"></param>
public void LinkInputSig(UShortInputSig sig)
{
LinkedInputSigs.Add(sig);
UpdateSig(InvokeValueFunc(), sig);
}
/// <summary>
/// Unlinks an inputs sig
/// </summary>
/// <param name="sig"></param>
public void UnlinkInputSig(UShortInputSig sig)
{
LinkedInputSigs.Remove(sig);
}
void UpdateSig(ushort value, UShortInputSig sig)
{
sig.UShortValue = value;
}
}
}

View File

@@ -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
{
/// <summary>
/// A helper class to make it easier to work with Crestron Sigs
/// </summary>
public abstract class SigCommandBase : IKeyed
{
public string Key { get; private set; }
/// <summary>
/// Base Constructor - empty
/// </summary>
protected SigCommandBase()
{
}
protected SigCommandBase(string key)
{
if (key == null)
Key = "";
else
Key = key;
}
/// <summary>
/// Computes the value by running the ValueFunc and if the value has changed, updates the linked sigs and fires the OnOutputChange event
/// </summary>
public abstract void FireUpdate();
/// <summary>
/// Fires the update asynchronously within a CrestronInvoke
/// </summary>
public void InvokeFireUpdate()
{
CrestronInvoke.BeginInvoke(o => FireUpdate());
}
}
}

View File

@@ -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
{
/// <summary>
/// A helper class to make it easier to work with Crestron Sigs
/// </summary>
public class StringSigCommand : SigCommandBase
{
/// <summary>
/// Func that evaluates on FireUpdate and updates the linked sigs
/// </summary>
public Func<string> ValueFunc { get; private set; }
List<StringInputSig> LinkedInputSigs = new List<StringInputSig>();
public StringSigCommand(Func<string> valueFunc)
: this(null, valueFunc)
{
}
/// <summary>
/// Creates the SigCommand with the Func as described.
/// </summary>
/// <remarks>
/// When linking to a sig, the sig value func will be run and that sigs value updated.
/// To update all the linked sigs, <seealso cref="FireUpdate"/> must been called
/// </remarks>
/// <param name="valueFunc">Delegate to invoke when this SigCommand gets fired</param>
public StringSigCommand(string key, Func<string> valueFunc)
: base(key)
{
ValueFunc = valueFunc;
}
public override void FireUpdate()
{
var value = InvokeValueFunc();
LinkedInputSigs.ForEach(s => UpdateSig(value, s));
}
string InvokeValueFunc()
{
return ValueFunc.Invoke();
}
/// <summary>
/// Links an input sig
/// </summary>
/// <param name="sig"></param>
public void LinkInputSig(StringInputSig sig)
{
LinkedInputSigs.Add(sig);
UpdateSig(InvokeValueFunc(), sig);
}
/// <summary>
/// Unlinks an inputs sig
/// </summary>
/// <param name="sig"></param>
public void UnlinkInputSig(StringInputSig sig)
{
LinkedInputSigs.Remove(sig);
}
void UpdateSig(string value, StringInputSig sig)
{
sig.StringValue = value;
}
}
}