From e50a71408fb517e2aa1d81af5e68ecbf77f9275f Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Wed, 6 May 2020 09:51:25 -0600 Subject: [PATCH] adds protections for devices doing things they shouldn't... --- PepperDashEssentials/ControlSystem.cs | 17 +- .../Devices/DeviceManager.cs | 173 +++++++----------- 2 files changed, 79 insertions(+), 111 deletions(-) diff --git a/PepperDashEssentials/ControlSystem.cs b/PepperDashEssentials/ControlSystem.cs index f0cdc9d5..00748815 100644 --- a/PepperDashEssentials/ControlSystem.cs +++ b/PepperDashEssentials/ControlSystem.cs @@ -26,6 +26,8 @@ namespace PepperDash.Essentials { HttpLogoServer LogoServer; + private CTimer _startTimer; + private const long StartupTime = 500; public ControlSystem() : base() @@ -39,6 +41,11 @@ namespace PepperDash.Essentials /// Entry point for the program /// public override void InitializeSystem() + { + _startTimer = new CTimer(StartSystem,StartupTime); + } + + private void StartSystem(object obj) { DeterminePlatform(); @@ -75,11 +82,11 @@ namespace PepperDash.Essentials }, "showconfig", "Shows the current running merged config", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(s => - { - CrestronConsole.ConsoleCommandResponse("This system can be found at the following URLs:\r" + - "System URL: {0}\r" + - "Template URL: {1}", ConfigReader.ConfigObject.SystemUrl, ConfigReader.ConfigObject.TemplateUrl); - }, "portalinfo", "Shows portal URLS from configuration", ConsoleAccessLevelEnum.AccessOperator); + { + CrestronConsole.ConsoleCommandResponse("This system can be found at the following URLs:\r" + + "System URL: {0}\r" + + "Template URL: {1}", ConfigReader.ConfigObject.SystemUrl, ConfigReader.ConfigObject.TemplateUrl); + }, "portalinfo", "Shows portal URLS from configuration", ConsoleAccessLevelEnum.AccessOperator); if (!Debug.DoNotLoadOnNextBoot) diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs index 2f37b016..01563db7 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs @@ -5,10 +5,6 @@ using System.Text; using System.Text.RegularExpressions; using Crestron.SimplSharp; using Crestron.SimplSharpPro; -using Crestron.SimplSharpPro.DeviceSupport; -using Crestron.SimplSharpPro.EthernetCommunication; -using Crestron.SimplSharpPro.UI; -using Crestron.SimplSharp.Reflection; using PepperDash.Core; @@ -18,18 +14,22 @@ namespace PepperDash.Essentials.Core public static class DeviceManager { private static readonly CCriticalSection DeviceCriticalSection = new CCriticalSection(); + private static readonly CEvent AllowAddDevicesCEvent = new CEvent(false, true); //public static List Devices { get { return _Devices; } } //static List _Devices = new List(); - static Dictionary Devices = new Dictionary(StringComparer.OrdinalIgnoreCase); + static readonly Dictionary Devices = new Dictionary(StringComparer.OrdinalIgnoreCase); /// /// Returns a copy of all the devices in a list /// public static List AllDevices { get { return new List(Devices.Values); } } + public static bool AddDeviceEnabled; + public static void Initialize(CrestronControlSystem cs) { + AddDeviceEnabled = true; CrestronConsole.AddNewConsoleCommand(ListDeviceCommStatuses, "devcommstatus", "Lists the communication status of all devices", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(ListDeviceFeedbacks, "devfb", "Lists current feedbacks", @@ -38,18 +38,9 @@ namespace PepperDash.Essentials.Core ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(DeviceJsonApi.DoDeviceActionWithJson, "devjson", "", ConsoleAccessLevelEnum.AccessOperator); - CrestronConsole.AddNewConsoleCommand(s => - { - CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetProperties(s)); - }, "devprops", "", ConsoleAccessLevelEnum.AccessOperator); - CrestronConsole.AddNewConsoleCommand(s => - { - CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetMethods(s)); - }, "devmethods", "", ConsoleAccessLevelEnum.AccessOperator); - CrestronConsole.AddNewConsoleCommand(s => - { - CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetApiMethods(s)); - }, "apimethods", "", ConsoleAccessLevelEnum.AccessOperator); + CrestronConsole.AddNewConsoleCommand(s => CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetProperties(s)), "devprops", "", ConsoleAccessLevelEnum.AccessOperator); + CrestronConsole.AddNewConsoleCommand(s => CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetMethods(s)), "devmethods", "", ConsoleAccessLevelEnum.AccessOperator); + CrestronConsole.AddNewConsoleCommand(s => CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetApiMethods(s)), "apimethods", "", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(SimulateComReceiveOnDevice, "devsimreceive", "Simulates incoming data on a com device", ConsoleAccessLevelEnum.AccessOperator); } @@ -62,6 +53,7 @@ namespace PepperDash.Essentials.Core try { DeviceCriticalSection.Enter(); + AddDeviceEnabled = false; // PreActivate all devices foreach (var d in Devices.Values) { @@ -118,10 +110,9 @@ namespace PepperDash.Essentials.Core try { DeviceCriticalSection.Enter(); - foreach (var d in Devices.Values) + foreach (var d in Devices.Values.OfType()) { - if (d is Device) - (d as Device).Deactivate(); + d.Deactivate(); } } finally @@ -151,50 +142,37 @@ namespace PepperDash.Essentials.Core // } //} - static void ListDevices(string s) - { - try - { - DeviceCriticalSection.Enter(); - Debug.Console(0, "{0} Devices registered with Device Manager:", Devices.Count); - var sorted = Devices.Values.ToList(); - sorted.Sort((a, b) => a.Key.CompareTo(b.Key)); + private static void ListDevices(string s) + { + Debug.Console(0, "{0} Devices registered with Device Manager:", Devices.Count); + var sorted = Devices.Values.ToList(); + sorted.Sort((a, b) => a.Key.CompareTo(b.Key)); - foreach (var d in sorted) - { - var name = d is IKeyName ? (d as IKeyName).Name : "---"; - Debug.Console(0, " [{0}] {1}", d.Key, name); - } - } - finally {DeviceCriticalSection.Leave();} - } + foreach (var d in sorted) + { + var name = d is IKeyName ? (d as IKeyName).Name : "---"; + Debug.Console(0, " [{0}] {1}", d.Key, name); + } + } - static void ListDeviceFeedbacks(string devKey) - { - try - { - DeviceCriticalSection.Enter(); - var dev = GetDeviceForKey(devKey); - if (dev == null) - { - Debug.Console(0, "Device '{0}' not found", devKey); - return; - } - var statusDev = dev as IHasFeedback; - if (statusDev == null) - { - Debug.Console(0, "Device '{0}' does not have visible feedbacks", devKey); - return; - } - statusDev.DumpFeedbacksToConsole(true); - } - finally - { - DeviceCriticalSection.Leave(); - } - } + private static void ListDeviceFeedbacks(string devKey) + { + var dev = GetDeviceForKey(devKey); + if (dev == null) + { + Debug.Console(0, "Device '{0}' not found", devKey); + return; + } + var statusDev = dev as IHasFeedback; + if (statusDev == null) + { + Debug.Console(0, "Device '{0}' does not have visible feedbacks", devKey); + return; + } + statusDev.DumpFeedbacksToConsole(true); + } - //static void ListDeviceCommands(string devKey) + //static void ListDeviceCommands(string devKey) //{ // var dev = GetDeviceForKey(devKey); // if (dev == null) @@ -205,29 +183,19 @@ namespace PepperDash.Essentials.Core // Debug.Console(0, "This needs to be reworked. Stay tuned.", devKey); //} - static void ListDeviceCommStatuses(string input) - { - try - { - DeviceCriticalSection.Enter(); - - var sb = new StringBuilder(); - foreach (var dev in Devices.Values) - { - if (dev is ICommunicationMonitor) - sb.Append(string.Format("{0}: {1}\r", dev.Key, - (dev as ICommunicationMonitor).CommunicationMonitor.Status)); - } - CrestronConsole.ConsoleCommandResponse(sb.ToString()); - } - finally - { - DeviceCriticalSection.Leave(); - } - } + private static void ListDeviceCommStatuses(string input) + { + var sb = new StringBuilder(); + foreach (var dev in Devices.Values.OfType()) + { + sb.Append(string.Format("{0}: {1}\r", dev, + dev.CommunicationMonitor.Status)); + } + CrestronConsole.ConsoleCommandResponse(sb.ToString()); + } - //static void DoDeviceCommand(string command) + //static void DoDeviceCommand(string command) //{ // Debug.Console(0, "Not yet implemented. Stay tuned"); //} @@ -236,11 +204,22 @@ namespace PepperDash.Essentials.Core { try { - DeviceCriticalSection.Enter(); + if (!DeviceCriticalSection.TryEnter()) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "Currently unable to add devices to Device Manager. Please try again"); + return; + } // Check for device with same key //var existingDevice = _Devices.FirstOrDefault(d => d.Key.Equals(newDev.Key, StringComparison.OrdinalIgnoreCase)); ////// If it exists, remove or warn?? //if (existingDevice != null) + + if (!AddDeviceEnabled) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "All devices have been activated. Adding new devices is not allowed."); + return; + } + if (Devices.ContainsKey(newDev.Key)) { Debug.Console(0, newDev, "WARNING: A device with this key already exists. Not added to manager"); @@ -250,7 +229,10 @@ namespace PepperDash.Essentials.Core //if (!(_Devices.Contains(newDev))) // _Devices.Add(newDev); } - finally {DeviceCriticalSection.Leave();} + finally + { + DeviceCriticalSection.Leave(); + } } public static void RemoveDevice(IKeyed newDev) @@ -275,44 +257,23 @@ namespace PepperDash.Essentials.Core public static IEnumerable GetDeviceKeys() { - try - { - DeviceCriticalSection.Enter(); //return _Devices.Select(d => d.Key).ToList(); return Devices.Keys; - } - finally - { - DeviceCriticalSection.Leave(); - } } public static IEnumerable GetDevices() { - try - { - DeviceCriticalSection.Enter(); //return _Devices.Select(d => d.Key).ToList(); return Devices.Values; - } - finally {DeviceCriticalSection.Leave();} } public static IKeyed GetDeviceForKey(string key) { - try - { - DeviceCriticalSection.Enter(); //return _Devices.FirstOrDefault(d => d.Key.Equals(key, StringComparison.OrdinalIgnoreCase)); if (key != null && Devices.ContainsKey(key)) return Devices[key]; return null; - } - finally - { - DeviceCriticalSection.Leave(); - } } /// @@ -330,7 +291,7 @@ namespace PepperDash.Essentials.Core } //Debug.Console(2, "**** {0} - {1} ****", match.Groups[1].Value, match.Groups[2].Value); - ComPortController com = GetDeviceForKey(match.Groups[1].Value) as ComPortController; + var com = GetDeviceForKey(match.Groups[1].Value) as ComPortController; if (com == null) { CrestronConsole.ConsoleCommandResponse("'{0}' is not a comm port device", match.Groups[1].Value);