Skip to content

Commit

Permalink
Proper implementation of regex replacement for the capture names
Browse files Browse the repository at this point in the history
This means you can now put a "regex replace" string in the "CaptureAs" field in the snapshot config and it will use any capture groups specified in the "Name" field as the source input.

This is great for creating a single sensor entry that will grab and enumerate every cpu core (for example)
  • Loading branch information
DanForever committed Feb 14, 2022
1 parent f19ed7b commit 0fb15a1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 45 deletions.
60 changes: 27 additions & 33 deletions Windows/Workhorse/Monitor/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,38 @@ namespace HardwareMonitor.Monitor.Config
{
public abstract class CaptureDescriptor
{
#region Private fields

private Regex _nameRegex = null;

#endregion Private fields

#region Public Properties

/// <summary>
/// The name of the hardware or sensor to look for. Regex (where appropriate)
/// </summary>
[XmlAttribute()]
public string Name { get; set; }

/// <summary>
/// A unique Id used to identify this captured value from the layouts xml. Regex
/// </summary>
[XmlAttribute("CaptureAs")]
public string CaptureName { get; set; }

[XmlIgnore()]
public Regex NameRegex
{
get
{
if (_nameRegex == null)
_nameRegex = new Regex(Name);

return _nameRegex;
}
}

#endregion Public Properties
}

Expand All @@ -48,20 +75,8 @@ public class Component : CaptureDescriptor
/// </summary>
public class Sensor : CaptureDescriptor
{
#region Private fields

private Regex _nameRegex = null;

#endregion Private fields

#region Public Properties

/// <summary>
/// The name of the sensor to look for. Regex
/// </summary>
[XmlAttribute()]
public string Name { get; set; }

/// <summary>
/// The type of hardware the sensor is tied to
/// </summary>
Expand All @@ -74,18 +89,6 @@ public class Sensor : CaptureDescriptor
[XmlAttribute()]
public SensorType Type { get; set; }

[XmlIgnore()]
public Regex NameRegex
{
get
{
if (_nameRegex == null)
_nameRegex = new Regex(Name);

return _nameRegex;
}
}

#endregion Public Properties
}

Expand All @@ -99,15 +102,6 @@ public class CompoundSensor : CaptureDescriptor

#region Public Properties

/// <summary>
/// User defined name
/// </summary>
/// <remarks>
/// The compount sensor will appear as a regular sensor in the snapshot, so this is used as the display name
/// </remarks>
[XmlAttribute()]
public string Name { get; set; }

/// <summary>
/// How the values of all the sensors will be aggregated
/// </summary>
Expand Down
26 changes: 21 additions & 5 deletions Windows/Workhorse/Monitor/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ public class Capture
public dynamic Value { get; set; }

#endregion Public Properties

public Capture(Config.Component config, Plugin.IHardware hardware)
{
if (string.IsNullOrEmpty(config.Name))
Name = config.CaptureName;
else
Name = config.NameRegex.Replace(hardware.Name, config.CaptureName);
Value = hardware.Name;
}

public Capture(Config.Sensor config, Plugin.ISensor sensor)
{
Name = config.NameRegex.Replace(sensor.Name, config.CaptureName);
Value = sensor.Value;
}

public Capture(string name, dynamic value)
{
Name = name;
Value = value;
}
}

/// <summary>
Expand All @@ -94,16 +115,11 @@ public class Snapshot
{
#region Private fields

private List<HardwareSample> _hardwareSamples = new List<HardwareSample>();
private List<SensorSample> _sensorSamples = new List<SensorSample>();
private Dictionary<string, Capture> _captures = new Dictionary<string, Capture>();

#endregion Private fields

#region Public Properties

public List<HardwareSample> HardwareSamples => _hardwareSamples;
public List<SensorSample> SensorSamples => _sensorSamples;
public Dictionary<string, Capture> Captures => _captures;

#endregion Public Properties
Expand Down
17 changes: 10 additions & 7 deletions Windows/Workhorse/Monitor/Watcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ private static void PollHardware(IEnumerable<Plugin.IHardware> hardware, PollDat
if (watchedHardware == null)
continue;

data.Snapshot.HardwareSamples.Add(new HardwareSample() { Component = watchedHardware, Name = hardwareItem.Name });

if (!string.IsNullOrWhiteSpace(watchedHardware.CaptureName))
data.Snapshot.Captures[watchedHardware.CaptureName] = new Capture() { Name = watchedHardware.CaptureName, Value = hardwareItem.Name };
{
Capture capture = new Capture(watchedHardware, hardwareItem);
data.Snapshot.Captures[capture.Name] = capture;
}

PollSensors(hardwareItem, data);
}
Expand All @@ -162,10 +163,11 @@ private static void CheckSensor(Plugin.ISensor sensor, PollData data)
if (watchedSensor == null)
return;

data.Snapshot.SensorSamples.Add(new SensorSample() { Sensor = watchedSensor, Name = sensor.Name, Value = sensor.Value });

if (!string.IsNullOrWhiteSpace(watchedSensor.CaptureName))
data.Snapshot.Captures.Add(watchedSensor.CaptureName, new Capture() { Name = watchedSensor.CaptureName, Value = sensor.Value });
{
Capture capture = new Capture(watchedSensor, sensor);
data.Snapshot.Captures[capture.Name] = capture;
}
}

private static void CheckCompoundSensor(Plugin.ISensor sensor, PollData data)
Expand Down Expand Up @@ -243,7 +245,8 @@ private static void ProcessCompoundSensors(PollData data)

float result = instance.Calculate(from sample in compoundSensorData.Samples select sample.Value);

data.Snapshot.Captures.Add(compoundSensorData.Sensor.CaptureName, new Capture() { Name = compoundSensorData.Sensor.CaptureName, Value = result });
Capture capture = new Capture(compoundSensorData.Sensor.CaptureName, result);
data.Snapshot.Captures[capture.Name] = capture;
}
}

Expand Down

0 comments on commit 0fb15a1

Please sign in to comment.