Single-widget monitor app (XTempHistory, 80 C threshold) reading /sys/class/thermal + /sys/class/hwmon every 2 s. hot_dallas has no thermal sensors, so the app shows the widget empty state (no data, no fake readings) with a title that says so. Adds additive MonitorWidget.clear() and an empty-state readout.
169 lines
5.0 KiB
Python
169 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Temperature Monitor - monitor (Python).
|
|
|
|
A btop-style temperature monitor in one 1024x768 window, built only
|
|
from LightGUI XFrame monitor widgets (XTempHistory): the first
|
|
thermal zone's temperature history with an 80 C threshold line,
|
|
re-scanned every 2 s from /sys/class/thermal/thermal_zone*/temp and
|
|
/sys/class/hwmon/hwmon*/temp*_input.
|
|
|
|
On this machine (hot_dallas, a VPS) there are NO thermal sensors at
|
|
all: /sys/class/thermal/ holds only cooling devices, there are no
|
|
thermal_zone*/temp files, /sys/class/hwmon/ is empty and `sensors`
|
|
is not installed. So the app shows the widget's honest empty state
|
|
("no data", no line, no fake 0.0 C reading) and says so in the
|
|
title. No mock or demo temperatures, ever: the deterministic demo
|
|
data is dropped at startup with MonitorWidget.clear(). If sensors
|
|
ever appear (e.g. on a machine that has them) the rescan picks them
|
|
up and the history starts filling with live readings.
|
|
|
|
Run with:
|
|
PYTHONPATH=/root/monitor-wt/temperature-monitor/lightgui_python python3 \\
|
|
examples/monitor_temperature_monitor/main.py
|
|
(or from the repo root with PYTHONPATH=. )
|
|
"""
|
|
|
|
import glob
|
|
import os
|
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "3.0")
|
|
gi.require_version("Gdk", "3.0")
|
|
|
|
from gi.repository import Gdk, GLib, Gtk # noqa: E402
|
|
from lightgui.xframe.core import create, parse_color # noqa: E402
|
|
|
|
_TITLE = "Temperature Monitor - monitor"
|
|
_DESIGN_SIZE = (1024, 768)
|
|
_BACKGROUND = ""
|
|
_WIDGETS = []
|
|
_AREA = []
|
|
_HAD_ZONES = [False]
|
|
_NO_ZONES_TITLE = "Temperature history -- no thermal zones found"
|
|
|
|
|
|
def _read_first_line(path):
|
|
try:
|
|
with open(path) as f:
|
|
return f.readline().strip()
|
|
except (OSError, ValueError):
|
|
return ""
|
|
|
|
|
|
def _read_temp_c(path):
|
|
try:
|
|
return int(_read_first_line(path)) / 1000.0
|
|
except (OSError, ValueError):
|
|
return None
|
|
|
|
|
|
def _scan_zones():
|
|
"""All temperature inputs the kernel exposes, thermal zones first.
|
|
|
|
Each zone is (name, temp-path). Millidegree files that do not
|
|
parse are skipped, so a half-populated sysfs still yields the
|
|
zones that do read.
|
|
"""
|
|
zones = []
|
|
for temp_path in sorted(glob.glob("/sys/class/thermal/thermal_zone*/temp")):
|
|
if _read_temp_c(temp_path) is None:
|
|
continue
|
|
base = os.path.dirname(temp_path)
|
|
label = _read_first_line(os.path.join(base, "type"))
|
|
zones.append((label or os.path.basename(base), temp_path))
|
|
for temp_path in sorted(glob.glob("/sys/class/hwmon/hwmon*/temp*_input")):
|
|
if _read_temp_c(temp_path) is None:
|
|
continue
|
|
base = os.path.dirname(temp_path)
|
|
chip = _read_first_line(os.path.join(base, "name")) or \
|
|
os.path.basename(base)
|
|
stem = os.path.basename(temp_path)[:-len("_input")]
|
|
label = _read_first_line(os.path.join(base, stem + "_label"))
|
|
zones.append((("%s %s" % (chip, label)) if label else chip,
|
|
temp_path))
|
|
return zones
|
|
|
|
|
|
def _build_widgets():
|
|
"""Create every placed widget with its props."""
|
|
temp = create("XTempHistory", "TempHistory_001", 16, 16, 992, 736)
|
|
temp.set_prop("label", _NO_ZONES_TITLE)
|
|
temp.set_prop("threshold", "80")
|
|
# Drop the deterministic demo data: with no live source the
|
|
# honest state is an empty graph, not a fake warm-up curve.
|
|
temp.clear()
|
|
return [temp]
|
|
|
|
|
|
def _apply_scan():
|
|
"""Re-scan sysfs and feed the first zone's live reading (if any)."""
|
|
(temp,) = _WIDGETS
|
|
zones = _scan_zones()
|
|
if not zones:
|
|
if _HAD_ZONES[0]:
|
|
_HAD_ZONES[0] = False
|
|
temp.clear()
|
|
temp.set_prop("label", _NO_ZONES_TITLE)
|
|
return
|
|
name, path = zones[0]
|
|
if not _HAD_ZONES[0]:
|
|
_HAD_ZONES[0] = True
|
|
temp.set_prop("label", "Temperature history -- %s" % name)
|
|
value = _read_temp_c(path)
|
|
if value is not None:
|
|
temp.push(value)
|
|
|
|
|
|
def _tick():
|
|
"""2 s tick: re-scan sysfs, then redraw."""
|
|
_apply_scan()
|
|
if _AREA:
|
|
_AREA[0].queue_draw()
|
|
return True
|
|
|
|
|
|
def _on_draw(area, cr):
|
|
bg = parse_color(_BACKGROUND, None)
|
|
if bg is None:
|
|
bg = (1.0, 1.0, 1.0)
|
|
cr.set_source_rgb(*bg)
|
|
cr.paint()
|
|
for widget in _WIDGETS:
|
|
widget.draw(cr)
|
|
return False
|
|
|
|
|
|
def _on_key_press(window, event):
|
|
if event.keyval in (Gdk.KEY_Escape, Gdk.KEY_q):
|
|
Gtk.main_quit()
|
|
return True
|
|
return False
|
|
|
|
|
|
def main():
|
|
window = Gtk.Window(title=_TITLE)
|
|
window.set_default_size(*_DESIGN_SIZE)
|
|
window.set_resizable(False)
|
|
window.connect("destroy", Gtk.main_quit)
|
|
window.connect("key-press-event", _on_key_press)
|
|
area = Gtk.DrawingArea()
|
|
area.set_hexpand(True)
|
|
area.set_vexpand(True)
|
|
area.connect("draw", _on_draw)
|
|
window.add(area)
|
|
_WIDGETS.extend(_build_widgets())
|
|
_AREA.append(area)
|
|
# First scan before the first paint, so the title already tells
|
|
# the truth on screen one.
|
|
_apply_scan()
|
|
GLib.timeout_add(2000, _tick)
|
|
window.show_all()
|
|
Gtk.main()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
sys.exit(main())
|