From 97bd30e9c9f64d98c274ed23d8dff9471363aaf0 Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Fri, 26 Jul 2024 06:47:13 -0500 Subject: [PATCH] fix: add async/await patterns for activation/deactivation --- .../Devices/DeviceJsonApi.cs | 3 - .../Room/Combining/EssentialsRoomCombiner.cs | 69 +++++++++---------- .../Room/Combining/IEssentialsRoomCombiner.cs | 5 +- .../Room/Combining/RoomCombinationScenario.cs | 25 ++++--- 4 files changed, 52 insertions(+), 50 deletions(-) diff --git a/src/PepperDash.Essentials.Core/Devices/DeviceJsonApi.cs b/src/PepperDash.Essentials.Core/Devices/DeviceJsonApi.cs index 02a31e98..150313e7 100644 --- a/src/PepperDash.Essentials.Core/Devices/DeviceJsonApi.cs +++ b/src/PepperDash.Essentials.Core/Devices/DeviceJsonApi.cs @@ -153,9 +153,6 @@ namespace PepperDash.Essentials.Core Debug.LogMessage(e, "Error invoking method {methodName} on device {deviceKey}", null, method.Name, action.DeviceKey); } }); - - Debug.LogMessage(LogEventLevel.Information, "Method {methodName} successfully called on device {deviceKey} with {@params}", null, method.Name, - action.DeviceKey, action.Params); } catch (Exception ex) { diff --git a/src/PepperDash.Essentials.Core/Room/Combining/EssentialsRoomCombiner.cs b/src/PepperDash.Essentials.Core/Room/Combining/EssentialsRoomCombiner.cs index 9c8bfa49..11086821 100644 --- a/src/PepperDash.Essentials.Core/Room/Combining/EssentialsRoomCombiner.cs +++ b/src/PepperDash.Essentials.Core/Room/Combining/EssentialsRoomCombiner.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading; +using System.Threading.Tasks; namespace PepperDash.Essentials.Core { @@ -201,10 +202,40 @@ namespace PepperDash.Essentials.Core if (currentScenario != null) { this.LogInformation("Found combination Scenario {scenarioKey}", currentScenario.Key); - CurrentScenario = currentScenario; + ChangeScenario(currentScenario); } } + private async Task ChangeScenario(IRoomCombinationScenario newScenario) + { + + + if (newScenario == _currentScenario) + { + return; + } + + // Deactivate the old scenario first + if (_currentScenario != null) + { + Debug.LogMessage(LogEventLevel.Information, "Deactivating scenario {currentScenario}", this, _currentScenario.Name); + await _currentScenario.Deactivate(); + } + + _currentScenario = newScenario; + + // Activate the new scenario + if (_currentScenario != null) + { + Debug.LogMessage(LogEventLevel.Debug, $"Current Scenario: {_currentScenario.Name}", this); + await _currentScenario.Activate(); + } + + RoomCombinationScenarioChanged?.Invoke(this, new EventArgs()); + + + } + #region IEssentialsRoomCombiner Members public event EventHandler RoomCombinationScenarioChanged; @@ -215,42 +246,6 @@ namespace PepperDash.Essentials.Core { return _currentScenario; } - private set - { - try - { - _scenarioChange.WaitOne(); - - if (value != _currentScenario) - { - // Deactivate the old scenario first - if (_currentScenario != null) - { - _currentScenario.Deactivate(); - } - - _currentScenario = value; - - // Activate the new scenario - if (_currentScenario != null) - { - _currentScenario.Activate(); - - Debug.LogMessage(LogEventLevel.Debug, $"Current Scenario: {_currentScenario.Name}", this); - } - - var handler = RoomCombinationScenarioChanged; - if (handler != null) - { - handler(this, new EventArgs()); - } - } - } - finally - { - _scenarioChange.ReleaseMutex(); - } - } } public BoolFeedback IsInAutoModeFeedback { get; private set; } diff --git a/src/PepperDash.Essentials.Core/Room/Combining/IEssentialsRoomCombiner.cs b/src/PepperDash.Essentials.Core/Room/Combining/IEssentialsRoomCombiner.cs index 0d4a40cc..fefdc2da 100644 --- a/src/PepperDash.Essentials.Core/Room/Combining/IEssentialsRoomCombiner.cs +++ b/src/PepperDash.Essentials.Core/Room/Combining/IEssentialsRoomCombiner.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using Newtonsoft.Json; using PepperDash.Core; @@ -87,12 +88,12 @@ namespace PepperDash.Essentials.Core /// /// Activates this room combination scenario /// - void Activate(); + Task Activate(); /// /// Deactivates this room combination scenario /// - void Deactivate(); + Task Deactivate(); /// /// The state of the partitions that would activate this scenario diff --git a/src/PepperDash.Essentials.Core/Room/Combining/RoomCombinationScenario.cs b/src/PepperDash.Essentials.Core/Room/Combining/RoomCombinationScenario.cs index 84674d02..f36b807c 100644 --- a/src/PepperDash.Essentials.Core/Room/Combining/RoomCombinationScenario.cs +++ b/src/PepperDash.Essentials.Core/Room/Combining/RoomCombinationScenario.cs @@ -3,6 +3,7 @@ using PepperDash.Core; using PepperDash.Core.Logging; using Serilog.Events; using System.Collections.Generic; +using System.Threading.Tasks; namespace PepperDash.Essentials.Core { @@ -69,35 +70,43 @@ namespace PepperDash.Essentials.Core IsActiveFeedback = new BoolFeedback(() => _isActive); } - public void Activate() + public async Task Activate() { - Debug.LogMessage(LogEventLevel.Debug, "Activating Scenario: '{name}' with {activationActionCount} action(s) defined", this, Name, activationActions.Count); + this.LogInformation("Activating Scenario {name} with {activationActionCount} action(s) defined", Name, activationActions.Count); + + List tasks = new List(); if (activationActions != null) { foreach (var action in activationActions) { - this.LogDebug("Running Activation action {@action}", action); - DeviceJsonApi.DoDeviceAction(action); + this.LogInformation("Running Activation action {@action}", action); + tasks.Add(DeviceJsonApi.DoDeviceActionAsync(action)); } } + await Task.WhenAll(tasks); + IsActive = true; } - public void Deactivate() + public async Task Deactivate() { - Debug.LogMessage(LogEventLevel.Debug, "Deactivating Scenario: '{name}' with {deactivationActionCount} action(s) defined", this, Name, deactivationActions.Count); + this.LogInformation("Deactivating Scenario {name} with {deactivationActionCount} action(s) defined", Name, deactivationActions.Count); + + List tasks = new List(); if (deactivationActions != null) { foreach (var action in deactivationActions) { - this.LogDebug("Running deactivation action {@action}", action); - DeviceJsonApi.DoDeviceAction(action); + this.LogInformation("Running deactivation action {actionDeviceKey}:{actionMethod}", action.DeviceKey, action.MethodName); + tasks.Add( DeviceJsonApi.DoDeviceActionAsync(action)); } } + await Task.WhenAll(tasks); + IsActive = false; }