fix some compiler warnings and explicitly define platform for some stuff

notify^2
sleevezipper 4 years ago
parent dfc8d6cdf3
commit 67e551bc6f

@ -8,7 +8,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<Platform>Any CPU</Platform>
<PublishDir>..\hass-workstation-service\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<PublishSingleFile>True</PublishSingleFile>

@ -8,7 +8,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<Platform>Any CPU</Platform>
<PublishDir>bin\Userinterface-standalone</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>

@ -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; }
public bool _brokerSettingsFileLocked { get; set; }
public bool _sensorsSettingsFileLocked { get; set; }
private bool BrokerSettingsFileLocked { get; set; }
private bool SensorsSettingsFileLocked { 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._sensorsSettingsFileLocked)
while (this.SensorsSettingsFileLocked)
{
await Task.Delay(500);
}
this._sensorsSettingsFileLocked = true;
this.SensorsSettingsFileLocked = 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._sensorsSettingsFileLocked = false;
this.SensorsSettingsFileLocked = 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._brokerSettingsFileLocked)
while (this.BrokerSettingsFileLocked)
{
await Task.Delay(500);
}
this._brokerSettingsFileLocked = true;
this.BrokerSettingsFileLocked = 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._brokerSettingsFileLocked = false;
this.BrokerSettingsFileLocked = false;
return configuredBroker;
}
public async void WriteSettingsAsync()
{
while (this._sensorsSettingsFileLocked)
while (this.SensorsSettingsFileLocked)
{
await Task.Delay(500);
}
this._sensorsSettingsFileLocked = true;
this.SensorsSettingsFileLocked = 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._sensorsSettingsFileLocked = false;
this.SensorsSettingsFileLocked = 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._brokerSettingsFileLocked)
while (this.BrokerSettingsFileLocked)
{
await Task.Delay(500);
}
this._brokerSettingsFileLocked = true;
this.BrokerSettingsFileLocked = 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._brokerSettingsFileLocked = false;
this.BrokerSettingsFileLocked = 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);

