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
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Enhanced CSV Fleet Logger for BENCHLAB
|
|
3
|
+
Lightweight, robust, and cross-platform compatible
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
import configparser
|
|
8
|
+
import threading
|
|
9
|
+
import time
|
|
10
|
+
import types
|
|
11
|
+
from datetime import datetime
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Dict, List, Optional, Any
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
import logging
|
|
16
|
+
|
|
17
|
+
# Core imports
|
|
18
|
+
from benchlab.core.datasource_manager import DataSourceManager
|
|
19
|
+
from benchlab.core.statistics import ChannelStats, create_stats_callback
|
|
20
|
+
from benchlab.csv_log.message_batcher import BatchingLogger, create_csv_batcher
|
|
21
|
+
from benchlab.csv_log.smart_retry import SmartRetryManager, RetryConfig
|
|
22
|
+
|
|
23
|
+
# ----------------------------------------------------------------------
|
|
24
|
+
# Configuration and Data Classes
|
|
25
|
+
# ----------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass
|
|
29
|
+
class LoggerConfig:
|
|
30
|
+
"""Configuration for the CSV logger."""
|
|
31
|
+
interval: float = 1.0
|
|
32
|
+
output_dir: str = "logs"
|
|
33
|
+
buffer_size: int = 100
|
|
34
|
+
format: str = "csv" # csv | json
|
|
35
|
+
silent_mode: bool = False
|
|
36
|
+
auto_select: bool = False
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class DeviceConfig:
|
|
40
|
+
"""Configuration for a single device."""
|
|
41
|
+
|
|
42
|
+
def __init__(
|
|
43
|
+
self,
|
|
44
|
+
port: str,
|
|
45
|
+
uid: str,
|
|
46
|
+
firmware: str = "?",
|
|
47
|
+
enabled: bool = True,
|
|
48
|
+
last_seen: Optional[datetime] = None,
|
|
49
|
+
):
|
|
50
|
+
self.port = port
|
|
51
|
+
self.uid = uid
|
|
52
|
+
self.firmware = firmware
|
|
53
|
+
self.enabled = enabled
|
|
54
|
+
self.last_seen = last_seen
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# ----------------------------------------------------------------------
|
|
58
|
+
# EnhancedCSVLogger
|
|
59
|
+
# ----------------------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
class EnhancedCSVLogger:
|
|
62
|
+
"""Enhanced CSV logger with DataSourceManager and batching integration."""
|
|
63
|
+
|
|
64
|
+
def __init__(self, config: LoggerConfig, args=None):
|
|
65
|
+
"""
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
config:
|
|
69
|
+
LoggerConfig instance.
|
|
70
|
+
args:
|
|
71
|
+
Standard benchlab args namespace. Fields used:
|
|
72
|
+
source – "direct" | "fastapi" | "mqtt"
|
|
73
|
+
interval – poll interval in seconds
|
|
74
|
+
api_url – FastAPI base URL
|
|
75
|
+
mqtt_broker – MQTT broker host
|
|
76
|
+
mqtt_port – MQTT broker port
|
|
77
|
+
May be None for direct mode.
|
|
78
|
+
"""
|
|
79
|
+
self.config = config
|
|
80
|
+
self.args = args
|
|
81
|
+
self.selected_uids: List[str] = []
|
|
82
|
+
self.stats = ChannelStats()
|
|
83
|
+
self.batcher: Optional[BatchingLogger] = None
|
|
84
|
+
self.logging_active = False
|
|
85
|
+
self._stop_event = threading.Event()
|
|
86
|
+
self.logging_thread: Optional[threading.Thread] = None
|
|
87
|
+
self.manager: Optional[DataSourceManager] = None
|
|
88
|
+
self._setup_logging()
|
|
89
|
+
Path(self.config.output_dir).mkdir(parents=True, exist_ok=True)
|
|
90
|
+
|
|
91
|
+
def __enter__(self):
|
|
92
|
+
return self
|
|
93
|
+
|
|
94
|
+
def __exit__(self, *_):
|
|
95
|
+
self.stop_logging()
|
|
96
|
+
|
|
97
|
+
# ------------------------------------------------------------------
|
|
98
|
+
# Internal setup
|
|
99
|
+
# ------------------------------------------------------------------
|
|
100
|
+
|
|
101
|
+
def _setup_logging(self):
|
|
102
|
+
level = logging.WARNING if self.config.silent_mode else logging.INFO
|
|
103
|
+
logging.basicConfig(
|
|
104
|
+
level=level,
|
|
105
|
+
format="%(asctime)s - %(levelname)s - %(message)s")
|
|
106
|
+
|
|
107
|
+
# ------------------------------------------------------------------
|
|
108
|
+
# Device discovery and selection
|
|
109
|
+
# ------------------------------------------------------------------
|
|
110
|
+
|
|
111
|
+
def discover_devices(self) -> List[DeviceConfig]:
|
|
112
|
+
"""Discover devices via the DataSourceManager."""
|
|
113
|
+
if not self.manager:
|
|
114
|
+
return []
|
|
115
|
+
devices_info = self.manager.list_devices()
|
|
116
|
+
return [
|
|
117
|
+
DeviceConfig(
|
|
118
|
+
port=info.get("port", "unknown"),
|
|
119
|
+
uid=uid,
|
|
120
|
+
firmware=info.get("firmware", "?"),
|
|
121
|
+
)
|
|
122
|
+
for uid, info in devices_info.items()
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
def select_devices(
|
|
126
|
+
self,
|
|
127
|
+
devices: List[DeviceConfig]) -> List[DeviceConfig]:
|
|
128
|
+
"""Select devices to log.
|
|
129
|
+
|
|
130
|
+
Auto-selects all devices when ``config.auto_select`` or
|
|
131
|
+
``config.silent_mode`` is set — never blocks on stdin in those cases.
|
|
132
|
+
"""
|
|
133
|
+
if not devices:
|
|
134
|
+
logging.error("No devices available for logging")
|
|
135
|
+
return []
|
|
136
|
+
|
|
137
|
+
if self.config.auto_select or self.config.silent_mode:
|
|
138
|
+
logging.info(f"Auto-selecting all {len(devices)} device(s)")
|
|
139
|
+
return devices
|
|
140
|
+
|
|
141
|
+
print("\n--- Available Devices ---")
|
|
142
|
+
for i, dev in enumerate(devices, 1):
|
|
143
|
+
print(
|
|
144
|
+
f" {i}: Port: {
|
|
145
|
+
dev.port:<12} UID: {
|
|
146
|
+
dev.uid} FW: {
|
|
147
|
+
dev.firmware}")
|
|
148
|
+
|
|
149
|
+
selection = input(
|
|
150
|
+
"\nEnter device numbers (comma-separated), 'all', "
|
|
151
|
+
"or Enter for all: "
|
|
152
|
+
).strip().lower()
|
|
153
|
+
|
|
154
|
+
if not selection or selection == "all":
|
|
155
|
+
return devices
|
|
156
|
+
|
|
157
|
+
try:
|
|
158
|
+
indices = [int(s.strip()) - 1 for s in selection.split(",")]
|
|
159
|
+
return [devices[i] for i in indices if 0 <= i < len(devices)]
|
|
160
|
+
except (ValueError, IndexError):
|
|
161
|
+
logging.error("Invalid selection")
|
|
162
|
+
return []
|
|
163
|
+
|
|
164
|
+
# ------------------------------------------------------------------
|
|
165
|
+
# Batcher
|
|
166
|
+
# ------------------------------------------------------------------
|
|
167
|
+
|
|
168
|
+
def create_batcher(self):
|
|
169
|
+
"""Create a batching logger for buffered CSV writes."""
|
|
170
|
+
self.batcher = create_csv_batcher(
|
|
171
|
+
output_dir=self.config.output_dir,
|
|
172
|
+
batch_size=self.config.buffer_size,
|
|
173
|
+
flush_interval=5.0,
|
|
174
|
+
)
|
|
175
|
+
return self.batcher
|
|
176
|
+
|
|
177
|
+
# ------------------------------------------------------------------
|
|
178
|
+
# Device data logging
|
|
179
|
+
# ------------------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
def log_device_data(self, uid: str) -> bool:
|
|
182
|
+
"""Collect telemetry for a device and buffer it via batcher."""
|
|
183
|
+
if not self.manager:
|
|
184
|
+
logging.warning(f"No manager for device {uid}")
|
|
185
|
+
return False
|
|
186
|
+
try:
|
|
187
|
+
self.manager.select_device(uid)
|
|
188
|
+
snapshot = self.manager.snapshot()
|
|
189
|
+
data = snapshot.get("sensor_data")
|
|
190
|
+
if not data:
|
|
191
|
+
logging.warning(f"No telemetry for device {uid}")
|
|
192
|
+
return False
|
|
193
|
+
|
|
194
|
+
# Strip the device-supplied 'timestamp' key (case-insensitive) to
|
|
195
|
+
# avoid a duplicate column alongside our own 'Timestamp'.
|
|
196
|
+
data = {k: v for k, v in data.items() if k.lower() != "timestamp"}
|
|
197
|
+
row = {"Timestamp": datetime.now().isoformat(), "uid": uid, **data}
|
|
198
|
+
|
|
199
|
+
if self.batcher:
|
|
200
|
+
self.batcher.add_message(row)
|
|
201
|
+
else:
|
|
202
|
+
logging.debug(f"Data for {uid}: {row}")
|
|
203
|
+
|
|
204
|
+
if not self.config.silent_mode:
|
|
205
|
+
logging.debug(
|
|
206
|
+
f"[{uid}] SYS:{data.get('SYS_Power', 0):.0f}W "
|
|
207
|
+
f"CPU:{data.get('CPU_Power', 0):.0f}W "
|
|
208
|
+
f"GPU:{data.get('GPU_Power', 0):.0f}W"
|
|
209
|
+
)
|
|
210
|
+
return True
|
|
211
|
+
except Exception as e:
|
|
212
|
+
logging.error(f"Error logging data for {uid}: {e}")
|
|
213
|
+
return False
|
|
214
|
+
|
|
215
|
+
# ------------------------------------------------------------------
|
|
216
|
+
# Connection with retry
|
|
217
|
+
# ------------------------------------------------------------------
|
|
218
|
+
|
|
219
|
+
def _connect_with_retry(self, manager: DataSourceManager) -> bool:
|
|
220
|
+
retry_cfg = RetryConfig(max_retries=3, base_delay=1.0)
|
|
221
|
+
try:
|
|
222
|
+
SmartRetryManager(retry_cfg).execute(lambda: manager.connect())
|
|
223
|
+
return True
|
|
224
|
+
except Exception as e:
|
|
225
|
+
logging.error(f"Connection failed after retries: {e}")
|
|
226
|
+
return False
|
|
227
|
+
|
|
228
|
+
# ------------------------------------------------------------------
|
|
229
|
+
# Public control
|
|
230
|
+
# ------------------------------------------------------------------
|
|
231
|
+
|
|
232
|
+
def start_logging(self):
|
|
233
|
+
"""Connect to datasource, discover devices, and begin logging."""
|
|
234
|
+
args = self.args
|
|
235
|
+
source_type = (args.source if args else None) or "direct"
|
|
236
|
+
|
|
237
|
+
ds_kwargs: Dict[str, Any] = {}
|
|
238
|
+
if source_type in ("fastapi", "fastapi_custom") and args:
|
|
239
|
+
ds_kwargs["base_url"] = args.api_url
|
|
240
|
+
elif source_type == "mqtt" and args:
|
|
241
|
+
ds_kwargs["broker"] = args.mqtt_broker
|
|
242
|
+
ds_kwargs["port"] = args.mqtt_port
|
|
243
|
+
|
|
244
|
+
self.manager = DataSourceManager(
|
|
245
|
+
source_type=source_type,
|
|
246
|
+
stats_callback=create_stats_callback(self.stats),
|
|
247
|
+
**ds_kwargs,
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
if not self._connect_with_retry(self.manager):
|
|
251
|
+
logging.error("Failed to connect to datasource")
|
|
252
|
+
return
|
|
253
|
+
|
|
254
|
+
discovered = self.discover_devices()
|
|
255
|
+
selected = self.select_devices(discovered)
|
|
256
|
+
if not selected:
|
|
257
|
+
logging.error("No devices selected")
|
|
258
|
+
return
|
|
259
|
+
self.selected_uids = [d.uid for d in selected]
|
|
260
|
+
|
|
261
|
+
self.create_batcher()
|
|
262
|
+
|
|
263
|
+
log_path = Path(self.config.output_dir).resolve()
|
|
264
|
+
print(f"\nLogging to: {log_path}")
|
|
265
|
+
if not self.config.silent_mode:
|
|
266
|
+
print("Press Ctrl+C to stop.\n")
|
|
267
|
+
|
|
268
|
+
self.logging_active = True
|
|
269
|
+
self._stop_event.clear()
|
|
270
|
+
|
|
271
|
+
try:
|
|
272
|
+
while not self._stop_event.is_set():
|
|
273
|
+
# Sleep in small chunks so Ctrl+C is responsive on Windows
|
|
274
|
+
deadline = time.monotonic() + self.config.interval
|
|
275
|
+
while time.monotonic() < deadline:
|
|
276
|
+
if self._stop_event.is_set():
|
|
277
|
+
break
|
|
278
|
+
time.sleep(min(0.1, deadline - time.monotonic()))
|
|
279
|
+
|
|
280
|
+
if self._stop_event.is_set():
|
|
281
|
+
break
|
|
282
|
+
|
|
283
|
+
if self.batcher:
|
|
284
|
+
self.batcher.flush()
|
|
285
|
+
for uid in self.selected_uids:
|
|
286
|
+
self.log_device_data(uid)
|
|
287
|
+
|
|
288
|
+
except KeyboardInterrupt:
|
|
289
|
+
print("\nStopping...")
|
|
290
|
+
finally:
|
|
291
|
+
self.stop_logging()
|
|
292
|
+
|
|
293
|
+
def stop_logging(self):
|
|
294
|
+
"""Stop logging, flush buffers, and disconnect."""
|
|
295
|
+
if not self.logging_active:
|
|
296
|
+
return # already stopped
|
|
297
|
+
self.logging_active = False
|
|
298
|
+
self._stop_event.set()
|
|
299
|
+
if self.batcher:
|
|
300
|
+
self.batcher.shutdown()
|
|
301
|
+
if self.manager:
|
|
302
|
+
self.manager.disconnect()
|
|
303
|
+
logging.info("Logging stopped")
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
# ----------------------------------------------------------------------
|
|
307
|
+
# Configuration loader
|
|
308
|
+
# ----------------------------------------------------------------------
|
|
309
|
+
|
|
310
|
+
def load_config(config_file: str = "csv_logger.config") -> LoggerConfig:
|
|
311
|
+
"""Load configuration from file and/or environment variables."""
|
|
312
|
+
config = LoggerConfig()
|
|
313
|
+
|
|
314
|
+
if os.path.exists(config_file):
|
|
315
|
+
parser = configparser.ConfigParser()
|
|
316
|
+
parser.read(config_file)
|
|
317
|
+
if "logger" in parser:
|
|
318
|
+
s = parser["logger"]
|
|
319
|
+
config.interval = float(s.get("interval", config.interval))
|
|
320
|
+
config.output_dir = s.get("output_dir", config.output_dir)
|
|
321
|
+
config.buffer_size = int(s.get("buffer_size", config.buffer_size))
|
|
322
|
+
config.format = s.get("format", config.format)
|
|
323
|
+
config.silent_mode = s.getboolean(
|
|
324
|
+
"silent_mode", config.silent_mode)
|
|
325
|
+
config.auto_select = s.getboolean(
|
|
326
|
+
"auto_select", config.auto_select)
|
|
327
|
+
|
|
328
|
+
config.interval = float(os.getenv("CSV_LOG_INTERVAL", config.interval))
|
|
329
|
+
config.output_dir = os.getenv("CSV_LOG_OUTPUT_DIR", config.output_dir)
|
|
330
|
+
config.buffer_size = int(
|
|
331
|
+
os.getenv(
|
|
332
|
+
"CSV_LOG_BUFFER_SIZE",
|
|
333
|
+
config.buffer_size))
|
|
334
|
+
config.silent_mode = os.getenv(
|
|
335
|
+
"CSV_LOG_SILENT", str(
|
|
336
|
+
config.silent_mode)).lower() == "true"
|
|
337
|
+
config.auto_select = os.getenv(
|
|
338
|
+
"CSV_LOG_AUTO_SELECT", str(
|
|
339
|
+
config.auto_select)).lower() == "true"
|
|
340
|
+
|
|
341
|
+
return config
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
# ----------------------------------------------------------------------
|
|
345
|
+
# Entry point
|
|
346
|
+
# ----------------------------------------------------------------------
|
|
347
|
+
|
|
348
|
+
def run_enhanced_csv_logger(args=None):
|
|
349
|
+
"""Run the enhanced CSV logger.
|
|
350
|
+
|
|
351
|
+
Parameters
|
|
352
|
+
----------
|
|
353
|
+
args:
|
|
354
|
+
Standard benchlab args namespace. If None, runs in direct mode
|
|
355
|
+
with default settings (backwards-compatible).
|
|
356
|
+
"""
|
|
357
|
+
print("Running Enhanced BENCHLAB CSV fleet logger...\n")
|
|
358
|
+
|
|
359
|
+
# Build a default args if called without one (e.g. from old code paths)
|
|
360
|
+
if args is None:
|
|
361
|
+
args = types.SimpleNamespace(
|
|
362
|
+
source="direct",
|
|
363
|
+
interval=1.0,
|
|
364
|
+
api_url="http://127.0.0.1:8000",
|
|
365
|
+
mqtt_broker="localhost",
|
|
366
|
+
mqtt_port=1883,
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
config = load_config()
|
|
370
|
+
config.interval = args.interval
|
|
371
|
+
|
|
372
|
+
if os.getenv("BENCHLAB_AUTO_SELECT", "").lower() == "true":
|
|
373
|
+
config.auto_select = True
|
|
374
|
+
|
|
375
|
+
with EnhancedCSVLogger(config, args=args) as logger:
|
|
376
|
+
logger.start_logging()
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
# ----------------------------------------------------------------------
|
|
380
|
+
# CLI entry point
|
|
381
|
+
# ----------------------------------------------------------------------
|
|
382
|
+
|
|
383
|
+
if __name__ == "__main__":
|
|
384
|
+
import argparse as _argparse
|
|
385
|
+
|
|
386
|
+
_parser = _argparse.ArgumentParser(
|
|
387
|
+
description="Enhanced BENCHLAB CSV Fleet Logger")
|
|
388
|
+
_parser.add_argument("-i", "--interval", type=float, default=1.0,
|
|
389
|
+
help="Logging interval in seconds")
|
|
390
|
+
_parser.add_argument("-c", "--config", default="csv_logger.config",
|
|
391
|
+
help="Configuration file path")
|
|
392
|
+
_parser.add_argument("--source", default="direct",
|
|
393
|
+
choices=["direct", "fastapi", "mqtt"],
|
|
394
|
+
help="Data source")
|
|
395
|
+
_parser.add_argument("--api-url", default="http://127.0.0.1:8000")
|
|
396
|
+
_parser.add_argument("--mqtt-broker", default="localhost")
|
|
397
|
+
_parser.add_argument("--mqtt-port", type=int, default=1883)
|
|
398
|
+
_parser.add_argument("--silent", action="store_true")
|
|
399
|
+
_parser.add_argument("--auto-select", action="store_true")
|
|
400
|
+
_cli_args = _parser.parse_args()
|
|
401
|
+
|
|
402
|
+
_config = load_config(_cli_args.config)
|
|
403
|
+
_config.interval = _cli_args.interval
|
|
404
|
+
_config.silent_mode = _cli_args.silent
|
|
405
|
+
_config.auto_select = _cli_args.auto_select
|
|
406
|
+
|
|
407
|
+
_args = types.SimpleNamespace(
|
|
408
|
+
source=_cli_args.source,
|
|
409
|
+
interval=_cli_args.interval,
|
|
410
|
+
api_url=_cli_args.api_url,
|
|
411
|
+
mqtt_broker=_cli_args.mqtt_broker,
|
|
412
|
+
mqtt_port=_cli_args.mqtt_port,
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
print("Running Enhanced BENCHLAB CSV fleet logger...\n")
|
|
416
|
+
EnhancedCSVLogger(_config, args=_args).start_logging()
|