42 lines
2.2 KiB
Python
42 lines
2.2 KiB
Python
#!/usr/bin/python3
|
|
import os, json, sys
|
|
|
|
# comments added for Panini
|
|
|
|
try:
|
|
# get arguments from command line
|
|
sensor_group = sys.argv[1] # 1st argument
|
|
sensor = sys.argv[2] # 2nd arggument
|
|
except IndexError: # could not get arguments bc the arguments array doesn't contain enough elements
|
|
print("usage: python3 "+sys.argv[0]+" <sensor group> <sensor>") # show usage information, sys.argv[0] is the name of the python file itself
|
|
sys.exit(1) #exit with error code 1 bc we want scripts to be able to reliably tell that soemthing went wrong
|
|
|
|
sensors_process = os.popen("sensors -j") # run 'sensors -j' to get a json formatted list of hardware sensors and their values
|
|
temps_str = sensors_process.read() # read output of 'sensors -j'
|
|
sensors_process.close() # close handle bc we don't need or want to have it around anymore
|
|
|
|
temps = json.loads(temps_str) # load json string into a python dict
|
|
|
|
try:
|
|
# this is ... well ... a oneliner I would use...
|
|
print([value for key, value in temps[sensor_group][sensor].items() if key.endswith("input")][0])
|
|
# temps is a dict with multiple dicts inside it
|
|
# first step is to get the desired sensor group
|
|
# temps[sensor_group]
|
|
# then we get the desired sensor from that sensor group
|
|
# temps[sensor_group][sensor]
|
|
# from that, we go through all the items of that dict putting the keys and their corresponding values in two variables
|
|
# for key, value in temps[sensor_group][sensor].items()
|
|
# check whether the key ends with "input"
|
|
# if key.endswith("input")
|
|
# and if it does, we put it in an array
|
|
# [value for key, value in temps[sensor_group][sensor].items() if key.endswith("input")]
|
|
# we take the first entry of that array
|
|
# [value for key, value in temps[sensor_group][sensor].items() if key.endswith("input")][0]
|
|
# and put it on the console
|
|
# print([value for key, value in temps[sensor_group][sensor].items() if key.endswith("input")][0])
|
|
|
|
except KeyError: # if we can't find an element in the dict that corresponds to the sensor group / sensor given on the command line
|
|
print("Sensor \""+sensor_group+":"+sensor+" not found!")
|
|
sys.exit(1) #exit with error code 1 bc we want scripts to be able to reliably tell that soemthing went wrong
|