benchlab-pytools 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- benchlab/__init__.py +14 -0
- benchlab/__main__.py +21 -0
- benchlab/config/README.md +325 -0
- benchlab/config/__init__.py +9 -0
- benchlab/config/config_client.py +631 -0
- benchlab/config/config_manager.py +495 -0
- benchlab/config/config_tool.py +358 -0
- benchlab/config/diff.py +163 -0
- benchlab/config/examples/complete_config.json +56 -0
- benchlab/config/examples/rgb_lighting.json +24 -0
- benchlab/config/examples/simple_fan.json +32 -0
- benchlab/config/schema.py +138 -0
- benchlab/core/README.md +154 -0
- benchlab/core/__init__.py +78 -0
- benchlab/core/config.py +145 -0
- benchlab/core/datasource.py +1209 -0
- benchlab/core/datasource_manager.py +418 -0
- benchlab/core/device_registry.py +206 -0
- benchlab/core/discovery.py +92 -0
- benchlab/core/infrastructure.py +309 -0
- benchlab/core/process_manager.py +356 -0
- benchlab/core/retry.py +85 -0
- benchlab/core/shared_serial.py +235 -0
- benchlab/core/statistics.py +224 -0
- benchlab/csv_log/README.md +206 -0
- benchlab/csv_log/__init__.py +5 -0
- benchlab/csv_log/csv_logger_enhanced.py +416 -0
- benchlab/csv_log/message_batcher.py +361 -0
- benchlab/csv_log/requirements.txt +3 -0
- benchlab/csv_log/smart_retry.py +466 -0
- benchlab/graph/README.md +117 -0
- benchlab/graph/__init__.py +5 -0
- benchlab/graph/app.py +353 -0
- benchlab/graph/requirements.txt +2 -0
- benchlab/graph/runner.py +42 -0
- benchlab/graph/ui.py +288 -0
- benchlab/hwinfo/README.md +135 -0
- benchlab/hwinfo/__init__.py +5 -0
- benchlab/hwinfo/hwinfo_export.py +386 -0
- benchlab/hwinfo/requirements.txt +1 -0
- benchlab/launcher.py +387 -0
- benchlab/link/.env.EXAMPLE +10 -0
- benchlab/link/README.md +172 -0
- benchlab/link/__init__.py +1 -0
- benchlab/link/link_main.py +479 -0
- benchlab/main.py +462 -0
- benchlab/menu.py +729 -0
- benchlab/menu_classic.py +410 -0
- benchlab/mqtt/README.md +201 -0
- benchlab/mqtt/__init__.py +5 -0
- benchlab/mqtt/mqtt.config_template +2 -0
- benchlab/mqtt/mqtt_publisher.py +540 -0
- benchlab/mqtt/requirements.txt +3 -0
- benchlab/prefs.py +67 -0
- benchlab/restapi/.env.example +17 -0
- benchlab/restapi/__init__.py +5 -0
- benchlab/restapi/favicon.ico +0 -0
- benchlab/restapi/readme.md +202 -0
- benchlab/restapi/requirements.txt +5 -0
- benchlab/restapi/telemetry_api.py +808 -0
- benchlab/sources.py +515 -0
- benchlab/tools.py +147 -0
- benchlab/tui/README.md +266 -0
- benchlab/tui/__init__.py +5 -0
- benchlab/tui/config.py +477 -0
- benchlab/tui/requirements.txt +2 -0
- benchlab/tui/tui_core.py +1298 -0
- benchlab/tui/tui_main.py +418 -0
- benchlab/vu/README.md +64 -0
- benchlab/vu/VU-Server/NOTICE.md +13 -0
- benchlab/vu/VU-Server/README.md +80 -0
- benchlab/vu/VU-Server/Running_from_source_on_linux.md +97 -0
- benchlab/vu/VU-Server/__init__.py +0 -0
- benchlab/vu/VU-Server/assets/vu1_hello_world.png +0 -0
- benchlab/vu/VU-Server/community_applications.md +13 -0
- benchlab/vu/VU-Server/config.yaml +9 -0
- benchlab/vu/VU-Server/database.py +256 -0
- benchlab/vu/VU-Server/dial_driver.py +534 -0
- benchlab/vu/VU-Server/dials/Comms_Hub_Gauge.py +78 -0
- benchlab/vu/VU-Server/dials/Comms_Hub_Server.py +100 -0
- benchlab/vu/VU-Server/dials/__init__.py +0 -0
- benchlab/vu/VU-Server/dials/base_logger.py +100 -0
- benchlab/vu/VU-Server/installer/inc/icon.ico +0 -0
- benchlab/vu/VU-Server/installer/install.nsi +148 -0
- benchlab/vu/VU-Server/make_version.py +40 -0
- benchlab/vu/VU-Server/pylintrc +628 -0
- benchlab/vu/VU-Server/requirements.txt +9 -0
- benchlab/vu/VU-Server/serial_driver.py +244 -0
- benchlab/vu/VU-Server/server.py +670 -0
- benchlab/vu/VU-Server/server_config.py +255 -0
- benchlab/vu/VU-Server/server_dial_handler.py +334 -0
- benchlab/vu/VU-Server/vu_notifications.py +24 -0
- benchlab/vu/VU-Server/www/android-chrome-192x192.png +0 -0
- benchlab/vu/VU-Server/www/android-chrome-512x512.png +0 -0
- benchlab/vu/VU-Server/www/apple-touch-icon.png +0 -0
- benchlab/vu/VU-Server/www/assets/css/demo.css +291 -0
- benchlab/vu/VU-Server/www/assets/css/demo.min.css +9 -0
- benchlab/vu/VU-Server/www/assets/css/demo.rtl.css +276 -0
- benchlab/vu/VU-Server/www/assets/css/demo.rtl.min.css +9 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-flags.css +1084 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-flags.min.css +9 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-flags.rtl.css +1084 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-flags.rtl.min.css +9 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-payments.css +528 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-payments.min.css +9 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-payments.rtl.css +528 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-payments.rtl.min.css +9 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-social.css +10 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-social.min.css +8 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-social.rtl.css +10 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-social.rtl.min.css +8 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-vendors.css +1504 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-vendors.min.css +9 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-vendors.rtl.css +1504 -0
- benchlab/vu/VU-Server/www/assets/css/tabler-vendors.rtl.min.css +9 -0
- benchlab/vu/VU-Server/www/assets/css/tabler.css +25813 -0
- benchlab/vu/VU-Server/www/assets/css/tabler.min.css +13 -0
- benchlab/vu/VU-Server/www/assets/css/tabler.rtl.css +25777 -0
- benchlab/vu/VU-Server/www/assets/css/tabler.rtl.min.css +13 -0
- benchlab/vu/VU-Server/www/assets/css/theme.min.css +261 -0
- benchlab/vu/VU-Server/www/assets/img/header.png +0 -0
- benchlab/vu/VU-Server/www/assets/img/logo.png +0 -0
- benchlab/vu/VU-Server/www/assets/js/dark-theme.js +35 -0
- benchlab/vu/VU-Server/www/assets/js/dark-theme.min.js +9 -0
- benchlab/vu/VU-Server/www/assets/js/demo.js +132 -0
- benchlab/vu/VU-Server/www/assets/js/demo.min.js +9 -0
- benchlab/vu/VU-Server/www/assets/js/jquery.min.js +2 -0
- benchlab/vu/VU-Server/www/assets/js/tabler.esm.js +7482 -0
- benchlab/vu/VU-Server/www/assets/js/tabler.esm.min.js +15 -0
- benchlab/vu/VU-Server/www/assets/js/tabler.js +7543 -0
- benchlab/vu/VU-Server/www/assets/js/tabler.min.js +15 -0
- benchlab/vu/VU-Server/www/assets/js/vu1_api.js +68 -0
- benchlab/vu/VU-Server/www/assets/js/vu1_gui_api_key_settings.js +167 -0
- benchlab/vu/VU-Server/www/assets/js/vu1_gui_api_keys.js +138 -0
- benchlab/vu/VU-Server/www/assets/js/vu1_gui_dial.js +179 -0
- benchlab/vu/VU-Server/www/assets/js/vu1_gui_home.js +60 -0
- benchlab/vu/VU-Server/www/assets/js/vu1_gui_root.js +125 -0
- benchlab/vu/VU-Server/www/favicon-16x16.png +0 -0
- benchlab/vu/VU-Server/www/favicon-32x32.png +0 -0
- benchlab/vu/VU-Server/www/favicon.ico +0 -0
- benchlab/vu/VU-Server/www/favicon.png +0 -0
- benchlab/vu/VU-Server/www/index.html +47 -0
- benchlab/vu/VU-Server/www/views/api_keys.html +151 -0
- benchlab/vu/VU-Server/www/views/dial.html +286 -0
- benchlab/vu/VU-Server/www/views/footer.html +27 -0
- benchlab/vu/VU-Server/www/views/header.html +86 -0
- benchlab/vu/VU-Server/www/views/key_settings.html +132 -0
- benchlab/vu/VU-Server/www/views/start.html +124 -0
- benchlab/vu/__init__.py +5 -0
- benchlab/vu/assets/Barlow-Bold.ttf +0 -0
- benchlab/vu/assets/bl_dial_144x200.png +0 -0
- benchlab/vu/assets/bl_logo_144x200.png +0 -0
- benchlab/vu/devices.py +166 -0
- benchlab/vu/requirements.txt +24 -0
- benchlab/vu/sensors.py +18 -0
- benchlab/vu/vu_dial.config_template +90 -0
- benchlab/vu/vu_logo_gen.py +97 -0
- benchlab/vu/vu_server.config_template +5 -0
- benchlab/vu/vu_server_config.py +40 -0
- benchlab/vu/vu_server_manager.py +192 -0
- benchlab/vu/vu_tui.py +919 -0
- benchlab/vu/vu_updater.py +540 -0
- benchlab/wigidash/README.md +182 -0
- benchlab/wigidash/__init__.py +5 -0
- benchlab/wigidash/assets/Inter-Bold.ttf +0 -0
- benchlab/wigidash/assets/Inter.ttf +0 -0
- benchlab/wigidash/assets/benchlab.png +0 -0
- benchlab/wigidash/benchlab_fleet.py +150 -0
- benchlab/wigidash/benchlab_graph.py +517 -0
- benchlab/wigidash/benchlab_overview.py +426 -0
- benchlab/wigidash/benchlab_telemetry.py +105 -0
- benchlab/wigidash/benchlab_ui.py +190 -0
- benchlab/wigidash/benchlab_utils.py +182 -0
- benchlab/wigidash/requirements.txt +9 -0
- benchlab/wigidash/wigidash_device.py +541 -0
- benchlab/wigidash/wigidash_manager.py +342 -0
- benchlab/wigidash/wigidash_session.py +355 -0
- benchlab/wigidash/wigidash_usb.py +247 -0
- benchlab/wigidash/wigidash_widget.py +88 -0
- benchlab_pytools-3.0.0.dist-info/METADATA +239 -0
- benchlab_pytools-3.0.0.dist-info/RECORD +184 -0
- benchlab_pytools-3.0.0.dist-info/WHEEL +4 -0
- benchlab_pytools-3.0.0.dist-info/entry_points.txt +2 -0
- benchlab_pytools-3.0.0.dist-info/licenses/LICENSE +21 -0
benchlab/graph/app.py
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
# benchlab/graph/app.py
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import threading
|
|
5
|
+
import time
|
|
6
|
+
from collections import deque
|
|
7
|
+
|
|
8
|
+
from dearpygui import dearpygui as dpg
|
|
9
|
+
from benchlab.graph import ui
|
|
10
|
+
|
|
11
|
+
_logger = logging.getLogger("benchlab.graph")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class GraphApp:
|
|
15
|
+
def __init__(self, datasource):
|
|
16
|
+
self.datasource = datasource
|
|
17
|
+
|
|
18
|
+
# Device state
|
|
19
|
+
self.devices = []
|
|
20
|
+
self.active_device = None
|
|
21
|
+
self.latest_uid = "?"
|
|
22
|
+
self.latest_fw = "?"
|
|
23
|
+
self.connected = False
|
|
24
|
+
self.sensor_struct = None # plain dict from datasource snapshot
|
|
25
|
+
|
|
26
|
+
# Threads
|
|
27
|
+
self.lock = threading.Lock()
|
|
28
|
+
self.stop_event = threading.Event()
|
|
29
|
+
self.worker_thread = None
|
|
30
|
+
self.graph_updater_thread = None
|
|
31
|
+
|
|
32
|
+
# Graphing state
|
|
33
|
+
self.selected_sensor = None
|
|
34
|
+
self.selected_device = None
|
|
35
|
+
self.graph_x_axis = None
|
|
36
|
+
self.graph_y_axis = None
|
|
37
|
+
self.graph_line = None
|
|
38
|
+
|
|
39
|
+
# Configuration
|
|
40
|
+
self.sensor_read_interval = 1.0
|
|
41
|
+
self.graph_update_interval = 0.2
|
|
42
|
+
self.history_length = 50
|
|
43
|
+
|
|
44
|
+
# Statistics
|
|
45
|
+
self.session_stats = {
|
|
46
|
+
"min": None,
|
|
47
|
+
"max": None,
|
|
48
|
+
"avg": None,
|
|
49
|
+
"count": 0}
|
|
50
|
+
|
|
51
|
+
# ------------------------------------------------------------------
|
|
52
|
+
# Device detection (called by Detect button in UI)
|
|
53
|
+
# ------------------------------------------------------------------
|
|
54
|
+
|
|
55
|
+
def detect_devices(self):
|
|
56
|
+
"""Re-query the datasource for devices and update the combo box."""
|
|
57
|
+
try:
|
|
58
|
+
raw = self.datasource.list_devices()
|
|
59
|
+
devices = self._normalise_device_list(raw)
|
|
60
|
+
devices_sorted = sorted(devices, key=lambda d: d["port"])
|
|
61
|
+
with self.lock:
|
|
62
|
+
self.devices = devices_sorted
|
|
63
|
+
if self.devices:
|
|
64
|
+
self.active_device = self.devices[0]
|
|
65
|
+
items = [d["port"] for d in self.devices]
|
|
66
|
+
if dpg.does_item_exist("##device_combo"):
|
|
67
|
+
dpg.configure_item("##device_combo",
|
|
68
|
+
items=items, default_value=items[0])
|
|
69
|
+
self.start_sensor_thread()
|
|
70
|
+
else:
|
|
71
|
+
self.active_device = None
|
|
72
|
+
if dpg.does_item_exist("##device_combo"):
|
|
73
|
+
dpg.configure_item("##device_combo",
|
|
74
|
+
items=["<No devices>"],
|
|
75
|
+
default_value="<No devices>")
|
|
76
|
+
except Exception as e:
|
|
77
|
+
_logger.error("detect_devices error: %s", e)
|
|
78
|
+
|
|
79
|
+
def device_changed(self, sender, app_data):
|
|
80
|
+
"""Combo box callback — switch active device."""
|
|
81
|
+
with self.lock:
|
|
82
|
+
self.active_device = next(
|
|
83
|
+
(d for d in self.devices if d["port"] == app_data), None
|
|
84
|
+
)
|
|
85
|
+
if self.active_device:
|
|
86
|
+
threading.Thread(target=self._restart_sensor_thread,
|
|
87
|
+
daemon=True).start()
|
|
88
|
+
|
|
89
|
+
# ------------------------------------------------------------------
|
|
90
|
+
# Sensor value helper
|
|
91
|
+
# ------------------------------------------------------------------
|
|
92
|
+
|
|
93
|
+
def get_sensor_value(self, sensor_struct, sensor_name):
|
|
94
|
+
"""Return value for sensor_name from a telemetry dict."""
|
|
95
|
+
if not sensor_struct or not sensor_name:
|
|
96
|
+
return None
|
|
97
|
+
return sensor_struct.get(sensor_name)
|
|
98
|
+
|
|
99
|
+
# ------------------------------------------------------------------
|
|
100
|
+
# UI delegation
|
|
101
|
+
# ------------------------------------------------------------------
|
|
102
|
+
|
|
103
|
+
def open_graph_window(self, sender, app_data):
|
|
104
|
+
ui.open_graph_window(self, sender, app_data)
|
|
105
|
+
|
|
106
|
+
# ------------------------------------------------------------------
|
|
107
|
+
# Sensor polling thread
|
|
108
|
+
# ------------------------------------------------------------------
|
|
109
|
+
|
|
110
|
+
def start_sensor_thread(self):
|
|
111
|
+
self._stop_sensor_thread()
|
|
112
|
+
self.worker_thread = threading.Thread(
|
|
113
|
+
target=self._datasource_loop, daemon=True, name="GraphSensor"
|
|
114
|
+
)
|
|
115
|
+
self.worker_thread.start()
|
|
116
|
+
|
|
117
|
+
def _restart_sensor_thread(self):
|
|
118
|
+
self._stop_sensor_thread()
|
|
119
|
+
self.start_sensor_thread()
|
|
120
|
+
|
|
121
|
+
def _stop_sensor_thread(self):
|
|
122
|
+
if self.worker_thread and self.worker_thread.is_alive():
|
|
123
|
+
self.stop_event.set()
|
|
124
|
+
self.worker_thread.join(timeout=2)
|
|
125
|
+
self.stop_event.clear()
|
|
126
|
+
self.connected = False
|
|
127
|
+
self.sensor_struct = None
|
|
128
|
+
|
|
129
|
+
def _normalise_device_list(self, raw) -> list:
|
|
130
|
+
if isinstance(raw, dict):
|
|
131
|
+
return [{"uid": uid, "port": info.get("port", "?"), **info}
|
|
132
|
+
for uid, info in raw.items()]
|
|
133
|
+
return [{"uid": d.get("uid", "?"), "port": d.get("port", "?"), **d}
|
|
134
|
+
for d in raw]
|
|
135
|
+
|
|
136
|
+
def _datasource_loop(self):
|
|
137
|
+
"""Poll DataSourceManager.snapshot() and update sensor_struct."""
|
|
138
|
+
try:
|
|
139
|
+
raw = self.datasource.list_devices()
|
|
140
|
+
device_list = self._normalise_device_list(raw)
|
|
141
|
+
|
|
142
|
+
selected_port = (self.active_device or {}).get("port")
|
|
143
|
+
target = next((d for d in device_list
|
|
144
|
+
if d.get("port") == selected_port), None)
|
|
145
|
+
if target is None and device_list:
|
|
146
|
+
target = device_list[0]
|
|
147
|
+
if not target:
|
|
148
|
+
_logger.warning("No devices found in datasource")
|
|
149
|
+
return
|
|
150
|
+
|
|
151
|
+
uid = target.get("uid", "?")
|
|
152
|
+
|
|
153
|
+
with self.lock:
|
|
154
|
+
self.connected = True
|
|
155
|
+
self.latest_uid = uid
|
|
156
|
+
if self.active_device:
|
|
157
|
+
self.active_device["uid"] = uid
|
|
158
|
+
else:
|
|
159
|
+
self.active_device = {
|
|
160
|
+
"port": target.get(
|
|
161
|
+
"port", "?"), "uid": uid}
|
|
162
|
+
all_ports = [
|
|
163
|
+
d["port"] for d in self.devices] if self.devices else [
|
|
164
|
+
self.active_device["port"]]
|
|
165
|
+
if dpg.does_item_exist("##device_combo"):
|
|
166
|
+
dpg.configure_item(
|
|
167
|
+
"##device_combo",
|
|
168
|
+
items=all_ports,
|
|
169
|
+
default_value=self.active_device["port"])
|
|
170
|
+
|
|
171
|
+
while not self.stop_event.is_set():
|
|
172
|
+
try:
|
|
173
|
+
self.datasource.select_device(uid)
|
|
174
|
+
snap = self.datasource.snapshot()
|
|
175
|
+
data = (snap.get("sensor_data")
|
|
176
|
+
or snap.get("all_telemetry", {}).get(uid)
|
|
177
|
+
or {})
|
|
178
|
+
if data:
|
|
179
|
+
with self.lock:
|
|
180
|
+
self.sensor_struct = data
|
|
181
|
+
except Exception as e:
|
|
182
|
+
_logger.warning("Poll error: %s", e)
|
|
183
|
+
self.stop_event.wait(self.sensor_read_interval)
|
|
184
|
+
|
|
185
|
+
except Exception as e:
|
|
186
|
+
_logger.error("Datasource loop error: %s", e)
|
|
187
|
+
finally:
|
|
188
|
+
with self.lock:
|
|
189
|
+
self.connected = False
|
|
190
|
+
self.latest_uid = "?"
|
|
191
|
+
self.sensor_struct = None
|
|
192
|
+
|
|
193
|
+
# ------------------------------------------------------------------
|
|
194
|
+
# Graph update loop
|
|
195
|
+
# ------------------------------------------------------------------
|
|
196
|
+
|
|
197
|
+
def update_graph_loop(self):
|
|
198
|
+
x_data = deque(maxlen=self.history_length)
|
|
199
|
+
y_data = deque(maxlen=self.history_length)
|
|
200
|
+
t = 0
|
|
201
|
+
|
|
202
|
+
if not self.graph_line or not dpg.does_item_exist(self.graph_line):
|
|
203
|
+
time.sleep(1)
|
|
204
|
+
return
|
|
205
|
+
|
|
206
|
+
user_data = dpg.get_item_user_data(self.graph_line) or {
|
|
207
|
+
"x_data": [], "y_data": []}
|
|
208
|
+
dpg.set_item_user_data(self.graph_line, user_data)
|
|
209
|
+
|
|
210
|
+
current_sensor = self.selected_sensor
|
|
211
|
+
current_device = self.selected_device
|
|
212
|
+
|
|
213
|
+
while dpg.does_item_exist(
|
|
214
|
+
"##main_plot") and self.connected and self.graph_line:
|
|
215
|
+
try:
|
|
216
|
+
if (self.selected_sensor != current_sensor
|
|
217
|
+
or self.selected_device != current_device):
|
|
218
|
+
t = 0
|
|
219
|
+
x_data.clear()
|
|
220
|
+
y_data.clear()
|
|
221
|
+
user_data["x_data"].clear()
|
|
222
|
+
user_data["y_data"].clear()
|
|
223
|
+
self.session_stats = {
|
|
224
|
+
"min": None,
|
|
225
|
+
"max": None,
|
|
226
|
+
"avg": None,
|
|
227
|
+
"count": 0,
|
|
228
|
+
"history": deque(
|
|
229
|
+
maxlen=1000)}
|
|
230
|
+
current_sensor = self.selected_sensor
|
|
231
|
+
current_device = self.selected_device
|
|
232
|
+
|
|
233
|
+
value = None
|
|
234
|
+
with self.lock:
|
|
235
|
+
if self.sensor_struct:
|
|
236
|
+
value = self.get_sensor_value(
|
|
237
|
+
self.sensor_struct, self.selected_sensor)
|
|
238
|
+
|
|
239
|
+
if (value is not None and self.graph_line
|
|
240
|
+
and dpg.does_item_exist(self.graph_line)):
|
|
241
|
+
t += 1
|
|
242
|
+
x_data.append(t)
|
|
243
|
+
y_data.append(value)
|
|
244
|
+
self._update_session_stats(value)
|
|
245
|
+
|
|
246
|
+
user_data["x_data"] = list(x_data)
|
|
247
|
+
user_data["y_data"] = list(y_data)
|
|
248
|
+
dpg.set_value(
|
|
249
|
+
self.graph_line, [
|
|
250
|
+
list(x_data), list(y_data)])
|
|
251
|
+
|
|
252
|
+
min_y = min(y_data)
|
|
253
|
+
max_y = max(y_data)
|
|
254
|
+
margin = (max_y - min_y) * 0.1 if max_y != min_y else 1
|
|
255
|
+
|
|
256
|
+
if (x_data and self.graph_x_axis is not None
|
|
257
|
+
and dpg.does_item_exist(self.graph_x_axis)):
|
|
258
|
+
dpg.set_axis_limits(self.graph_x_axis, float(
|
|
259
|
+
x_data[0]), float(x_data[-1]))
|
|
260
|
+
if self.graph_y_axis is not None and dpg.does_item_exist(
|
|
261
|
+
self.graph_y_axis):
|
|
262
|
+
dpg.set_axis_limits(self.graph_y_axis,
|
|
263
|
+
min_y - margin, max_y + margin)
|
|
264
|
+
|
|
265
|
+
s = self.session_stats
|
|
266
|
+
min_text = f"Min: {
|
|
267
|
+
s['min']:.2f}" if s["min"] is not None else "Min: --"
|
|
268
|
+
max_text = f"Max: {
|
|
269
|
+
s['max']:.2f}" if s["max"] is not None else "Max: --"
|
|
270
|
+
avg_text = f"Avg: {
|
|
271
|
+
s['avg']:.2f}" if s["avg"] is not None else "Avg: --"
|
|
272
|
+
for tag, text in (
|
|
273
|
+
("graph_min", min_text),
|
|
274
|
+
("graph_min_float", min_text),
|
|
275
|
+
("graph_max", max_text),
|
|
276
|
+
("graph_max_float", max_text),
|
|
277
|
+
("graph_avg", avg_text),
|
|
278
|
+
("graph_avg_float", avg_text)):
|
|
279
|
+
if dpg.does_item_exist(tag):
|
|
280
|
+
dpg.set_value(tag, text)
|
|
281
|
+
except Exception as e:
|
|
282
|
+
_logger.warning("Graph update error: %s", e)
|
|
283
|
+
|
|
284
|
+
time.sleep(self.graph_update_interval)
|
|
285
|
+
|
|
286
|
+
def _update_session_stats(self, value):
|
|
287
|
+
if not isinstance(self.session_stats.get("history"), deque):
|
|
288
|
+
self.session_stats["history"] = deque(maxlen=1000)
|
|
289
|
+
self.session_stats["history"].append(value)
|
|
290
|
+
s = self.session_stats
|
|
291
|
+
s["min"] = value if s["min"] is None else min(s["min"], value)
|
|
292
|
+
s["max"] = value if s["max"] is None else max(s["max"], value)
|
|
293
|
+
s["count"] += 1
|
|
294
|
+
s["avg"] = sum(s["history"]) / len(s["history"])
|
|
295
|
+
|
|
296
|
+
# ------------------------------------------------------------------
|
|
297
|
+
# Main run loop
|
|
298
|
+
# ------------------------------------------------------------------
|
|
299
|
+
|
|
300
|
+
def run(self):
|
|
301
|
+
dpg.create_context()
|
|
302
|
+
dpg.create_viewport(
|
|
303
|
+
title="BENCHLAB Graph Interface",
|
|
304
|
+
width=1000,
|
|
305
|
+
height=700)
|
|
306
|
+
ui.build_unified_window(self)
|
|
307
|
+
dpg.setup_dearpygui()
|
|
308
|
+
dpg.show_viewport()
|
|
309
|
+
|
|
310
|
+
self.start_sensor_thread()
|
|
311
|
+
self.graph_updater_thread = threading.Thread(
|
|
312
|
+
target=self.update_graph_loop, daemon=True, name="GraphUpdater"
|
|
313
|
+
)
|
|
314
|
+
self.graph_updater_thread.start()
|
|
315
|
+
|
|
316
|
+
_sensors_populated = False
|
|
317
|
+
try:
|
|
318
|
+
while dpg.is_dearpygui_running():
|
|
319
|
+
with self.lock:
|
|
320
|
+
status_text = (
|
|
321
|
+
"Connected" if self.connected else "Disconnected")
|
|
322
|
+
status_color = (
|
|
323
|
+
0, 255, 0) if self.connected else (
|
|
324
|
+
255, 0, 0)
|
|
325
|
+
if dpg.does_item_exist("device_status"):
|
|
326
|
+
dpg.set_value("device_status", status_text)
|
|
327
|
+
dpg.configure_item("device_status", color=status_color)
|
|
328
|
+
if dpg.does_item_exist("device_uid"):
|
|
329
|
+
dpg.set_value(
|
|
330
|
+
"device_uid",
|
|
331
|
+
self.latest_uid
|
|
332
|
+
if self.latest_uid != "?" else "Unknown")
|
|
333
|
+
|
|
334
|
+
# Populate sensor combo once first data arrives
|
|
335
|
+
if not _sensors_populated and self.sensor_struct:
|
|
336
|
+
keys = [
|
|
337
|
+
k for k in self.sensor_struct
|
|
338
|
+
if k.lower() != "timestamp"]
|
|
339
|
+
if keys and dpg.does_item_exist("##sensor_combo"):
|
|
340
|
+
dpg.configure_item(
|
|
341
|
+
"##sensor_combo", items=keys,
|
|
342
|
+
default_value=keys[0])
|
|
343
|
+
_sensors_populated = True
|
|
344
|
+
|
|
345
|
+
ui.update_current_values_display(self)
|
|
346
|
+
dpg.render_dearpygui_frame()
|
|
347
|
+
finally:
|
|
348
|
+
self.stop_event.set()
|
|
349
|
+
self._stop_sensor_thread()
|
|
350
|
+
if (self.graph_updater_thread
|
|
351
|
+
and self.graph_updater_thread.is_alive()):
|
|
352
|
+
self.graph_updater_thread.join(timeout=2)
|
|
353
|
+
dpg.destroy_context()
|
benchlab/graph/runner.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# benchlab/graph/runner.py
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import types
|
|
5
|
+
|
|
6
|
+
from benchlab.core.datasource_manager import DataSourceManager
|
|
7
|
+
from .app import GraphApp
|
|
8
|
+
|
|
9
|
+
_logger = logging.getLogger("benchlab.graph")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def run_graph_mode(args=None):
|
|
13
|
+
if args is None:
|
|
14
|
+
args = types.SimpleNamespace(
|
|
15
|
+
source="direct", interval=1.0,
|
|
16
|
+
api_url="http://127.0.0.1:8000", api_port=8000,
|
|
17
|
+
mqtt_broker="localhost", mqtt_port=1883,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
source = args.source
|
|
21
|
+
_logger.info(f"Graph: connecting via {source} datasource")
|
|
22
|
+
|
|
23
|
+
ds_kwargs = {}
|
|
24
|
+
if source in ("fastapi", "fastapi_custom"):
|
|
25
|
+
ds_kwargs["base_url"] = args.api_url
|
|
26
|
+
elif source == "mqtt":
|
|
27
|
+
ds_kwargs["broker"] = args.mqtt_broker
|
|
28
|
+
ds_kwargs["port"] = args.mqtt_port
|
|
29
|
+
|
|
30
|
+
datasource = DataSourceManager(source_type=source, **ds_kwargs)
|
|
31
|
+
if not datasource.connect():
|
|
32
|
+
_logger.error(f"Graph: failed to connect to {source} datasource")
|
|
33
|
+
return
|
|
34
|
+
|
|
35
|
+
app = GraphApp(datasource=datasource)
|
|
36
|
+
app.sensor_read_interval = args.interval
|
|
37
|
+
try:
|
|
38
|
+
app.run()
|
|
39
|
+
except KeyboardInterrupt:
|
|
40
|
+
pass
|
|
41
|
+
finally:
|
|
42
|
+
datasource.disconnect()
|
benchlab/graph/ui.py
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# benchlab/graph/ui.py
|
|
2
|
+
|
|
3
|
+
import threading
|
|
4
|
+
from collections import deque
|
|
5
|
+
from dearpygui import dearpygui as dpg
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def build_unified_window(app):
|
|
9
|
+
"""Build the main two-column window."""
|
|
10
|
+
with dpg.window(label="BENCHLAB Graph Interface",
|
|
11
|
+
width=950, height=700, tag="##main_window"):
|
|
12
|
+
with dpg.group(horizontal=True):
|
|
13
|
+
|
|
14
|
+
# ── Left column: controls ────────────────────────────────
|
|
15
|
+
with dpg.child_window(label="Controls", width=400, border=True):
|
|
16
|
+
|
|
17
|
+
# Device Management
|
|
18
|
+
dpg.add_text("Device Management", color=(200, 200, 200))
|
|
19
|
+
dpg.add_separator()
|
|
20
|
+
with dpg.group(horizontal=True):
|
|
21
|
+
dpg.add_text("Port:", color=(180, 180, 180))
|
|
22
|
+
dpg.add_spacer(width=4)
|
|
23
|
+
dpg.add_combo(
|
|
24
|
+
items=[
|
|
25
|
+
d["port"] for d in app.devices],
|
|
26
|
+
default_value=(
|
|
27
|
+
app.devices[0]["port"]
|
|
28
|
+
if app.devices else "<No devices>"
|
|
29
|
+
),
|
|
30
|
+
callback=app.device_changed,
|
|
31
|
+
tag="##device_combo",
|
|
32
|
+
width=180,
|
|
33
|
+
)
|
|
34
|
+
dpg.add_button(label="Detect",
|
|
35
|
+
callback=lambda: app.detect_devices(),
|
|
36
|
+
tag="##detect_button", width=80)
|
|
37
|
+
with dpg.group(horizontal=True):
|
|
38
|
+
dpg.add_text("Status:", color=(180, 180, 180))
|
|
39
|
+
dpg.add_spacer(width=4)
|
|
40
|
+
dpg.add_text(
|
|
41
|
+
"Disconnected",
|
|
42
|
+
tag="device_status",
|
|
43
|
+
color=(
|
|
44
|
+
255,
|
|
45
|
+
0,
|
|
46
|
+
0))
|
|
47
|
+
dpg.add_spacer(width=8)
|
|
48
|
+
dpg.add_text("UID:", color=(180, 180, 180))
|
|
49
|
+
dpg.add_text(
|
|
50
|
+
"Unknown", tag="device_uid", color=(
|
|
51
|
+
200, 200, 200))
|
|
52
|
+
|
|
53
|
+
dpg.add_separator()
|
|
54
|
+
|
|
55
|
+
# Configuration
|
|
56
|
+
dpg.add_text("Configuration", color=(200, 200, 200))
|
|
57
|
+
with dpg.group(horizontal=True):
|
|
58
|
+
dpg.add_text("History:", color=(180, 180, 180))
|
|
59
|
+
dpg.add_spacer(width=8)
|
|
60
|
+
dpg.add_input_int(
|
|
61
|
+
default_value=app.history_length,
|
|
62
|
+
min_value=10,
|
|
63
|
+
max_value=1000,
|
|
64
|
+
callback=lambda s,
|
|
65
|
+
v: setattr(
|
|
66
|
+
app,
|
|
67
|
+
"history_length",
|
|
68
|
+
v),
|
|
69
|
+
tag="##history_length",
|
|
70
|
+
width=120,
|
|
71
|
+
)
|
|
72
|
+
with dpg.group(horizontal=True):
|
|
73
|
+
dpg.add_text("Graph Update:", color=(180, 180, 180))
|
|
74
|
+
dpg.add_spacer(width=4)
|
|
75
|
+
dpg.add_input_float(
|
|
76
|
+
default_value=app.graph_update_interval,
|
|
77
|
+
min_value=0.05,
|
|
78
|
+
max_value=5.0,
|
|
79
|
+
callback=lambda s,
|
|
80
|
+
v: setattr(
|
|
81
|
+
app,
|
|
82
|
+
"graph_update_interval",
|
|
83
|
+
v),
|
|
84
|
+
tag="##graph_update_interval",
|
|
85
|
+
width=120,
|
|
86
|
+
)
|
|
87
|
+
with dpg.group(horizontal=True):
|
|
88
|
+
dpg.add_text("Sensor Read:", color=(180, 180, 180))
|
|
89
|
+
dpg.add_spacer(width=4)
|
|
90
|
+
dpg.add_input_float(
|
|
91
|
+
default_value=app.sensor_read_interval,
|
|
92
|
+
min_value=0.1,
|
|
93
|
+
max_value=10.0,
|
|
94
|
+
callback=lambda s,
|
|
95
|
+
v: setattr(
|
|
96
|
+
app,
|
|
97
|
+
"sensor_read_interval",
|
|
98
|
+
v),
|
|
99
|
+
tag="##sensor_read_interval",
|
|
100
|
+
width=120,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
dpg.add_separator()
|
|
104
|
+
|
|
105
|
+
# Sensor Selection
|
|
106
|
+
dpg.add_text("Sensor Selection", color=(200, 200, 200))
|
|
107
|
+
with dpg.group(horizontal=True):
|
|
108
|
+
dpg.add_text("Sensor:", color=(180, 180, 180))
|
|
109
|
+
dpg.add_spacer(width=4)
|
|
110
|
+
dpg.add_combo(
|
|
111
|
+
items=[],
|
|
112
|
+
default_value="",
|
|
113
|
+
tag="##sensor_combo",
|
|
114
|
+
width=180,
|
|
115
|
+
callback=lambda s, v: _update_sensor_selection(app),
|
|
116
|
+
)
|
|
117
|
+
dpg.add_button(label="Start Graph",
|
|
118
|
+
callback=lambda: start_graph(app),
|
|
119
|
+
tag="##start_graph_button", width=180)
|
|
120
|
+
|
|
121
|
+
dpg.add_separator()
|
|
122
|
+
|
|
123
|
+
# Current Values
|
|
124
|
+
dpg.add_text("Current Values", color=(200, 200, 200))
|
|
125
|
+
with dpg.group(horizontal=True):
|
|
126
|
+
dpg.add_text("Current:", color=(180, 180, 180))
|
|
127
|
+
dpg.add_spacer(width=4)
|
|
128
|
+
dpg.add_text("--", tag="current_value")
|
|
129
|
+
dpg.add_spacer(width=8)
|
|
130
|
+
dpg.add_text("Min:", color=(180, 180, 180))
|
|
131
|
+
dpg.add_text("--", tag="current_min")
|
|
132
|
+
with dpg.group(horizontal=True):
|
|
133
|
+
dpg.add_text("Max:", color=(180, 180, 180))
|
|
134
|
+
dpg.add_spacer(width=12)
|
|
135
|
+
dpg.add_text("--", tag="current_max")
|
|
136
|
+
dpg.add_spacer(width=8)
|
|
137
|
+
dpg.add_text("Avg:", color=(180, 180, 180))
|
|
138
|
+
dpg.add_text("--", tag="current_avg")
|
|
139
|
+
dpg.add_button(label="Reset Stats",
|
|
140
|
+
callback=lambda: _reset_session_stats(app),
|
|
141
|
+
tag="##reset_stats_button", width=180)
|
|
142
|
+
|
|
143
|
+
# ── Right column: graph ──────────────────────────────────
|
|
144
|
+
with dpg.child_window(label="Graph", border=True):
|
|
145
|
+
dpg.add_text("Live Graph", color=(200, 200, 200))
|
|
146
|
+
dpg.add_separator()
|
|
147
|
+
with dpg.group(horizontal=True):
|
|
148
|
+
dpg.add_text("Sensor:", color=(180, 180, 180))
|
|
149
|
+
dpg.add_text("None", tag="selected_sensor_name")
|
|
150
|
+
dpg.add_spacer(width=8)
|
|
151
|
+
dpg.add_text("Device:", color=(180, 180, 180))
|
|
152
|
+
dpg.add_text("None", tag="selected_device_name")
|
|
153
|
+
with dpg.group(horizontal=True):
|
|
154
|
+
dpg.add_text("Session Stats:", color=(180, 180, 180))
|
|
155
|
+
dpg.add_text("Min: --", tag="graph_min")
|
|
156
|
+
dpg.add_text("Max: --", tag="graph_max")
|
|
157
|
+
dpg.add_text("Avg: --", tag="graph_avg")
|
|
158
|
+
dpg.add_separator()
|
|
159
|
+
with dpg.plot(
|
|
160
|
+
label="Sensor Data", height=-1, width=-1,
|
|
161
|
+
tag="##main_plot",
|
|
162
|
+
):
|
|
163
|
+
dpg.add_plot_legend()
|
|
164
|
+
app.graph_x_axis = dpg.add_plot_axis(
|
|
165
|
+
dpg.mvXAxis, label="Time")
|
|
166
|
+
app.graph_y_axis = dpg.add_plot_axis(
|
|
167
|
+
dpg.mvYAxis, label="Value")
|
|
168
|
+
app.graph_line = dpg.add_line_series(
|
|
169
|
+
[], [], label="Sensor Data", parent=app.graph_y_axis
|
|
170
|
+
)
|
|
171
|
+
dpg.set_item_user_data(
|
|
172
|
+
app.graph_line, {
|
|
173
|
+
"x_data": [], "y_data": []})
|
|
174
|
+
|
|
175
|
+
# Remember the main window's graph items so the floating window can
|
|
176
|
+
# hand control back to them when it is closed.
|
|
177
|
+
app.main_graph_x_axis = app.graph_x_axis
|
|
178
|
+
app.main_graph_y_axis = app.graph_y_axis
|
|
179
|
+
app.main_graph_line = app.graph_line
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _reset_session_stats(app):
|
|
183
|
+
app.session_stats = {"min": None, "max": None, "avg": None, "count": 0,
|
|
184
|
+
"history": deque(maxlen=1000)}
|
|
185
|
+
for tag, val in [
|
|
186
|
+
("graph_min", "Min: --"),
|
|
187
|
+
("graph_max", "Max: --"),
|
|
188
|
+
("graph_avg", "Avg: --"),
|
|
189
|
+
("graph_min_float", "Min: --"),
|
|
190
|
+
("graph_max_float", "Max: --"),
|
|
191
|
+
("graph_avg_float", "Avg: --"),
|
|
192
|
+
]:
|
|
193
|
+
if dpg.does_item_exist(tag):
|
|
194
|
+
dpg.set_value(tag, val)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _update_sensor_selection(app):
|
|
198
|
+
app.selected_sensor = dpg.get_value("##sensor_combo")
|
|
199
|
+
app.selected_device = app.latest_uid if app.latest_uid != "?" else None
|
|
200
|
+
if dpg.does_item_exist("selected_sensor_name"):
|
|
201
|
+
dpg.set_value("selected_sensor_name", app.selected_sensor or "None")
|
|
202
|
+
if dpg.does_item_exist("selected_device_name"):
|
|
203
|
+
dpg.set_value("selected_device_name", app.selected_device or "None")
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def start_graph(app):
|
|
207
|
+
"""Start graphing the selected sensor."""
|
|
208
|
+
if not app.selected_sensor:
|
|
209
|
+
app.selected_sensor = dpg.get_value("##sensor_combo")
|
|
210
|
+
if not app.selected_device:
|
|
211
|
+
app.selected_device = app.latest_uid if app.latest_uid != "?" else None
|
|
212
|
+
_reset_session_stats(app)
|
|
213
|
+
if not (getattr(app, "graph_updater_thread", None)
|
|
214
|
+
and app.graph_updater_thread.is_alive()):
|
|
215
|
+
app.graph_updater_thread = threading.Thread(
|
|
216
|
+
target=app.update_graph_loop, daemon=True
|
|
217
|
+
)
|
|
218
|
+
app.graph_updater_thread.start()
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def update_current_values_display(app):
|
|
222
|
+
"""Update the live value display. Called from the GUI render loop."""
|
|
223
|
+
if not app.selected_sensor or not app.sensor_struct:
|
|
224
|
+
return
|
|
225
|
+
value = app.get_sensor_value(app.sensor_struct, app.selected_sensor)
|
|
226
|
+
if value is None:
|
|
227
|
+
return
|
|
228
|
+
if dpg.does_item_exist("current_value"):
|
|
229
|
+
dpg.set_value("current_value", f"{value:.2f}")
|
|
230
|
+
h = app.session_stats.get("history")
|
|
231
|
+
if h:
|
|
232
|
+
if dpg.does_item_exist("current_min"):
|
|
233
|
+
dpg.set_value("current_min", f"{min(h):.2f}")
|
|
234
|
+
if dpg.does_item_exist("current_max"):
|
|
235
|
+
dpg.set_value("current_max", f"{max(h):.2f}")
|
|
236
|
+
if dpg.does_item_exist("current_avg"):
|
|
237
|
+
dpg.set_value("current_avg", f"{sum(h) / len(h):.2f}")
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def open_graph_window(app, sender=None, app_data=None):
|
|
241
|
+
"""Open a floating graph window for the selected sensor."""
|
|
242
|
+
app.selected_sensor = dpg.get_value("##sensor_combo")
|
|
243
|
+
app.selected_device = app.latest_uid if app.latest_uid != "?" else None
|
|
244
|
+
|
|
245
|
+
if dpg.does_item_exist("graph_window"):
|
|
246
|
+
dpg.delete_item("graph_window")
|
|
247
|
+
|
|
248
|
+
def _on_close(sender, app_data):
|
|
249
|
+
if dpg.does_item_exist(app.main_graph_line):
|
|
250
|
+
app.graph_x_axis = app.main_graph_x_axis
|
|
251
|
+
app.graph_y_axis = app.main_graph_y_axis
|
|
252
|
+
app.graph_line = app.main_graph_line
|
|
253
|
+
else:
|
|
254
|
+
app.graph_x_axis = None
|
|
255
|
+
app.graph_y_axis = None
|
|
256
|
+
app.graph_line = None
|
|
257
|
+
|
|
258
|
+
with dpg.window(label=f"Graph: {app.selected_sensor}",
|
|
259
|
+
tag="graph_window", width=701, height=400, pos=(0, 151),
|
|
260
|
+
on_close=_on_close):
|
|
261
|
+
dpg.add_text(
|
|
262
|
+
f"Real-time: {app.selected_sensor} — {app.selected_device}")
|
|
263
|
+
with dpg.group(horizontal=True):
|
|
264
|
+
dpg.add_text("Min: --", tag="graph_min_float")
|
|
265
|
+
dpg.add_text("Max: --", tag="graph_max_float")
|
|
266
|
+
dpg.add_text("Avg: --", tag="graph_avg_float")
|
|
267
|
+
dpg.add_button(label="Reset Stats",
|
|
268
|
+
callback=lambda: _reset_session_stats(app),
|
|
269
|
+
tag="##reset_stats")
|
|
270
|
+
with dpg.plot(label="Sensor Data", height=-1, width=-1):
|
|
271
|
+
dpg.add_plot_legend()
|
|
272
|
+
x_axis = dpg.add_plot_axis(dpg.mvXAxis, label="Time")
|
|
273
|
+
y_axis = dpg.add_plot_axis(dpg.mvYAxis, label=app.selected_sensor)
|
|
274
|
+
line_series = dpg.add_line_series(
|
|
275
|
+
[], [], label=app.selected_sensor, parent=y_axis
|
|
276
|
+
)
|
|
277
|
+
dpg.set_item_user_data(line_series, {"x_data": [], "y_data": []})
|
|
278
|
+
|
|
279
|
+
app.graph_x_axis = x_axis
|
|
280
|
+
app.graph_y_axis = y_axis
|
|
281
|
+
app.graph_line = line_series
|
|
282
|
+
|
|
283
|
+
if not (getattr(app, "graph_updater_thread", None)
|
|
284
|
+
and app.graph_updater_thread.is_alive()):
|
|
285
|
+
app.graph_updater_thread = threading.Thread(
|
|
286
|
+
target=app.update_graph_loop, daemon=True
|
|
287
|
+
)
|
|
288
|
+
app.graph_updater_thread.start()
|