mirror of
https://github.com/KevinMidboe/Node-Com-Handler.git
synced 2025-10-29 09:40:27 +00:00
Better pre-check that temp reading is supported for running OS and better return text.
This commit is contained in:
@@ -1,16 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import psutil
|
||||
from platform import system
|
||||
|
||||
def getCpuTemp():
|
||||
# Check if sensors_temperatures exists
|
||||
try:
|
||||
# Define cpu as function of sensors_temperatures
|
||||
cpu = psutil.sensors_temperatures()
|
||||
except AttributeError:
|
||||
error = "'sensors_temperatures' is not supported in this verison of psutil or your OS."
|
||||
print(error)
|
||||
return None
|
||||
if system() != 'Linux':
|
||||
return {"Error": "Temp reader is not supported for this OS."}
|
||||
# Define cpu as function of sensors_temperatures
|
||||
cpu = psutil.sensors_temperatures()
|
||||
|
||||
# Array for temps for each core.
|
||||
curCpuTemps = []
|
||||
@@ -19,16 +17,9 @@ def getCpuTemp():
|
||||
curCpuTemps.append(temp[1]) # Append to list
|
||||
print(temp[0]+': '+str(temp[1])) # Print output
|
||||
|
||||
# Check if len of curCpuTemps is something so not to
|
||||
# calculate on a empty list
|
||||
if len(curCpuTemps) > 0:
|
||||
# Compute avg of curCpuTemps
|
||||
avgCpuTemps = sum(curCpuTemps)/len(curCpuTemps)
|
||||
return avgCpuTemps
|
||||
print("Avg: " + str(avgCpuTemps))
|
||||
else:
|
||||
print("Couldn't get cpu temp. (division by zero)")
|
||||
return None
|
||||
# Compute avg of curCpuTemps
|
||||
avgCpuTemps = sum(curCpuTemps)/len(curCpuTemps)
|
||||
return {"CPU Temp": avgCpuTemps}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user