mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-15 12:44:58 +00:00
feat: add async method for devjson
This commit is contained in:
@@ -1,17 +1,15 @@
|
|||||||
|
|
||||||
|
|
||||||
|
using Crestron.SimplSharp;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using PepperDash.Core;
|
||||||
|
using Serilog.Events;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Crestron.SimplSharp;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Newtonsoft.Json;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using PepperDash.Core;
|
|
||||||
using Serilog.Events;
|
|
||||||
|
|
||||||
|
|
||||||
namespace PepperDash.Essentials.Core
|
namespace PepperDash.Essentials.Core
|
||||||
@@ -36,7 +34,7 @@ namespace PepperDash.Essentials.Core
|
|||||||
|
|
||||||
DoDeviceAction(action);
|
DoDeviceAction(action);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
CrestronConsole.ConsoleCommandResponse("Incorrect format for JSON. Please check that the format matches {\"deviceKey\":\"myDevice\", \"methodName\":\"someMethod\", \"params\": [\"param1\", true]}");
|
CrestronConsole.ConsoleCommandResponse("Incorrect format for JSON. Please check that the format matches {\"deviceKey\":\"myDevice\", \"methodName\":\"someMethod\", \"params\": [\"param1\", true]}");
|
||||||
}
|
}
|
||||||
@@ -103,7 +101,66 @@ namespace PepperDash.Essentials.Core
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
CrestronConsole.ConsoleCommandResponse("Unable to call method with name {0}. {1}", action.MethodName,
|
CrestronConsole.ConsoleCommandResponse("Unable to call method with name {0}. {1}", action.MethodName,
|
||||||
ex.Message);}
|
ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task DoDeviceActionAsync(DeviceActionWrapper action)
|
||||||
|
{
|
||||||
|
var key = action.DeviceKey;
|
||||||
|
var obj = FindObjectOnPath(key);
|
||||||
|
if (obj == null)
|
||||||
|
{
|
||||||
|
Debug.LogMessage(LogEventLevel.Warning, "Unable to find object at path {deviceKey}", null, key);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action.Params == null)
|
||||||
|
{
|
||||||
|
//no params, so setting action.Params to empty array
|
||||||
|
action.Params = new object[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
Type t = obj.GetType();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var methods = t.GetMethods().Where(m => m.Name == action.MethodName).ToList();
|
||||||
|
|
||||||
|
var method = methods.Count == 1 ? methods[0] : methods.FirstOrDefault(m => m.GetParameters().Length == action.Params.Length);
|
||||||
|
|
||||||
|
if (method == null)
|
||||||
|
{
|
||||||
|
Debug.LogMessage(LogEventLevel.Warning,
|
||||||
|
"Unable to find method with name {methodName} and that matches parameters {@parameters}", null, action.MethodName,
|
||||||
|
action.Params);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var mParams = method.GetParameters();
|
||||||
|
|
||||||
|
var convertedParams = mParams
|
||||||
|
.Select((p, i) => ConvertType(action.Params[i], p.ParameterType))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
await Task.Run(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Debug.LogMessage(LogEventLevel.Verbose, "Calling method {methodName} on device {deviceKey} with {@params}", null, method.Name, action.DeviceKey, action.Params);
|
||||||
|
method.Invoke(obj, convertedParams);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.LogMessage(e, "Error invoking method {methodName} on device {deviceKey}", null, method.Name, action.DeviceKey);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Debug.LogMessage(LogEventLevel.Information, "Method {methodName} successfully called on device {deviceKey} with {@params}", null, method.Name,
|
||||||
|
action.DeviceKey, action.Params);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Debug.LogMessage(ex, "Unable to call method with name {methodName} with {@parameters}", null, action.MethodName, action.Params);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static object ConvertType(object value, Type conversionType)
|
private static object ConvertType(object value, Type conversionType)
|
||||||
@@ -320,13 +377,15 @@ namespace PepperDash.Essentials.Core
|
|||||||
|
|
||||||
public class PropertyNameType
|
public class PropertyNameType
|
||||||
{
|
{
|
||||||
object Parent;
|
private object Parent;
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public PropertyInfo PropInfo { get; private set; }
|
public PropertyInfo PropInfo { get; private set; }
|
||||||
public string Name { get { return PropInfo.Name; } }
|
public string Name { get { return PropInfo.Name; } }
|
||||||
public string Type { get { return PropInfo.PropertyType.Name; } }
|
public string Type { get { return PropInfo.PropertyType.Name; } }
|
||||||
public string Value { get
|
public string Value
|
||||||
|
{
|
||||||
|
get
|
||||||
{
|
{
|
||||||
if (PropInfo.CanRead)
|
if (PropInfo.CanRead)
|
||||||
{
|
{
|
||||||
@@ -341,7 +400,8 @@ namespace PepperDash.Essentials.Core
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
} }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool CanRead { get { return PropInfo.CanRead; } }
|
public bool CanRead { get { return PropInfo.CanRead; } }
|
||||||
public bool CanWrite { get { return PropInfo.CanWrite; } }
|
public bool CanWrite { get { return PropInfo.CanWrite; } }
|
||||||
@@ -360,10 +420,14 @@ namespace PepperDash.Essentials.Core
|
|||||||
public MethodInfo MethodInfo { get; private set; }
|
public MethodInfo MethodInfo { get; private set; }
|
||||||
|
|
||||||
public string Name { get { return MethodInfo.Name; } }
|
public string Name { get { return MethodInfo.Name; } }
|
||||||
public IEnumerable<NameType> Params { get {
|
public IEnumerable<NameType> Params
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
return MethodInfo.GetParameters().Select(p =>
|
return MethodInfo.GetParameters().Select(p =>
|
||||||
new NameType { Name = p.Name, Type = p.ParameterType.Name });
|
new NameType { Name = p.Name, Type = p.ParameterType.Name });
|
||||||
} }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public MethodNameParams(MethodInfo info)
|
public MethodNameParams(MethodInfo info)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user