Updates factory method signature to take parent room as parameter. Adds methods to build components in ComponentRoom constructor

This commit is contained in:
Neil Dorin
2021-02-01 14:20:57 -07:00
parent f0299729e6
commit 7a426ebc3a
2 changed files with 46 additions and 7 deletions

View File

@@ -16,7 +16,7 @@ namespace PepperDash.Essentials.Core.Room.Components
{ {
public CType CType { get; set; } public CType CType { get; set; }
public string Description { get; set; } public string Description { get; set; }
public Func<RoomComponentConfig, IKeyed> FactoryMethod { get; set; } public Func<RoomComponentConfig, IComponentRoom, IKeyed> FactoryMethod { get; set; }
public ComponentFactoryWrapper() public ComponentFactoryWrapper()
{ {
@@ -70,13 +70,13 @@ namespace PepperDash.Essentials.Core.Room.Components
/// </summary> /// </summary>
/// <param name="dc"></param> /// <param name="dc"></param>
/// <returns></returns> /// <returns></returns>
public static void AddFactoryForType(string typeName, Func<RoomComponentConfig, IKeyed> method) public static void AddFactoryForType(string typeName, Func<RoomComponentConfig, IComponentRoom, IKeyed> method)
{ {
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Adding factory method for type '{0}'", typeName); Debug.Console(0, Debug.ErrorLogLevel.Notice, "Adding factory method for type '{0}'", typeName);
ComponentFactory.FactoryMethods.Add(typeName, new ComponentFactoryWrapper() { FactoryMethod = method }); ComponentFactory.FactoryMethods.Add(typeName, new ComponentFactoryWrapper() { FactoryMethod = method });
} }
public static void AddFactoryForType(string typeName, string description, CType cType, Func<RoomComponentConfig, IKeyed> method) public static void AddFactoryForType(string typeName, string description, CType cType, Func<RoomComponentConfig, IComponentRoom, IKeyed> method)
{ {
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Adding factory method for type '{0}'", typeName); Debug.Console(0, Debug.ErrorLogLevel.Notice, "Adding factory method for type '{0}'", typeName);
@@ -96,7 +96,7 @@ namespace PepperDash.Essentials.Core.Room.Components
/// </summary> /// </summary>
/// <param name="dc"></param> /// <param name="dc"></param>
/// <returns></returns> /// <returns></returns>
public static IKeyed GetComponent(RoomComponentConfig dc) public static IKeyed GetComponent(RoomComponentConfig dc, IComponentRoom parent)
{ {
var key = dc.Key; var key = dc.Key;
var name = dc.Name; var name = dc.Name;
@@ -109,7 +109,7 @@ namespace PepperDash.Essentials.Core.Room.Components
if (FactoryMethods.ContainsKey(typeName)) if (FactoryMethods.ContainsKey(typeName))
{ {
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Loading '{0}' from Essentials Core", dc.Type); Debug.Console(0, Debug.ErrorLogLevel.Notice, "Loading '{0}' from Essentials Core", dc.Type);
return FactoryMethods[typeName].FactoryMethod(dc); return FactoryMethods[typeName].FactoryMethod(dc, parent);
} }
return null; return null;
@@ -186,7 +186,7 @@ namespace PepperDash.Essentials.Core.Room.Components
/// </summary> /// </summary>
/// <param name="dc">The device config</param> /// <param name="dc">The device config</param>
/// <returns>An instance of the device</returns> /// <returns>An instance of the device</returns>
public abstract IComponent BuildComponent(RoomComponentConfig dc); public abstract IComponent BuildComponent(RoomComponentConfig dc, IComponentRoom parent);
#endregion #endregion
} }

View File

@@ -6,6 +6,7 @@ using Crestron.SimplSharp;
using PepperDash.Core; using PepperDash.Core;
using PepperDash.Essentials.Core.Interfaces.Components; using PepperDash.Essentials.Core.Interfaces.Components;
using PepperDash.Essentials.Core.Room.Components;
using PepperDash.Essentials.Core.Config; using PepperDash.Essentials.Core.Config;
using PepperDash.Essentials.Core.Devices; using PepperDash.Essentials.Core.Devices;
@@ -84,21 +85,59 @@ namespace PepperDash.Essentials.Core.Room
try try
{ {
PropertiesConfig = config.Properties.ToObject<ComponentRoomPropertiesConfig>(); PropertiesConfig = config.Properties.ToObject<ComponentRoomPropertiesConfig>();
BuildComponents();
BuildActivities();
} }
catch (Exception e) catch (Exception e)
{ {
Debug.Console(1, this, "Error building ComponentRoom: \n{0}", e); Debug.Console(1, this, "Error building ComponentRoom: \n{0}", e);
} }
BuildComponents();
} }
private void BuildComponents() private void BuildComponents()
{ {
foreach (var compConf in PropertiesConfig.Components)
{
IKeyed newComponent = null;
newComponent = ComponentFactory.GetComponent(compConf, this);
if (newComponent != null)
{
Components.Add(newComponent as IActivatableComponent);
DeviceManager.AddDevice(newComponent);
}
else
{
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Unable to build room component with key: {0}", compConf.Key);
}
}
} }
private void BuildActivities()
{
foreach (var compConf in PropertiesConfig.Activities)
{
IKeyed newComponent = null;
newComponent = ComponentFactory.GetComponent(compConf, this);
if (newComponent != null)
{
Activities.Add(newComponent as IRoomActivityComponent);
DeviceManager.AddDevice(newComponent);
}
else
{
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Unable to build room activity with key: {0}", compConf.Key);
}
}
}
/// <summary> /// <summary>