-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock_esp32.py
More file actions
444 lines (406 loc) · 16.4 KB
/
Copy pathmock_esp32.py
File metadata and controls
444 lines (406 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/env python3
"""
Mock ESP32 Device for Testing
Simulates ESP32 device behavior:
- Registration with factory key
- Heartbeat sending
- SSE stream connection
- Test event handling
Usage:
python mock_esp32.py --device-id ESP32-AABBCC --server http://app:5000
Environment Variables:
MOCK_DEVICE_ID: Device ID (default: ESP32-MOCK01)
MOCK_SERVER_URL: Server URL (default: http://app:5000)
FACTORY_SECRET: Factory secret for key generation (reads from app config)
MOCK_FACTORY_SECRET: Factory secret (fallback)
MOCK_HEARTBEAT_INTERVAL: Heartbeat interval in seconds (default: 10)
MOCK_STREAM_RETRY_INTERVAL: SSE retry interval in seconds (default: 15)
MOCK_TEST_MODE: Test mode - 'register', 'heartbeat', 'stream', or 'full' (default: 'full')
"""
import argparse
import hashlib
import hmac
import json
import os
import random
import signal
import sys
import threading
import time
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional
import requests
class TestMode(Enum):
REGISTER = "register"
HEARTBEAT = "heartbeat"
STREAM = "stream"
FULL = "full"
@dataclass
class MockESP32Config:
device_id: str
server_url: str
factory_secret: str
heartbeat_interval: int
stream_retry_interval: int
test_mode: TestMode
auto_complete_tests: bool = True
tremor_data_points: int = 100
class MockESP32:
def __init__(self, config: MockESP32Config):
self.config = config
self.api_key: Optional[str] = None
self.is_connected = False
self.sse_thread: Optional[threading.Thread] = None
self.stop_event = threading.Event()
self.heartbeat_thread: Optional[threading.Thread] = None
self.test_events_received: list = []
self._factory_key_cache: Optional[str] = None
self._current_event: Optional[str] = None
self._current_test_data: Optional[dict] = None
def generate_mock_imu_data(self) -> dict:
points = self.config.tremor_data_points
return {
"ax": [round(random.uniform(-0.5, 0.5), 4) for _ in range(points)],
"ay": [round(random.uniform(-0.5, 0.5), 4) for _ in range(points)],
"az": [round(random.uniform(9.5, 10.5), 4) for _ in range(points)],
"gx": [round(random.uniform(-0.05, 0.05), 6) for _ in range(points)],
"gy": [round(random.uniform(-0.05, 0.05), 6) for _ in range(points)],
"gz": [round(random.uniform(-0.05, 0.05), 6) for _ in range(points)],
}
def upload_tremor_subtest(self, test_id: int, subtest_id: str, hand: str) -> bool:
if not self.api_key:
print(f"[{self.config.device_id}] Cannot upload: not registered")
return False
imu_data = self.generate_mock_imu_data()
try:
response = requests.post(
f"{self.config.server_url}/api/tests/{test_id}/tremor",
json={
"subtest_id": subtest_id,
"hand": hand,
"imu_data": imu_data,
},
headers={
"X-Device-API-Key": self.api_key,
"Content-Type": "application/json",
},
timeout=30,
)
if response.status_code == 200:
data = response.json()
print(
f"[{self.config.device_id}] Uploaded tremor: test={test_id} subtest={subtest_id} hand={hand}"
)
return True
else:
print(
f"[{self.config.device_id}] Upload failed: {response.status_code} - {response.text}"
)
return False
except Exception as e:
print(f"[{self.config.device_id}] Upload error: {e}")
return False
def complete_test(self, test_id: int) -> bool:
if not self.api_key:
print(f"[{self.config.device_id}] Cannot complete: not registered")
return False
try:
response = requests.post(
f"{self.config.server_url}/api/tests/{test_id}/complete",
headers={"X-Device-API-Key": self.api_key},
timeout=30,
)
if response.status_code in [200, 202]:
print(f"[{self.config.device_id}] Test {test_id} marked as complete")
return True
else:
print(
f"[{self.config.device_id}] Complete failed: {response.status_code} - {response.text}"
)
return False
except Exception as e:
print(f"[{self.config.device_id}] Complete error: {e}")
return False
def handle_test_started(self, test_data: dict):
test_id = test_data.get("test_id")
config = test_data.get("config", {})
if not test_id:
print(
f"[{self.config.device_id}] Invalid test_started event: missing test_id"
)
return
print(
f"[{self.config.device_id}] Handling test_started: test_id={test_id} config={config}"
)
enabled_steps = [k for k, v in config.items() if v is True]
if not enabled_steps:
print(f"[{self.config.device_id}] No enabled subtests in config")
return
print(
f"[{self.config.device_id}] Will upload {len(enabled_steps)} subtests for both hands"
)
for step in enabled_steps:
for hand in ["left", "right"]:
time.sleep(0.5)
if self.stop_event.is_set():
return
self.upload_tremor_subtest(test_id, step, hand)
print(f"[{self.config.device_id}] All subtests uploaded, completing test...")
self.complete_test(test_id)
def generate_factory_key(self) -> str:
if self._factory_key_cache:
return self._factory_key_cache
h = hmac.new(
self.config.factory_secret.encode(),
self.config.device_id.encode(),
hashlib.sha256,
)
self._factory_key_cache = f"fk_{h.hexdigest()[:32]}"
return self._factory_key_cache
def register(self) -> bool:
factory_key = self.generate_factory_key()
try:
response = requests.post(
f"{self.config.server_url}/api/esp32/register",
json={"device_id": self.config.device_id},
headers={
"X-Device-API-Key": factory_key,
"Content-Type": "application/json",
},
timeout=10,
)
if response.status_code == 200:
data = response.json()
self.api_key = data.get("data", {}).get("api_key")
if self.api_key:
print(f"[{self.config.device_id}] Registered successfully")
print(f"[{self.config.device_id}] API Key: {self.api_key[:20]}...")
else:
print(
f"[{self.config.device_id}] Registration response missing api_key"
)
return False
return True
else:
print(
f"[{self.config.device_id}] Registration failed: {response.status_code}"
)
print(f"[{self.config.device_id}] Response: {response.text}")
return False
except Exception as e:
print(f"[{self.config.device_id}] Registration error: {e}")
return False
def send_heartbeat(self) -> bool:
if not self.api_key:
print(f"[{self.config.device_id}] Cannot send heartbeat: not registered")
return False
try:
response = requests.post(
f"{self.config.server_url}/api/esp32/heartbeat",
headers={"X-Device-API-Key": self.api_key},
timeout=10,
)
if response.status_code == 200:
print(f"[{self.config.device_id}] Heartbeat sent")
return True
else:
print(
f"[{self.config.device_id}] Heartbeat failed: {response.status_code}"
)
return False
except Exception as e:
print(f"[{self.config.device_id}] Heartbeat error: {e}")
return False
def heartbeat_loop(self):
while not self.stop_event.is_set():
self.stop_event.wait(self.config.heartbeat_interval)
if not self.stop_event.is_set():
self.send_heartbeat()
def connect_stream(self):
if not self.api_key:
print(f"[{self.config.device_id}] Cannot connect stream: not registered")
return
retry_interval = max(1, self.config.stream_retry_interval)
while not self.stop_event.is_set():
response = None
should_retry = False
print(f"[{self.config.device_id}] Connecting to SSE stream...")
try:
response = requests.get(
f"{self.config.server_url}/api/esp32/stream",
headers={
"X-Device-API-Key": self.api_key,
"Accept": "text/event-stream",
"Cache-Control": "no-cache",
},
stream=True,
timeout=None,
)
if response.status_code == 403:
print(
f"[{self.config.device_id}] Stream connection failed: 403 (device likely not paired yet)"
)
should_retry = True
elif response.status_code != 200:
print(
f"[{self.config.device_id}] Stream connection failed: {response.status_code}"
)
return
else:
print(f"[{self.config.device_id}] SSE stream connected")
self.is_connected = True
for line in response.iter_lines(decode_unicode=True):
if self.stop_event.is_set():
break
if line:
self._handle_sse_line(line)
if not self.stop_event.is_set():
print(
f"[{self.config.device_id}] SSE stream closed, will reconnect"
)
should_retry = True
except Exception as e:
if not self.stop_event.is_set():
print(f"[{self.config.device_id}] Stream error: {e}")
should_retry = True
finally:
if response is not None:
response.close()
if self.is_connected:
self.is_connected = False
print(f"[{self.config.device_id}] SSE stream disconnected")
if should_retry and not self.stop_event.is_set():
print(
f"[{self.config.device_id}] Retrying SSE stream in {retry_interval} seconds..."
)
self.stop_event.wait(retry_interval)
def _handle_sse_line(self, line: str):
if line.startswith("event:"):
self._current_event = line[6:].strip()
elif line.startswith("data:"):
data_str = line[5:].strip()
try:
data = json.loads(data_str)
except json.JSONDecodeError:
data = {"raw": data_str}
event_type = self._current_event or "unknown"
print(f"[{self.config.device_id}] Received event: {event_type}")
if event_type == "test_started":
self.test_events_received.append(
{"event": event_type, "data": data, "timestamp": time.time()}
)
print(f"[{self.config.device_id}] Test started event: {data}")
if self.config.auto_complete_tests:
threading.Thread(
target=self.handle_test_started, args=(data,), daemon=True
).start()
elif event_type == "connected":
print(f"[{self.config.device_id}] SSE connected confirmation")
elif event_type == "heartbeat":
pass
else:
print(f"[{self.config.device_id}] Unknown event: {event_type} - {data}")
def start(self):
print("=" * 60)
print(f"MOCK ESP32 DEVICE")
print(f"Device ID: {self.config.device_id}")
print(f"Mode: {self.config.test_mode.value}")
print("=" * 60)
if self.config.test_mode in [TestMode.REGISTER, TestMode.FULL]:
if not self.register():
print(f"[{self.config.device_id}] Registration failed, exiting")
return
if self.config.test_mode in [TestMode.HEARTBEAT, TestMode.FULL]:
self.heartbeat_thread = threading.Thread(
target=self.heartbeat_loop, daemon=True
)
self.heartbeat_thread.start()
if self.config.test_mode in [TestMode.STREAM, TestMode.FULL]:
self.sse_thread = threading.Thread(target=self.connect_stream, daemon=True)
self.sse_thread.start()
if self.config.test_mode == TestMode.REGISTER:
print(f"[{self.config.device_id}] Register-only mode complete")
return
try:
while not self.stop_event.is_set():
self.stop_event.wait(1)
except KeyboardInterrupt:
pass
def stop(self):
print(f"[{self.config.device_id}] Stopping mock ESP32...")
self.stop_event.set()
if self.heartbeat_thread and self.heartbeat_thread.is_alive():
self.heartbeat_thread.join(timeout=2)
if self.sse_thread and self.sse_thread.is_alive():
self.sse_thread.join(timeout=2)
print(f"[{self.config.device_id}] Mock ESP32 stopped")
def get_status(self) -> dict:
return {
"device_id": self.config.device_id,
"api_key": self.api_key[:20] + "..." if self.api_key else None,
"is_connected": self.is_connected,
"test_events_received": len(self.test_events_received),
}
def main():
parser = argparse.ArgumentParser(description="Mock ESP32 Device for Testing")
parser.add_argument(
"--device-id", default=os.getenv("MOCK_DEVICE_ID", "ESP32-A1B2C3")
)
parser.add_argument(
"--server", default=os.getenv("MOCK_SERVER_URL", "http://app:5000")
)
parser.add_argument(
"--factory-secret",
default=os.getenv("FACTORY_SECRET")
or os.getenv("MOCK_FACTORY_SECRET", "your-factory-secret-change-in-production"),
)
parser.add_argument(
"--heartbeat-interval",
type=int,
default=int(os.getenv("MOCK_HEARTBEAT_INTERVAL", "10")),
)
parser.add_argument(
"--stream-retry-interval",
type=int,
default=int(os.getenv("MOCK_STREAM_RETRY_INTERVAL", "15")),
help="Seconds to wait before retrying stream connection",
)
parser.add_argument(
"--mode",
choices=["register", "heartbeat", "stream", "full"],
default=os.getenv("MOCK_TEST_MODE", "full"),
)
parser.add_argument(
"--no-auto-complete",
action="store_true",
help="Disable automatic test completion on test_started event",
)
parser.add_argument(
"--data-points",
type=int,
default=int(os.getenv("MOCK_DATA_POINTS", "1000")),
help="Number of IMU data points per subtest",
)
args = parser.parse_args()
print(f"[DEBUG] Factory secret loaded: {args.factory_secret[:10]}...")
device_id = args.device_id.upper()
config = MockESP32Config(
device_id=device_id,
server_url=args.server,
factory_secret=args.factory_secret,
heartbeat_interval=args.heartbeat_interval,
stream_retry_interval=args.stream_retry_interval,
test_mode=TestMode(args.mode),
auto_complete_tests=not args.no_auto_complete,
tremor_data_points=args.data_points,
)
device = MockESP32(config)
def signal_handler(sig, frame):
device.stop()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
device.start()
if __name__ == "__main__":
main()