From fa38e8a9a8862c1e8c3ffb5fe7497a6758812af8 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Fri, 4 Oct 2024 10:33:09 -0600 Subject: [PATCH] feat: Adds mechanism to track initialization status of EssentialsDevice as well as an event on DeviceManager to notify when all devices initialized. Room combiner now waits for all initialize before setting current scenario. --- .../Devices/DeviceManager.cs | 27 ++++++++++++++++++- .../Devices/EssentialsDevice.cs | 20 ++++++++++++++ .../Room/Combining/EssentialsRoomCombiner.cs | 10 ++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/PepperDash.Essentials.Core/Devices/DeviceManager.cs b/src/PepperDash.Essentials.Core/Devices/DeviceManager.cs index 0552faeb..ae8eba03 100644 --- a/src/PepperDash.Essentials.Core/Devices/DeviceManager.cs +++ b/src/PepperDash.Essentials.Core/Devices/DeviceManager.cs @@ -14,6 +14,7 @@ namespace PepperDash.Essentials.Core { public static event EventHandler AllDevicesActivated; public static event EventHandler AllDevicesRegistered; + public static event EventHandler AllDevicesInitialized; private static readonly CCriticalSection DeviceCriticalSection = new CCriticalSection(); private static readonly CEvent AllowAddDevicesCEvent = new CEvent(false, true); @@ -67,7 +68,7 @@ namespace PepperDash.Essentials.Core foreach (var d in Devices.Values) { try - { + { if (d is Device) (d as Device).PreActivate(); } @@ -123,6 +124,18 @@ namespace PepperDash.Essentials.Core } } + private static void DeviceManager_Initialized(object sender, EventArgs e) + { + var allInitialized = Devices.Values.OfType().All(d => d.IsInitialized); + + if (allInitialized) + { + Debug.LogMessage(LogEventLevel.Information, "****All Devices Initalized****"); + + OnAllDevicesInitialized(); + } + } + private static void OnAllDevicesActivated() { var handler = AllDevicesActivated; @@ -141,6 +154,15 @@ namespace PepperDash.Essentials.Core } } + private static void OnAllDevicesInitialized() + { + var handler = AllDevicesInitialized; + if (handler != null) + { + handler(null, new EventArgs()); + } + } + /// /// Calls activate on all Device class items /// @@ -264,6 +286,9 @@ namespace PepperDash.Essentials.Core Devices.Add(newDev.Key, newDev); //if (!(_Devices.Contains(newDev))) // _Devices.Add(newDev); + + if (newDev is EssentialsDevice essentialsDev) + essentialsDev.Initialized += DeviceManager_Initialized; } finally { diff --git a/src/PepperDash.Essentials.Core/Devices/EssentialsDevice.cs b/src/PepperDash.Essentials.Core/Devices/EssentialsDevice.cs index 8b0fc9fe..bdb8b45a 100644 --- a/src/PepperDash.Essentials.Core/Devices/EssentialsDevice.cs +++ b/src/PepperDash.Essentials.Core/Devices/EssentialsDevice.cs @@ -17,6 +17,24 @@ namespace PepperDash.Essentials.Core [Description("The base Essentials Device Class")] public abstract class EssentialsDevice : Device { + public event EventHandler Initialized; + + private bool _isInitialized; + public bool IsInitialized { + get { return _isInitialized; } + private set + { + if (_isInitialized == value) return; + + _isInitialized = value; + + if (_isInitialized) + { + Initialized?.Invoke(this, new EventArgs()); + } + } + } + protected EssentialsDevice(string key) : base(key) { @@ -41,6 +59,8 @@ namespace PepperDash.Essentials.Core try { Initialize(); + + IsInitialized = true; } catch (Exception ex) { diff --git a/src/PepperDash.Essentials.Core/Room/Combining/EssentialsRoomCombiner.cs b/src/PepperDash.Essentials.Core/Room/Combining/EssentialsRoomCombiner.cs index 11086821..f0fb5e98 100644 --- a/src/PepperDash.Essentials.Core/Room/Combining/EssentialsRoomCombiner.cs +++ b/src/PepperDash.Essentials.Core/Room/Combining/EssentialsRoomCombiner.cs @@ -84,7 +84,15 @@ namespace PepperDash.Essentials.Core SetupPartitionStateProviders(); SetRooms(); + }); + + // Subscribe to the AllDevicesInitialized event + // We need to wait until all devices are initialized in case + // any actions are dependent on 3rd party devices already being + // connected and initialized + DeviceManager.AllDevicesInitialized += (o, a) => + { if (IsInAutoMode) { DetermineRoomCombinationScenario(); @@ -93,7 +101,7 @@ namespace PepperDash.Essentials.Core { SetRoomCombinationScenario(_propertiesConfig.defaultScenarioKey); } - }); + }; } private void CreateScenarios()