Compare commits

..

6 Commits

Author SHA1 Message Date
Neil Dorin
276aa11710 Adds new devinvoke console command to handle overloaded methods 2020-04-08 22:06:45 -06:00
Andrew Welker
da50272980 Merge pull request #88 from PepperDash/feature/fix-get-name-attribute-issue
Feature/fix get name attribute issue
2020-04-05 09:25:28 -06:00
Neil Dorin
c8f095f0a3 Updates CameraControllerJoinMap to use new constructor for base class 2020-04-05 09:07:05 -06:00
Andrew Welker
42fbd813a2 updated methods for creating joinMap dict 2020-04-04 22:06:56 -06:00
Andrew Welker
69f5460442 trying to fix JoinMapBaseAdvanced 2020-04-04 21:09:00 -06:00
Neil Dorin
b32212083d Adds debug statment to detect if JoinNameAttribute constructor has been called 2020-04-04 14:44:43 -06:00
6 changed files with 154 additions and 64 deletions

View File

@@ -18,7 +18,7 @@ namespace PepperDash.Essentials.Bridges
public static void LinkToApi(this PepperDash.Essentials.Devices.Common.Cameras.CameraBase cameraDevice, BasicTriList trilist, uint joinStart, string joinMapKey, EiscApi bridge)
{
CameraControllerJoinMap joinMap = new CameraControllerJoinMap(joinStart);
// Adds the join map to the bridge
bridge.AddJoinMap(cameraDevice.Key, joinMap);

View File

@@ -57,7 +57,7 @@ namespace PepperDash.Essentials.Bridges
public JoinDataComplete SupportsPresets = new JoinDataComplete(new JoinData() { JoinNumber = 57, JoinSpan = 1 }, new JoinMetadata() { Label = "Supports Presets", JoinCapabilities = eJoinCapabilities.FromSIMPL, JoinType = eJoinType.Digital });
public CameraControllerJoinMap(uint joinStart)
:base(joinStart)
: base(joinStart, typeof(CameraControllerJoinMap))
{
}
}

View File

@@ -19,10 +19,36 @@ namespace PepperDash.Essentials.Core
/// </summary>
/// <param name="json"></param>
public static void DoDeviceActionWithJson(string json)
{
var action = JsonConvert.DeserializeObject<DeviceActionWrapper>(json);
DoDeviceAction(action);
}
{
try
{
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>
@@ -58,6 +84,61 @@ namespace PepperDash.Essentials.Core
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>
/// Gets the properties on a device
/// </summary>
@@ -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
{

View File

@@ -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);
}

View File

@@ -30,7 +30,7 @@ namespace PepperDash.Essentials.Core
public static void AddFactoryForType(string type, Func<DeviceConfig, IKeyed> 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);
}
/// <summary>

View File

