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

CPU Temperature (and similar values) support for Windows #1238

Merged
merged 19 commits into from
Nov 9, 2020

Conversation

pgrawehr
Copy link
Contributor

This adds basic CPU temperature support for windows (unfortunately, it is not very generic, so it might work on your hardware or not).

A separate binding to get values from OpenHardwareMonitor (https://openhardwaremonitor.org/) is provided which gets a full set of whatever values you might want to know about your PC. This leverages the requirement to do anything hardware-dependent on our side. It is also a good example on how to work with UnitsNet.

@ghost ghost added the area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio label Oct 30, 2020
Copy link
Member

@Ellerbach Ellerbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this addition, really like it all up. Great usage of the UnitsNet especially to add calculated additional sensors. That makes life so easy.

src/Iot.Device.Bindings/Iot.Device.Bindings.csproj Outdated Show resolved Hide resolved
src/devices/OpenHardwareMonitor/OpenHardwareMonitor.cs Outdated Show resolved Hide resolved
src/devices/OpenHardwareMonitor/OpenHardwareMonitor.cs Outdated Show resolved Hide resolved
/// <exception cref="PlatformNotSupportedException">The operating system is not Windows.</exception>
public OpenHardwareMonitor()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I read the Open Hardware Monitor dcumentation, I understand it's also working on Linux. Now, on Linux there is no WMI prodider, so clearly the logic would be different. So is the CpuTemperature for Linux largely enough or is there something that can be done for Linux as well. I doesn't mean it had to be done for this PR but it's somethings that can be done later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check what it takes to use it on linux (and at least document that).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be some sort of support on linux, using mono. I haven't tried that, though. There's an alternate connection api that creates a small HTTP server that could be used, but this would need to be investigated separately.

src/devices/OpenHardwareMonitor/OpenHardwareMonitor.cs Outdated Show resolved Hide resolved
src/devices/OpenHardwareMonitor/OpenHardwareMonitor.cs Outdated Show resolved Hide resolved
return false;
}

float newValue = Convert.ToSingle(_instance["Value"]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this pattern, how do we ensure that the unit is created with the right default unit? Are _instance["Value"] always returning the default international unit? I may have miss something here, I would just like to understand

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the values returned are always SI units. Which unit belongs to which sensor type is defined in the global _typeMap array.

src/devices/OpenHardwareMonitor/OpenHardwareMonitor.csproj Outdated Show resolved Hide resolved
- From the CPU Package power sensor, the CPU heat flux is calculated (estimating the size of the die).
- If both a power and a voltage sensor are available for CPU Package, the current is calculated.

## References
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like you are missing the references

src/devices/OpenHardwareMonitor/SensorType.cs Outdated Show resolved Hide resolved
@@ -3,29 +3,64 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case because implementation is completely different I'd recommend to create platform specific files: CpuTemperature.Linux.cs, CpuTemperature.Windows.cs

Also you should not have public API differences between Windows/Linux, extra stuff can be available through Windows-only library though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Iot.Devices library has no platform dependency (and shouldn't have one). Therefore the platform test is done at runtime, so that two files don't really add value, I think.

The API is exactly the same for all cases.

@@ -10,4 +10,8 @@
<None Include="README.md" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Management" Version="5.0.0-alpha.1.19618.1" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend to make it Windows-only dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above. There's no platform dependency on this dll.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, any platform dependency introduced at this level would apply to the source computer, not the one running it.

@@ -0,0 +1,740 @@
using System;
using System.Collections;
using System.Collections.Generic;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should Windows implementation of CpuTemperature use this class instead of doing queries directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered this as well, but since this class requires an external application to be running, I tought that it might be good to not add that dependency to the basic CpuTemperature class. But since the current implementation of CpuTemperature often doesn't work (depending on hardware), it may be better to change it anyway.

So we have a solution (current implementation of CpuTemperature) that often doesn't work and needs elevation vs a solution that requires external components. Which do you prefer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think whichever one is more useful. We should start with making the code work with good APIs, next steps are perf and size

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to use the OpenHardwareMonitor implementation on Windows now, and use the direct approach only as fallback.

@joperezr
Copy link
Member

joperezr commented Nov 4, 2020

Sorry @pgrawehr I didn't have time yesterday to push the change but I just did, so that should now use the right 5.0 versions of the packages that will ship in 5.0

@pgrawehr
Copy link
Contributor Author

pgrawehr commented Nov 4, 2020

@joperezr No problem, I've got other loose ends to work on ;-)

Unfortunately, it looks as if you've broken the build now (something about package downgrading). Did you miss an occurence?

@joperezr
Copy link
Member

joperezr commented Nov 4, 2020

Oops, my bad, looking into it now.

@joperezr
Copy link
Member

joperezr commented Nov 4, 2020

I'll rebase to fix the merge conflict

pgrawehr and others added 12 commits November 5, 2020 11:48
Downside: Requires elevated permissions
Properties should not have side effects
On Windows, the example requires elevated permissions. It
may still not find a suitable sensor, dependent on the hardware.
Also changed namespace, to prevent namespace to class name collisions
@joperezr
Copy link
Member

joperezr commented Nov 5, 2020

@richlander given you are the nullable expert in the repo, let me know if you have any feedback on the last commit changes: 855a8e6

@richlander
Copy link
Member

Would be nice to see this marked in some way to remove the ! when we stop targeting netcoreapp2.1. We want as much compiler checking as we can get.

855a8e6#diff-962c5fd4c57ced02999e704a7a51c628ec345eacb38dcd0bc3fb83a2d2056eafR461

An #if and #elif would certainly resolve that.

Copy link
Member

@joperezr joperezr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR looks good to me, thanks for the contribution @pgrawehr, I'll go ahead and merge once this is green in order to also get the update on the package dependency versions in.

@joperezr joperezr merged commit 17d497e into dotnet:master Nov 9, 2020
ZhangGaoxing pushed a commit to ZhangGaoxing/iot that referenced this pull request Mar 15, 2021
* Add basic CpuTemperature implementation for Windows

Downside: Requires elevated permissions

* Clean init sequence

Properties should not have side effects

* Add application manifest for Windows

On Windows, the example requires elevated permissions. It
may still not find a suitable sensor, dependent on the hardware.

* Update readme

* Add new binding for OpenHardwareMonitor connection

* Clean up

* Add documentation

* Better documentation for TimeSpan constants

* Allow arbitrary thread cycle times

* Value must not be negative

* More generic binding name

Also changed namespace, to prevent namespace to class name collisions

* Use HardwareMonitor on Windows, if available

And add missing Dispose

* Use more recent version of System.Management

Latest Version not available on official sources yet

* Minor documentation improvements

* Updating dependency package versions from dotnet/runtime and adding new package subscription

* Fixing build break due to dependencies

* Removing restore sources so that NuGet.Config is used

* Rebasing and addressing nullability changes

* Addressing feedback

Co-authored-by: Jose Perez Rodriguez <joperezr@microsoft.com>
@github-actions github-actions bot locked and limited conversation to collaborators Dec 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants