New device loading methodology working via reflection

This commit is contained in:
Neil Dorin
2020-04-23 14:59:54 -06:00
parent 911bd8daba
commit a93ded8c79
12 changed files with 121 additions and 268 deletions

View File

@@ -9,7 +9,7 @@ using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Essentials.Core.Bridges;
using PepperDash.Essentials.Core.Config;
using PepperDash_Essentials_Core.Devices;
namespace PepperDash.Essentials.Core.CrestronIO
{

View File

@@ -9,7 +9,6 @@ using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Essentials.Core.Bridges;
using PepperDash.Essentials.Core.Config;
using PepperDash_Essentials_Core.Devices;
namespace PepperDash.Essentials.Core.CrestronIO
{

View File

@@ -29,6 +29,30 @@ namespace PepperDash.Essentials.Core
public class DeviceFactory
{
public DeviceFactory()
{
var assy = Assembly.GetExecutingAssembly();
PluginLoader.SetEssentialsAssembly(assy.GetName().Name, assy);
var types = assy.GetTypes().Where(ct => typeof(IDeviceFactory).IsAssignableFrom(ct) && !ct.IsInterface && !ct.IsAbstract);
if (types != null)
{
foreach (var type in types)
{
try
{
var factory = (IDeviceFactory)Crestron.SimplSharp.Reflection.Activator.CreateInstance(type);
factory.LoadTypeFactories();
}
catch (Exception e)
{
Debug.Console(0, Debug.ErrorLogLevel.Error, "Unable to load type: '{1}' DeviceFactory: {0}", e, type.Name);
}
}
}
}
/// <summary>
/// A dictionary of factory methods, keyed by config types, added by plugins.
/// These methods are looked up and called by GetDevice in this class.
@@ -122,28 +146,4 @@ namespace PepperDash.Essentials.Core
}
}
}
/// <summary>
/// Responsible for loading all of the device types for this library
/// </summary>
public class CoreDeviceFactory
{
public CoreDeviceFactory()
{
Debug.Console(1, "Essentials.Core Factory Adding Types...");
//cast to IDeviceFactory isn't explicitly required here...but will be when instantiating using reflection, which I'm assuming is the next step
var genCommFactory = new GenericCommFactory() as IDeviceFactory;
genCommFactory.LoadTypeFactories();
var c2nRthsFactory = new C2nRthsControllerFactory() as IDeviceFactory;
c2nRthsFactory.LoadTypeFactories();
var statusSignFactory = new StatusSignControllerFactory() as IDeviceFactory;
statusSignFactory.LoadTypeFactories();
var cenIoControllerFactory = new CenIoDigIn104ControllerFactory() as IDeviceFactory;
cenIoControllerFactory.LoadTypeFactories();
}
}
}

View File

@@ -68,25 +68,21 @@ namespace PepperDash.Essentials
case ("PepperDashEssentials.dll"):
{
version = Global.AssemblyVersion;
assembly = Assembly.GetExecutingAssembly();
break;
}
case ("PepperDash_Essential_Core.dll"):
case ("PepperDash_Essentials_Core.dll"):
{
version = Global.AssemblyVersion;
assembly = System.Reflection
break;
}
case ("PepperDash_Essentials_DM.dll"):
{
version = Global.AssemblyVersion;
assembly = Assembly.GetExecutingAssembly();
break;
}
case ("Essentials Devices Common.dll"):
{
version = Global.AssemblyVersion;
assembly = Assembly.GetExecutingAssembly();
break;
}
case ("PepperDash_Core.dll"):
@@ -110,6 +106,17 @@ namespace PepperDash.Essentials
}
}
public static void SetEssentialsAssembly(string name, Assembly assembly)
{
var loadedAssembly = LoadedAssemblies.FirstOrDefault(la => la.Name.Equals(name));
if (loadedAssembly != null)
{
loadedAssembly.SetAssembly(assembly);
}
}
/// <summary>
/// Loads an assembly via Reflection and adds it to the list of loaded assemblies
/// </summary>
@@ -503,5 +510,10 @@ namespace PepperDash.Essentials
Version = version;
Assembly = assembly;
}
public void SetAssembly(Assembly assembly)
{
Assembly = assembly;
}
}
}