From af0e2180bd20aa5866c680c5bafbcfd9945bef1e Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Thu, 12 Nov 2020 11:44:11 -0700 Subject: [PATCH] Adds back in a factory and necessary methods to build a GenericVersiportInputDevice --- .../Inputs/GenericDigitalInputDevice.cs | 108 ++++++++--------- .../Inputs/GenericVersiportInputDevice.cs | 112 +++++++++++++++++- 2 files changed, 163 insertions(+), 57 deletions(-) diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron IO/Inputs/GenericDigitalInputDevice.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron IO/Inputs/GenericDigitalInputDevice.cs index e57e869d..23b261c0 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron IO/Inputs/GenericDigitalInputDevice.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron IO/Inputs/GenericDigitalInputDevice.cs @@ -1,32 +1,32 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; -using Crestron.SimplSharpPro; -using Crestron.SimplSharpPro.DeviceSupport; -using Newtonsoft.Json; -using PepperDash.Core; -using PepperDash.Essentials.Core.Bridges; -using PepperDash.Essentials.Core.Config; - - -namespace PepperDash.Essentials.Core.CrestronIO +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Crestron.SimplSharpPro; +using Crestron.SimplSharpPro.DeviceSupport; +using Newtonsoft.Json; +using PepperDash.Core; +using PepperDash.Essentials.Core.Bridges; +using PepperDash.Essentials.Core.Config; + + +namespace PepperDash.Essentials.Core.CrestronIO { [Description("Wrapper class for Digital Input")] - public class GenericDigitalInputDevice : EssentialsBridgeableDevice, IDigitalInput - { - public DigitalInput InputPort { get; private set; } - - public BoolFeedback InputStateFeedback { get; private set; } - - Func InputStateFeedbackFunc - { - get - { - return () => InputPort.State; - } - } + public class GenericDigitalInputDevice : EssentialsBridgeableDevice, IDigitalInput + { + public DigitalInput InputPort { get; private set; } + + public BoolFeedback InputStateFeedback { get; private set; } + + Func InputStateFeedbackFunc + { + get + { + return () => InputPort.State; + } + } public GenericDigitalInputDevice(string key, string name, Func postActivationFunc, @@ -40,15 +40,15 @@ namespace PepperDash.Essentials.Core.CrestronIO InputPort.Register(); - InputPort.StateChange += InputPort_StateChange; + InputPort.StateChange += InputPort_StateChange; }); } #region Events - void InputPort_StateChange(DigitalInput digitalInput, DigitalInputEventArgs args) - { + void InputPort_StateChange(DigitalInput digitalInput, DigitalInputEventArgs args) + { InputStateFeedback.FireUpdate(); } @@ -81,7 +81,7 @@ namespace PepperDash.Essentials.Core.CrestronIO } if (ioPortDevice == null) { - Debug.Console(0, "GetDigitalInput: Device '0' is not a valid IRelayPorts Device", dc.PortDeviceKey); + Debug.Console(0, "GetDigitalInput: Device '0' is not a valid IDigitalInputPorts Device", dc.PortDeviceKey); return null; } @@ -99,13 +99,13 @@ namespace PepperDash.Essentials.Core.CrestronIO #region Bridge Linking - public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge) + public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge) { - var joinMap = new IDigitalInputJoinMap(joinStart); - - var joinMapSerialized = JoinMapHelper.GetSerializedJoinMapForDevice(joinMapKey); - - if (!string.IsNullOrEmpty(joinMapSerialized)) + var joinMap = new IDigitalInputJoinMap(joinStart); + + var joinMapSerialized = JoinMapHelper.GetSerializedJoinMapForDevice(joinMapKey); + + if (!string.IsNullOrEmpty(joinMapSerialized)) joinMap = JsonConvert.DeserializeObject(joinMapSerialized); if (bridge != null) @@ -115,19 +115,19 @@ namespace PepperDash.Essentials.Core.CrestronIO else { Debug.Console(0, this, "Please update config to use 'eiscapiadvanced' to get all join map features for this device."); - } - - try - { - Debug.Console(1, this, "Linking to Trilist '{0}'", trilist.ID.ToString("X")); - - // Link feedback for input state - InputStateFeedback.LinkInputSig(trilist.BooleanInput[joinMap.InputState.JoinNumber]); - } - catch (Exception e) - { - Debug.Console(1, this, "Unable to link device '{0}'. Input is null", Key); - Debug.Console(1, this, "Error: {0}", e); + } + + try + { + Debug.Console(1, this, "Linking to Trilist '{0}'", trilist.ID.ToString("X")); + + // Link feedback for input state + InputStateFeedback.LinkInputSig(trilist.BooleanInput[joinMap.InputState.JoinNumber]); + } + catch (Exception e) + { + Debug.Console(1, this, "Unable to link device '{0}'. Input is null", Key); + Debug.Console(1, this, "Error: {0}", e); } } @@ -144,7 +144,7 @@ namespace PepperDash.Essentials.Core.CrestronIO public override EssentialsDevice BuildDevice(DeviceConfig dc) { - Debug.Console(1, "Factory Attempting to create new Generic Relay Device"); + Debug.Console(1, "Factory Attempting to create new Generic Digital Input Device"); var props = JsonConvert.DeserializeObject(dc.Properties.ToString()); @@ -158,7 +158,7 @@ namespace PepperDash.Essentials.Core.CrestronIO #endregion - } - - + } + + } \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron IO/Inputs/GenericVersiportInputDevice.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron IO/Inputs/GenericVersiportInputDevice.cs index 90ff4fa0..be9ad367 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron IO/Inputs/GenericVersiportInputDevice.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron IO/Inputs/GenericVersiportInputDevice.cs @@ -4,15 +4,21 @@ using System.Linq; using System.Text; using Crestron.SimplSharp; using Crestron.SimplSharpPro; +using Crestron.SimplSharpPro.DeviceSupport; using PepperDash.Core; +using PepperDash.Essentials.Core.Config; +using PepperDash.Essentials.Core.Bridges; + + +using Newtonsoft.Json; namespace PepperDash.Essentials.Core.CrestronIO { /// /// Represents a generic digital input deviced tied to a versiport /// - public class GenericVersiportDigitalInputDevice : EssentialsDevice, IDigitalInput + public class GenericVersiportDigitalInputDevice : EssentialsBridgeableDevice, IDigitalInput { public Versiport InputPort { get; private set; } @@ -26,8 +32,8 @@ namespace PepperDash.Essentials.Core.CrestronIO } } - public GenericVersiportDigitalInputDevice(string key, Versiport inputPort, IOPortConfig props): - base(key) + public GenericVersiportDigitalInputDevice(string key, string name, Versiport inputPort, IOPortConfig props): + base(key, name) { InputStateFeedback = new BoolFeedback(InputStateFeedbackFunc); InputPort = inputPort; @@ -46,5 +52,105 @@ namespace PepperDash.Essentials.Core.CrestronIO if(args.Event == eVersiportEvent.DigitalInChange) InputStateFeedback.FireUpdate(); } + + + #region Bridge Linking + + public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge) + { + var joinMap = new IDigitalInputJoinMap(joinStart); + + var joinMapSerialized = JoinMapHelper.GetSerializedJoinMapForDevice(joinMapKey); + + if (!string.IsNullOrEmpty(joinMapSerialized)) + joinMap = JsonConvert.DeserializeObject(joinMapSerialized); + + if (bridge != null) + { + bridge.AddJoinMap(Key, joinMap); + } + else + { + Debug.Console(0, this, "Please update config to use 'eiscapiadvanced' to get all join map features for this device."); + } + + try + { + Debug.Console(1, this, "Linking to Trilist '{0}'", trilist.ID.ToString("X")); + + // Link feedback for input state + InputStateFeedback.LinkInputSig(trilist.BooleanInput[joinMap.InputState.JoinNumber]); + } + catch (Exception e) + { + Debug.Console(1, this, "Unable to link device '{0}'. Input is null", Key); + Debug.Console(1, this, "Error: {0}", e); + } + } + + #endregion + + + public static Versiport GetVersiportDigitalInput(IOPortConfig dc) + { + + IIOPorts ioPortDevice; + + if (dc.PortDeviceKey.Equals("processor")) + { + if (!Global.ControlSystem.SupportsDigitalInput) + { + Debug.Console(0, "GetVersiportDigitalInput: Processor does not support Versiports"); + return null; + } + ioPortDevice = Global.ControlSystem; + } + else + { + var ioPortDev = DeviceManager.GetDeviceForKey(dc.PortDeviceKey) as IIOPorts; + if (ioPortDev == null) + { + Debug.Console(0, "GetVersiportDigitalInput: Device {0} is not a valid device", dc.PortDeviceKey); + return null; + } + ioPortDevice = ioPortDev; + } + if (ioPortDevice == null) + { + Debug.Console(0, "GetVersiportDigitalInput: Device '0' is not a valid IIOPorts Device", dc.PortDeviceKey); + return null; + } + + if (dc.PortNumber > ioPortDevice.NumberOfVersiPorts) + { + Debug.Console(0, "GetVersiportDigitalInput: Device {0} does not contain a port {1}", dc.PortDeviceKey, dc.PortNumber); + } + + return ioPortDevice.VersiPorts[dc.PortNumber]; + + + } + } + + + public class GenericVersiportDigitalInputDeviceFactory : EssentialsDeviceFactory + { + public GenericVersiportDigitalInputDeviceFactory() + { + TypeNames = new List() { "versiportinput" }; + } + + public override EssentialsDevice BuildDevice(DeviceConfig dc) + { + Debug.Console(1, "Factory Attempting to create new Generic Versiport Device"); + + var props = JsonConvert.DeserializeObject(dc.Properties.ToString()); + + if (props == null) return null; + + var portDevice = new GenericVersiportDigitalInputDevice(dc.Key, dc.Name, GenericVersiportDigitalInputDevice.GetVersiportDigitalInput(props), props); + + return portDevice; + } } } \ No newline at end of file