-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmed-api.html
More file actions
567 lines (527 loc) · 25.3 KB
/
med-api.html
File metadata and controls
567 lines (527 loc) · 25.3 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MED-API - MEDWEAR</title>
<link rel="stylesheet" href="style.css" />
<link rel="icon" href="favicon.ico" />
</head>
<body>
<header>
<div class="header-upper">
<!-- <img src="assets/images/medwear_logo.png" alt="MEDWEAR Logo" class="logo" /> -->
</div>
<div class="header-lower">
<h1>MEDWEAR</h1>
<p>Open, Interoperable Standards for Medical Wearables</p>
</div>
<hr class="header-separator" />
<div class="under-construction-banner">
🚧 This website is currently under construction. Some content may be incomplete. 🚧
</div>
<nav>
<a href="index.html">Home</a>
<a href="about.html">About</a>
<a href="standards.html">Standards</a>
<a href="documentation.html">Documentation</a>
<a href="schemas.html">Schemas</a>
<a href="contribute.html">Contribute</a>
<a href="med-api.html" class="active">MED-API</a>
</nav>
</header>
<div class="content-container">
<main>
<section>
<h2>MED-API</h2>
<p>
MED-API is the backend service of the MEDWEAR platform. It provides a secure REST API for ingesting,
querying, and managing wearable health data. It is deployed alongside MinIO, InfluxDB, PostgreSQL,
Keycloak, Dagster, Grafana, and pgAdmin as a single-server stack.
</p>
<p>Full documentation is available on Google Drive:</p>
<p><a href="https://drive.google.com/drive/folders/1VAd-iTaK5wsSysOTB_jY4xuWnfBtODRa?usp=sharing" target="_blank">→ Open Full Documentation</a></p>
</section>
<section>
<h2>Quick Setup</h2>
<pre><code>./setup.sh</code></pre>
<p>This will automatically:</p>
<ul>
<li>Generate random secure credentials for all services (PostgreSQL, MinIO, Grafana, Keycloak, InfluxDB)</li>
<li>Create <code>.env</code> and <code>.env.medwear</code> files</li>
<li>Start all Docker services</li>
<li>Initialize InfluxDB and PostgreSQL tables</li>
<li>Save all generated credentials to <code>.credentials.txt</code></li>
</ul>
<p>After setup, view your credentials:</p>
<pre><code>cat .credentials.txt</code></pre>
<pre><code>PostgreSQL : medwear / xK7mP2qR9nL4wY8j
MinIO root : minioadmin / hT3vB6cN1mX5pQ2r
MinIO API key : medwear-api / dF8yA4kW7nJ2mR6s...
Keycloak admin : admin / hJ9nQ3vB6cN1mX5p
InfluxDB : admin / wY8jxK7mP2qR9nL4
Grafana : admin / T3vB6cN1mX5pQ2rd
pgAdmin : admin@medwear.local / F8yA4kW7nJ2mR6s</code></pre>
<p><strong>Note:</strong> Do not commit <code>.credentials.txt</code> to version control. It is already listed in <code>.gitignore</code>.</p>
</section>
<section>
<h2>Start & Stop</h2>
<pre><code># First time or after code changes
docker compose -f docker-compose.medwear-minimal.yml build
docker compose -f docker-compose.medwear-minimal.yml up -d
# Subsequent runs (no code changes)
docker compose -f docker-compose.medwear-minimal.yml up -d</code></pre>
<p>After starting, Keycloak takes ~30 seconds to be ready.</p>
<pre><code># Restart a single service (e.g. after API code change)
docker compose -f docker-compose.medwear-minimal.yml restart med-api
# Stop all services
docker compose -f docker-compose.medwear-minimal.yml down</code></pre>
</section>
<section>
<h2>Access</h2>
<table>
<thead>
<tr><th>Service</th><th>URL</th><th>Credentials</th></tr>
</thead>
<tbody>
<tr><td>MED-API</td><td>https://localhost/v1/health</td><td>Token required</td></tr>
<tr><td>API Docs</td><td>https://localhost/docs</td><td>—</td></tr>
<tr><td>Frontend</td><td>https://localhost</td><td>Keycloak login</td></tr>
<tr><td>Keycloak</td><td>http://localhost:8180/admin</td><td>admin / see .credentials.txt</td></tr>
<tr><td>pgAdmin</td><td>http://localhost:5050</td><td>admin@medwear.com / see .credentials.txt</td></tr>
<tr><td>InfluxDB</td><td>http://localhost:8086</td><td>admin / see .credentials.txt</td></tr>
<tr><td>MinIO</td><td>http://localhost:9001</td><td>minioadmin / see .credentials.txt</td></tr>
<tr><td>Grafana</td><td>http://localhost:3001</td><td>admin / see .credentials.txt</td></tr>
<tr><td>Dagster</td><td>http://localhost:3000</td><td>—</td></tr>
</tbody>
</table>
<p>The MedWear platform is deployed at <code>https://172.20.83.11</code>. Frontend and API are accessible directly. Admin services require SSH tunnel or direct server access.</p>
<details>
<summary>Remote access via SSH tunnel</summary>
<pre><code>ssh -L 443:localhost:443 \
-L 80:localhost:80 \
-L 8180:localhost:8180 \
-L 9001:localhost:9001 \
-L 8086:localhost:8086 \
-L 3001:localhost:3001 \
-L 5050:localhost:5050 \
user@your-server.com</code></pre>
</details>
</section>
<section>
<h2>Authentication</h2>
<p>All endpoints require a Keycloak JWT token.</p>
<details>
<summary>Get a token (via API proxy — recommended)</summary>
<pre><code>curl -k -X POST https://localhost/v1/data/auth/token \
-H "Content-Type: application/json" \
-d '{"username":"<user>","password":"<pass>"}'</code></pre>
</details>
<details>
<summary>Get a token (direct Keycloak — for internal/testing use only)</summary>
<pre><code>curl -X POST http://localhost:8180/realms/medwear/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=password&client_id=med-api&client_secret=<secret>&username=<user>&password=<pass>"</code></pre>
</details>
<details>
<summary>Use the token</summary>
<pre><code>curl -k -H "Authorization: Bearer <access_token>" https://localhost/v1/me</code></pre>
</details>
<details>
<summary>Health check</summary>
<pre><code>curl -k https://localhost/v1/health</code></pre>
</details>
</section>
<section>
<h2>Roles</h2>
<table>
<thead>
<tr><th>Role</th><th>Access</th></tr>
</thead>
<tbody>
<tr><td><code>device</code></td><td>Ingest data only</td></tr>
<tr><td><code>collaborator</code></td><td>Read data from assigned projects</td></tr>
<tr><td><code>researcher</code></td><td>Read data, download files</td></tr>
<tr><td><code>project_admin</code></td><td>Manage users within their project</td></tr>
<tr><td><code>global_admin</code></td><td>Full access: all projects, user/project management</td></tr>
</tbody>
</table>
</section>
<section>
<h2>Web Interface</h2>
<p>Available at <code>https://localhost</code>. Login with Keycloak credentials.</p>
<table>
<thead>
<tr><th>Tab</th><th>Access</th><th>Description</th></tr>
</thead>
<tbody>
<tr><td>Dashboard</td><td>All roles</td><td>Batch statistics, recent uploads</td></tr>
<tr><td>Upload</td><td>collaborator+</td><td>JSON, CSV, raw file upload (.cwa / .json / .csv)</td></tr>
<tr><td>Query Data</td><td>collaborator+</td><td>Filter and search batches, view/download signals</td></tr>
<tr><td>File Manager</td><td>collaborator+</td><td>MinIO files: list, download, soft-delete, restore</td></tr>
<tr><td>Activities</td><td>project_admin, global_admin</td><td>Audit log — All Activities + File Activities tabs</td></tr>
<tr><td>Admin</td><td>project_admin, global_admin</td><td>User and project management</td></tr>
<tr><td>Health Check</td><td>All roles</td><td>Real-time service status</td></tr>
</tbody>
</table>
</section>
<section>
<h2>Data Ingestion</h2>
<h3>POST /v1/data/batch — Structured sensor data</h3>
<p>Stores metadata in PostgreSQL and signals in InfluxDB. Supports JSON and CSV.</p>
<p><strong>Supported signal types:</strong> ECG, BCG, PPG, IMU, EEG, EDA, HR, HRV</p>
<details>
<summary>Example request</summary>
<pre><code>curl -k -X POST https://localhost/v1/data/batch \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"batches": [{
"header": {
"uuid": "your-uuid",
"schema_id": "ecg-lead-1.x",
"source_creation_date_time": "2026-03-12T10:00:00Z",
"acquisition_rate": {"value": 250.0, "unit": "Hz", "number_of_times": 1}
},
"body": {
"ecg_lead_data": [0.12, 0.15, 0.11],
"unit": "mV",
"effective_time_frame": {"time_interval": {
"start_date_time": "2026-03-12T10:00:00Z",
"end_date_time": "2026-03-12T10:00:03Z"
}}
},
"signal_type": "ECG",
"patient_id": 123456,
"device_id": "polar-h10-01",
"idempotency_key": "polar-h10-01-20260312-001"
}]
}'</code></pre>
</details>
<h3>POST /v1/data/upload — Raw file upload</h3>
<p>Stores raw files (<code>.cwa</code>, <code>.json</code>, <code>.csv</code>) in MinIO. Max 1 GB.</p>
<details>
<summary>Example request</summary>
<pre><code>curl -k -X POST https://localhost/v1/data/upload \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/octet-stream" \
-H "X-Filename: ecg_data.csv" \
--data-binary @ecg_data.csv</code></pre>
</details>
</section>
<section>
<h2>Query Endpoints</h2>
<table>
<thead>
<tr><th>Method</th><th>Endpoint</th><th>Description</th></tr>
</thead>
<tbody>
<tr><td>GET</td><td>/v1/data</td><td>List/filter batches</td></tr>
<tr><td>GET</td><td>/v1/data/{uuid}</td><td>Single batch (metadata + signals)</td></tr>
<tr><td>GET</td><td>/v1/data/{uuid}/metadata</td><td>Metadata only</td></tr>
<tr><td>GET</td><td>/v1/data/{uuid}/signals</td><td>Signals only</td></tr>
<tr><td>GET</td><td>/v1/data/patients/{patient_id}</td><td>All batches for a patient</td></tr>
<tr><td>DELETE</td><td>/v1/data/{uuid}</td><td>Soft delete batch</td></tr>
<tr><td>POST</td><td>/v1/data/{uuid}/restore</td><td>Restore soft-deleted batch</td></tr>
<tr><td>GET</td><td>/v1/data/files</td><td>List MinIO files</td></tr>
<tr><td>GET</td><td>/v1/data/files/{path}</td><td>Download file</td></tr>
<tr><td>DELETE</td><td>/v1/data/files/{path}</td><td>Soft delete file</td></tr>
<tr><td>POST</td><td>/v1/data/files/{path}/restore</td><td>Restore deleted file</td></tr>
</tbody>
</table>
<p>Query parameters for <code>/v1/data</code>: <code>patient_ids</code>, <code>signal_type</code>, <code>start</code>, <code>end</code>, <code>device_id</code>, <code>project_id</code>, <code>limit</code>, <code>offset</code>, <code>metadata_only</code>, <code>show_deleted</code>, <code>format</code> (json/csv), <code>downsample</code></p>
</section>
<section>
<h2>Admin Endpoints</h2>
<table>
<thead>
<tr><th>Method</th><th>Endpoint</th><th>Role required</th><th>Description</th></tr>
</thead>
<tbody>
<tr><td>POST</td><td>/v1/data/admin/users</td><td>project_admin+</td><td>Create user in Keycloak and assign to project</td></tr>
<tr><td>GET</td><td>/v1/data/admin/users</td><td>project_admin+</td><td>List users</td></tr>
<tr><td>DELETE</td><td>/v1/data/admin/user-project</td><td>project_admin+</td><td>Remove user from project</td></tr>
<tr><td>POST</td><td>/v1/data/admin/projects</td><td>global_admin</td><td>Create new project</td></tr>
<tr><td>GET</td><td>/v1/data/admin/projects</td><td>project_admin+</td><td>List projects</td></tr>
<tr><td>DELETE</td><td>/v1/data/admin/projects/{name}</td><td>global_admin</td><td>Soft delete project</td></tr>
<tr><td>POST</td><td>/v1/data/admin/projects/{name}/restore</td><td>global_admin</td><td>Restore deleted project</td></tr>
<tr><td>GET</td><td>/v1/data/admin/activities</td><td>project_admin+</td><td>Audit log</td></tr>
<tr><td>POST</td><td>/v1/data/auth/change-password</td><td>any</td><td>Change temporary password</td></tr>
</tbody>
</table>
</section>
<section>
<h2>Error Responses</h2>
<p>All API errors return a consistent JSON format:</p>
<pre><code>{
"detail": {
"error": "UPPER_SNAKE_CODE",
"detail": "Human-readable message",
"timestamp": "2026-03-31T20:00:00Z"
}
}</code></pre>
<table>
<thead>
<tr><th>HTTP</th><th>Error Code</th><th>Meaning</th></tr>
</thead>
<tbody>
<tr><td>400</td><td>INVALID_JSON</td><td>Malformed JSON body</td></tr>
<tr><td>400</td><td>SCHEMA_VALIDATION_FAILURE</td><td>Missing or invalid fields</td></tr>
<tr><td>400</td><td>CHECKSUM_MISMATCH</td><td>SHA-256 checksum did not match</td></tr>
<tr><td>400</td><td>CSV_PARSE_ERROR</td><td>CSV could not be parsed</td></tr>
<tr><td>400</td><td>UNSUPPORTED_FILE_TYPE</td><td>File extension not allowed</td></tr>
<tr><td>401</td><td>UNAUTHORIZED</td><td>Missing or invalid token</td></tr>
<tr><td>403</td><td>FORBIDDEN</td><td>Insufficient role</td></tr>
<tr><td>404</td><td>NOT_FOUND</td><td>Resource does not exist</td></tr>
<tr><td>409</td><td>DUPLICATE_BATCH</td><td>Idempotency key already used</td></tr>
<tr><td>409</td><td>DUPLICATE_FILE</td><td>File already exists in MinIO</td></tr>
<tr><td>413</td><td>PAYLOAD_TOO_LARGE</td><td>Batch exceeds 10 MB limit</td></tr>
<tr><td>422</td><td>INVALID_TIMESTAMP_ORDER</td><td>end_date_time ≤ start_date_time</td></tr>
<tr><td>422</td><td>MISSING_SAMPLING_RATE</td><td>sampling_rate = 0 and cannot be inferred</td></tr>
<tr><td>429</td><td>RATE_LIMIT_EXCEEDED</td><td>100 requests/minute per token exceeded</td></tr>
<tr><td>500</td><td>STORAGE_ERROR</td><td>MinIO / InfluxDB write failure</td></tr>
<tr><td>500</td><td>INTERNAL_SERVER_ERROR</td><td>Unexpected server error</td></tr>
</tbody>
</table>
</section>
<section>
<h2>Rate Limiting</h2>
<table>
<thead>
<tr><th>Endpoint</th><th>Limit</th></tr>
</thead>
<tbody>
<tr><td>POST /v1/data/batch</td><td>50 / minute</td></tr>
<tr><td>POST /v1/data/upload</td><td>20 / minute</td></tr>
<tr><td>All other endpoints</td><td>100 / minute</td></tr>
</tbody>
</table>
</section>
<section>
<h2>Multi-Project Architecture</h2>
<p>Each project has its own PostgreSQL database (<code>medwear_project1</code>, <code>medwear_project2</code>, etc.).</p>
<ul>
<li>Tables per project: <code>measurement_batch</code>, <code>audit_log</code>, <code>user_project</code>, <code>deleted_files</code></li>
<li>Central postgres DB tracks soft-deleted projects in <code>deleted_projects</code></li>
<li><code>global_admin</code> can create and delete projects via the Admin tab or API</li>
<li>Grafana datasources are auto-provisioned for each project on startup and sync hourly</li>
</ul>
</section>
<section>
<h2>Soft Delete & Purge</h2>
<p>All deletes are soft — data is marked deleted and permanently removed after 24 hours.</p>
<table>
<thead>
<tr><th>Resource</th><th>Soft delete</th><th>Hard delete</th></tr>
</thead>
<tbody>
<tr><td>Batch (metadata + signals)</td><td>is_deleted=true in PostgreSQL</td><td>Purge removes PostgreSQL row + InfluxDB data</td></tr>
<tr><td>File</td><td>Entry in deleted_files table</td><td>Purge removes from MinIO + table</td></tr>
<tr><td>Project</td><td>Entry in deleted_projects table</td><td>Purge drops database</td></tr>
</tbody>
</table>
<p>The purge loop runs every hour. Deleted items can be restored before the 24h window via the frontend or API.</p>
</section>
<section>
<h2>Two-Factor Authentication (2FA)</h2>
<p>Optional TOTP-based 2FA using any authenticator app (e.g. Google Authenticator).</p>
<table>
<thead>
<tr><th>Method</th><th>Endpoint</th><th>Description</th></tr>
</thead>
<tbody>
<tr><td>GET</td><td>/v1/auth/2fa/status</td><td>Check if 2FA is enabled</td></tr>
<tr><td>POST</td><td>/v1/auth/2fa/setup</td><td>Generate secret and QR code</td></tr>
<tr><td>POST</td><td>/v1/auth/2fa/enable</td><td>Verify code and activate 2FA</td></tr>
<tr><td>POST</td><td>/v1/auth/2fa/disable</td><td>Verify code and deactivate 2FA</td></tr>
<tr><td>POST</td><td>/v1/auth/2fa/verify</td><td>Verify a TOTP code after login</td></tr>
</tbody>
</table>
</section>
<section>
<h2>GDPR / Privacy</h2>
<p>MedWear implements GDPR compliance via <code>/v1/data/gdpr/*</code> endpoints. All write operations require <code>project_admin</code> or <code>global_admin</code> role.</p>
<table>
<thead>
<tr><th>Article</th><th>Endpoint</th><th>Description</th></tr>
</thead>
<tbody>
<tr><td>Art. 7</td><td>POST /v1/data/gdpr/consent/{patient_id}</td><td>Grant consent</td></tr>
<tr><td>Art. 7 §3</td><td>DELETE /v1/data/gdpr/consent/{patient_id}/{consent_type}</td><td>Revoke consent</td></tr>
<tr><td>Art. 7</td><td>GET /v1/data/gdpr/consent/{patient_id}</td><td>List all consents for a patient</td></tr>
<tr><td>Art. 17</td><td>DELETE /v1/data/gdpr/erasure/{patient_id}</td><td>Right to erasure — cascades through PostgreSQL → InfluxDB → MinIO</td></tr>
<tr><td>Art. 20</td><td>GET /v1/data/gdpr/export/{patient_id}</td><td>Data portability — exports all patient data as a JSON archive</td></tr>
</tbody>
</table>
</section>
<section>
<h2>CARDAMON Integration</h2>
<p>
The MEDWEAR platform is integrated with the <strong>CARDAMON</strong> study. A dedicated conversion script
imports CARDAMON study wearable data (Corsano wristband recordings) directly into the MEDWEAR database.
</p>
<h3>Directory structure</h3>
<pre><code><cardamon-root>/
0/
2/
<participantID>/ ← patient ID (integer folder name)
<timestampFolder>/
*.txt ← ECG (JSON-log lines, 128 Hz)
Corsano_bioz/ ← .wiff files (bioz + acc) + Emography.csv
Corsano_ppg/ ← .wiff files (ppg + acc)</code></pre>
<h3>Import full CARDAMON tree</h3>
<pre><code>python medwear_med_api/scripts/cardamon_to_medwear.py \
--cardamon-root /path/to/CARDAMON \
--device-id corsano-cw \
--api-url https://localhost \
--token <JWT> \
--project-id medwear_cardamon</code></pre>
<h3>Import a single participant</h3>
<pre><code>python medwear_med_api/scripts/cardamon_to_medwear.py \
--cardamon-root /path/to/CARDAMON \
--participant 123456 \
--device-id corsano-cw \
--token <JWT> \
--project-id medwear_cardamon</code></pre>
<h3>Signal mappings</h3>
<table>
<thead>
<tr><th>Source</th><th>MEDWEAR signal type</th><th>Notes</th></tr>
</thead>
<tbody>
<tr><td>*.txt</td><td>ECG</td><td>JSON-log format, 128 Hz</td></tr>
<tr><td>Corsano_bioz/*.wiff</td><td>EDA + IMU</td><td>Parsed by mmt_parse_wiff.py</td></tr>
<tr><td>Corsano_ppg/*.wiff</td><td>PPG + IMU</td><td>Parsed by mmt_parse_wiff.py</td></tr>
<tr><td>Corsano_bioz/Emography.csv</td><td>EDA</td><td>Skin conductance</td></tr>
<tr><td>ppg2_*.csv</td><td>PPG</td><td>Grouped by chunk_index</td></tr>
<tr><td>acc.csv</td><td>IMU</td><td>X, Y, Z axes as separate batches</td></tr>
<tr><td>bioz.csv</td><td>EDA</td><td>Bioimpedance</td></tr>
<tr><td>rr_interval.csv</td><td>ECG</td><td>RR intervals in ms</td></tr>
<tr><td>temperature.csv, activity.csv, sleep.csv, questionnaire*, adl*</td><td>—</td><td>Raw file upload to MinIO</td></tr>
</tbody>
</table>
</section>
<section>
<h2>CLAID Server Ingestion</h2>
<p>
The CLAID server (<code>172.20.83.11</code>) receives live Corsano data from phones via the CLAID framework.
Data lands at: <code>/home/scai/CLAID-Server/receivedFiles/cardio/{phone_name}/{study_id}/{clinic_id}/{participant_id}/</code>
</p>
<details>
<summary>Run directly on the server</summary>
<pre><code>python medwear_med_api/scripts/claid_to_medwear.py \
--claid-root /home/scai/CLAID-Server/receivedFiles \
--device-id corsano-cw \
--api-url https://172.20.83.11 \
--token <JWT> \
--project-id medwear_cardamon</code></pre>
</details>
<details>
<summary>Run locally after rsync</summary>
<pre><code># Sync data from server (SPZ VPN must be active)
rsync -avz --progress \
scai@172.20.83.11:/home/scai/CLAID-Server/receivedFiles/cardio/ \
/local/claid_data/cardio/
# Or use --sync flag (calls rsync automatically)
python medwear_med_api/scripts/claid_to_medwear.py \
--claid-root /local/claid_data \
--sync \
--device-id corsano-cw \
--api-url https://<medwear-server> \
--token <JWT> \
--project-id medwear_cardamon</code></pre>
</details>
</section>
<section>
<h2>ROS 2 Real-Time Bridge</h2>
<p>Streams live biosignal data from ROS 2 topics directly into the MedWear API.</p>
<table>
<thead>
<tr><th>Signal</th><th>Message type</th><th>Unit</th></tr>
</thead>
<tbody>
<tr><td>EEG</td><td>healthcare_msgs/msg/EEG</td><td>uV</td></tr>
<tr><td>EDA</td><td>healthcare_msgs/msg/EDA</td><td>uS</td></tr>
<tr><td>HR</td><td>healthcare_msgs/msg/HeartRate</td><td>bpm</td></tr>
<tr><td>HRV</td><td>healthcare_msgs/msg/HeartRateVariability</td><td>ms</td></tr>
</tbody>
</table>
<details>
<summary>Run with Docker</summary>
<pre><code>docker build -t medwear-ros2-bridge ./ros2_to_medwear
docker run --rm \
--network host \
-e MEDWEAR_USERNAME=<keycloak_user> \
-e MEDWEAR_PASSWORD=<keycloak_password> \
-e API_BASE_URL=https://<server-ip>/v1 \
medwear-ros2-bridge \
--config config.yaml \
--device corsano-001 \
--patient <patient_id> \
--project <project_id></code></pre>
</details>
</section>
<section>
<h2>Code Structure</h2>
<pre><code>medwear_med_api/
medwear_med_api/
routers/
utils.py — shared helpers
batches.py — batch query/delete/restore endpoints
files.py — MinIO file endpoints
patients.py — patient timeline endpoint
admin.py — user/project/activity management
totp.py — 2FA (TOTP) endpoints
query_router.py
batch_router.py — data ingestion endpoints
auth.py — Keycloak JWT validation
audit.py — request audit logging middleware
db.py — per-project session factory
models.py — SQLModel table definitions
grafana_utils.py — Grafana datasource auto-provisioning
influx_reader.py — InfluxDB signal queries
influx_writer.py — InfluxDB signal writes
scripts/
run_med_api.py
cardamon_to_medwear.py
claid_to_medwear.py
test_phase3.py / test_phase4.py / test_phase5.py</code></pre>
</section>
<section>
<h2>Testing</h2>
<pre><code>cd /path/to/repo/medwear_med_api
source venv/bin/activate
python3 scripts/test_phase3.py # Batch upload, CSV, validation
python3 scripts/test_phase4.py # Query, delete, restore, admin, multi-project
python3 scripts/test_phase5.py # Error handling: 46/46 tests</code></pre>
<h3>Quick smoke test</h3>
<pre><code># 1. Get a token
TOKEN=$(curl -sk -X POST https://localhost/v1/data/auth/token \
-H "Content-Type: application/json" \
-d '{"username":"<your-username>","password":"<your-password>"}' | jq -r .access_token)
# 2. Check health
curl -sk https://localhost/v1/health | jq .
# 3. Query recent batches
curl -sk -H "Authorization: Bearer $TOKEN" \
"https://localhost/v1/data?limit=5" | jq .</code></pre>
</section>
<section>
<h2>Backup</h2>
<p>Backs up PostgreSQL, InfluxDB, and MinIO to a compressed archive. Backups older than 30 days are deleted automatically.</p>
<pre><code># Run manually
./scripts/backup.sh /opt/medwear_backups
# Add to crontab (daily at 2 AM)
0 2 * * * /path/to/med-api/scripts/backup.sh /opt/medwear_backups >> /var/log/medwear_backup.log 2>&1</code></pre>
</section>
</main>
</div>
<footer>
<div class="footer-logos">
<img src="assets/images/scai_logo.png" alt="SCAI Lab Logo" class="logo" style="height: 40px; width: auto; margin-right: 10px;" />
<img src="assets/images/dart_logo.png" alt="DART Logo" class="logo" style="height: 40px; width: auto; margin-right: 10px;" />
<img src="assets/images/ethz_logo.png" alt="ETH Zurich Logo" class="logo" style="height: 20px; width: auto; margin-right: 10px;" />
</div>
<p>© 2025 ETH Zurich – MEDWEAR Project | Contact: diego.paez@scai.ethz.ch</p>
</footer>
</body>
</html>