From 88702f97c65ea167d6a8524a588653d25e4a4bc0 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Sat, 4 Apr 2020 14:12:49 -0600 Subject: [PATCH 1/5] moves logic to add joins out of the constructor and into new AddJoins() method to allow it to be called from extended class after fields have been initialized. --- .../JoinMaps/JoinMapBase.cs | 73 +++++++++++-------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs index fbbad609..7a3e242d 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs @@ -177,34 +177,39 @@ namespace PepperDash.Essentials.Core _joinOffset = joinStart - 1; + } + + protected void AddJoins() + { // Add all the JoinDataComplete properties to the Joins Dictionary and pass in the offset - Joins = this.GetType() - .GetCType() - .GetFields(BindingFlags.Public | BindingFlags.Instance) - .Where(field => field.IsDefined(typeof(JoinNameAttribute), false)) - .Select(prop => (JoinDataComplete)prop.GetValue(this)) - .ToDictionary(join => join.GetNameAttribute(), join => - { - join.SetJoinOffset(_joinOffset); - return join; - }); - - //var type = this.GetType(); - //var cType = type.GetCType(); - //var fields = cType.GetFields(BindingFlags.Public | BindingFlags.Instance); - //foreach (var field in fields) - //{ - // if (field.IsDefined(typeof(JoinNameAttribute), true)) - // { - // var value = field.GetValue(this) as JoinDataComplete; - - // if (value != null) + //Joins = this.GetType() + // .GetCType() + // .GetFields(BindingFlags.Public | BindingFlags.Instance) + // .Where(field => field.IsDefined(typeof(JoinNameAttribute), true)) + // .Select(field => (JoinDataComplete)field.GetValue(this)) + // .ToDictionary(join => join.GetNameAttribute(), join => // { - // value.SetJoinOffset(_joinOffset); - // Joins.Add(value.GetNameAttribute(), value); - // } - // } - //} + // join.SetJoinOffset(_joinOffset); + // return join; + // }); + + var type = this.GetType(); + var cType = type.GetCType(); + var fields = cType.GetFields(BindingFlags.Public | BindingFlags.Instance); + foreach (var field in fields) + { + if (field.IsDefined(typeof(JoinNameAttribute), true)) + { + JoinDataComplete value = field.GetValue(this) as JoinDataComplete; + + if (value != null) + { + value.SetJoinOffset(_joinOffset); + Joins.Add(value.GetNameAttribute(typeof(JoinDataComplete)), value); + } + } + } + PrintJoinMapInfo(); } @@ -435,26 +440,32 @@ namespace PepperDash.Essentials.Core _data = customJoinData; } - public string GetNameAttribute() + public string GetNameAttribute(Type t) { string name = string.Empty; - JoinNameAttribute attribute = (JoinNameAttribute)Attribute.GetCustomAttribute(typeof(JoinDataComplete), typeof(JoinNameAttribute)); + JoinNameAttribute attribute = (JoinNameAttribute)Attribute.GetCustomAttribute(t, typeof(JoinNameAttribute)); if (attribute != null) { name = attribute.Name; + Debug.Console(2, "JoinName Attribute value: {0}", name); } return name; } } - [AttributeUsage(AttributeTargets.Field)] + [AttributeUsage(AttributeTargets.All)] public class JoinNameAttribute : Attribute { - public string Name { get; set; } + private string _Name; public JoinNameAttribute(string name) { - Name = name; + _Name = name; + } + + public string Name + { + get { return _Name; } } } } \ No newline at end of file From b32212083da598c0f464de801fb4e9bdfa0655e7 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Sat, 4 Apr 2020 14:44:43 -0600 Subject: [PATCH 2/5] Adds debug statment to detect if JoinNameAttribute constructor has been called --- .../PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs index 7a3e242d..82694610 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs @@ -460,6 +460,7 @@ namespace PepperDash.Essentials.Core public JoinNameAttribute(string name) { + Debug.Console(0, "Setting Attribute Name: {0}", name); _Name = name; } From 69f54604420a61c6fb47fd578c29b848afb15c64 Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Sat, 4 Apr 2020 21:09:00 -0600 Subject: [PATCH 3/5] trying to fix JoinMapBaseAdvanced --- .../JoinMaps/JoinMapBase.cs | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs index 82694610..68804b93 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs @@ -164,7 +164,7 @@ namespace PepperDash.Essentials.Core /// public abstract class JoinMapBaseAdvanced { - protected uint _joinOffset; + protected uint JoinOffset; /// /// The collection of joins and associated metadata @@ -175,11 +175,15 @@ namespace PepperDash.Essentials.Core { Joins = new Dictionary(); - _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 +197,26 @@ namespace PepperDash.Essentials.Core // return join; // }); - var type = this.GetType(); + //type = this.GetType(); <- this wasn't working because 'this' was always the base class, never the derived class var cType = type.GetCType(); var fields = cType.GetFields(BindingFlags.Public | BindingFlags.Instance); + foreach (var field in fields) { - if (field.IsDefined(typeof(JoinNameAttribute), true)) - { - JoinDataComplete value = field.GetValue(this) as JoinDataComplete; + if (!field.IsDefined(typeof (JoinNameAttribute), true)) continue; - if (value != null) - { - value.SetJoinOffset(_joinOffset); - Joins.Add(value.GetNameAttribute(typeof(JoinDataComplete)), value); - } + var childClass = Convert.ChangeType(this, type, null); + + var value = field.GetValue(childClass) as JoinDataComplete; //this here is JoinMapBaseAdvanced, not the child class...which has no fields. + + if (value == null) + { + Debug.Console(0, "Unable to caset base class to {0}", type.Name); + continue; } + + value.SetJoinOffset(JoinOffset); + Joins.Add(value.GetNameAttribute(typeof(JoinDataComplete)), value); } From 42fbd813a22421a44fc7439c26fbb47ab4feb534 Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Sat, 4 Apr 2020 22:06:56 -0600 Subject: [PATCH 4/5] updated methods for creating joinMap dict --- .../JoinMaps/JoinMapBase.cs | 73 ++++++++----------- 1 file changed, 29 insertions(+), 44 deletions(-) diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs index 68804b93..ad17a69a 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs @@ -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; } /// @@ -46,17 +39,11 @@ namespace PepperDash.Essentials.Core var joinMapSerialzed = ConfigReader.ConfigObject.JoinMaps[joinMapKey]; - if (joinMapSerialzed != null) - { - var joinMapData = JsonConvert.DeserializeObject>(joinMapSerialzed); + if (joinMapSerialzed == null) return null; - if (joinMapData != null) - return joinMapData; - else - return null; - } - else - return null; + var joinMapData = JsonConvert.DeserializeObject>(joinMapSerialzed); + + return joinMapData; } } @@ -83,7 +70,7 @@ namespace PepperDash.Essentials.Core /// 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 /// public uint GetJoinForKey(string key) { - if (Joins.ContainsKey(key)) - return Joins[key].JoinNumber; - - else return 0; + return Joins.ContainsKey(key) ? Joins[key].JoinNumber : 0; } /// @@ -151,12 +135,8 @@ namespace PepperDash.Essentials.Core /// public uint GetJoinSpanForKey(string key) { - if (Joins.ContainsKey(key)) - return Joins[key].JoinSpan; - - else return 0; + return Joins.ContainsKey(key) ? Joins[key].JoinSpan : 0; } - } /// @@ -197,17 +177,17 @@ namespace PepperDash.Essentials.Core // return join; // }); - //type = this.GetType(); <- this wasn't working because 'this' was always the base class, never the derived class - var cType = type.GetCType(); - var fields = cType.GetFields(BindingFlags.Public | BindingFlags.Instance); + //type = this.GetType(); <- this wasn'memberInfo 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)) continue; - var childClass = Convert.ChangeType(this, type, null); - var value = field.GetValue(childClass) as JoinDataComplete; //this here is JoinMapBaseAdvanced, not the child class...which has no fields. + var value = field.GetValue(childClass) as JoinDataComplete; //this here is JoinMapBaseAdvanced, not the child class. JoinMapBaseAdvanced has no fields. if (value == null) { @@ -216,7 +196,12 @@ namespace PepperDash.Essentials.Core } value.SetJoinOffset(JoinOffset); - Joins.Add(value.GetNameAttribute(typeof(JoinDataComplete)), value); + + var joinName = value.GetNameAttribute(field); + + if (String.IsNullOrEmpty(joinName)) continue; + + Joins.Add(joinName, value); } @@ -228,7 +213,7 @@ namespace PepperDash.Essentials.Core /// 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:"); @@ -449,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; } } From c8f095f0a34b7a9a231e2cfe67c2a610ace667ba Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Sun, 5 Apr 2020 09:07:05 -0600 Subject: [PATCH 5/5] Updates CameraControllerJoinMap to use new constructor for base class --- PepperDashEssentials/Bridges/CameraControllerBridge.cs | 2 +- .../Bridges/JoinMaps/CameraControllerJoinMap.cs | 2 +- .../PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/PepperDashEssentials/Bridges/CameraControllerBridge.cs b/PepperDashEssentials/Bridges/CameraControllerBridge.cs index 8e25300a..732fee2e 100644 --- a/PepperDashEssentials/Bridges/CameraControllerBridge.cs +++ b/PepperDashEssentials/Bridges/CameraControllerBridge.cs @@ -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); diff --git a/PepperDashEssentials/Bridges/JoinMaps/CameraControllerJoinMap.cs b/PepperDashEssentials/Bridges/JoinMaps/CameraControllerJoinMap.cs index 44666a4e..5a37892f 100644 --- a/PepperDashEssentials/Bridges/JoinMaps/CameraControllerJoinMap.cs +++ b/PepperDashEssentials/Bridges/JoinMaps/CameraControllerJoinMap.cs @@ -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)) { } } diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs index ad17a69a..8f792659 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/JoinMaps/JoinMapBase.cs @@ -177,7 +177,7 @@ namespace PepperDash.Essentials.Core // return join; // }); - //type = this.GetType(); <- this wasn'memberInfo working because 'this' was always the base class, never the derived class + //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) @@ -454,7 +454,7 @@ namespace PepperDash.Essentials.Core public JoinNameAttribute(string name) { - Debug.Console(0, "Setting Attribute Name: {0}", name); + Debug.Console(2, "Setting Attribute Name: {0}", name); _Name = name; }