using Newtonsoft.Json; using PepperDash.Core; using PepperDash.Core.Logging; using Serilog.Events; using System.Collections.Generic; using System.Threading.Tasks; namespace PepperDash.Essentials.Core { /// /// Represents a room combination scenario /// public class RoomCombinationScenario : IRoomCombinationScenario, IKeyName { private RoomCombinationScenarioConfig _config; /// /// Gets or sets the key associated with the object. /// [JsonProperty("key")] public string Key { get; set; } /// /// Gets or sets the name associated with the object. /// [JsonProperty("name")] public string Name { get; set; } /// /// Gets a value indicating whether to hide this scenario in the UI. /// /// [JsonProperty("hideInUi")] public bool HideInUi { get { return _config.HideInUi; } } /// /// Gets or sets the PartitionStates /// /// [JsonProperty("partitionStates")] public List PartitionStates { get; private set; } /// /// Determines which UI devices get mapped to which room in this scenario. The Key should be the key of the UI device and the Value should be the key of the room to map to /// [JsonProperty("uiMap")] public Dictionary UiMap { get; set; } private bool _isActive; [JsonProperty("isActive")] public bool IsActive { get { return _isActive; } set { if (value == _isActive) { return; } _isActive = value; IsActiveFeedback.FireUpdate(); } } [JsonIgnore] /// /// Gets or sets the IsActiveFeedback /// public BoolFeedback IsActiveFeedback { get; private set; } private List activationActions; private List deactivationActions; public RoomCombinationScenario(RoomCombinationScenarioConfig config) { Key = config.Key; Name = config.Name; PartitionStates = config.PartitionStates; UiMap = config.UiMap; activationActions = config.ActivationActions; deactivationActions = config.DeactivationActions; _config = config; IsActiveFeedback = new BoolFeedback(() => _isActive); } public async Task Activate() { 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.LogInformation("Running Activation action {@action}", action); await DeviceJsonApi.DoDeviceActionAsync(action); } } IsActive = true; } public async Task Deactivate() { 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.LogInformation("Running deactivation action {actionDeviceKey}:{actionMethod}", action.DeviceKey, action.MethodName); await DeviceJsonApi.DoDeviceActionAsync(action); } } IsActive = false; } } }