diff --git a/src/PepperDash.Essentials.Core/Devices/DeviceManager.cs b/src/PepperDash.Essentials.Core/Devices/DeviceManager.cs index 0552faeba..ae8eba033 100644 --- a/src/PepperDash.Essentials.Core/Devices/DeviceManager.cs +++ b/src/PepperDash.Essentials.Core/Devices/DeviceManager.cs @@ -14,6 +14,7 @@ public static class DeviceManager { 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 @@ public static void ActivateAll() foreach (var d in Devices.Values) { try - { + { if (d is Device) (d as Device).PreActivate(); } @@ -123,6 +124,18 @@ public static void ActivateAll() } } + 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 @@ private static void OnAllDevicesRegistered() } } + 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 @@ public static void AddDevice(IKeyed newDev) 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 8b0fc9fe7..bdb8b45ad 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 @@ private void DeviceManagerOnAllDevicesActivated(object sender, EventArgs eventAr 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 110868219..f0fb5e988 100644 --- a/src/PepperDash.Essentials.Core/Room/Combining/EssentialsRoomCombiner.cs +++ b/src/PepperDash.Essentials.Core/Room/Combining/EssentialsRoomCombiner.cs @@ -84,7 +84,15 @@ public EssentialsRoomCombiner(string key, EssentialsRoomCombinerPropertiesConfig 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 @@ public EssentialsRoomCombiner(string key, EssentialsRoomCombinerPropertiesConfig { SetRoomCombinationScenario(_propertiesConfig.defaultScenarioKey); } - }); + }; } private void CreateScenarios()