From 4d608eef0642da82e0e10e343a3a086541f0b57e Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Wed, 7 Feb 2024 08:44:58 -0600 Subject: [PATCH] feat: refactor LoadRooms method In order to facilitate using custom messengers, I added an interface, `IStandardMobileControl` that devices or rooms can implement if they should use the standard/existing MobileControl messengers. If a room or device wants to implement non-standard messengers, or is a new type of device that doesn't yet have a corresponding messenger in the Mobile Control plugin, do NOT implement this interface and MC won't attempt to build a messenger for it. --- .../DeviceTypeInterfaces/IMobileControl.cs | 7 ++ src/PepperDash.Essentials/ControlSystem.cs | 100 +++++------------- 2 files changed, 31 insertions(+), 76 deletions(-) diff --git a/src/PepperDash.Essentials.Core/DeviceTypeInterfaces/IMobileControl.cs b/src/PepperDash.Essentials.Core/DeviceTypeInterfaces/IMobileControl.cs index 944e4a50..80e9cc8c 100644 --- a/src/PepperDash.Essentials.Core/DeviceTypeInterfaces/IMobileControl.cs +++ b/src/PepperDash.Essentials.Core/DeviceTypeInterfaces/IMobileControl.cs @@ -5,6 +5,13 @@ using PepperDash.Core; namespace PepperDash.Essentials.Core.DeviceTypeInterfaces { + /// + /// Use this interface on a device or room if it should use default Mobile Control messengers + /// + public interface IStandardMobileControl : IKeyed + { + } + /// /// Describes a MobileControlSystemController /// diff --git a/src/PepperDash.Essentials/ControlSystem.cs b/src/PepperDash.Essentials/ControlSystem.cs index d9836aba..7d4281bd 100644 --- a/src/PepperDash.Essentials/ControlSystem.cs +++ b/src/PepperDash.Essentials/ControlSystem.cs @@ -11,6 +11,9 @@ using PepperDash.Essentials.Core.Bridges; using PepperDash.Essentials.Core.Config; using PepperDash.Essentials.Core.DeviceTypeInterfaces; using PepperDash.Essentials.Core.Web; +using PepperDash.Essentials.Devices.Common.Room; +using PepperDash.Essentials.Devices.Common.Rooms; +using PepperDash.Essentials.Room.Config; using System; using System.Linq; @@ -468,103 +471,48 @@ namespace PepperDash.Essentials return; } - // uint fusionIpId = 0xf1; - foreach (var roomConfig in ConfigReader.ConfigObject.Rooms) { - /* - var room = EssentialsRoomConfigHelper.GetRoomObject(roomConfig) as IEssentialsRoom; - if (room != null) - { - // default to no join map key - string fusionJoinMapKey = string.Empty; + var room = Core.DeviceFactory.GetDevice(roomConfig); - if (room.Config.Properties["fusion"] != null) - { - Debug.Console(2, "Custom Fusion config found. Using custom values"); + DeviceManager.AddDevice(room); + if (!(room is IStandardMobileControl)) + { + continue; + } - var fusionConfig = room.Config.Properties["fusion"].ToObject(); - - if (fusionConfig != null) - { - fusionIpId = fusionConfig.IpIdInt; - fusionJoinMapKey = fusionConfig.JoinMapKey; - } - } - - AddRoomAndBuildMC(room); - - if (room is IEssentialsHuddleSpaceRoom) - { - - Debug.Console(0, Debug.ErrorLogLevel.Notice, "Room is EssentialsHuddleSpaceRoom, attempting to add to DeviceManager with Fusion with IP-ID {0:X2}", fusionIpId); - DeviceManager.AddDevice(new Core.Fusion.EssentialsHuddleSpaceFusionSystemControllerBase(room, fusionIpId, fusionJoinMapKey)); - - } - else if (room is IEssentialsHuddleVtc1Room) - { - - if (!(room is EssentialsCombinedHuddleVtc1Room)) - { - Debug.Console(0, Debug.ErrorLogLevel.Notice, "Room is EssentialsHuddleVtc1Room, attempting to add to DeviceManager with Fusion with IP-ID {0:X2}", fusionIpId); - DeviceManager.AddDevice(new EssentialsHuddleVtc1FusionController((IEssentialsHuddleVtc1Room)room, fusionIpId, fusionJoinMapKey)); - } - - } - else if (room is EssentialsTechRoom) - { - - Debug.Console(0, Debug.ErrorLogLevel.Notice, - "Room is EssentialsTechRoom, Attempting to add to DeviceManager with Fusion with IP-ID {0:X2}", fusionIpId); - DeviceManager.AddDevice(new EssentialsTechRoomFusionSystemController((EssentialsTechRoom)room, fusionIpId, fusionJoinMapKey)); - - } - fusionIpId += 1; - } - else - { - Debug.Console(0, Debug.ErrorLogLevel.Notice, "Notice: Cannot create room from config, key '{0}' - Is this intentional? This may be a valid configuration.", roomConfig.Key); - - } - */ + BuildMC(room as IStandardMobileControl); } Debug.Console(0, Debug.ErrorLogLevel.Notice, "All Rooms Loaded."); } - private static void AddRoomAndBuildMC(IEssentialsRoom room) - { - DeviceManager.AddDevice(room); + private static void BuildMC(IStandardMobileControl room) + { + Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to build Mobile Control Bridge for "); - Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to build Mobile Control Bridge"); - - CreateMobileControlBridge(room); + CreateMobileControlBridge(room as IEssentialsRoom); } - private static void CreateMobileControlBridge(object room) + private static void CreateMobileControlBridge(IEssentialsRoom room) { + if(room == null) + { + Debug.Console(0, Debug.ErrorLogLevel.Warning, $"Room does not implement IEssentialsRoom"); + return; + } + var mobileControl = GetMobileControlDevice(); - if (mobileControl == null) return; - - var mobileControl3 = mobileControl as IMobileControl3; - - if (mobileControl3 != null) - { - mobileControl3.CreateMobileControlRoomBridge(room as IEssentialsRoom, mobileControl); - } - else - { - mobileControl.CreateMobileControlRoomBridge(room as EssentialsRoomBase, mobileControl); - } + mobileControl?.CreateMobileControlRoomBridge(room, mobileControl); Debug.Console(0, Debug.ErrorLogLevel.Notice, "Mobile Control Bridge Added..."); } - private static IMobileControl GetMobileControlDevice() + private static IMobileControl3 GetMobileControlDevice() { - var mobileControlList = DeviceManager.AllDevices.OfType().ToList(); + var mobileControlList = DeviceManager.AllDevices.OfType().ToList(); if (mobileControlList.Count > 1) {