Skip to content

Commit

Permalink
feat: Adds mechanism to track initialization status of EssentialsDevi…
Browse files Browse the repository at this point in the history
…ce as well as an event on DeviceManager to notify when all devices initialized. Room combiner now waits for all initialize before setting current scenario.
  • Loading branch information
ndorin committed Oct 4, 2024
1 parent f351c03 commit fa38e8a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/PepperDash.Essentials.Core/Devices/DeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static class DeviceManager
{
public static event EventHandler<EventArgs> AllDevicesActivated;
public static event EventHandler<EventArgs> AllDevicesRegistered;
public static event EventHandler<EventArgs> AllDevicesInitialized;

private static readonly CCriticalSection DeviceCriticalSection = new CCriticalSection();
private static readonly CEvent AllowAddDevicesCEvent = new CEvent(false, true);
Expand Down Expand Up @@ -67,7 +68,7 @@ public static void ActivateAll()
foreach (var d in Devices.Values)
{
try
{
{
if (d is Device)
(d as Device).PreActivate();
}
Expand Down Expand Up @@ -123,6 +124,18 @@ public static void ActivateAll()
}
}

private static void DeviceManager_Initialized(object sender, EventArgs e)
{
var allInitialized = Devices.Values.OfType<EssentialsDevice>().All(d => d.IsInitialized);

if (allInitialized)
{
Debug.LogMessage(LogEventLevel.Information, "****All Devices Initalized****");

OnAllDevicesInitialized();
}
}

private static void OnAllDevicesActivated()
{
var handler = AllDevicesActivated;
Expand All @@ -141,6 +154,15 @@ private static void OnAllDevicesRegistered()
}
}

private static void OnAllDevicesInitialized()
{
var handler = AllDevicesInitialized;
if (handler != null)
{
handler(null, new EventArgs());
}
}

/// <summary>
/// Calls activate on all Device class items
/// </summary>
Expand Down Expand Up @@ -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
{
Expand Down
20 changes: 20 additions & 0 deletions src/PepperDash.Essentials.Core/Devices/EssentialsDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -41,6 +59,8 @@ private void DeviceManagerOnAllDevicesActivated(object sender, EventArgs eventAr
try
{
Initialize();
IsInitialized = true;
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -93,7 +101,7 @@ public EssentialsRoomCombiner(string key, EssentialsRoomCombinerPropertiesConfig
{
SetRoomCombinationScenario(_propertiesConfig.defaultScenarioKey);
}
});
};
}

private void CreateScenarios()
Expand Down

0 comments on commit fa38e8a

Please sign in to comment.