Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sleevezipper committed Dec 12, 2021
2 parents c54d545 + 06f87d0 commit d86513c
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 16 deletions.
7 changes: 7 additions & 0 deletions UserInterface/Views/AddSensorDialog.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ private void FillDefaultValues()
item.UpdateInterval = 10;
break;

case AvailableSensors.WebcamProcessSensor:
item.Description = "This sensor shows which process is using the webcam.";
item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service/blob/master/documentation/Sensors.md#webcamprocesssensor";
item.ShowQueryInput = false;
item.UpdateInterval = 10;
break;

case AvailableSensors.MicrophoneActiveSensor:
item.Description = "This sensor shows if the microphone is currently in use.";
item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service/blob/master/documentation/Sensors.md#microphoneactivesensor";
Expand Down
5 changes: 1 addition & 4 deletions UserInterface/Views/BackgroundServiceSettings.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
<ContentControl FontSize="18" FontWeight="Bold">Background service</ContentControl>
<TextBlock IsVisible="{Binding IsRunning}" Foreground="Green" Text="{Binding Message}"></TextBlock >
<TextBlock IsVisible="{Binding !IsRunning}" Foreground="Red" Text="{Binding Message}"></TextBlock >

<Button IsVisible="{Binding !IsRunning}" Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="Start">Start</Button>


<TextBlock TextWrapping="Wrap" IsVisible="{Binding !IsRunning}" Text="The UI will crash after a while if the service wasn't started first. Please make sure the service is running. If you used the installer and you are seeing this message, please check if your antivirus might be interfering. "></TextBlock >
</StackPanel>
<StackPanel HorizontalAlignment="Stretch">
<ContentControl FontSize="14" FontWeight="Bold">Autostart</ContentControl>
Expand Down
2 changes: 1 addition & 1 deletion UserInterface/Views/BrokerSettings.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450"
x:Class="UserInterface.Views.BrokerSettings">
<StackPanel Margin="30" HorizontalAlignment="Left">
<StackPanel Margin="30" HorizontalAlignment="Left" ScrollViewer.VerticalScrollBarVisibility="Auto">
<ContentControl FontSize="18" FontWeight="Bold">MQTT Broker</ContentControl>
<TextBlock IsVisible="{Binding IsConnected}" Foreground="Green" Text="{Binding Message}"></TextBlock >
<TextBlock IsVisible="{Binding !IsConnected}" Foreground="Red" Text="{Binding Message}"></TextBlock >
Expand Down
11 changes: 7 additions & 4 deletions UserInterface/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Window xmlns="https://github.com/avaloniaui"
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:UserInterface.ViewModels;assembly=UserInterface"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Expand All @@ -15,13 +15,16 @@
<vm:MainWindowViewModel/>
</Design.DataContext>

<Grid ColumnDefinitions="Auto, *, Auto" RowDefinitions="*, *" Margin="10">
<views:BrokerSettings Grid.Column="0" Grid.Row="0" Margin="10" Grid.RowSpan="2" Background="#2D2D30"/>
<Grid ColumnDefinitions="Auto, *, Auto" Margin="10">
<Grid.RowDefinitions>
<RowDefinition MinHeight="500"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<views:BrokerSettings Grid.Column="0" Grid.Row="0" Margin="10" Grid.RowSpan="2" MinHeight="500" Background="#2D2D30"/>
<views:GeneralSettingsView Grid.Column="0" Grid.Row="1" Margin="10" Grid.RowSpan="2" Background="#2D2D30"/>
<views:SensorSettings Grid.Column="1" Grid.Row="0" Margin="10" Background="#2D2D30"/>
<views:CommandSettings Grid.Column="1" Grid.Row="1" Margin="10" Background="#2D2D30"/>
<views:BackgroundServiceSettings Grid.Column="2" Grid.Row="0" Margin="10" Background="#2D2D30"/>
<views:AppInfo Grid.Column="2" Grid.Row="1" Margin="10" Background="#2D2D30"/>
</Grid>

</Window>
4 changes: 4 additions & 0 deletions documentation/Sensors.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ This sensor watches the UserNotificationState. This is normally used in applicat

The webcam active sensor returns the status of the webcam.

### WebcamProcessSensor

The webcam process sensor returns the process which is using the webcam.

### WMIQuerySensor

