Developer Guide
Architecture Overview
src/
├── main.py # FastAPI application entry point
├── schemas.py # Pydantic response models
├── core/ # Core business logic
│ ├── config_loader.py # Configuration management
│ ├── service_manager.py # Service orchestration
│ ├── models/ # Data structures and models
│ ├── services/ # Microservices (sensor, serial, test)
│ └── processing/ # Data processing pipeline
└── routers/ # API endpoints
├── api.py # Main router aggregation
├── sensor.py # Sensor operations
├── test.py # Test management
├── history.py # Historical data
└── graphique.py # Data visualization
Core Services
Sensor Manager
SensorManager
Manages sensor data acquisition from serial ports (via EventHub) or emulation.
Source code in src/core/services/sensor_manager.py
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 | |
add_func_notify(func)
Add a function that will be called with new sensor data.
Source code in src/core/services/sensor_manager.py
312 313 314 | |
add_write_func(write_func)
Add a write function to sensors task.
Source code in src/core/services/sensor_manager.py
308 309 310 | |
is_sensor_connected(sensor_id)
Check if a sensor is currently connected. Handles ARC and emulation logic.
Source code in src/core/services/sensor_manager.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
set_mode(emulated_sensors)
Set the operation mode (emulation or real hardware).
Source code in src/core/services/sensor_manager.py
80 81 82 83 84 85 86 | |
set_zero(sensor_id)
Manually zero a sensor by updating its offset.
Source code in src/core/services/sensor_manager.py
146 147 148 149 | |
start(emulated_sensors=None, sensor_ports=None)
Start sensor data acquisition.
Source code in src/core/services/sensor_manager.py
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 | |
stop()
Stop sensor data acquisition.
Source code in src/core/services/sensor_manager.py
111 112 113 114 115 116 117 118 119 120 121 122 | |
Serial Handler
SerialHandler
Source code in src/core/services/serial_handler.py
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 | |
read_serial()
async
Continuously read from the serial port, handle reconnections, and put data into the queue.
Source code in src/core/services/serial_handler.py
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 | |
start()
Start the serial handler by launching the read loop in an asynchronous task.
Source code in src/core/services/serial_handler.py
93 94 95 96 | |
stop()
Stop the serial handler and close the serial port if open.
Source code in src/core/services/serial_handler.py
98 99 100 101 102 | |
check_linux_serial_limits()
Check Linux system limits that affect serial performance
Source code in src/core/services/serial_handler.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
optimize_linux_serial(ser)
Linux-specific optimizations for low-latency serial communication
Source code in src/core/services/serial_handler.py
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 | |
Test Manager
TestManager
Source code in src/core/services/test_manager.py
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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 | |
add_file(file, filename)
Add a file to the current test directory.
Source code in src/core/services/test_manager.py
587 588 589 590 591 592 593 594 595 596 597 | |
archive_test(test_id)
Moves a test folder to the archive directory.
Source code in src/core/services/test_manager.py
403 404 405 406 407 408 409 410 411 412 | |
create_export_csv()
Create a comprehensive export CSV file for the current test, combining metadata, description, and data.csv content.
Source code in src/core/services/test_manager.py
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 | |
delete_file(filename)
Delete a file that was added to the current test.
Source code in src/core/services/test_manager.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 | |
delete_test(test_id)
Irreversibly deletes a test.
Source code in src/core/services/test_manager.py
414 415 416 417 418 419 420 421 422 | |
finalize_test()
Move stopped test to history and clear current test.
Source code in src/core/services/test_manager.py
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 | |
get_description(test_id)
Get the description.md content for a test.
Source code in src/core/services/test_manager.py
552 553 554 555 556 557 558 559 560 561 562 563 564 565 | |
get_file(filename)
Get the content of a file added to the current test.
Source code in src/core/services/test_manager.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 | |
get_graphique_png(sensor_name)
Return the graphique as PNG bytes.
Source code in src/core/services/test_manager.py
543 544 545 546 547 548 549 550 | |
get_history()
Get list of all test histories, reloaded from disk.
Source code in src/core/services/test_manager.py
520 521 522 523 | |
get_relative_time()
Get current time relative to test start, or 0.0 if no test is running.
Source code in src/core/services/test_manager.py
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | |
get_sensor_history(sensor_id, window_seconds)
Return recent data for a sensor over the requested window (seconds).
Source code in src/core/services/test_manager.py
513 514 515 516 517 518 | |
get_test_state()
Get the current state of the test system.
Returns:
| Type | Description |
|---|---|
TestState
|
TestState.NOTHING: No test running and no test prepared |
TestState
|
TestState.PREPARED: No test running but metadata has been set |
TestState
|
TestState.RUNNING: A test is currently running |
TestState
|
TestState.STOPPED: A test has been stopped but not yet finalized |
Source code in src/core/services/test_manager.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
get_treatment_json(name)
Get the content of a treatment JSON file added to the current test.
Source code in src/core/services/test_manager.py
638 639 640 641 642 643 644 645 646 647 648 | |
get_treatment_png(name)
Get the content of a treatment PNG file added to the current test.
Source code in src/core/services/test_manager.py
650 651 652 653 654 655 656 657 658 659 660 | |
get_treatment_png_base64(name)
Get the content of a treatment PNG file added to the current test, encoded in base64 for API transmission.
Source code in src/core/services/test_manager.py
662 663 664 665 666 667 | |
list_files()
List files added to the current test by the API calls.
Source code in src/core/services/test_manager.py
599 600 601 602 603 604 605 | |
prepare_test(metadata)
Sets the test as current but does not start it yet. Creates the test directory and description file.
Source code in src/core/services/test_manager.py
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 | |
reload_history()
Scans the disk for existing tests.
Source code in src/core/services/test_manager.py
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 | |
set_description(test_id, content)
Update the description.md content for a test.
Source code in src/core/services/test_manager.py
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
stop_test()
Stop recording data but keep test in memory for review/finalization.
Source code in src/core/services/test_manager.py
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 | |
Configuration
Config Loader
ConfigLoader
Loads and manages sensor configuration from JSON file.
Source code in src/core/config_loader.py
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 | |
get_all_sensors()
Get all sensor configurations.
Source code in src/core/config_loader.py
142 143 144 | |
get_config_path()
staticmethod
Get the path to the sensors_config.json file.
Source code in src/core/config_loader.py
29 30 31 32 33 34 | |
get_emulation_mode()
Get the emulation mode setting.
Source code in src/core/config_loader.py
112 113 114 | |
get_enabled_sensors()
Get only sensors that represent real hardware (have a baud).
Source code in src/core/config_loader.py
146 147 148 149 150 151 152 | |
get_sensor_baud(sensor_id)
Get the baud rate for a specific sensor.
Source code in src/core/config_loader.py
120 121 122 123 124 125 | |
get_sensor_config(sensor_id)
Get configuration for a specific sensor.
Source code in src/core/config_loader.py
116 117 118 | |
is_sensor_enabled(sensor_id)
Check if a sensor is enabled in configuration (enabled: true, except ARC).
Source code in src/core/config_loader.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
load_config()
Load configuration from JSON file.
Source code in src/core/config_loader.py
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 | |
reload_config()
Reload configuration from file.
Source code in src/core/config_loader.py
154 155 156 157 | |
Development Workflow
Setup
# Create virtual environment
bash scripts/create_venv.sh
# Install with dev dependencies
pip install -e '.[dev]'
Running the Application
# Start the server
python src/main.py
Testing
# Run tests
pytest
# Run with coverage
pytest --cov=src
Type Safety
The project uses type hints throughout for better IDE support and error catching. Always include type annotations in new code:
def process_sensor_data(
sensor_id: SensorId,
value: float,
timestamp: float
) -> Point:
"""Process raw sensor data."""
return Point(time=timestamp, value=value)
Documentation Standards
Docstring Style
Use Google-style docstrings for consistency:
def calculate_moving_average(
values: list[float],
window_size: int = 10
) -> float:
\"\"\"Calculate moving average of values.
Args:
values: List of numeric values.
window_size: Number of recent values to average.
Returns:
Moving average as float.
Raises:
ValueError: If window_size exceeds list length.
\"\"\"
if window_size > len(values):
raise ValueError("Window size exceeds list length")
return sum(values[-window_size:]) / window_size
Module Docstrings
Every module should have a top-level docstring:
\"\"\"Module description.
Brief explanation of what this module does.
\"\"\"