diff --git a/UserInterface/ViewModels/AddSensorViewModel.cs b/UserInterface/ViewModels/AddSensorViewModel.cs index 4e22e0f..f458e82 100644 --- a/UserInterface/ViewModels/AddSensorViewModel.cs +++ b/UserInterface/ViewModels/AddSensorViewModel.cs @@ -13,6 +13,7 @@ namespace UserInterface.ViewModels private bool _showWindowNameInput; private string _moreInfoLink; private string _query; + private string _scope; private string _windowName; public AvailableSensors SelectedType { get => _selectedType; set => this.RaiseAndSetIfChanged(ref _selectedType, value); } @@ -23,6 +24,7 @@ namespace UserInterface.ViewModels public bool ShowWindowNameInput { get => _showWindowNameInput; set => this.RaiseAndSetIfChanged(ref _showWindowNameInput, value); } public string MoreInfoLink { get => _moreInfoLink; set => this.RaiseAndSetIfChanged(ref _moreInfoLink, value); } public string Query { get => _query; set => this.RaiseAndSetIfChanged(ref _query, value); } + public string Scope { get => _scope; set => this.RaiseAndSetIfChanged(ref _scope, value); } public string WindowName { get => _windowName; set => this.RaiseAndSetIfChanged(ref _windowName, value); } } } \ No newline at end of file diff --git a/UserInterface/Views/AddSensorDialog.axaml b/UserInterface/Views/AddSensorDialog.axaml index 8b3b295..865ab19 100644 --- a/UserInterface/Views/AddSensorDialog.axaml +++ b/UserInterface/Views/AddSensorDialog.axaml @@ -21,8 +21,10 @@ - Query - + Scope (optional) + + Query + Window name This is case-insensitive and loosely matched. A window called "Spotify Premium" will match "spotify" or "premium". diff --git a/UserInterface/Views/AddSensorDialog.axaml.cs b/UserInterface/Views/AddSensorDialog.axaml.cs index b75f2aa..ef1a29a 100644 --- a/UserInterface/Views/AddSensorDialog.axaml.cs +++ b/UserInterface/Views/AddSensorDialog.axaml.cs @@ -67,6 +67,7 @@ namespace UserInterface.Views item.Name = sensor.Name; item.UpdateInterval = sensor.UpdateInterval; item.Query = sensor.Query; + item.Scope = sensor.Scope; item.WindowName = sensor.WindowName; Title = $"Edit {sensor.Name}"; @@ -75,7 +76,7 @@ namespace UserInterface.Views public async void Save(object sender, RoutedEventArgs args) { var item = (AddSensorViewModel)DataContext; - dynamic model = new { item.Name, item.Query, item.UpdateInterval, item.WindowName }; + dynamic model = new { item.Name, item.Query, item.UpdateInterval, item.WindowName, item.Scope }; string json = JsonSerializer.Serialize(model); if (SensorId == Guid.Empty) await _client.InvokeAsync(x => x.AddSensor(item.SelectedType, json)); diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs index bf4b3a4..bc26f5b 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -173,7 +173,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication AvailableSensors.DummySensor => new DummySensor(_publisher, (int)model.UpdateInterval, model.Name), AvailableSensors.CurrentClockSpeedSensor => new CurrentClockSpeedSensor(_publisher, (int)model.UpdateInterval, model.Name), AvailableSensors.CPULoadSensor => new CPULoadSensor(_publisher, (int)model.UpdateInterval, model.Name), - AvailableSensors.WMIQuerySensor => new WMIQuerySensor(_publisher, model.Query, (int)model.UpdateInterval, model.Name), + AvailableSensors.WMIQuerySensor => new WMIQuerySensor(_publisher, model.Query, (int)model.UpdateInterval, model.Name, scope: model.Scope), 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), diff --git a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs index 5423707..e803f52 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs @@ -30,6 +30,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models public string Name { get; set; } public string Value { get; set; } public string Query { get; set; } + public string Scope { get; set; } public string WindowName { get; set; } public int UpdateInterval { get; set; } public string UnitOfMeasurement { get; set; } @@ -44,6 +45,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models if (sensor is WMIQuerySensor wMIQuerySensor) { this.Query = wMIQuerySensor.Query; + this.Scope = wMIQuerySensor.Scope; } if (sensor is NamedWindowSensor namedWindowSensor) { diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index 1cd59c4..88ba77c 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -157,7 +157,7 @@ namespace hass_workstation_service.Data break; // keep this one last! case "WMIQuerySensor": - sensor = new WMIQuerySensor(publisher, configuredSensor.Query, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); + sensor = new WMIQuerySensor(publisher, configuredSensor.Query, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id, configuredSensor.Scope); break; default: Log.Logger.Error("unsupported sensor type in config"); @@ -210,22 +210,22 @@ namespace hass_workstation_service.Data case "CustomCommand": command = new CustomCommand(publisher, configuredCommand.Command, configuredCommand.Name, configuredCommand.Id); break; - case "MediaPlayPauseCommand": + case "PlayPauseCommand": command = new PlayPauseCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; - case "MediaNextCommand": + case "NextCommand": command = new NextCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; - case "MediaPreviousCommand": + case "PreviousCommand": command = new PreviousCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; - case "MediaVolumeUpCommand": + case "VolumeUpCommand": command = new VolumeUpCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; - case "MediaVolumeDownCommand": + case "VolumeDownCommand": command = new VolumeDownCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; - case "MediaMuteCommand": + case "MuteCommand": command = new MuteCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; case "KeyCommand": @@ -412,7 +412,7 @@ namespace hass_workstation_service.Data if (sensor is WMIQuerySensor wmiSensor) { #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 }); + configuredSensorsToSave.Add(new ConfiguredSensor() { Id = wmiSensor.Id, Name = wmiSensor.Name, Type = wmiSensor.GetType().Name, UpdateInterval = wmiSensor.UpdateInterval, Query = wmiSensor.Query, Scope = wmiSensor.Scope }); #pragma warning restore CA1416 // Validate platform compatibility } else if (sensor is NamedWindowSensor namedWindowSensor) diff --git a/hass-workstation-service/Data/ConfiguredSensor.cs b/hass-workstation-service/Data/ConfiguredSensor.cs index dcb29eb..7a18d87 100644 --- a/hass-workstation-service/Data/ConfiguredSensor.cs +++ b/hass-workstation-service/Data/ConfiguredSensor.cs @@ -9,6 +9,7 @@ namespace hass_workstation_service.Data public Guid Id { get; set; } public string Name { get; set; } public string Query { get; set; } + public string Scope { get; set; } public int? UpdateInterval { get; set; } public string WindowName { get; set; } } diff --git a/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs b/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs index a0b6984..c1601cf 100644 --- a/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs +++ b/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs @@ -12,13 +12,26 @@ namespace hass_workstation_service.Domain.Sensors public class WMIQuerySensor : AbstractSensor { public string Query { get; private set; } + public string Scope { 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) : base(publisher, name ?? "WMIQuerySensor", updateInterval ?? 10, id) + public WMIQuerySensor(MqttPublisher publisher, string query, int? updateInterval = null, string name = "WMIQuerySensor", Guid id = default, string scope = "") : base(publisher, name ?? "WMIQuerySensor", updateInterval ?? 10, id) { this.Query = query; + this.Scope = scope; _objectQuery = new ObjectQuery(this.Query); - _searcher = new ManagementObjectSearcher(query); + ManagementScope managementscope; + // if we have a custom scope, use that + if (!string.IsNullOrWhiteSpace(scope)) + { + managementscope = new ManagementScope(scope); + } + // otherwise, use the default + else + { + managementscope = new ManagementScope(@"\\localhost\"); + } + _searcher = new ManagementObjectSearcher(managementscope, _objectQuery); } public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig() {