@ -2,6 +2,8 @@ using System;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Runtime.InteropServices ;
using System.Runtime.Versioning ;
using System.Security ;
using System.Text.Json ;
using System.Threading.Tasks ;
@ -23,8 +25,8 @@ namespace hass_workstation_service.Data
public ICollection < AbstractSensor > ConfiguredSensors { get ; private set ; }
public Action < IMqttClientOptions > MqqtConfigChangedHandler { get ; set ; }
p ublic bool _b rokerSettingsFileLocked { get ; set ; }
p ublic bool _s ensorsSettingsFileLocked { get ; set ; }
p rivate bool B rokerSettingsFileLocked { get ; set ; }
p rivate bool S ensorsSettingsFileLocked { get ; set ; }
private readonly string path = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , "Hass Workstation Service" ) ;
@ -45,11 +47,11 @@ namespace hass_workstation_service.Data
public async void ReadSensorSettings ( MqttPublisher publisher )
{
while ( this . _s ensorsSettingsFileLocked)
while ( this . S ensorsSettingsFileLocked)
{
await Task . Delay ( 500 ) ;
}
this . _s ensorsSettingsFileLocked = true ;
this . S ensorsSettingsFileLocked = true ;
List < ConfiguredSensor > sensors = new List < ConfiguredSensor > ( ) ;
using ( var stream = new FileStream ( Path . Combine ( path , "configured-sensors.json" ) , FileMode . Open ) )
{
@ -59,7 +61,7 @@ namespace hass_workstation_service.Data
sensors = await JsonSerializer . DeserializeAsync < List < ConfiguredSensor > > ( stream ) ;
}
stream . Close ( ) ;
this . _s ensorsSettingsFileLocked = false ;
this . S ensorsSettingsFileLocked = false ;
}
foreach ( ConfiguredSensor configuredSensor in sensors )
@ -83,7 +85,10 @@ namespace hass_workstation_service.Data
sensor = new CPULoadSensor ( publisher , configuredSensor . UpdateInterval , configuredSensor . Name , configuredSensor . Id ) ;
break ;
case "MemoryUsageSensor" :
sensor = new MemoryUsageSensor ( publisher , configuredSensor . UpdateInterval , configuredSensor . Name , configuredSensor . Id ) ;
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
{
sensor = new MemoryUsageSensor ( publisher , configuredSensor . UpdateInterval , configuredSensor . Name , configuredSensor . Id ) ;
}
break ;
case "ActiveWindowSensor" :
sensor = new ActiveWindowSensor ( publisher , configuredSensor . UpdateInterval , configuredSensor . Name , configuredSensor . Id ) ;
@ -137,11 +142,11 @@ namespace hass_workstation_service.Data
/// <returns></returns>
public async Task < ConfiguredMqttBroker > ReadMqttSettingsAsync ( )
{
while ( this . _b rokerSettingsFileLocked)
while ( this . B rokerSettingsFileLocked)
{
await Task . Delay ( 500 ) ;
}
this . _b rokerSettingsFileLocked = true ;
this . B rokerSettingsFileLocked = true ;
ConfiguredMqttBroker configuredBroker = null ;
using ( FileStream stream = new FileStream ( Path . Combine ( path , "mqttbroker.json" ) , FileMode . Open ) )
{
@ -153,17 +158,17 @@ namespace hass_workstation_service.Data
stream . Close ( ) ;
}
this . _b rokerSettingsFileLocked = false ;
this . B rokerSettingsFileLocked = false ;
return configuredBroker ;
}
public async void WriteSettingsAsync ( )
{
while ( this . _s ensorsSettingsFileLocked)
while ( this . S ensorsSettingsFileLocked)
{
await Task . Delay ( 500 ) ;
}
this . _s ensorsSettingsFileLocked = true ;
this . S ensorsSettingsFileLocked = true ;
List < ConfiguredSensor > configuredSensorsToSave = new List < ConfiguredSensor > ( ) ;
using ( FileStream stream = new FileStream ( Path . Combine ( path , "configured-sensors.json" ) , FileMode . Open ) )
{
@ -171,14 +176,14 @@ namespace hass_workstation_service.Data
Log . Logger . Information ( $"writing configured sensors to: {stream.Name}" ) ;
foreach ( AbstractSensor sensor in this . ConfiguredSensors )
{
if ( sensor is WMIQuerySensor )
if ( sensor is WMIQuerySensor wmiSensor )
{
var wmiSensor = ( WMIQuerySensor ) sensor ;
#pragma warning disable CA1416 // Validate platform compatibility. We ignore it here because this would never happen. A cleaner solution may be implemented later.
configuredSensorsToSave . Add ( new ConfiguredSensor ( ) { Id = wmiSensor . Id , Name = wmiSensor . Name , Type = wmiSensor . GetType ( ) . Name , UpdateInterval = wmiSensor . UpdateInterval , Query = wmiSensor . Query } ) ;
#pragma warning restore CA1416 // Validate platform compatibility
}
if ( sensor is NamedWindowSensor )
if ( sensor is NamedWindowSensor namedWindowSensor )
{
var namedWindowSensor = ( NamedWindowSensor ) sensor ;
configuredSensorsToSave . Add ( new ConfiguredSensor ( ) { Id = namedWindowSensor . Id , Name = namedWindowSensor . Name , Type = namedWindowSensor . GetType ( ) . Name , UpdateInterval = namedWindowSensor . UpdateInterval , WindowName = namedWindowSensor . WindowName } ) ;
}
else
@ -191,7 +196,7 @@ namespace hass_workstation_service.Data
await JsonSerializer . SerializeAsync ( stream , configuredSensorsToSave ) ;
stream . Close ( ) ;
}
this . _s ensorsSettingsFileLocked = false ;
this . S ensorsSettingsFileLocked = false ;
}
public void AddConfiguredSensor ( AbstractSensor sensor )
@ -229,11 +234,11 @@ namespace hass_workstation_service.Data
/// <param name="settings"></param>
public async void WriteMqttBrokerSettingsAsync ( MqttSettings settings )
{
while ( this . _b rokerSettingsFileLocked)
while ( this . B rokerSettingsFileLocked)
{
await Task . Delay ( 500 ) ;
}
this . _b rokerSettingsFileLocked = true ;
this . B rokerSettingsFileLocked = true ;
using ( FileStream stream = new FileStream ( Path . Combine ( path , "mqttbroker.json" ) , FileMode . Open ) )
{
stream . SetLength ( 0 ) ;
@ -249,7 +254,7 @@ namespace hass_workstation_service.Data
await JsonSerializer . SerializeAsync ( stream , configuredBroker ) ;
stream . Close ( ) ;
}
this . _b rokerSettingsFileLocked = false ;
this . B rokerSettingsFileLocked = false ;
this . MqqtConfigChangedHandler . Invoke ( await this . GetMqttClientOptionsAsync ( ) ) ;
}
@ -268,6 +273,7 @@ namespace hass_workstation_service.Data
/// Enable or disable autostarting the background service. It does this by adding the application shortcut (appref-ms) to the registry run key for the current user
/// </summary>
/// <param name="enable"></param>
[SupportedOSPlatform("windows")]
public void EnableAutoStart ( bool enable )
{
if ( enable )
@ -302,6 +308,7 @@ namespace hass_workstation_service.Data
}
}
[SupportedOSPlatform("windows")]
public bool IsAutoStartEnabled ( )
{
RegistryKey rkApp = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" , true ) ;