-
Notifications
You must be signed in to change notification settings - Fork 585
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
Conversation
c2a1d44
to
adf5ecf
Compare
There was a problem hiding this 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.
/// <exception cref="PlatformNotSupportedException">The operating system is not Windows.</exception> | ||
public OpenHardwareMonitor() | ||
{ | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
return false; | ||
} | ||
|
||
float newValue = Convert.ToSingle(_instance["Value"]); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
- 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 |
There was a problem hiding this comment.
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
@@ -3,29 +3,64 @@ | |||
// See the LICENSE file in the project root for more information. | |||
|
|||
using System; | |||
using System.Collections.Generic; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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" /> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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 |
@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? |
Oops, my bad, looking into it now. |
I'll rebase to fix the merge conflict |
767e7bd
to
11de1a6
Compare
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
And add missing Dispose
Latest Version not available on official sources yet
…ew package subscription
b2508ea
to
855a8e6
Compare
@richlander given you are the nullable expert in the repo, let me know if you have any feedback on the last commit changes: 855a8e6 |
Would be nice to see this marked in some way to remove the 855a8e6#diff-962c5fd4c57ced02999e704a7a51c628ec345eacb38dcd0bc3fb83a2d2056eafR461 An |
There was a problem hiding this 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.
* 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>
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.