using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using Crestron.SimplSharpPro; namespace PepperDash.Essentials.Core { public abstract class Feedback { public event EventHandler OutputChange; public virtual bool BoolValue { get { return false; } } public virtual int IntValue { get { return 0; } } public virtual string StringValue { get { return ""; } } public Cue Cue { get; private set; } public abstract eCueType Type { get; } /// /// /// public bool InTestMode { get; protected set; } /// /// Base Constructor - empty /// protected Feedback() { } protected Feedback(Cue cue) { Cue = cue; } /// /// Clears test mode and fires update. /// public void ClearTestValue() { InTestMode = false; FireUpdate(); } /// /// Fires an update synchronously /// public abstract void FireUpdate(); /// /// Fires the update asynchronously within a CrestronInvoke /// public void InvokeFireUpdate() { CrestronInvoke.BeginInvoke(o => FireUpdate()); } protected void OnOutputChange() { if (OutputChange != null) OutputChange(this, EventArgs.Empty); } } /// /// A Feedback whose output is derived from the return value of a provided Func. /// public class BoolFeedback : Feedback { /// /// Returns the current value of the feedback, derived from the ValueFunc /// public override bool BoolValue { get { return _BoolValue; } } bool _BoolValue; public override eCueType Type { get { return eCueType.Bool; } } /// /// Fake value to be used in test mode /// public bool TestValue { get; private set; } /// /// Func that evaluates on FireUpdate /// public Func ValueFunc { get; private set; } List LinkedInputSigs = new List(); List LinkedComplementInputSigs = new List(); public BoolFeedback(Func valueFunc) : this(Cue.DefaultBoolCue, valueFunc) { } public BoolFeedback(Cue cue, Func valueFunc) : base(cue) { if (cue == null) throw new ArgumentNullException("cue"); ValueFunc = valueFunc; } public override void FireUpdate() { bool newValue = InTestMode ? TestValue : ValueFunc.Invoke(); if (newValue != _BoolValue) { _BoolValue = newValue; LinkedInputSigs.ForEach(s => UpdateSig(s)); LinkedComplementInputSigs.ForEach(s => UpdateComplementSig(s)); OnOutputChange(); } } public void LinkInputSig(BoolInputSig sig) { LinkedInputSigs.Add(sig); UpdateSig(sig); } public void UnlinkInputSig(BoolInputSig sig) { LinkedInputSigs.Remove(sig); } public void LinkComplementInputSig(BoolInputSig sig) { LinkedComplementInputSigs.Add(sig); UpdateComplementSig(sig); } public void UnlinkComplementInputSig(BoolInputSig sig) { LinkedComplementInputSigs.Remove(sig); } public override string ToString() { return (InTestMode ? "TEST -- " : "") + BoolValue.ToString(); } /// /// Puts this in test mode, sets the test value and fires an update. /// /// public void SetTestValue(bool value) { TestValue = value; InTestMode = true; FireUpdate(); } void UpdateSig(BoolInputSig sig) { sig.BoolValue = _BoolValue; } void UpdateComplementSig(BoolInputSig sig) { sig.BoolValue = !_BoolValue; } } //****************************************************************************** public class IntFeedback : Feedback { public override int IntValue { get { return _IntValue; } } // ValueFunc.Invoke(); } } int _IntValue; public ushort UShortValue { get { return (ushort)_IntValue; } } public override eCueType Type { get { return eCueType.Int; } } public int TestValue { get; private set; } /// /// Func evaluated on FireUpdate /// Func ValueFunc; List LinkedInputSigs = new List(); public IntFeedback(Func valueFunc) : this(Cue.DefaultIntCue, valueFunc) { } public IntFeedback(Cue cue, Func valueFunc) : base(cue) { if (cue == null) throw new ArgumentNullException("cue"); ValueFunc = valueFunc; } public override void FireUpdate() { var newValue = InTestMode ? TestValue : ValueFunc.Invoke(); if (newValue != _IntValue) { _IntValue = newValue; LinkedInputSigs.ForEach(s => UpdateSig(s)); OnOutputChange(); } } public void LinkInputSig(UShortInputSig sig) { LinkedInputSigs.Add(sig); UpdateSig(sig); } public void UnlinkInputSig(UShortInputSig sig) { LinkedInputSigs.Remove(sig); } public override string ToString() { return (InTestMode ? "TEST -- " : "") + IntValue.ToString(); } /// /// Puts this in test mode, sets the test value and fires an update. /// /// public void SetTestValue(int value) { TestValue = value; InTestMode = true; FireUpdate(); } void UpdateSig(UShortInputSig sig) { sig.UShortValue = UShortValue; } } //****************************************************************************** public class StringFeedback : Feedback { public override string StringValue { get { return _StringValue; } } // ValueFunc.Invoke(); } } string _StringValue; public override eCueType Type { get { return eCueType.String; } } /// /// Used in testing. Set/Clear functions /// public string TestValue { get; private set; } /// /// Evalutated on FireUpdate /// public Func ValueFunc { get; private set; } List LinkedInputSigs = new List(); public StringFeedback(Func valueFunc) : this(Cue.DefaultStringCue, valueFunc) { } public StringFeedback(Cue cue, Func valueFunc) : base(cue) { if (cue == null) throw new ArgumentNullException("cue"); ValueFunc = valueFunc; } public override void FireUpdate() { var newValue = InTestMode ? TestValue : ValueFunc.Invoke(); if (newValue != _StringValue) { _StringValue = newValue; LinkedInputSigs.ForEach(s => UpdateSig(s)); OnOutputChange(); } } public void LinkInputSig(StringInputSig sig) { LinkedInputSigs.Add(sig); UpdateSig(sig); } public void UnlinkInputSig(StringInputSig sig) { LinkedInputSigs.Remove(sig); } public override string ToString() { return (InTestMode ? "TEST -- " : "") + StringValue; } /// /// Puts this in test mode, sets the test value and fires an update. /// /// public void SetTestValue(string value) { TestValue = value; InTestMode = true; FireUpdate(); } void UpdateSig(StringInputSig sig) { sig.StringValue = _StringValue; } } }