|
|
@ -3,30 +3,30 @@ using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO.IsolatedStorage;
|
|
|
|
using System.IO.IsolatedStorage;
|
|
|
|
using System.Text.Json;
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using hass_workstation_service.Communication;
|
|
|
|
using hass_workstation_service.Communication;
|
|
|
|
using hass_workstation_service.Domain.Sensors;
|
|
|
|
using hass_workstation_service.Domain.Sensors;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
|
|
using MQTTnet;
|
|
|
|
|
|
|
|
using MQTTnet.Client;
|
|
|
|
|
|
|
|
using MQTTnet.Client.Options;
|
|
|
|
using Serilog;
|
|
|
|
using Serilog;
|
|
|
|
|
|
|
|
|
|
|
|
namespace hass_workstation_service.Data
|
|
|
|
namespace hass_workstation_service.Data
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class ConfiguredSensorsService
|
|
|
|
public class ConfigurationService
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public ICollection<AbstractSensor> ConfiguredSensors { get; private set; }
|
|
|
|
public ICollection<AbstractSensor> ConfiguredSensors { get; private set; }
|
|
|
|
public IConfiguration Configuration { get; private set; }
|
|
|
|
|
|
|
|
private readonly MqttPublisher _publisher;
|
|
|
|
|
|
|
|
private readonly IsolatedStorageFile _fileStorage;
|
|
|
|
private readonly IsolatedStorageFile _fileStorage;
|
|
|
|
|
|
|
|
|
|
|
|
public ConfiguredSensorsService(MqttPublisher publisher)
|
|
|
|
public ConfigurationService()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
this._fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
|
|
|
|
this._fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
|
|
|
|
|
|
|
|
|
|
|
|
ConfiguredSensors = new List<AbstractSensor>();
|
|
|
|
ConfiguredSensors = new List<AbstractSensor>();
|
|
|
|
_publisher = publisher;
|
|
|
|
|
|
|
|
ReadSettings();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async void ReadSettings()
|
|
|
|
public async void ReadSensorSettings(MqttPublisher publisher)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
IsolatedStorageFileStream stream = this._fileStorage.OpenFile("configured-sensors.json", FileMode.OpenOrCreate);
|
|
|
|
IsolatedStorageFileStream stream = this._fileStorage.OpenFile("configured-sensors.json", FileMode.OpenOrCreate);
|
|
|
|
Log.Logger.Information($"reading configured sensors from: {stream.Name}");
|
|
|
|
Log.Logger.Information($"reading configured sensors from: {stream.Name}");
|
|
|
@ -35,25 +35,53 @@ namespace hass_workstation_service.Data
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sensors = await JsonSerializer.DeserializeAsync<List<ConfiguredSensor>>(stream);
|
|
|
|
sensors = await JsonSerializer.DeserializeAsync<List<ConfiguredSensor>>(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.Close();
|
|
|
|
foreach (ConfiguredSensor configuredSensor in sensors)
|
|
|
|
foreach (ConfiguredSensor configuredSensor in sensors)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
AbstractSensor sensor;
|
|
|
|
AbstractSensor sensor;
|
|
|
|
#pragma warning disable IDE0066
|
|
|
|
|
|
|
|
switch (configuredSensor.Type)
|
|
|
|
switch (configuredSensor.Type)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
case "UserNotificationStateSensor":
|
|
|
|
case "UserNotificationStateSensor":
|
|
|
|
sensor = new UserNotificationStateSensor(_publisher, configuredSensor.Name, configuredSensor.Id);
|
|
|
|
sensor = new UserNotificationStateSensor(publisher, configuredSensor.Name, configuredSensor.Id);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case "DummySensor":
|
|
|
|
case "DummySensor":
|
|
|
|
sensor = new DummySensor(_publisher, configuredSensor.Name, configuredSensor.Id);
|
|
|
|
sensor = new DummySensor(publisher, configuredSensor.Name, configuredSensor.Id);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
throw new InvalidOperationException("unsupported sensor type in config");
|
|
|
|
throw new InvalidOperationException("unsupported sensor type in config");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.ConfiguredSensors.Add(sensor);
|
|
|
|
this.ConfiguredSensors.Add(sensor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IMqttClientOptions> ReadMqttSettings()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
IsolatedStorageFileStream stream = this._fileStorage.OpenFile("mqttbroker.json", FileMode.OpenOrCreate);
|
|
|
|
|
|
|
|
Log.Logger.Information($"reading configured mqttbroker from: {stream.Name}");
|
|
|
|
|
|
|
|
ConfiguredMqttBroker configuredBroker = null;
|
|
|
|
|
|
|
|
if (stream.Length > 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
configuredBroker = await JsonSerializer.DeserializeAsync<ConfiguredMqttBroker>(stream);
|
|
|
|
|
|
|
|
}
|
|
|
|
stream.Close();
|
|
|
|
stream.Close();
|
|
|
|
|
|
|
|
if (configuredBroker != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var mqttClientOptions = new MqttClientOptionsBuilder()
|
|
|
|
|
|
|
|
.WithTcpServer(configuredBroker.Host)
|
|
|
|
|
|
|
|
// .WithTls()
|
|
|
|
|
|
|
|
.WithCredentials(configuredBroker.Username, configuredBroker.Password.ToString())
|
|
|
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
return mqttClientOptions;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//for now we return defaults until we can actually configure this
|
|
|
|
|
|
|
|
return new MqttClientOptionsBuilder()
|
|
|
|
|
|
|
|
.WithTcpServer("192.168.2.6")
|
|
|
|
|
|
|
|
// .WithTls()
|
|
|
|
|
|
|
|
.WithCredentials("tester", "tester")
|
|
|
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async void WriteSettings()
|
|
|
|
public async void WriteSettings()
|
|
|
@ -64,7 +92,7 @@ namespace hass_workstation_service.Data
|
|
|
|
|
|
|
|
|
|
|
|
foreach (AbstractSensor sensor in this.ConfiguredSensors)
|
|
|
|
foreach (AbstractSensor sensor in this.ConfiguredSensors)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
configuredSensorsToSave.Add(new ConfiguredSensor(){Id = sensor.Id, Name = sensor.Name, Type = sensor.GetType().Name});
|
|
|
|
configuredSensorsToSave.Add(new ConfiguredSensor() { Id = sensor.Id, Name = sensor.Name, Type = sensor.GetType().Name });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await JsonSerializer.SerializeAsync(stream, configuredSensorsToSave);
|
|
|
|
await JsonSerializer.SerializeAsync(stream, configuredSensorsToSave);
|
|
|
@ -76,5 +104,11 @@ namespace hass_workstation_service.Data
|
|
|
|
this.ConfiguredSensors.Add(sensor);
|
|
|
|
this.ConfiguredSensors.Add(sensor);
|
|
|
|
WriteSettings();
|
|
|
|
WriteSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void AddConfiguredSensors(List<AbstractSensor> sensors)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
sensors.ForEach((sensor) => this.ConfiguredSensors.Add(sensor));
|
|
|
|
|
|
|
|
WriteSettings();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|