Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: GPU temperature and load sensor fix #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public GpuLoadSensor(int? updateInterval = null, string name = DefaultName, stri
IsNetworkEnabled = false,
IsStorageEnabled = false,
};

computer.Open();
_gpu = computer.Hardware.FirstOrDefault(h => h.HardwareType == HardwareType.GpuAmd || h.HardwareType == HardwareType.GpuNvidia);

Expand Down Expand Up @@ -53,14 +53,17 @@ public override DiscoveryConfigModel GetAutoDiscoveryConfig()

public override string GetState()
{
if (_gpu == null) return "NotSupported";
if (_gpu == null)
return null;

_gpu.Update();

var sensor = _gpu.Sensors.FirstOrDefault(s => s.SensorType == SensorType.Load);

if (sensor?.Value == null) return "NotSupported";
if (sensor?.Value == null)
return null;

return sensor.Value.HasValue ? sensor.Value.Value.ToString("#.##", CultureInfo.InvariantCulture) : "Unknown";
return sensor.Value.HasValue ? sensor.Value.Value.ToString("#.##", CultureInfo.InvariantCulture) : null;
}

public override string GetAttributes() => string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ public override DiscoveryConfigModel GetAutoDiscoveryConfig()

public override string GetState()
{
if (_gpu == null) return "NotSupported";
if (_gpu == null)
return null;

_gpu.Update();

var sensor = _gpu.Sensors.FirstOrDefault(s => s.SensorType == SensorType.Temperature);

if (sensor?.Value == null) return "NotSupported";
if (sensor?.Value == null)
return null;

return sensor.Value.HasValue ? sensor.Value.Value.ToString("#.##", CultureInfo.InvariantCulture) : "Unknown";
return sensor.Value.HasValue ? sensor.Value.Value.ToString("#.##", CultureInfo.InvariantCulture) : null;
}

public override string GetAttributes() => string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ protected SensorDiscoveryConfigModel SetAutoDiscoveryConfigModel(SensorDiscovery

public override void ClearAutoDiscoveryConfig() => AutoDiscoveryConfigModel = null;

public abstract string GetState();
public abstract string GetAttributes();
// nullable in preparation for possible future "nullable enablement"
public abstract string? GetState();

// nullable in preparation for possible future "nullable enablement"
public abstract string? GetAttributes();

public void ResetChecks()
{
Expand All @@ -60,6 +63,9 @@ public async Task PublishStateAsync(bool respectChecks = true)

// get the current state/attributes
var state = GetState();
if (state == null)
return;

var attributes = GetAttributes();

// are we asked to check state changes?
Expand All @@ -82,13 +88,10 @@ public async Task PublishStateAsync(bool respectChecks = true)
// send it
var published = await Variables.MqttManager.PublishAsync(message);
if (!published)
{
// failed, don't store the state
return;
}
return; // failed, don't store the state

// optionally prepare and send attributes
if (UseAttributes)
if (UseAttributes && attributes != null)
{
message = new MqttApplicationMessageBuilder()
.WithTopic(autoDiscoConfig.Json_attributes_topic)
Expand All @@ -98,18 +101,18 @@ public async Task PublishStateAsync(bool respectChecks = true)

published = await Variables.MqttManager.PublishAsync(message);
if (!published)
{
// failed, don't store the state
return;
}
return; // failed, don't store the state
}

// only store the values if the checks are respected
// otherwise, we might stay in 'unknown' state untill the value changes
if (!respectChecks) return;
// otherwise, we might stay in 'unknown' state until the value changes
if (!respectChecks)
return;

PreviousPublishedState = state;
PreviousPublishedAttributes = attributes;
if (attributes != null)
PreviousPublishedAttributes = attributes;

LastUpdated = DateTime.Now;
}
catch (Exception ex)
Expand Down