From 22de0c41f7ff2594830a70a25165161d5e462f0c Mon Sep 17 00:00:00 2001 From: Stefan Roelofs Date: Sat, 27 Mar 2021 01:15:11 +0100 Subject: [PATCH] Update ConfigurationServices with methods to update sensors / commands --- .../Data/ConfigurationService.cs | 93 +++++++++++++------ .../Data/IConfigurationService.cs | 11 ++- 2 files changed, 71 insertions(+), 33 deletions(-) diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index db11c32..e6802a0 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -181,7 +181,7 @@ namespace hass_workstation_service.Data command = new LogOffCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; case "CustomCommand": - command = new CustomCommand(publisher, configuredCommand.Command, configuredCommand.Name, configuredCommand.Id); + command = new CustomCommand(publisher, configuredCommand.Command, configuredCommand.Name, configuredCommand.Id); break; case "MediaPlayPauseCommand": command = new MediaPlayPauseCommand(publisher, configuredCommand.Name, configuredCommand.Id); @@ -340,54 +340,89 @@ namespace hass_workstation_service.Data public void AddConfiguredSensor(AbstractSensor sensor) { - this.ConfiguredSensors.Add(sensor); - sensor.PublishAutoDiscoveryConfigAsync(); + AddSensor(sensor); WriteSensorSettingsAsync(); } public void AddConfiguredCommand(AbstractCommand command) { - this.ConfiguredCommands.Add(command); - command.PublishAutoDiscoveryConfigAsync(); + AddCommand(command); WriteCommandSettingsAsync(); } - public async void DeleteConfiguredSensor(Guid id) + public void AddConfiguredSensors(List sensors) { - var sensorToRemove = this.ConfiguredSensors.FirstOrDefault(s => s.Id == id); - if (sensorToRemove != null) - { - await sensorToRemove.UnPublishAutoDiscoveryConfigAsync(); - this.ConfiguredSensors.Remove(sensorToRemove); - WriteSensorSettingsAsync(); - } - else - { - Log.Logger.Warning($"sensor with id {id} not found"); - } + sensors.ForEach(sensor => AddSensor(sensor)); + WriteSensorSettingsAsync(); + } + public void AddConfiguredCommands(List commands) + { + commands.ForEach(command => AddCommand(command)); + WriteCommandSettingsAsync(); + } + + public async void DeleteConfiguredSensor(Guid id) + { + await DeleteSensor(id); + WriteSensorSettingsAsync(); } public async void DeleteConfiguredCommand(Guid id) { - var commandToRemove = this.ConfiguredCommands.FirstOrDefault(s => s.Id == id); - if (commandToRemove != null) - { - await commandToRemove.UnPublishAutoDiscoveryConfigAsync(); - this.ConfiguredCommands.Remove(commandToRemove); - WriteCommandSettingsAsync(); - } - else + await DeleteCommand(id); + WriteCommandSettingsAsync(); + } + + public async void UpdateConfiguredSensor(Guid id, AbstractSensor sensor) + { + await DeleteSensor(id); + AddSensor(sensor); + WriteSensorSettingsAsync(); + } + + public async void UpdateConfiguredCommand(Guid id, AbstractCommand command) + { + await DeleteCommand(id); + AddCommand(command); + WriteCommandSettingsAsync(); + } + + private void AddSensor(AbstractSensor sensor) + { + ConfiguredSensors.Add(sensor); + sensor.PublishAutoDiscoveryConfigAsync(); + } + + private void AddCommand(AbstractCommand command) + { + ConfiguredCommands.Add(command); + command.PublishAutoDiscoveryConfigAsync(); + } + + private async Task DeleteSensor(Guid id) + { + var sensorToRemove = ConfiguredSensors.FirstOrDefault(s => s.Id == id); + if (sensorToRemove == null) { - Log.Logger.Warning($"command with id {id} not found"); + Log.Logger.Warning($"sensor with id {id} not found"); + return; } + await sensorToRemove.UnPublishAutoDiscoveryConfigAsync(); + ConfiguredSensors.Remove(sensorToRemove); } - public void AddConfiguredSensors(List sensors) + private async Task DeleteCommand(Guid id) { - sensors.ForEach((sensor) => this.ConfiguredSensors.Add(sensor)); - WriteSensorSettingsAsync(); + var commandToRemove = ConfiguredCommands.FirstOrDefault(c => c.Id == id); + if (commandToRemove == null) + { + Log.Logger.Warning($"command with id {id} not found"); + return; + } + await commandToRemove.UnPublishAutoDiscoveryConfigAsync(); + ConfiguredCommands.Remove(commandToRemove); } /// diff --git a/hass-workstation-service/Data/IConfigurationService.cs b/hass-workstation-service/Data/IConfigurationService.cs index 43d81dc..b2b63e5 100644 --- a/hass-workstation-service/Data/IConfigurationService.cs +++ b/hass-workstation-service/Data/IConfigurationService.cs @@ -13,13 +13,18 @@ namespace hass_workstation_service.Data { public interface IConfigurationService { - ICollection ConfiguredSensors { get; } Action MqqtConfigChangedHandler { get; set; } + ICollection ConfiguredSensors { get; } ICollection ConfiguredCommands { get; } - void AddConfiguredCommand(AbstractCommand command); void AddConfiguredSensor(AbstractSensor sensor); + void AddConfiguredCommand(AbstractCommand command); void AddConfiguredSensors(List sensors); + void AddConfiguredCommands(List commands); + void DeleteConfiguredSensor(Guid id); + void DeleteConfiguredCommand(Guid id); + void UpdateConfiguredSensor(Guid id, AbstractSensor sensor); + void UpdateConfiguredCommand(Guid id, AbstractCommand command); Task GetMqttClientOptionsAsync(); void ReadSensorSettings(MqttPublisher publisher); void WriteMqttBrokerSettingsAsync(MqttSettings settings); @@ -27,8 +32,6 @@ namespace hass_workstation_service.Data Task GetMqttBrokerSettings(); void EnableAutoStart(bool enable); bool IsAutoStartEnabled(); - void DeleteConfiguredSensor(Guid id); - void DeleteConfiguredCommand(Guid id); void WriteCommandSettingsAsync(); void ReadCommandSettings(MqttPublisher publisher); Task> GetSensorsAfterLoadingAsync();