@ -19,7 +19,7 @@ namespace hass_workstation_service.Domain.Sensors
public MqttPublisher Publisher { get; protected set; }
public AbstractSensor(MqttPublisher publisher, string name, int updateInterval = 10, Guid id = default(Guid))
{
if (id == Guid.Empty || id == null)
if (id == Guid.Empty)
{
this.Id = Guid.NewGuid();
}

@ -4,10 +4,13 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Management;
using System.Runtime.Versioning;
using System.Text;
namespace hass_workstation_service.Domain.Sensors
{
[SupportedOSPlatform("windows")]
public class CPULoadSensor : WMIQuerySensor
{
public CPULoadSensor(MqttPublisher publisher, int? updateInterval = null, string name = "CPULoadSensor", Guid id = default) : base(publisher, "SELECT PercentProcessorTime FROM Win32_PerfFormattedData_PerfOS_Processor", updateInterval ?? 10, name ?? "CPULoadSensor", id)
@ -26,6 +29,8 @@ namespace hass_workstation_service.Domain.Sensors
Unit_of_measurement = "%"
});
}
[SupportedOSPlatform("windows")]
public override string GetState()
{
ManagementObjectCollection collection = _searcher.Get();

@ -3,10 +3,13 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Management;
using System.Runtime.Versioning;
using System.Text;
namespace hass_workstation_service.Domain.Sensors
{
[SupportedOSPlatform("windows")]
public class MemoryUsageSensor : WMIQuerySensor
{
public MemoryUsageSensor(MqttPublisher publisher, int? updateInterval = null, string name = "WMIQuerySensor", Guid id = default) : base(publisher, "SELECT FreePhysicalMemory,TotalVisibleMemorySize FROM Win32_OperatingSystem", updateInterval ?? 10, name, id)

@ -2,6 +2,8 @@
using Microsoft.Win32;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace hass_workstation_service.Domain.Sensors
{
@ -13,7 +15,11 @@ namespace hass_workstation_service.Domain.Sensors
}
public override string GetState()
{
return IsMicrophoneInUse() ? "True" : "False";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return IsMicrophoneInUse() ? "True" : "False";
}
else return "unsupported";
}
public override AutoDiscoveryConfigModel GetAutoDiscoveryConfig()
{
@ -27,6 +33,7 @@ namespace hass_workstation_service.Domain.Sensors
});
}
[SupportedOSPlatform("windows")]
private bool IsMicrophoneInUse()
{
using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\microphone\NonPackaged"))

@ -2,16 +2,19 @@
using System;
using System.Collections.Generic;
using System.Management;
using System.Runtime.Versioning;
using System.Text;
namespace hass_workstation_service.Domain.Sensors
{
[SupportedOSPlatform("windows")]
public class WMIQuerySensor : AbstractSensor
{
public string Query { get; private set; }
protected readonly ObjectQuery _objectQuery;
protected readonly ManagementObjectSearcher _searcher;
public WMIQuerySensor(MqttPublisher publisher, string query, int? updateInterval = null, string name = "WMIQuerySensor", Guid id = default(Guid)) : base(publisher, name ?? "WMIQuerySensor", updateInterval ?? 10, id)
public WMIQuerySensor(MqttPublisher publisher, string query, int? updateInterval = null, string name = "WMIQuerySensor", Guid id = default) : base(publisher, name ?? "WMIQuerySensor", updateInterval ?? 10, id)
{
this.Query = query;
_objectQuery = new ObjectQuery(this.Query);

@ -3,6 +3,8 @@ using Microsoft.Win32;
using OpenCvSharp;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace hass_workstation_service.Domain.Sensors
{
@ -14,22 +16,28 @@ namespace hass_workstation_service.Domain.Sensors
public class WebcamActiveSensor : AbstractSensor
{
public DetectionMode DetectionMode { get; private set; }
public WebcamActiveSensor(MqttPublisher publisher, int? updateInterval = null, string name = "WebcamActive", DetectionMode detectionMode = DetectionMode.Registry, Guid id = default(Guid)) : base(publisher, name, updateInterval ?? 10, id)
public WebcamActiveSensor(MqttPublisher publisher, int? updateInterval = null, string name = "WebcamActive", DetectionMode detectionMode = DetectionMode.Registry, Guid id = default(Guid)) : base(publisher, name, updateInterval ?? 10, id)
{
this.DetectionMode = detectionMode;
}
public override string GetState()
{
switch (this.DetectionMode)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
case DetectionMode.Registry:
return IsWebCamInUseRegistry() ? "True" : "False";
case DetectionMode.OpenCV:
return IsWebCamInUseOpenCV() ? "True" : "False";
default:
return "Error";
switch (this.DetectionMode)
{
case DetectionMode.Registry:
return IsWebCamInUseRegistry() ? "True" : "False";
case DetectionMode.OpenCV:
return IsWebCamInUseOpenCV() ? "True" : "False";
default:
return "Error";
}
}
else
{
return "unsopported";
}
}
public override AutoDiscoveryConfigModel GetAutoDiscoveryConfig()
{
@ -63,7 +71,7 @@ namespace hass_workstation_service.Domain.Sensors
capture.Dispose();
return true;
}
}
catch (Exception)
{
@ -72,6 +80,7 @@ namespace hass_workstation_service.Domain.Sensors
}
}
[SupportedOSPlatform("windows")]
private bool IsWebCamInUseRegistry()
{
using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam\NonPackaged"))

@ -33,15 +33,15 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<SignatureAlgorithm>sha256RSA</SignatureAlgorithm>
<SignManifests>True</SignManifests>
<SupportUrl>https://github.com/sleevezipper/hass-workstation-service</SupportUrl>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<TrustUrlParameters>True</TrustUrlParameters>
<UpdateEnabled>True</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
</PropertyGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.NetCore.CoreRuntime.3.1.x64">
<BootstrapperPackage Include="Microsoft.NetCore.CoreRuntime.5.0.x64">
<Install>true</Install>
<ProductName>.NET Core Runtime 3.1.10 (x64)</ProductName>
<ProductName>.NET Runtime 5.0.1 (x64)</ProductName>
</BootstrapperPackage>
</ItemGroup>
</Project>

@ -28,14 +28,14 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<SelfContained>False</SelfContained>
<SignatureAlgorithm>sha256RSA</SignatureAlgorithm>
<SignManifests>True</SignManifests>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<UpdateEnabled>False</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
</PropertyGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.NetCore.CoreRuntime.3.1.x64">
<BootstrapperPackage Include="Microsoft.NetCore.CoreRuntime.5.0.x64">
<Install>true</Install>
<ProductName>.NET Core Runtime 3.1.10 (x64)</ProductName>
<ProductName>.NET Runtime 5.0.1 (x64)</ProductName>
</BootstrapperPackage>
</ItemGroup>
</Project>

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.*</ApplicationVersion>
<BootstrapperEnabled>True</BootstrapperEnabled>
<Configuration>Release</Configuration>
<GenerateManifests>True</GenerateManifests>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<IsRevisionIncremented>True</IsRevisionIncremented>
<IsWebBootstrapper>False</IsWebBootstrapper>
<MapFileExtensions>true</MapFileExtensions>
<OpenBrowserOnPublish>false</OpenBrowserOnPublish>
<Platform>Any CPU</Platform>
<PublishDir>bin\publish\</PublishDir>
<PublishUrl>bin\publish\</PublishUrl>
<PublishProtocol>ClickOnce</PublishProtocol>
<PublishReadyToRun>False</PublishReadyToRun>
<PublishSingleFile>False</PublishSingleFile>
<SelfContained>False</SelfContained>
<SignatureAlgorithm>(none)</SignatureAlgorithm>
<SignManifests>False</SignManifests>
<TargetFramework>net5.0</TargetFramework>
<UpdateEnabled>False</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
</PropertyGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.NetCore.CoreRuntime.5.0.x64">
<Install>true</Install>
<ProductName>.NET Runtime 5.0.1 (x64)</ProductName>
</BootstrapperPackage>
</ItemGroup>
</Project>

@ -8,7 +8,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<Platform>Any CPU</Platform>
<PublishDir>bin\Manual\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>False</PublishSingleFile>

@ -28,14 +28,14 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<SelfContained>False</SelfContained>
<SignatureAlgorithm>sha256RSA</SignatureAlgorithm>
<SignManifests>True</SignManifests>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<UpdateEnabled>False</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
</PropertyGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.NetCore.CoreRuntime.3.1.x64">
<BootstrapperPackage Include="Microsoft.NetCore.CoreRuntime.5.0.x64">
<Install>true</Install>
<ProductName>.NET Core Runtime 3.1.10 (x64)</ProductName>
<ProductName>.NET Runtime 5.0.1 (x64)</ProductName>
</BootstrapperPackage>
</ItemGroup>
</Project>
Loading…
Cancel
Save