using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using Crestron.SimplSharpPro; using Crestron.SimplSharpPro.DeviceSupport; using PepperDash.Core; namespace PepperDash.Essentials.Core.SmartObjects { public class SmartObjectHelperBase { public SmartObject SmartObject { get; private set; } /// /// This should be set by all inheriting classes, after the class has verified that it is linked to the right object. /// public bool Validated { get; protected set; } public SmartObjectHelperBase(SmartObject so, bool useUserObjectHandler) { SmartObject = so; if (useUserObjectHandler) { // Prevent this from double-registering SmartObject.SigChange -= this.SmartObject_SigChange; SmartObject.SigChange += this.SmartObject_SigChange; } } ~SmartObjectHelperBase() { SmartObject.SigChange -= this.SmartObject_SigChange; } /// /// Helper to get a sig name with debugging when fail /// /// /// public BoolOutputSig GetBoolOutputNamed(string name) { if (SmartObject.BooleanOutput.Contains(name)) return SmartObject.BooleanOutput[name]; else Debug.Console(0, "WARNING: Cannot get signal. Smart object {0} on trilist {1:x2} does not contain signal '{2}'", SmartObject.ID, SmartObject.Device.ID, name); return null; } /// /// Sets action on signal after checking for existence. /// /// /// public void SetBoolAction(string name, Action a) { if (SmartObject.BooleanOutput.Contains(name)) SmartObject.BooleanOutput[name].UserObject = a; else { Debug.Console(0, "WARNING: Cannot set action. Smart object {0} on trilist {1:x2} does not contain signal '{2}'", SmartObject.ID, SmartObject.Device.ID, name); } } /// /// Standard Action listener /// /// /// void SmartObject_SigChange(GenericBase currentDevice, SmartObjectEventArgs args) { var uo = args.Sig.UserObject; if (uo is Action) (uo as Action)(args.Sig.BoolValue); else if (uo is Action) (uo as Action)(args.Sig.UShortValue); else if (uo is Action) (uo as Action)(args.Sig.StringValue); } } }