stop using isolated storage and use AppData/local for config and logs

pull/9/head 1.0.0.0
sleevezipper 4 years ago
parent 1253d8fab9
commit b3f99806ca

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.IsolatedStorage;
using System.Linq; using System.Linq;
using System.Security; using System.Security;
using System.Text.Json; using System.Text.Json;
@ -27,23 +26,21 @@ namespace hass_workstation_service.Data
public bool _brokerSettingsFileLocked { get; set; } public bool _brokerSettingsFileLocked { get; set; }
public bool _sensorsSettingsFileLocked { get; set; } public bool _sensorsSettingsFileLocked { get; set; }
private readonly IsolatedStorageFile _fileStorage; private readonly string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Hass Workstation Service");
public ConfigurationService() public ConfigurationService()
{ {
this._fileStorage = IsolatedStorageFile.GetUserStoreForApplication(); if (!File.Exists(Path.Combine(path, "mqttbroker.json")))
ConfiguredSensors = new List<AbstractSensor>();
if (!this._fileStorage.FileExists("mqttbroker.json"))
{ {
this._fileStorage.CreateFile("mqttbroker.json"); File.Create(Path.Combine(path, "mqttbroker.json"));
} }
if (!this._fileStorage.FileExists("configured-sensors.json")) if (!File.Exists(Path.Combine(path, "configured-sensors.json")))
{ {
this._fileStorage.CreateFile("configured-sensors.json"); File.Create(Path.Combine(path, "configured-sensors.json"));
} }
ConfiguredSensors = new List<AbstractSensor>();
} }
public async void ReadSensorSettings(MqttPublisher publisher) public async void ReadSensorSettings(MqttPublisher publisher)
@ -54,7 +51,7 @@ namespace hass_workstation_service.Data
} }
this._sensorsSettingsFileLocked = true; this._sensorsSettingsFileLocked = true;
List<ConfiguredSensor> sensors = new List<ConfiguredSensor>(); List<ConfiguredSensor> sensors = new List<ConfiguredSensor>();
using (var stream = this._fileStorage.OpenFile("configured-sensors.json", FileMode.Open)) using (var stream = new FileStream(Path.Combine(path, "configured-sensors.json"), FileMode.Open))
{ {
Log.Logger.Information($"reading configured sensors from: {stream.Name}"); Log.Logger.Information($"reading configured sensors from: {stream.Name}");
if (stream.Length > 0) if (stream.Length > 0)
@ -134,7 +131,7 @@ namespace hass_workstation_service.Data
} }
this._brokerSettingsFileLocked = true; this._brokerSettingsFileLocked = true;
ConfiguredMqttBroker configuredBroker = null; ConfiguredMqttBroker configuredBroker = null;
using (IsolatedStorageFileStream stream = this._fileStorage.OpenFile("mqttbroker.json", FileMode.Open)) using (FileStream stream = new FileStream(Path.Combine(path, "mqttbroker.json"), FileMode.Open))
{ {
Log.Logger.Information($"reading configured mqttbroker from: {stream.Name}"); Log.Logger.Information($"reading configured mqttbroker from: {stream.Name}");
if (stream.Length > 0) if (stream.Length > 0)
@ -156,7 +153,7 @@ namespace hass_workstation_service.Data
} }
this._sensorsSettingsFileLocked = true; this._sensorsSettingsFileLocked = true;
List<ConfiguredSensor> configuredSensorsToSave = new List<ConfiguredSensor>(); List<ConfiguredSensor> configuredSensorsToSave = new List<ConfiguredSensor>();
using (IsolatedStorageFileStream stream = this._fileStorage.OpenFile("configured-sensors.json", FileMode.Open)) using (FileStream stream = new FileStream(Path.Combine(path, "configured-sensors.json"), FileMode.Open))
{ {
stream.SetLength(0); stream.SetLength(0);
Log.Logger.Information($"writing configured sensors to: {stream.Name}"); Log.Logger.Information($"writing configured sensors to: {stream.Name}");
@ -171,7 +168,7 @@ namespace hass_workstation_service.Data
{ {
configuredSensorsToSave.Add(new ConfiguredSensor() { Id = sensor.Id, Name = sensor.Name, Type = sensor.GetType().Name, UpdateInterval = sensor.UpdateInterval }); configuredSensorsToSave.Add(new ConfiguredSensor() { Id = sensor.Id, Name = sensor.Name, Type = sensor.GetType().Name, UpdateInterval = sensor.UpdateInterval });
} }
} }
await JsonSerializer.SerializeAsync(stream, configuredSensorsToSave); await JsonSerializer.SerializeAsync(stream, configuredSensorsToSave);
@ -220,7 +217,7 @@ namespace hass_workstation_service.Data
await Task.Delay(500); await Task.Delay(500);
} }
this._brokerSettingsFileLocked = true; this._brokerSettingsFileLocked = true;
using (IsolatedStorageFileStream stream = this._fileStorage.OpenFile("mqttbroker.json", FileMode.Open)) using (FileStream stream = new FileStream(Path.Combine(path, "mqttbroker.json"), FileMode.Open))
{ {
stream.SetLength(0); stream.SetLength(0);
Log.Logger.Information($"writing configured mqttbroker to: {stream.Name}"); Log.Logger.Information($"writing configured mqttbroker to: {stream.Name}");

@ -26,10 +26,15 @@ namespace hass_workstation_service
{ {
public static async Task Main(string[] args) public static async Task Main(string[] args)
{ {
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Hass Workstation Service", "logs");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Log.Logger = new LoggerConfiguration() Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext() .Enrich.FromLogContext()
.WriteTo.Console() .WriteTo.Console()
.WriteTo.File(new RenderedCompactJsonFormatter(), "logs/log.ndjson") .WriteTo.File(new RenderedCompactJsonFormatter(), Path.Combine(path, "log.txt"), rollingInterval: RollingInterval.Day)
.CreateLogger(); .CreateLogger();
// We do it this way because there is currently no way to pass an argument to a dotnet core app when using clickonce // We do it this way because there is currently no way to pass an argument to a dotnet core app when using clickonce
if (Process.GetProcessesByName("hass-workstation-service").Count() > 1) //bg service running if (Process.GetProcessesByName("hass-workstation-service").Count() > 1) //bg service running

@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
--> -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<ApplicationRevision>23</ApplicationRevision> <ApplicationRevision>25</ApplicationRevision>
<ApplicationVersion>1.0.0.*</ApplicationVersion> <ApplicationVersion>1.0.0.*</ApplicationVersion>
<BootstrapperEnabled>True</BootstrapperEnabled> <BootstrapperEnabled>True</BootstrapperEnabled>
<Configuration>Release</Configuration> <Configuration>Release</Configuration>

Loading…
Cancel
Save