From b0c206eb616ee32a1c55a4e54b755828f32c31ae Mon Sep 17 00:00:00 2001 From: jdevito Date: Tue, 28 Feb 2023 13:26:24 -0600 Subject: [PATCH] fix: #1071 - Emergency Contact Closure Null Ref EssentialsRoomconfig.cs added debug statements to GetEmergency method changed 'return null' to return the object 'e' EssentialsRoomEmergencyConfig.cs added 'PortDeviceKey' to EssentialsRoomEmergencyTrigerConfig EssentialsRoomEmergencyContactClosure.cs added debug statements to constructor to help with debugging --- .../Room/Config/EssentialsRoomConfig.cs | 33 ++-- .../Config/EssentialsRoomEmergencyConfig.cs | 75 +++++---- .../EsentialsRoomEmergencyContactClosure.cs | 150 +++++++++++------- 3 files changed, 155 insertions(+), 103 deletions(-) diff --git a/PepperDashEssentials/Room/Config/EssentialsRoomConfig.cs b/PepperDashEssentials/Room/Config/EssentialsRoomConfig.cs index 38067528..bb35fdd0 100644 --- a/PepperDashEssentials/Room/Config/EssentialsRoomConfig.cs +++ b/PepperDashEssentials/Room/Config/EssentialsRoomConfig.cs @@ -55,22 +55,33 @@ namespace PepperDash.Essentials.Room.Config } } } - - /// + + /// /// Gets and operating, standalone emergegncy object that can be plugged into a room. /// Returns null if there is no emergency defined /// - public static EssentialsRoomEmergencyBase GetEmergency(EssentialsRoomPropertiesConfig props, IEssentialsRoom room) + public static EssentialsRoomEmergencyBase GetEmergency(EssentialsRoomPropertiesConfig props, IEssentialsRoom room) { - // This emergency + // This emergency var emergency = props.Emergency; - if (emergency != null) - { - //switch on emergency type here. Right now only contact and shutdown - var e = new EssentialsRoomEmergencyContactClosure(room.Key + "-emergency", props.Emergency, room); - DeviceManager.AddDevice(e); - } - return null; + if (emergency == null || room == null) return null; + + Debug.Console(0, @"emergency config values: +behavior: {0} +portDeviceKey: {1} +type: {2} +number: {3} +triggerOnClose: {4}", + emergency.Behavior, + emergency.Trigger.PortDeviceKey, + emergency.Trigger.Type, + emergency.Trigger.Number, + emergency.Trigger.TriggerOnClose); + + //switch on emergency type here. Right now only contact and shutdown + var e = new EssentialsRoomEmergencyContactClosure(room.Key + "-emergency", emergency, room); + DeviceManager.AddDevice(e); + return e; } /// diff --git a/PepperDashEssentials/Room/Config/EssentialsRoomEmergencyConfig.cs b/PepperDashEssentials/Room/Config/EssentialsRoomEmergencyConfig.cs index 69d04579..71090fa0 100644 --- a/PepperDashEssentials/Room/Config/EssentialsRoomEmergencyConfig.cs +++ b/PepperDashEssentials/Room/Config/EssentialsRoomEmergencyConfig.cs @@ -1,36 +1,41 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; - -namespace PepperDash.Essentials.Room.Config -{ - /// - /// - /// - public class EssentialsRoomEmergencyConfig - { - public EssentialsRoomEmergencyTriggerConfig Trigger { get; set; } - - public string Behavior { get; set; } - } - - /// - /// - /// - public class EssentialsRoomEmergencyTriggerConfig - { - /// - /// contact, - /// - public string Type { get; set; } - /// - /// Input number if contact - /// - public int Number { get; set; } - - public bool TriggerOnClose { get; set; } - - } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Essentials.Room.Config +{ + /// + /// + /// + public class EssentialsRoomEmergencyConfig + { + public EssentialsRoomEmergencyTriggerConfig Trigger { get; set; } + + public string Behavior { get; set; } + } + + /// + /// + /// + public class EssentialsRoomEmergencyTriggerConfig + { + /// + /// String representing the port device used to access the type + /// + public string PortDeviceKey { get; set; } + + /// + /// contact, + /// + public string Type { get; set; } + /// + /// Input number if contact + /// + public int Number { get; set; } + + public bool TriggerOnClose { get; set; } + + } } \ No newline at end of file diff --git a/PepperDashEssentials/Room/Emergency/EsentialsRoomEmergencyContactClosure.cs b/PepperDashEssentials/Room/Emergency/EsentialsRoomEmergencyContactClosure.cs index c40ed496..a4ee28c9 100644 --- a/PepperDashEssentials/Room/Emergency/EsentialsRoomEmergencyContactClosure.cs +++ b/PepperDashEssentials/Room/Emergency/EsentialsRoomEmergencyContactClosure.cs @@ -1,58 +1,94 @@ -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; -using PepperDash.Essentials.Core; -using PepperDash.Essentials.Room.Config; - -namespace PepperDash.Essentials.Room -{ - - - - public class EssentialsRoomEmergencyContactClosure : EssentialsRoomEmergencyBase - { - IEssentialsRoom Room; - string Behavior; - bool TriggerOnClose; - - public EssentialsRoomEmergencyContactClosure(string key, EssentialsRoomEmergencyConfig config, IEssentialsRoom room) : - base(key) - { - Room = room; - var cs = Global.ControlSystem; - - if (config.Trigger.Type.Equals("contact", StringComparison.OrdinalIgnoreCase)) - { - var portNum = (uint)config.Trigger.Number; - if (portNum <= cs.NumberOfDigitalInputPorts) - { - cs.DigitalInputPorts[portNum].Register(); - cs.DigitalInputPorts[portNum].StateChange += EsentialsRoomEmergencyContactClosure_StateChange; - } - } - Behavior = config.Behavior; - TriggerOnClose = config.Trigger.TriggerOnClose; - } - - void EsentialsRoomEmergencyContactClosure_StateChange(DigitalInput digitalInput, DigitalInputEventArgs args) - { - if (args.State && TriggerOnClose || !args.State && !TriggerOnClose) - RunEmergencyBehavior(); - } - - /// - /// - /// - public void RunEmergencyBehavior() - { - if (Behavior.Equals("shutdown")) - Room.Shutdown(); - } - } +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; +using PepperDash.Essentials.Core; +using PepperDash.Essentials.Room.Config; + +namespace PepperDash.Essentials.Room +{ + public class EssentialsRoomEmergencyContactClosure : EssentialsRoomEmergencyBase + { + IEssentialsRoom Room; + string Behavior; + bool TriggerOnClose; + + // TODO [ ] Issue #1071 - Emergency + public EssentialsRoomEmergencyContactClosure(string key, EssentialsRoomEmergencyConfig config, IEssentialsRoom room) : + base(key) + { + if (config == null || room == null) return; + + Room = room; + Behavior = config.Behavior; + TriggerOnClose = config.Trigger.TriggerOnClose; + + var cs = Global.ControlSystem; + + Debug.Console(0, this, "Control system supports digital inputs {0}", cs.SupportsDigitalInput); + Debug.Console(0, this, "Control system supports versiports {0}", cs.SupportsVersiport); + + Debug.Console(0, this, "------> type check"); + + if (string.IsNullOrEmpty(config.Trigger.Type)) + { + Debug.Console(0, this, "Emergency Contact Closure type is empty or null"); + return; + } + + if (!config.Trigger.Type.Equals("contact", StringComparison.OrdinalIgnoreCase)) + { + Debug.Console(0, this, "Emergency Contact Closure type is not 'contact'"); + return; + } + + Debug.Console(0, this, "------> type passed"); + Debug.Console(0, this, "------> portNum check"); + + var portNum = (uint)config.Trigger.Number; + if (portNum > cs.NumberOfDigitalInputPorts) return; + + Debug.Console(0, this, "------> portNum passed"); + Debug.Console(0, this, "------> port check"); + + var port = cs.DigitalInputPorts[portNum]; + if (port == null) + { + Debug.Console(0, this, "Control system does not support digital inputs"); + return; + } + + Debug.Console(0, this, "------> port passed"); + Debug.Console(0, this, "------> port register check"); + + port.Register(); + + Debug.Console(0, this, "------> port register passed"); + Debug.Console(0, this, "------> port event check"); + + port.StateChange += EsentialsRoomEmergencyContactClosure_StateChange; + + Debug.Console(0, this, "------> port event passed"); + } + + void EsentialsRoomEmergencyContactClosure_StateChange(DigitalInput digitalInput, DigitalInputEventArgs args) + { + if (args.State && TriggerOnClose || !args.State && !TriggerOnClose) + RunEmergencyBehavior(); + } + + /// + /// + /// + public void RunEmergencyBehavior() + { + if (Behavior.Equals("shutdown")) + Room.Shutdown(); + } + } } \ No newline at end of file