Update ConfigurationServices with methods to update sensors / commands

merge-updating
Stefan Roelofs 4 years ago
parent a50640d7d7
commit 22de0c41f7

@ -340,54 +340,89 @@ namespace hass_workstation_service.Data
public void AddConfiguredSensor(AbstractSensor sensor) public void AddConfiguredSensor(AbstractSensor sensor)
{ {
this.ConfiguredSensors.Add(sensor); AddSensor(sensor);
sensor.PublishAutoDiscoveryConfigAsync();
WriteSensorSettingsAsync(); WriteSensorSettingsAsync();
} }
public void AddConfiguredCommand(AbstractCommand command) public void AddConfiguredCommand(AbstractCommand command)
{ {
this.ConfiguredCommands.Add(command); AddCommand(command);
command.PublishAutoDiscoveryConfigAsync();
WriteCommandSettingsAsync(); WriteCommandSettingsAsync();
} }
public async void DeleteConfiguredSensor(Guid id) public void AddConfiguredSensors(List<AbstractSensor> sensors)
{
var sensorToRemove = this.ConfiguredSensors.FirstOrDefault(s => s.Id == id);
if (sensorToRemove != null)
{ {
await sensorToRemove.UnPublishAutoDiscoveryConfigAsync(); sensors.ForEach(sensor => AddSensor(sensor));
this.ConfiguredSensors.Remove(sensorToRemove);
WriteSensorSettingsAsync(); WriteSensorSettingsAsync();
} }
else
public void AddConfiguredCommands(List<AbstractCommand> commands)
{ {
Log.Logger.Warning($"sensor with id {id} not found"); commands.ForEach(command => AddCommand(command));
WriteCommandSettingsAsync();
} }
public async void DeleteConfiguredSensor(Guid id)
{
await DeleteSensor(id);
WriteSensorSettingsAsync();
} }
public async void DeleteConfiguredCommand(Guid id) public async void DeleteConfiguredCommand(Guid id)
{ {
var commandToRemove = this.ConfiguredCommands.FirstOrDefault(s => s.Id == id); await DeleteCommand(id);
if (commandToRemove != null) WriteCommandSettingsAsync();
}
public async void UpdateConfiguredSensor(Guid id, AbstractSensor sensor)
{ {
await commandToRemove.UnPublishAutoDiscoveryConfigAsync(); await DeleteSensor(id);
this.ConfiguredCommands.Remove(commandToRemove); AddSensor(sensor);
WriteSensorSettingsAsync();
}
public async void UpdateConfiguredCommand(Guid id, AbstractCommand command)
{
await DeleteCommand(id);
AddCommand(command);
WriteCommandSettingsAsync(); WriteCommandSettingsAsync();
} }
else
private void AddSensor(AbstractSensor sensor)
{ {
Log.Logger.Warning($"command with id {id} not found"); ConfiguredSensors.Add(sensor);
sensor.PublishAutoDiscoveryConfigAsync();
} }
private void AddCommand(AbstractCommand command)
{
ConfiguredCommands.Add(command);
command.PublishAutoDiscoveryConfigAsync();
} }
public void AddConfiguredSensors(List<AbstractSensor> sensors) private async Task DeleteSensor(Guid id)
{ {
sensors.ForEach((sensor) => this.ConfiguredSensors.Add(sensor)); var sensorToRemove = ConfiguredSensors.FirstOrDefault(s => s.Id == id);
WriteSensorSettingsAsync(); if (sensorToRemove == null)
{
Log.Logger.Warning($"sensor with id {id} not found");
return;
}
await sensorToRemove.UnPublishAutoDiscoveryConfigAsync();
ConfiguredSensors.Remove(sensorToRemove);
}
private async Task DeleteCommand(Guid id)
{
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);
} }
/// <summary> /// <summary>

@ -13,13 +13,18 @@ namespace hass_workstation_service.Data
{ {
public interface IConfigurationService public interface IConfigurationService
{ {
ICollection<AbstractSensor> ConfiguredSensors { get; }
Action<IManagedMqttClientOptions> MqqtConfigChangedHandler { get; set; } Action<IManagedMqttClientOptions> MqqtConfigChangedHandler { get; set; }
ICollection<AbstractSensor> ConfiguredSensors { get; }
ICollection<AbstractCommand> ConfiguredCommands { get; } ICollection<AbstractCommand> ConfiguredCommands { get; }
void AddConfiguredCommand(AbstractCommand command);
void AddConfiguredSensor(AbstractSensor sensor); void AddConfiguredSensor(AbstractSensor sensor);
void AddConfiguredCommand(AbstractCommand command);
void AddConfiguredSensors(List<AbstractSensor> sensors); void AddConfiguredSensors(List<AbstractSensor> sensors);
void AddConfiguredCommands(List<AbstractCommand> commands);
void DeleteConfiguredSensor(Guid id);
void DeleteConfiguredCommand(Guid id);
void UpdateConfiguredSensor(Guid id, AbstractSensor sensor);
void UpdateConfiguredCommand(Guid id, AbstractCommand command);
Task<IManagedMqttClientOptions> GetMqttClientOptionsAsync(); Task<IManagedMqttClientOptions> GetMqttClientOptionsAsync();
void ReadSensorSettings(MqttPublisher publisher); void ReadSensorSettings(MqttPublisher publisher);
void WriteMqttBrokerSettingsAsync(MqttSettings settings); void WriteMqttBrokerSettingsAsync(MqttSettings settings);
@ -27,8 +32,6 @@ namespace hass_workstation_service.Data
Task<MqttSettings> GetMqttBrokerSettings(); Task<MqttSettings> GetMqttBrokerSettings();
void EnableAutoStart(bool enable); void EnableAutoStart(bool enable);
bool IsAutoStartEnabled(); bool IsAutoStartEnabled();
void DeleteConfiguredSensor(Guid id);
void DeleteConfiguredCommand(Guid id);
void WriteCommandSettingsAsync(); void WriteCommandSettingsAsync();
void ReadCommandSettings(MqttPublisher publisher); void ReadCommandSettings(MqttPublisher publisher);
Task<ICollection<AbstractSensor>> GetSensorsAfterLoadingAsync(); Task<ICollection<AbstractSensor>> GetSensorsAfterLoadingAsync();

Loading…
Cancel
Save