add support for custom scopes for wmi sensors

pull/163/head
Sleevezipper 3 years ago
parent 06d132c570
commit f43ce8f9bf

@ -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); }
}
}

@ -21,6 +21,8 @@
<TextBox Text="{Binding UpdateInterval}" HorizontalAlignment="Right" MaxWidth="30"/>
</StackPanel>
<TextBlock Text="{Binding UpdateInterval, StringFormat= Update every {0} seconds}" HorizontalAlignment="Left" MinWidth="150"/>
<ContentControl IsVisible="{Binding ShowQueryInput}" Margin="0 20 0 10">Scope (optional)</ContentControl>
<TextBox IsVisible="{Binding ShowQueryInput}" Text="{Binding Scope}" Watermark="\\localhost\ROOT\StandardCimv2" HorizontalAlignment="Left" MinWidth="300"/>
<ContentControl IsVisible="{Binding ShowQueryInput}" Margin="0 20 0 10">Query</ContentControl>
<TextBox IsVisible="{Binding ShowQueryInput}" Text="{Binding Query}" Watermark="SELECT Name FROM Win32_Processor" HorizontalAlignment="Left" MinWidth="300"/>
<ContentControl IsVisible="{Binding ShowWindowNameInput}" Margin="0 20 0 5">Window name</ContentControl>

@ -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));

@ -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),

@ -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)
{

@ -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)

@ -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; }
}

@ -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()
{

Loading…
Cancel
Save