-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
162 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
hass-workstation-service/Domain/Sensors/WebcamProcessSensor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.