@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using Crestron.SimplSharp.Reflection;
using PepperDash.Core;
@@ -26,12 +24,7 @@ namespace PepperDash.Essentials.Core
var joinMap = ConfigReader.ConfigObject.JoinMaps[joinMapKey];
if (joinMap != null)
{
return joinMap;
}
else
return null;
return joinMap;
}
/// <summary>
@@ -46,17 +39,11 @@ namespace PepperDash.Essentials.Core
var joinMapSerialzed = ConfigReader.ConfigObject.JoinMaps[joinMapKey];
if (joinMapSerialzed != null)
{
var joinMapData = JsonConvert.DeserializeObject<Dictionary<string, JoinData>>(joinMapSerialzed);
if (joinMapSerialzed == null) return null;
if (joinMapData != null)
return joinMapData;
else
return null;
}
else
return null;
var joinMapData = JsonConvert.DeserializeObject<Dictionary<string, JoinData>>(joinMapSerialzed);
return joinMapData;
}
}
@@ -83,7 +70,7 @@ namespace PepperDash.Essentials.Core
/// </summary>
public void PrintJoinMapInfo()
{
Debug.Console(0, "{0}:\n", this.GetType().Name);
Debug.Console(0, "{0}:\n", GetType().Name);
// Get the joins of each type and print them
Debug.Console(0, "Digitals:");
@@ -138,10 +125,7 @@ namespace PepperDash.Essentials.Core
/// <returns></returns>
public uint GetJoinForKey(string key)
{
if (Joins.ContainsKey(key))
return Joins[key].JoinNumber;
else return 0;
return Joins.ContainsKey(key) ? Joins[key].JoinNumber : 0;
}
/// <summary>
@@ -151,12 +135,8 @@ namespace PepperDash.Essentials.Core
/// <returns></returns>
public uint GetJoinSpanForKey(string key)
{
if (Joins.ContainsKey(key))
return Joins[key].JoinSpan;
else return 0;
return Joins.ContainsKey(key) ? Joins[key].JoinSpan : 0;
}
}
/// <summary>
@@ -164,7 +144,7 @@ namespace PepperDash.Essentials.Core
/// </summary>
public abstract class JoinMapBaseAdvanced
{
protected uint _joinOffset;
protected uint JoinOffset;
/// <summary>
/// The collection of joins and associated metadata
@@ -175,11 +155,15 @@ namespace PepperDash.Essentials.Core
{
Joins = new Dictionary<string, JoinDataComplete>();
_joinOffset = joinStart - 1;
JoinOffset = joinStart - 1;
}
protected void AddJoins()
protected JoinMapBaseAdvanced(uint joinStart, Type type):this(joinStart)
{
AddJoins(type);
}
protected void AddJoins(Type type)
{
// Add all the JoinDataComplete properties to the Joins Dictionary and pass in the offset
//Joins = this.GetType()
@@ -193,21 +177,31 @@ namespace PepperDash.Essentials.Core
// return join;
// });
var type = this.GetType();
var cType = type.GetCType();
var fields = cType.GetFields(BindingFlags.Public | BindingFlags.Instance);
//type = this.GetType(); <- this wasn't working because 'this' was always the base class, never the derived class
var fields =
type.GetCType()
.GetFields(BindingFlags.Public | BindingFlags.Instance)
.Where(f => f.IsDefined(typeof (JoinNameAttribute), true));
foreach (var field in fields)
{
if (field.IsDefined(typeof(JoinNameAttribute), true))
{
JoinDataComplete value = field.GetValue(this) as JoinDataComplete;
var childClass = Convert.ChangeType(this, type, null);
if (value != null)
{
value.SetJoinOffset(_joinOffset);
Joins.Add(value.GetNameAttribute(typeof(JoinDataComplete)), value);
}
var value = field.GetValue(childClass) as JoinDataComplete; //this here is JoinMapBaseAdvanced, not the child class. JoinMapBaseAdvanced has no fields.
if (value == null)
{
Debug.Console(0, "Unable to caset base class to {0}", type.Name);
continue;
}
value.SetJoinOffset(JoinOffset);
var joinName = value.GetNameAttribute(field);
if (String.IsNullOrEmpty(joinName)) continue;
Joins.Add(joinName, value);
}
@@ -219,7 +213,7 @@ namespace PepperDash.Essentials.Core
/// </summary>
public void PrintJoinMapInfo()
{
Debug.Console(0, "{0}:\n", this.GetType().Name);
Debug.Console(0, "{0}:\n", GetType().Name);
// Get the joins of each type and print them
Debug.Console(0, "Digitals:");
@@ -440,15 +434,15 @@ namespace PepperDash.Essentials.Core
_data = customJoinData;
}
public string GetNameAttribute(Type t)
public string GetNameAttribute(MemberInfo memberInfo)
{
string name = string.Empty;
JoinNameAttribute attribute = (JoinNameAttribute)Attribute.GetCustomAttribute(t, typeof(JoinNameAttribute));
if (attribute != null)
{
name = attribute.Name;
Debug.Console(2, "JoinName Attribute value: {0}", name);
}
var name = string.Empty;
var attribute = (JoinNameAttribute)CAttribute.GetCustomAttribute(memberInfo, typeof(JoinNameAttribute));
if (attribute == null) return name;
name = attribute.Name;
Debug.Console(2, "JoinName Attribute value: {0}", name);
return name;
}
}
@@ -460,6 +454,7 @@ namespace PepperDash.Essentials.Core
public JoinNameAttribute(string name)
{
Debug.Console(2, "Setting Attribute Name: {0}", name);
_Name = name;
}