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;
}
}
}