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