mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-15 12:44:58 +00:00
Merge pull request #707 from PepperDash/hotfix/devjson-overload-fix
fix:(EssentialsCore) Add ability for `devjson` command to handle overloads
This commit is contained in:
@@ -20,8 +20,23 @@ namespace PepperDash.Essentials.Core
|
|||||||
/// <param name="json"></param>
|
/// <param name="json"></param>
|
||||||
public static void DoDeviceActionWithJson(string json)
|
public static void DoDeviceActionWithJson(string json)
|
||||||
{
|
{
|
||||||
var action = JsonConvert.DeserializeObject<DeviceActionWrapper>(json);
|
if (String.IsNullOrEmpty(json))
|
||||||
DoDeviceAction(action);
|
{
|
||||||
|
CrestronConsole.ConsoleCommandResponse(
|
||||||
|
"Please provide a JSON object matching the format {\"deviceKey\":\"myDevice\", \"methodName\":\"someMethod\", \"params\": [\"param1\", true]}.\r\nIf the method has no parameters, the \"params\" object may be omitted.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var action = JsonConvert.DeserializeObject<DeviceActionWrapper>(json);
|
||||||
|
|
||||||
|
DoDeviceAction(action);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
CrestronConsole.ConsoleCommandResponse("Incorrect format for JSON. Please check that the format matches {\"deviceKey\":\"myDevice\", \"methodName\":\"someMethod\", \"params\": [\"param1\", true]}");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -33,29 +48,47 @@ namespace PepperDash.Essentials.Core
|
|||||||
{
|
{
|
||||||
var key = action.DeviceKey;
|
var key = action.DeviceKey;
|
||||||
var obj = FindObjectOnPath(key);
|
var obj = FindObjectOnPath(key);
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
return;
|
{
|
||||||
|
CrestronConsole.ConsoleCommandResponse("Unable to find object at path {0}", key);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
CType t = obj.GetType();
|
if (action.Params == null)
|
||||||
var method = t.GetMethod(action.MethodName);
|
{
|
||||||
if (method == null)
|
//no params, so setting action.Params to empty array
|
||||||
{
|
action.Params = new object[0];
|
||||||
Debug.Console(0, "Method '{0}' not found", action.MethodName);
|
}
|
||||||
return;
|
|
||||||
}
|
CType t = obj.GetType();
|
||||||
var mParams = method.GetParameters();
|
try
|
||||||
// Add empty params if not provided
|
{
|
||||||
if (action.Params == null) action.Params = new object[0];
|
var methods = t.GetMethods().Where(m => m.Name == action.MethodName).ToList();
|
||||||
if (mParams.Length > action.Params.Length)
|
|
||||||
{
|
var method = methods.Count == 1 ? methods[0] : methods.FirstOrDefault(m => m.GetParameters().Length == action.Params.Length);
|
||||||
Debug.Console(0, "Method '{0}' requires {1} params", action.MethodName, mParams.Length);
|
|
||||||
return;
|
if (method == null)
|
||||||
}
|
{
|
||||||
object[] convertedParams = mParams
|
CrestronConsole.ConsoleCommandResponse(
|
||||||
.Select((p, i) => Convert.ChangeType(action.Params[i], p.ParameterType,
|
"Unable to find method with name {0} and that matches parameters {1}", action.MethodName,
|
||||||
System.Globalization.CultureInfo.InvariantCulture))
|
action.Params);
|
||||||
.ToArray();
|
return;
|
||||||
object ret = method.Invoke(obj, convertedParams);
|
}
|
||||||
|
var mParams = method.GetParameters();
|
||||||
|
|
||||||
|
var convertedParams = mParams
|
||||||
|
.Select((p, i) => Convert.ChangeType(action.Params[i], p.ParameterType,
|
||||||
|
System.Globalization.CultureInfo.InvariantCulture))
|
||||||
|
.ToArray();
|
||||||
|
var ret = method.Invoke(obj, convertedParams);
|
||||||
|
|
||||||
|
CrestronConsole.ConsoleCommandResponse("Method {0} successfully called on device {1}", method.Name,
|
||||||
|
action.DeviceKey);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
CrestronConsole.ConsoleCommandResponse("Unable to call method with name {0}. {1}", action.MethodName,
|
||||||
|
ex.Message);}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user