From 66ba965206cbde7e9fa8e48894b7e140a1614320 Mon Sep 17 00:00:00 2001 From: Muse Date: Sat, 5 Dec 2026 20:46:45 +0000 Subject: [PATCH] Temperature Monitor app: honest zero-sensor state + MonitorWidget.clear() 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. --- .../monitor_temperature_monitor/README.md | 32 ++++ examples/monitor_temperature_monitor/main.py | 168 ++++++++++++++++++ .../screenshot.png | Bin 0 -> 8995 bytes lightgui/xframe/monitor.py | 26 ++- tests/test_monitor.py | 13 ++ 5 files changed, 235 insertions(+), 4 deletions(-) create mode 100644 examples/monitor_temperature_monitor/README.md create mode 100644 examples/monitor_temperature_monitor/main.py create mode 100644 examples/monitor_temperature_monitor/screenshot.png diff --git a/examples/monitor_temperature_monitor/README.md b/examples/monitor_temperature_monitor/README.md new file mode 100644 index 0000000..45b90a1 --- /dev/null +++ b/examples/monitor_temperature_monitor/README.md @@ -0,0 +1,32 @@ +# Temperature Monitor — monitor (Python) + +A btop-style temperature monitor in one 1024x768 window, built only +from LightGUI XFrame monitor widgets: + +- **XTempHistory** — temperature history of the first thermal zone, + with an 80 C threshold line (readings above it draw red) + +Live data comes from `/sys/class/thermal/thermal_zone*/temp` and +`/sys/class/hwmon/hwmon*/temp*_input`, re-scanned every 2 s. On a +machine with sensors the history fills with live readings; on this +machine (hot_dallas, a VPS with no thermal sensors at all — only +cooling devices under `/sys/class/thermal/`, no `thermal_zone*/temp` +files, an empty `/sys/class/hwmon/`) the app shows the widget's +honest empty state: an empty graph, "no data" in the legend, and a +title that says no thermal zones were found. No mock or demo +temperatures: the deterministic demo data is dropped at startup with +`MonitorWidget.clear()`. + +## Run + +```sh +PYTHONPATH=. python3 examples/monitor_temperature_monitor/main.py +``` + +Esc, `q`, or closing the window quits. + +## Source + +- App: `examples/monitor_temperature_monitor/main.py` +- Widget: `lightgui/xframe/monitor.py` +- Repo: https://github.com/cnmcdee/lightgui_python diff --git a/examples/monitor_temperature_monitor/main.py b/examples/monitor_temperature_monitor/main.py new file mode 100644 index 0000000..5809b07 --- /dev/null +++ b/examples/monitor_temperature_monitor/main.py @@ -0,0 +1,168 @@ +#!/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()) diff --git a/examples/monitor_temperature_monitor/screenshot.png b/examples/monitor_temperature_monitor/screenshot.png new file mode 100644 index 0000000000000000000000000000000000000000..dda98ae3bdbc448f115c1fdcb9c7113822b3ec8a GIT binary patch literal 8995 zcmeHNc~DbXy1%xqOk)Gi^nl&SGLGFM(ts@pvPN6GX~U)|MF-W=bebii(0YQH)JAUlQso3#( z1~&ebxOs62*|q)3r}v-V{3B0GOEW?x6=8H!HK@F%h8{vr!1`Vc3$BUdmk%Stifc>~ zN<16KS=<_F<=%afrEv+9r@gmbKYaMX71he!$G`sUI9swihji_m%U}27nF3fmEzc^N zjp;?bmAuKz{&2o(3=HLEf9oz_?xBrQefn2W{AK$lP+tB6DE@E^6o>z^G15=|2#W33 zL6La)A2scsdzo-Cgs&GDH0bVXr)*WZKNdQ2BzzZ(QMIFGp=Tq)Rpqh+b?TNo&_n9a$ew25q0A)Fi_&W-fSsSzWgS8NXe%vhNwl* z^Ts?Z`yro9eSg!arTU9iT0oV}(reox@1q3P{T-^RsyQb5X6%ffEb{)E{`f3J+|peK z^+u(FIf#9MI&^6xBEgdWzZfI(Xl8U_C;DyTdj;cUv14p3Ttg#-m`0nydCsID> zNUl~s(N7Rvx5LIN%{wLfE`|2Z?r_H}6ZBr_k+W~i*bdyvoD~5djKxZJrRa<|L@Opx zRA09P4P|BD65l>Pnlh6P!*|{BV^B0lM#Ax{3>LX*d9dLoFJEhe((ytSo6q0kSb>{+q0`BaIkArkkJNDj5DQ zqB(YXrYe9oJ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>w#$D+qerFDfhvEZu@%p<{umxzmL;3mkfOg)F~k6dwF?f zJ$PV(XnIyrv2cBN@-2NBerdX6o7T7c$snS}#2kg*4|LDH*gX+g82&%>0gXO0v>AfRL;BPbD=XDD z>b0w1TODWtmH*ON<-dKX9}W2Ef&X|9+<8;5|N7sK{bIZCb!6*qJ)DpZ%PaU zxA^XHM@I)I-?E|u!@S|OSW|=k!oUSTeIsz+K8tJsenGBQR#v{0OL~^!q9T}`iDey& m$rWqagEu?g|L)nN0yb^(uw#3VcRT1AI{t&pv5N1{UHLEhp*!jT literal 0 HcmV?d00001 diff --git a/lightgui/xframe/monitor.py b/lightgui/xframe/monitor.py index 06c605b..4f9e9e1 100644 --- a/lightgui/xframe/monitor.py +++ b/lightgui/xframe/monitor.py @@ -501,6 +501,19 @@ class MonitorWidget(Widget): return list(self._series[series]) return list(self._plot_series()[series]) + def clear(self): + """Drop every sample from the history series (and reset the + peak hold), keeping the series slots: a host app that found + no live data yet shows the widget's honest empty state + ("no data") instead of the deterministic demo series. + Later push() calls fill the kept slots again as usual.""" + if self._series is not None: + self._series = [[] for _ in self._series] + else: + self._series = [[]] + self._peak = 0.0 + self._mark_dirty() + def series_count(self): """Number of series this widget plots.""" if self._series is not None: @@ -983,10 +996,15 @@ class MonitorWidget(Widget): threshold = _prop_float(self, "threshold", 80.0) values = self._single_series(58.0, 24.0, 0.0, 120.0) lo, hi = 0.0, max(100.0, threshold + 20.0) - cur = values[-1] if values else 0.0 - cur_rgb = _RED if cur > threshold else _TEXT - _wtext(cr, self, "%.1f C" % cur, self.width - 8, 15, size=14, - rgb=cur_rgb, align="right", bold=True) + if values: + cur = values[-1] + cur_rgb = _RED if cur > threshold else _TEXT + _wtext(cr, self, "%.1f C" % cur, self.width - 8, 15, size=14, + rgb=cur_rgb, align="right", bold=True) + else: + # Honest empty state: no samples, no reading to show. + _wtext(cr, self, "no data", self.width - 8, 15, size=14, + rgb=_DIM, align="right", bold=True) plot_x, plot_y = 34, top + 2 plot_w = max(1, self.width - 40) plot_h = max(1, self.height - plot_y - 20) diff --git a/tests/test_monitor.py b/tests/test_monitor.py index 0ce9af8..9a59e34 100644 --- a/tests/test_monitor.py +++ b/tests/test_monitor.py @@ -162,6 +162,19 @@ class HistoryDataTests(unittest.TestCase): graph.push(42.0) self.assertEqual(graph.get_series(0), [42.0]) + def test_clear_drops_demo_series(self): + # clear() empties the history so a widget with no live source + # shows its honest empty state instead of the demo series. + temp = core.create("XTempHistory", "XTempHistory_001") + self.assertEqual( + len(temp._single_series(58.0, 24.0, 0.0, 120.0)), 60) + temp.clear() + self.assertEqual(temp._single_series(58.0, 24.0, 0.0, 120.0), []) + _draw(temp) # empty state draws without error + temp.push(42.0) + self.assertEqual( + temp._single_series(58.0, 24.0, 0.0, 120.0), [42.0]) + def test_push_trims_to_window(self): graph = core.create("XHistoryGraph", "XHistoryGraph_001") graph.set_prop("window", 5)