Skip to content

Commit

Permalink
stop using isolated storage and use AppData/local for config and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sleevezipper committed Jan 3, 2021
1 parent 1253d8f commit b3f9980
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
Binary file modified .vs/hass-workstation-service/v16/.suo
Binary file not shown.
27 changes: 12 additions & 15 deletions hass-workstation-service/Data/ConfigurationService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Security;
using System.Text.Json;
Expand All @@ -27,23 +26,21 @@ public class ConfigurationService : IConfigurationService
public bool _brokerSettingsFileLocked { 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()
{
this._fileStorage = IsolatedStorageFile.GetUserStoreForApplication();

ConfiguredSensors = new List<AbstractSensor>();

if (!this._fileStorage.FileExists("mqttbroker.json"))
if (!File.Exists(Path.Combine(path, "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)
Expand All @@ -54,7 +51,7 @@ public async void ReadSensorSettings(MqttPublisher publisher)
}
this._sensorsSettingsFileLocked = true;
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}");
if (stream.Length > 0)
Expand Down Expand Up @@ -134,7 +131,7 @@ public async Task<ConfiguredMqttBroker> ReadMqttSettingsAsync()
}
this._brokerSettingsFileLocked = true;
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}");
if (stream.Length > 0)
Expand All @@ -156,7 +153,7 @@ public async void WriteSettingsAsync()
}
this._sensorsSettingsFileLocked = true;
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);
Log.Logger.Information($"writing configured sensors to: {stream.Name}");
Expand All @@ -171,7 +168,7 @@ public async void WriteSettingsAsync()
{
configuredSensorsToSave.Add(new ConfiguredSensor() { Id = sensor.Id, Name = sensor.Name, Type = sensor.GetType().Name, UpdateInterval = sensor.UpdateInterval });
}

}

await JsonSerializer.SerializeAsync(stream, configuredSensorsToSave);
Expand Down Expand Up @@ -220,7 +217,7 @@ public async void WriteMqttBrokerSettingsAsync(MqttSettings settings)
await Task.Delay(500);
}
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);
Log.Logger.Information($"writing configured mqttbroker to: {stream.Name}");
Expand Down
7 changes: 6 additions & 1 deletion hass-workstation-service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ public class Program
{
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()
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File(new RenderedCompactJsonFormatter(), "logs/log.ndjson")
.WriteTo.File(new RenderedCompactJsonFormatter(), Path.Combine(path, "log.txt"), rollingInterval: RollingInterval.Day)
.CreateLogger();
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ApplicationRevision>23</ApplicationRevision>
<ApplicationRevision>25</ApplicationRevision>
<ApplicationVersion>1.0.0.*</ApplicationVersion>
<BootstrapperEnabled>True</BootstrapperEnabled>
<Configuration>Release</Configuration>
Expand Down
Binary file modified hass-workstation-service/UserInterface.exe
Binary file not shown.

0 comments on commit b3f9980

Please sign in to comment.