From 1374afa2bbbb47abbf93b2d34480096986ea9e63 Mon Sep 17 00:00:00 2001 From: Trevor Payne Date: Wed, 12 Jul 2023 07:11:47 -0500 Subject: [PATCH] feature: add deveasy command to make DevJson easier in the development process --- .../Devices/DeviceJsonApi.cs | 48 +++++++++++++++++++ .../Devices/DeviceManager.cs | 5 ++ 2 files changed, 53 insertions(+) diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceJsonApi.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceJsonApi.cs index 2dda0bf8..2641f2ab 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceJsonApi.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceJsonApi.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using Crestron.SimplSharp; using Crestron.SimplSharp.Reflection; using Newtonsoft.Json; @@ -38,6 +39,53 @@ namespace PepperDash.Essentials.Core } } + private const string ParamsPattern = @"(?<=\[).*?(?=])"; + private const string DevKeySpacePattern = @"(?<=\().*?(?=\))"; + private const string MethodStringPattern = @"(?<=\)\s).*?(?=\s)"; + private static readonly Regex ParamsRegex = new Regex(ParamsPattern); + private static readonly Regex DevKeySpaceRegex = new Regex(DevKeySpacePattern); + private static readonly Regex MethodStringRegex = new Regex(MethodStringPattern); + + + + public static void BuildJsonForDeviceAction(string cmd) + { + if (String.IsNullOrEmpty(cmd)) + { + CrestronConsole.ConsoleCommandResponse( + "Please provide some data matching the format ' [parameters]' The parameters support strings, numbers, and booleans, and are optional. If your device key has spaces in it, wrap it in parenthesis"); + } + try + { + var paramsMatch = ParamsRegex.Match(cmd); + var devKeySpaceMatch = DevKeySpaceRegex.Match(cmd); + var methodStringMatch = MethodStringRegex.Match(cmd); + var deviceString = devKeySpaceMatch.Value ?? string.Empty; + var methodString = methodStringMatch.Value ?? string.Empty; + + var cmdParts = cmd.Split(' '); + if (cmdParts.Length <= 1) + { + CrestronConsole.ConsoleCommandResponse("Malformed 'deveasy' command - not enough arguments"); + return; + } + deviceString = String.IsNullOrEmpty(deviceString) ? cmdParts[0] : deviceString; + methodString = String.IsNullOrEmpty(methodString) ? cmdParts[1] : methodString; + var action = new DeviceActionWrapper() + { + DeviceKey = deviceString, + MethodName = methodString, + Params = (paramsMatch != null) ? new object[] { paramsMatch.Value } : new object[] { } + }; + + DoDeviceAction(action); + } + catch (Exception ex) + { + CrestronConsole.ConsoleCommandResponse("Malformed 'deveasy' command - consult command help."); + } + } + /// diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs index 55237496..0408a054 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs @@ -39,8 +39,13 @@ namespace PepperDash.Essentials.Core ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(ListDevices, "devlist", "Lists current managed devices", ConsoleAccessLevelEnum.AccessOperator); + CrestronConsole.AddNewConsoleCommand(DeviceJsonApi.BuildJsonForDeviceAction, "deveasy", + "Easier way to build devjson commands", + 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);