mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-16 13:15:03 +00:00
Adds new devinvoke console command to handle overloaded methods
This commit is contained in:
@@ -19,10 +19,36 @@ namespace PepperDash.Essentials.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <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);
|
try
|
||||||
DoDeviceAction(action);
|
{
|
||||||
}
|
var action = JsonConvert.DeserializeObject<DeviceActionWrapper>(json);
|
||||||
|
DoDeviceAction(action);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.Console(0, "Error attempting to execute device action: {0}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="json"></param>
|
||||||
|
public static void InvokeDeviceActionWithJson(string json)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var action = JsonConvert.DeserializeObject<DeviceActionWrapperAdvanced>(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -58,6 +84,61 @@ namespace PepperDash.Essentials.Core
|
|||||||
object ret = method.Invoke(obj, convertedParams);
|
object ret = method.Invoke(obj, convertedParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="methodName"></param>
|
||||||
|
/// <param name="type"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the properties on a device
|
/// Gets the properties on a device
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -251,6 +332,13 @@ namespace PepperDash.Essentials.Core
|
|||||||
public object[] Params { get; set; }
|
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
|
public class PropertyNameType
|
||||||
{
|
{
|
||||||
object Parent;
|
object Parent;
|
||||||
@@ -310,6 +398,11 @@ namespace PepperDash.Essentials.Core
|
|||||||
public string Type { get; set; }
|
public string Type { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class MethodParams : NameType
|
||||||
|
{
|
||||||
|
public object Value { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.All)]
|
[AttributeUsage(AttributeTargets.All)]
|
||||||
public class ApiAttribute : CAttribute
|
public class ApiAttribute : CAttribute
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -35,20 +35,22 @@ namespace PepperDash.Essentials.Core
|
|||||||
ConsoleAccessLevelEnum.AccessOperator);
|
ConsoleAccessLevelEnum.AccessOperator);
|
||||||
CrestronConsole.AddNewConsoleCommand(ListDevices, "devlist", "Lists current managed devices",
|
CrestronConsole.AddNewConsoleCommand(ListDevices, "devlist", "Lists current managed devices",
|
||||||
ConsoleAccessLevelEnum.AccessOperator);
|
ConsoleAccessLevelEnum.AccessOperator);
|
||||||
CrestronConsole.AddNewConsoleCommand(DeviceJsonApi.DoDeviceActionWithJson, "devjson", "",
|
CrestronConsole.AddNewConsoleCommand(DeviceJsonApi.DoDeviceActionWithJson, "devjson", "legacy command. Will be deprecated...",
|
||||||
ConsoleAccessLevelEnum.AccessOperator);
|
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));
|
CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetProperties(s));
|
||||||
}, "devprops", "", ConsoleAccessLevelEnum.AccessOperator);
|
}, "devprops", "gets the public properties on a device", ConsoleAccessLevelEnum.AccessOperator);
|
||||||
CrestronConsole.AddNewConsoleCommand(s =>
|
CrestronConsole.AddNewConsoleCommand(s =>
|
||||||
{
|
{
|
||||||
CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetMethods(s));
|
CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetMethods(s));
|
||||||
}, "devmethods", "", ConsoleAccessLevelEnum.AccessOperator);
|
}, "devmethods", "gets the public methods on a device", ConsoleAccessLevelEnum.AccessOperator);
|
||||||
CrestronConsole.AddNewConsoleCommand(s =>
|
CrestronConsole.AddNewConsoleCommand(s =>
|
||||||
{
|
{
|
||||||
CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetApiMethods(s));
|
CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetApiMethods(s));
|
||||||
}, "apimethods", "", ConsoleAccessLevelEnum.AccessOperator);
|
}, "apimethods", "gets the api methods on a device", ConsoleAccessLevelEnum.AccessOperator);
|
||||||
CrestronConsole.AddNewConsoleCommand(SimulateComReceiveOnDevice, "devsimreceive",
|
CrestronConsole.AddNewConsoleCommand(SimulateComReceiveOnDevice, "devsimreceive",
|
||||||
"Simulates incoming data on a com device", ConsoleAccessLevelEnum.AccessOperator);
|
"Simulates incoming data on a com device", ConsoleAccessLevelEnum.AccessOperator);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ namespace PepperDash.Essentials.Core
|
|||||||
public static void AddFactoryForType(string type, Func<DeviceConfig, IKeyed> method)
|
public static void AddFactoryForType(string type, Func<DeviceConfig, IKeyed> method)
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Adding factory method for type '{0}'", type);
|
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Adding factory method for type '{0}'", type);
|
||||||
DeviceFactory.FactoryMethods.Add(type, method);
|
DeviceFactory.FactoryMethods.Add(type.ToLower(), method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user