Please see the specific documentaion page [here](https://github.com/sleevezipper/hass-workstation-service/blob/master/documentation/WMIQuery.md#wmiquerysensor).
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ private AbstractSensor GetSensorToCreate(AvailableSensors sensorType, string jso
AvailableSensors.MemoryUsageSensor => new MemoryUsageSensor(_publisher, (int)model.UpdateInterval, model.Name),
AvailableSensors.ActiveWindowSensor => new ActiveWindowSensor(_publisher, (int)model.UpdateInterval, model.Name),
AvailableSensors.WebcamActiveSensor => new WebcamActiveSensor(_publisher, (int)model.UpdateInterval, model.Name),
AvailableSensors.WebcamProcessSensor => new WebcamProcessSensor(_publisher, (int)model.UpdateInterval, model.Name),
AvailableSensors.MicrophoneActiveSensor => new MicrophoneActiveSensor(_publisher, (int)model.UpdateInterval, model.Name),
AvailableSensors.NamedWindowSensor => new NamedWindowSensor(_publisher, model.WindowName, model.Name, (int)model.UpdateInterval),
AvailableSensors.LastActiveSensor => new LastActiveSensor(_publisher, (int)model.UpdateInterval, model.Name),
Expand Down Expand Up @@ -221,4 +222,4 @@ private AbstractCommand GetCommandToCreate(AvailableCommands commandType, string

public void WriteGeneralSettings(GeneralSettings settings) => _configurationService.WriteGeneralSettingsAsync(settings);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public enum AvailableSensors
WMIQuerySensor,
MemoryUsageSensor,
WebcamActiveSensor,
WebcamProcessSensor,
MicrophoneActiveSensor,
ActiveWindowSensor,
NamedWindowSensor,
Expand Down Expand Up @@ -120,4 +121,4 @@ public enum AvailableCommands
VolumeDownCommand,
MuteCommand
}
}
}
5 changes: 4 additions & 1 deletion hass-workstation-service/Data/ConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public async void ReadSensorSettings(MqttPublisher publisher)
case "WebcamActiveSensor":
sensor = new WebcamActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
break;
case "WebcamProcessSensor":
sensor = new WebcamProcessSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
break;
case "MicrophoneActiveSensor":
sensor = new MicrophoneActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
break;
Expand Down Expand Up @@ -619,4 +622,4 @@ public async Task<ICollection<AbstractSensor>> GetSensorsAfterLoadingAsync()
return this.ConfiguredSensors;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig()

public override string GetState()
{
return GetLastInputTime().ToString("s");
return GetLastInputTime().ToString("o", System.Globalization.CultureInfo.InvariantCulture);
}


Expand Down
4 changes: 2 additions & 2 deletions hass-workstation-service/Domain/Sensors/LastBootSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig()

public override string GetState()
{
return (DateTime.Now - TimeSpan.FromMilliseconds(GetTickCount64())).ToString("s");
return (DateTime.Now - TimeSpan.FromMilliseconds(GetTickCount64())).ToString("o", System.Globalization.CultureInfo.InvariantCulture);
}

[DllImport("kernel32")]
extern static UInt64 GetTickCount64();
}
}
}
130 changes: 130 additions & 0 deletions hass-workstation-service/Domain/Sensors/WebcamProcessSensor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using hass_workstation_service.Communication;
using Microsoft.Win32;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace hass_workstation_service.Domain.Sensors
{
public class WebcamProcessSensor : AbstractSensor
{
public WebcamProcessSensor(MqttPublisher publisher, int? updateInterval = null, string name = "WebcamProcess", Guid id = default) : base(publisher, name ?? "WebcamProcess", updateInterval ?? 10, id)
{
}

public override string GetState()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return IsWebCamInUseRegistry();
}
else
{
return "unsupported";
}
}
public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig()
{
return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel()
{
Name = this.Name,
NamePrefix = Publisher.NamePrefix,
Unique_id = this.Id.ToString(),
Device = this.Publisher.DeviceConfigModel,
State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state",
Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability"
});
}

[SupportedOSPlatform("windows")]
private string IsWebCamInUseRegistry()
{
using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam"))
{
foreach (var subKeyName in key.GetSubKeyNames())
{
// NonPackaged has multiple subkeys
if (subKeyName == "NonPackaged")
{
using (var nonpackagedkey = key.OpenSubKey(subKeyName))
{
foreach (var nonpackagedSubKeyName in nonpackagedkey.GetSubKeyNames())
{
using (var subKey = nonpackagedkey.OpenSubKey(nonpackagedSubKeyName))
{
if (subKey.GetValueNames().Contains("LastUsedTimeStop"))
{
var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1;
if (endTime <= 0)
{
return nonpackagedSubKeyName;
}
}
}
}
}
}
else
{
using (var subKey = key.OpenSubKey(subKeyName))
{
if (subKey.GetValueNames().Contains("LastUsedTimeStop"))
{
var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1;
if (endTime <= 0)
{
return subKeyName;
}
}
}
}
}
}

using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam"))
{
foreach (var subKeyName in key.GetSubKeyNames())
{
// NonPackaged has multiple subkeys
if (subKeyName == "NonPackaged")
{
using (var nonpackagedkey = key.OpenSubKey(subKeyName))
{
foreach (var nonpackagedSubKeyName in nonpackagedkey.GetSubKeyNames())
{
using (var subKey = nonpackagedkey.OpenSubKey(nonpackagedSubKeyName))
{
if (subKey.GetValueNames().Contains("LastUsedTimeStop"))
{
var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1;
if (endTime <= 0)
{
return nonpackagedSubKeyName;
}
}
}
}
}
}
else
{
using (var subKey = key.OpenSubKey(subKeyName))
{
if (subKey.GetValueNames().Contains("LastUsedTimeStop"))
{
var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1;
if (endTime <= 0)
{
return subKeyName;
}
}
}
}
}
}

return "off";
}
}
}
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>55</ApplicationRevision>
<ApplicationRevision>57</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.
Binary file modified hass-workstation-service/hass-workstation-service.exe
Binary file not shown.

0 comments on commit d86513c

Please sign in to comment.