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 0000000..dda98ae Binary files /dev/null and b/examples/monitor_temperature_monitor/screenshot.png differ 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)