diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceJsonApi.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceJsonApi.cs index c7bc7c68..73fdd4a4 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceJsonApi.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceJsonApi.cs @@ -19,10 +19,36 @@ namespace PepperDash.Essentials.Core /// /// public static void DoDeviceActionWithJson(string json) - { - var action = JsonConvert.DeserializeObject(json); - DoDeviceAction(action); - } + { + try + { + var action = JsonConvert.DeserializeObject(json); + DoDeviceAction(action); + } + catch (Exception e) + { + Debug.Console(0, "Error attempting to execute device action: {0}", e); + } + } + + /// + /// + /// + /// + public static void InvokeDeviceActionWithJson(string json) + { + try + { + var action = JsonConvert.DeserializeObject(json); + InvokeDeviceAction(action); + } + catch (Exception e) + { + Debug.Console(0, "Error attempting to execute device action: {0}", e); + // Attempt to use the old method + DoDeviceActionWithJson(json); + } + } /// @@ -58,6 +84,61 @@ namespace PepperDash.Essentials.Core object ret = method.Invoke(obj, convertedParams); } + /// + /// + /// + /// + /// + /// + public static void InvokeDeviceAction(DeviceActionWrapperAdvanced action) + { + var key = action.DeviceKey; + var obj = FindObjectOnPath(key); + if (obj == null) + return; + + + MethodInfo method; + CType t = obj.GetType(); + if (action.Params == null) action.Params = new MethodParams[0]; + + if (action.Params.Length > 0) + { + // Get the types from the incoming data + CType[] types = new CType[action.Params.Length]; + for (int i = 0; i < action.Params.Length; i++) + { + types[i] = (CType)Type.GetType(action.Params[i].Type); + } + if (action.Params == null) action.Params = new MethodParams[0]; + method = t.GetMethod(action.MethodName, types); + } + else + { + // Get the method by name only + method = t.GetMethod(action.MethodName); + } + + if (method == null) + { + Debug.Console(0, "Method '{0}' not found", action.MethodName); + return; + } + + var mParams = method.GetParameters(); + // Add empty params if not provided + if (mParams.Length > action.Params.Length) + { + Debug.Console(0, "Method '{0}' requires {1} params", action.MethodName, mParams.Length); + return; + } + object[] convertedParams = mParams + .Select((p, i) => Convert.ChangeType(action.Params[i].Value, p.ParameterType, + System.Globalization.CultureInfo.InvariantCulture)) + .ToArray(); + object ret = method.Invoke(obj, convertedParams); + } + /// /// Gets the properties on a device /// @@ -251,6 +332,13 @@ namespace PepperDash.Essentials.Core public object[] Params { get; set; } } + public class DeviceActionWrapperAdvanced + { + public string DeviceKey { get; set; } + public string MethodName { get; set; } + public MethodParams[] Params { get; set; } + } + public class PropertyNameType { object Parent; @@ -310,6 +398,11 @@ namespace PepperDash.Essentials.Core public string Type { get; set; } } + public class MethodParams : NameType + { + public object Value { get; set; } + } + [AttributeUsage(AttributeTargets.All)] public class ApiAttribute : CAttribute { diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs index 6fa1f558..3db9437d 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/DeviceManager.cs @@ -35,20 +35,22 @@ namespace PepperDash.Essentials.Core ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(ListDevices, "devlist", "Lists current managed devices", ConsoleAccessLevelEnum.AccessOperator); - CrestronConsole.AddNewConsoleCommand(DeviceJsonApi.DoDeviceActionWithJson, "devjson", "", + CrestronConsole.AddNewConsoleCommand(DeviceJsonApi.DoDeviceActionWithJson, "devjson", "legacy command. Will be deprecated...", ConsoleAccessLevelEnum.AccessOperator); - CrestronConsole.AddNewConsoleCommand(s => + CrestronConsole.AddNewConsoleCommand(DeviceJsonApi.InvokeDeviceActionWithJson, "devinvoke", "Invokes a method on a device", + ConsoleAccessLevelEnum.AccessOperator); + CrestronConsole.AddNewConsoleCommand(s => { CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetProperties(s)); - }, "devprops", "", ConsoleAccessLevelEnum.AccessOperator); + }, "devprops", "gets the public properties on a device", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(s => { CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetMethods(s)); - }, "devmethods", "", ConsoleAccessLevelEnum.AccessOperator); + }, "devmethods", "gets the public methods on a device", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(s => { CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetApiMethods(s)); - }, "apimethods", "", ConsoleAccessLevelEnum.AccessOperator); + }, "apimethods", "gets the api methods on a device", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(SimulateComReceiveOnDevice, "devsimreceive", "Simulates incoming data on a com device", ConsoleAccessLevelEnum.AccessOperator); } diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Factory/DeviceFactory.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Factory/DeviceFactory.cs index 201675d0..f0808a87 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Factory/DeviceFactory.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Factory/DeviceFactory.cs @@ -30,7 +30,7 @@ namespace PepperDash.Essentials.Core public static void AddFactoryForType(string type, Func method) { Debug.Console(0, Debug.ErrorLogLevel.Notice, "Adding factory method for type '{0}'", type); - DeviceFactory.FactoryMethods.Add(type, method); + DeviceFactory.FactoryMethods.Add(type.ToLower(), method); } ///