mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-16 13:15:03 +00:00
fix: add async/await patterns for activation/deactivation
This commit is contained in:
@@ -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(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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PepperDash.Essentials.Core
|
namespace PepperDash.Essentials.Core
|
||||||
{
|
{
|
||||||
@@ -201,10 +202,40 @@ namespace PepperDash.Essentials.Core
|
|||||||
if (currentScenario != null)
|
if (currentScenario != null)
|
||||||
{
|
{
|
||||||
this.LogInformation("Found combination Scenario {scenarioKey}", currentScenario.Key);
|
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
|
#region IEssentialsRoomCombiner Members
|
||||||
|
|
||||||
public event EventHandler<EventArgs> RoomCombinationScenarioChanged;
|
public event EventHandler<EventArgs> RoomCombinationScenarioChanged;
|
||||||
@@ -215,42 +246,6 @@ namespace PepperDash.Essentials.Core
|
|||||||
{
|
{
|
||||||
return _currentScenario;
|
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; }
|
public BoolFeedback IsInAutoModeFeedback { get; private set; }
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using PepperDash.Core;
|
using PepperDash.Core;
|
||||||
|
|
||||||
@@ -87,12 +88,12 @@ namespace PepperDash.Essentials.Core
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Activates this room combination scenario
|
/// Activates this room combination scenario
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void Activate();
|
Task Activate();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deactivates this room combination scenario
|
/// Deactivates this room combination scenario
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void Deactivate();
|
Task Deactivate();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The state of the partitions that would activate this scenario
|
/// The state of the partitions that would activate this scenario
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using PepperDash.Core;
|
|||||||
using PepperDash.Core.Logging;
|
using PepperDash.Core.Logging;
|
||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PepperDash.Essentials.Core
|
namespace PepperDash.Essentials.Core
|
||||||
{
|
{
|
||||||
@@ -69,35 +70,43 @@ namespace PepperDash.Essentials.Core
|
|||||||
IsActiveFeedback = new BoolFeedback(() => _isActive);
|
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<Task> tasks = new List<Task>();
|
||||||
|
|
||||||
if (activationActions != null)
|
if (activationActions != null)
|
||||||
{
|
{
|
||||||
foreach (var action in activationActions)
|
foreach (var action in activationActions)
|
||||||
{
|
{
|
||||||
this.LogDebug("Running Activation action {@action}", action);
|
this.LogInformation("Running Activation action {@action}", action);
|
||||||
DeviceJsonApi.DoDeviceAction(action);
|
tasks.Add(DeviceJsonApi.DoDeviceActionAsync(action));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await Task.WhenAll(tasks);
|
||||||
|
|
||||||
IsActive = true;
|
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<Task> tasks = new List<Task>();
|
||||||
|
|
||||||
if (deactivationActions != null)
|
if (deactivationActions != null)
|
||||||
{
|
{
|
||||||
foreach (var action in deactivationActions)
|
foreach (var action in deactivationActions)
|
||||||
{
|
{
|
||||||
this.LogDebug("Running deactivation action {@action}", action);
|
this.LogInformation("Running deactivation action {actionDeviceKey}:{actionMethod}", action.DeviceKey, action.MethodName);
|
||||||
DeviceJsonApi.DoDeviceAction(action);
|
tasks.Add( DeviceJsonApi.DoDeviceActionAsync(action));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await Task.WhenAll(tasks);
|
||||||
|
|
||||||
IsActive = false;
|
IsActive = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user