Update ConfigurationServices with methods to update sensors / commands

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

@ -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<AbstractSensor> 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<AbstractCommand> 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<AbstractSensor> 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);
}
/// <summary>

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

Loading…
Cancel
Save