2017-06-15 07:48:25 +02:00
|
|
|
|
# -*- coding: UTF-8 -*-
|
2017-06-05 04:54:23 +02:00
|
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
2017-04-19 13:36:35 +02:00
|
|
|
|
"""Displays sensor temperature
|
|
|
|
|
|
|
|
|
|
Parameters:
|
2017-07-19 13:40:57 +02:00
|
|
|
|
* sensors.path: path to temperature file (default /sys/class/thermal/thermal_zone0/temp).
|
2018-11-07 16:46:49 +01:00
|
|
|
|
* sensors.json: if set to "true", interpret sensors.path as JSON "path" in the output
|
|
|
|
|
of "sensors -j" (i.e. <key1>/<key2>/.../<value>), for example, path could
|
|
|
|
|
be: "coretemp-isa-00000/Core 0/temp1_input" (defaults to "false")
|
2017-07-22 11:44:21 +02:00
|
|
|
|
* sensors.match: (fallback) Line to match against output of 'sensors -u' (default: temp1_input)
|
|
|
|
|
* sensors.match_number: (fallback) which of the matches you want (default -1: last match).
|
|
|
|
|

|
2017-04-19 13:36:35 +02:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import re
|
2018-11-07 16:46:49 +01:00
|
|
|
|
import json
|
2017-07-22 11:44:21 +02:00
|
|
|
|
import logging
|
2017-04-19 13:36:35 +02:00
|
|
|
|
|
2017-07-22 11:44:21 +02:00
|
|
|
|
import bumblebee.util
|
2017-04-19 13:36:35 +02:00
|
|
|
|
import bumblebee.input
|
|
|
|
|
import bumblebee.output
|
|
|
|
|
import bumblebee.engine
|
|
|
|
|
|
2017-07-22 11:44:21 +02:00
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
2017-04-19 13:36:35 +02:00
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
|
super(Module, self).__init__(engine, config,
|
2017-06-05 04:54:23 +02:00
|
|
|
|
bumblebee.output.Widget(full_text=self.temperature))
|
2017-04-19 18:53:45 +02:00
|
|
|
|
self._temperature = "unknown"
|
2017-06-15 07:48:25 +02:00
|
|
|
|
self._mhz = "n/a"
|
2017-07-22 11:44:21 +02:00
|
|
|
|
self._match_number = int(self.parameter("match_number", "-1"))
|
|
|
|
|
self._pattern = re.compile(r"^\s*{}:\s*([\d.]+)$".format(self.parameter("match", "temp1_input")), re.MULTILINE)
|
2018-11-07 16:46:49 +01:00
|
|
|
|
self._json = bumblebee.util.asbool(self.parameter("json", "false"))
|
2017-04-19 13:36:35 +02:00
|
|
|
|
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="xsensors")
|
2018-07-30 18:25:37 +02:00
|
|
|
|
self.determine_method()
|
|
|
|
|
|
|
|
|
|
def determine_method(self):
|
2018-11-07 16:46:49 +01:00
|
|
|
|
if self.parameter("path") != None and self._json == False:
|
2018-09-24 18:15:04 +02:00
|
|
|
|
self.use_sensors = False # use thermal zone
|
|
|
|
|
else:
|
|
|
|
|
# try to use output of sensors -u
|
|
|
|
|
try:
|
|
|
|
|
output = bumblebee.util.execute("sensors -u")
|
|
|
|
|
self.use_sensors = True
|
|
|
|
|
log.debug("Sensors command available")
|
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
|
log.info("Sensors command not available, using /sys/class/thermal/thermal_zone*/")
|
|
|
|
|
self.use_sensors = False
|
2017-04-19 13:36:35 +02:00
|
|
|
|
|
2017-07-22 11:44:21 +02:00
|
|
|
|
def _get_temp_from_sensors(self):
|
2018-11-07 16:46:49 +01:00
|
|
|
|
if self._json == True:
|
|
|
|
|
try:
|
|
|
|
|
output = json.loads(bumblebee.util.execute("sensors -j"))
|
|
|
|
|
for key in self.parameter("path").split("/"):
|
|
|
|
|
output = output[key]
|
|
|
|
|
return int(float(output))
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error("unable to read sensors: {}".format(str(e)))
|
|
|
|
|
return "unknown"
|
|
|
|
|
else:
|
|
|
|
|
output = bumblebee.util.execute("sensors -u")
|
|
|
|
|
match = self._pattern.findall(output)
|
|
|
|
|
if match:
|
|
|
|
|
return int(float(match[self._match_number]))
|
2017-07-22 11:44:21 +02:00
|
|
|
|
return "unknown"
|
|
|
|
|
|
2017-04-19 13:36:35 +02:00
|
|
|
|
def get_temp(self):
|
2018-07-30 18:25:37 +02:00
|
|
|
|
if self.use_sensors:
|
2017-07-22 11:44:21 +02:00
|
|
|
|
temperature = self._get_temp_from_sensors()
|
2018-07-30 18:25:37 +02:00
|
|
|
|
log.debug("Retrieve temperature from sensors -u")
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
temperature = open(self.parameter("path", "/sys/class/thermal/thermal_zone0/temp")).read()[:2]
|
|
|
|
|
log.debug("retrieved temperature from /sys/class/")
|
|
|
|
|
# TODO: Iterate through all thermal zones to determine the correct one and use its value
|
|
|
|
|
# https://unix.stackexchange.com/questions/304845/discrepancy-between-number-of-cores-and-thermal-zones-in-sys-class-thermal
|
|
|
|
|
|
|
|
|
|
except IOError:
|
|
|
|
|
temperature = "unknown"
|
|
|
|
|
log.info("Can not determine temperature, please install lm-sensors")
|
|
|
|
|
|
2017-04-19 13:36:35 +02:00
|
|
|
|
return temperature
|
|
|
|
|
|
2017-10-13 17:06:18 +02:00
|
|
|
|
def get_mhz(self):
|
2017-09-29 04:13:04 +02:00
|
|
|
|
try:
|
|
|
|
|
output = open("/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq").read()
|
|
|
|
|
mhz = int(float(output)/1000.0)
|
|
|
|
|
except:
|
|
|
|
|
output = open("/proc/cpuinfo").read()
|
2017-10-13 17:06:18 +02:00
|
|
|
|
m = re.search(r"cpu MHz\s+:\s+(\d+)", output)
|
|
|
|
|
mhz = int(m.group(1))
|
2017-06-13 04:19:30 +02:00
|
|
|
|
|
|
|
|
|
if mhz < 1000:
|
2017-06-15 07:48:25 +02:00
|
|
|
|
return "{} MHz".format(mhz)
|
2017-06-13 04:19:30 +02:00
|
|
|
|
else:
|
2017-06-15 07:48:25 +02:00
|
|
|
|
return "{:0.01f} GHz".format(float(mhz)/1000.0)
|
2017-06-13 04:19:30 +02:00
|
|
|
|
|
2017-06-05 04:54:23 +02:00
|
|
|
|
def temperature(self, _):
|
2017-06-15 07:48:25 +02:00
|
|
|
|
return u"{}°c @ {}".format(self._temperature, self._mhz)
|
2017-04-19 13:36:35 +02:00
|
|
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
|
self._temperature = self.get_temp()
|
2017-06-13 04:19:30 +02:00
|
|
|
|
self._mhz = self.get_mhz()
|
2017-04-19 18:53:45 +02:00
|
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|