using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using Crestron.SimplSharpPro;
namespace PepperDash.Essentials.Core
{
///
/// 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. The ValueFunc is
/// evaluated whenever FireUpdate() is called
///
public override bool BoolValue { get { return _BoolValue; } }
bool _BoolValue;
///
/// 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();
List LinkedCrestronFeedbacks = new List();
///
/// Creates the feedback with the Func as described.
///
///
/// While the linked sig value will be updated with the current value stored when it is linked to a EISC Bridge,
/// it will NOT reflect an actual value from a device until has been called
///
/// Delegate to invoke when this feedback needs to be updated
public BoolFeedback(Func valueFunc)
: this(null, valueFunc)
{
}
///
/// Creates the feedback with the Func as described.
///
///
/// While the linked sig value will be updated with the current value stored when it is linked to a EISC Bridge,
/// it will NOT reflect an actual value from a device until has been called
///
/// Key to find this Feedback
/// Delegate to invoke when this feedback needs to be updated
public BoolFeedback(string key, Func valueFunc)
: base(key)
{
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(newValue);
}
}
///
/// Links an input sig
///
///
public void LinkInputSig(BoolInputSig sig)
{
LinkedInputSigs.Add(sig);
UpdateSig(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(sig);
}
///
/// Unlinks an input sig to the complement value
///
///
public void UnlinkComplementInputSig(BoolInputSig sig)
{
LinkedComplementInputSigs.Remove(sig);
}
///
/// Links a Crestron Feedback object
///
///
public void LinkCrestronFeedback(Crestron.SimplSharpPro.DeviceSupport.Feedback feedback)
{
LinkedCrestronFeedbacks.Add(feedback);
UpdateCrestronFeedback(feedback);
}
///
///
///
///
public void UnlinkCrestronFeedback(Crestron.SimplSharpPro.DeviceSupport.Feedback feedback)
{
LinkedCrestronFeedbacks.Remove(feedback);
}
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;
}
void UpdateCrestronFeedback(Crestron.SimplSharpPro.DeviceSupport.Feedback feedback)
{
feedback.State = _BoolValue;
}
}
}