-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwithings2influxdb.py
590 lines (461 loc) · 18.8 KB
/
withings2influxdb.py
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#!/usr/bin/python3
# encoding=utf-8
from pytz import timezone
from datetime import datetime, timedelta
from influxdb_client import InfluxDBClient, Point, WriteOptions
from influxdb_client.client.write_api import SYNCHRONOUS
import json
import os
import array
from os import path
from typing import cast
import pickle
from typing_extensions import Final
from oauthlib.oauth2.rfc6749.errors import MissingTokenError
from python_withings_api.withings_api import WithingsAuth, WithingsApi, AuthScope
from python_withings_api.withings_api.common import CredentialsType, get_measure_value, MeasureType, GetSleepField, GetSleepSummaryField, MeasureGetMeasGroupCategory
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# debug enviroment variables
debug_str=os.getenv("DEBUG", None)
if debug_str is not None:
debug = debug_str.lower() == "true"
else:
debug = False
showRaw = False
# netatmo environment variables
withings_clientId=os.getenv('WITHINGS_CLIENT_ID', "")
withings_clientSecret=os.getenv('WITHINGS_CLIENT_SECRET', "")
withings_callback=os.getenv('WITHINGS_CALLBACK', "")
withings_auth_code=os.getenv('WITHINGS_AUTH_CODE', "")
withings_sleep_at=os.getenv('WITHINGS_SLEEP_AT', "14")
# influxDBv2 environment variables
influxdb2_host=os.getenv('INFLUXDB2_HOST', "localhost")
influxdb2_port=int(os.getenv('INFLUXDB2_PORT', "8086"))
influxdb2_org=os.getenv('INFLUXDB2_ORG', "org")
influxdb2_token=os.getenv('INFLUXDB2_TOKEN', "token")
influxdb2_bucket=os.getenv('INFLUXDB2_BUCKET', "withings")
influxdb2_ssl_str=os.getenv('INFLUXDB2_SSL', "False")
if influxdb2_ssl_str is not None:
influxdb2_ssl = influxdb2_ssl_str.lower() == "true"
else:
influxdb2_ssl = False
influxdb2_ssl_verify_str=os.getenv('INFLUXDB2_SSL_VERIFY', "True")
if influxdb2_ssl_verify_str is not None:
influxdb2_ssl_verify = influxdb2_ssl_verify_str.lower() == "true"
else:
influxdb2_ssl_verify = False
# hard encoded environment variables
if os.path.exists('private-api.py'):
print("included: private-api.py")
exec(compile(source=open('private-api.py').read(), filename='private-api.py', mode='exec'))
withings_auth_code=""
debug = True
showRaw = False
# report debug status
date=datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f%z')
print ( "date:",date )
if debug:
print ( " debug: TRUE" )
if influxdb2_ssl:
print ( " SSL: TRUE" )
else:
print ( " SSL: FALSE" )
if influxdb2_ssl_verify:
print ( "verify: TRUE" )
else:
print ( "verify: FALSE" )
else:
print ( " debug: FALSE" )
# setup withings API
tokenPath = path.abspath(path.join(path.dirname(path.abspath(__file__)), "./oauth"))
os.makedirs(tokenPath, exist_ok=True)
tokenFile = tokenPath+"/token"
def save_credentials(credentials: CredentialsType) -> None:
print("saving token to:", tokenFile)
with open(tokenFile, "wb") as file_handle:
pickle.dump(credentials, file_handle)
def load_credentials() -> CredentialsType:
print("reading token from:", tokenFile)
with open(tokenFile, "rb") as file_handle:
return cast(CredentialsType, pickle.load(file_handle))
auth = WithingsAuth(
client_id=withings_clientId,
consumer_secret=withings_clientSecret,
callback_uri=withings_callback,
scope=(
AuthScope.USER_ACTIVITY,
AuthScope.USER_METRICS,
)
)
if withings_auth_code == "INIT":
authorise_url = auth.get_authorize_url()
print("Goto this URL to authorise:\n\n",authorise_url)
quit()
else:
if withings_auth_code != "":
print("Getting oauth token with auth code:", withings_auth_code)
save_credentials(auth.get_credentials(withings_auth_code))
withings_auth_code=""
read_api = WithingsApi(load_credentials(), refresh_cb=save_credentials)
print("Refreshing token...")
old_token = read_api.get_credentials().access_token
read_api.refresh_token()
if old_token != read_api.get_credentials().access_token:
print("NO Refresh: old and new token are the same!")
else:
print("Refresh: old and new are different")
# setup influxDBv2
if influxdb2_ssl:
influxdb2_url="https://" + influxdb2_host + ":" + str(influxdb2_port)
else:
influxdb2_url="http://" + influxdb2_host + ":" + str(influxdb2_port)
if debug:
print ( "influxdb: "+influxdb2_url+" bucket: "+influxdb2_bucket )
if influxdb2_ssl_verify:
if debug:
print ( "verify: True" )
client = InfluxDBClient(url=influxdb2_url, token=influxdb2_token, org=influxdb2_org, verify_ssl=True)
else:
if debug:
print ( "verify: False" )
client = InfluxDBClient(url=influxdb2_url, token=influxdb2_token, org=influxdb2_org, verify_ssl=False)
write_api = client.write_api(write_options=SYNCHRONOUS)
def write_influxdb():
if showRaw:
print ("INFLUX: "+influxdb2_bucket)
print (json.dumps(senddata,indent=4))
write_api.write(bucket=influxdb2_bucket, org=influxdb2_org, record=[senddata])
# setup time ranges
hour=datetime.now().strftime('%H')
now=datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f%z')
ago=(datetime.utcnow()+timedelta(days=-2)).strftime('%Y-%m-%dT%H:%M:%S.%f%z')
start=(datetime.utcnow()+timedelta(days=-15*365)).strftime('%Y-%m-%dT%H:%M:%S.%f%z')
print ( " current hour: ",hour )
print ( "sleep at hour: ",withings_sleep_at )
# get height
print("Getting height...")
heights = read_api.measure_get_meas(
category=MeasureGetMeasGroupCategory.REAL,
startdate=start,
enddate=now,
lastupdate=None,
)
height=0
for measurement in heights.measuregrps:
for measure in measurement.measures:
if measure.type == MeasureType.HEIGHT:
height = round(measure.value * 10 ** measure.unit, 2)
if debug:
print("RAW:\n HEIGHT ",height)
# get weight/bp/temp
print("Getting weight/bp/temp...")
measurements = read_api.measure_get_meas(
category=MeasureGetMeasGroupCategory.REAL,
startdate=ago,
enddate=now,
lastupdate=None,
)
# pass weight/bp/temp
for measurement in measurements.measuregrps:
weight=0
fat=0
sys=0
body=0
time = measurement.date.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
if debug:
print("RAW:\n time",time)
for measure in measurement.measures:
value = round(measure.value * 10 ** measure.unit, 2)
if measure.type == MeasureType.WEIGHT: weight = value
if measure.type == MeasureType.FAT_RATIO: fat = value
if measure.type == MeasureType.FAT_MASS_WEIGHT: weightFat = value
if measure.type == MeasureType.FAT_FREE_MASS: weightLean = value
if measure.type == MeasureType.HEART_RATE: hr = value
if measure.type == MeasureType.DIASTOLIC_BLOOD_PRESSURE: dia = value
if measure.type == MeasureType.SYSTOLIC_BLOOD_PRESSURE: sys = value
if measure.type == MeasureType.BODY_TEMPERATURE: body = value
if measure.type == MeasureType.SKIN_TEMPERATURE: skin = value
if debug:
print(" ",measure.type.name, value)
senddata={}
senddata["measurement"]="weight"
senddata["time"]=time
senddata["tags"]={}
senddata["tags"]["source"]="docker withings-influxdbv2"
senddata["tags"]["origin"]="Withings"
senddata["fields"]={}
if weight !=0:
senddata["tags"]["type"]="total"
senddata["fields"]["kg"]=round(float(weight),1)
if height > 0:
senddata["fields"]["bmi"]=round(weight/(height*height),1)
write_influxdb()
del senddata["fields"]["bmi"]
senddata["tags"]["type"]="overweight"
senddata["fields"]["kg"]=round(weight-(25*height*height),1)
write_influxdb()
else:
write_influxdb()
del senddata["fields"]["kg"]
del senddata["tags"]["type"]
if fat !=0:
senddata["tags"]["type"]="fat"
senddata["fields"]["kg"]=float(weightFat)
senddata["fields"]["percent"]=round(float(fat),1)
write_influxdb()
senddata["tags"]["type"]="lean"
senddata["fields"]["kg"]=round(float(weightLean),1)
senddata["fields"]["percent"]=round(float(100-fat),1)
write_influxdb()
del senddata["fields"]["kg"]
del senddata["fields"]["percent"]
del senddata["tags"]["type"]
if sys !=0:
senddata["measurement"]="heart"
senddata["tags"]["type"]="systolic"
senddata["tags"]["mode"]="avg"
senddata["fields"]["bp"]=round(float(sys),2)
write_influxdb()
senddata["tags"]["type"]="diastolic"
senddata["fields"]["bp"]=round(float(dia),2)
write_influxdb()
del senddata["fields"]["bp"]
senddata["tags"]["type"]="resting"
senddata["fields"]["bpm"]=round(float(hr),2)
write_influxdb()
del senddata["fields"]["bpm"]
del senddata["tags"]["type"]
del senddata["tags"]["mode"]
if body !=0:
senddata["measurement"]="temperature"
senddata["tags"]["sensor"]="Body"
senddata["fields"]["temp"]=round(float(body),2)
write_influxdb()
senddata["tags"]["sensor"]="Skin"
senddata["fields"]["temp"]=round(float(skin),2)
write_influxdb()
# get sleep summary and raw sleep
# note need modified api to access all fields
# only run at specific hour given by withings_sleep_at
if hour == withings_sleep_at:
print("Getting sleep data [",hour," == ",withings_sleep_at,"]")
print("Getting sleep summary...")
sleepSummary = read_api.sleep_get_summary(
data_fields=GetSleepSummaryField,
startdateymd=ago,
enddateymd=now,
lastupdate=None,
)
# pass sleep summary
for serie in sleepSummary.series:
hrAvg=0
time = (serie.startdate+(serie.enddate-serie.startdate)/2).strftime("%Y-%m-%dT%H:%M:%S.%f%z");
if debug:
print("RAW:")
print(" start ",serie.startdate)
print(" time ",time)
print(" end ",serie.enddate)
for data in serie.data:
if debug:
print(" ",data[0]," = ",data[1])
if data[0] == "hr_average": hrAvg = data[1]
if data[0] == "hr_max": hrMax = data[1]
if data[0] == "hr_min": hrMin = data[1]
if data[0] == "rr_average": rrAvg = data[1]
if data[0] == "rr_max": rrMax = data[1]
if data[0] == "rr_min": rrMin = data[1]
if data[0] == "deepsleepduration": dDeep = data[1]
if data[0] == "lightsleepduration": dLite = data[1]
if data[0] == "remsleepduration": dREM = data[1]
if data[0] == "wakeupduration": dAwake = data[1]
if data[0] == "snoring": dSnoring = data[1]
if data[0] == "waso": dWokeup = data[1]
if data[0] == "total_sleep_time": dSleep = data[1]
if data[0] == "total_timeinbed": dInBed = data[1]
if data[0] == "sleep_latency": lSleep = data[1]
if data[0] == "wakeup_latency": lWakeup = data[1]
if data[0] == "wakeupcount": nWakeup = data[1]
if data[0] == "snoringepisodecount": nSnore = data[1]
if data[0] == "nb_rem_episodes": nREM = data[1]
if data[0] == "out_of_bed_count": nOutBed = data[1]
if data[0] == "sleep_score": score = data[1]
if data[0] == "sleep_efficiency": efficiency = data[1]
if data[0] == "breathing_disturbances_intensity": disturbed = data[1]
if data[0] == "apnea_hypopnea_index": ahi = data[1]
## Depreciated
#if data[0] == "durationtowakeup": dToWake = data[1]
#if data[0] == "durationtosleep": dToSleep = data[1]
senddata={}
senddata["time"]=time
senddata["tags"]={}
senddata["tags"]["source"]="docker withings-influxdbv2"
senddata["tags"]["origin"]="Withings"
senddata["fields"]={}
if rrAvg !=0:
senddata["measurement"]="respiration"
senddata["tags"]["type"]="sleeping"
senddata["fields"]["ahi"]=round(float(ahi),2)
write_influxdb()
del senddata["fields"]["ahi"]
senddata["fields"]["disturbed"]=round(float(disturbed),2)
write_influxdb()
del senddata["fields"]["disturbed"]
senddata["tags"]["mode"]="avg"
senddata["fields"]["bpm"]=round(float(rrAvg),2)
write_influxdb()
senddata["tags"]["mode"]="max"
senddata["fields"]["bpm"]=round(float(rrMax),2)
write_influxdb()
senddata["tags"]["mode"]="min"
senddata["fields"]["bpm"]=round(float(rrMin),2)
write_influxdb()
del senddata["fields"]["bpm"]
del senddata["tags"]["mode"]
if hrAvg !=0:
senddata["measurement"]="heart"
senddata["tags"]["type"]="sleeping"
senddata["tags"]["mode"]="avg"
senddata["fields"]["bpm"]=round(float(hrAvg),2)
write_influxdb()
senddata["tags"]["mode"]="max"
senddata["fields"]["bpm"]=round(float(hrMax),2)
write_influxdb()
senddata["tags"]["mode"]="min"
senddata["fields"]["bpm"]=round(float(hrMin),2)
write_influxdb()
del senddata["fields"]["bpm"]
del senddata["tags"]["mode"]
if dDeep !=0:
senddata["measurement"]="sleep"
senddata["tags"]["type"]="deep"
senddata["tags"]["mode"]="total"
senddata["fields"]["duration"]=round(dDeep/3600,2)
write_influxdb()
senddata["tags"]["type"]="light"
senddata["fields"]["duration"]=round(dLite/3600,2)
write_influxdb()
senddata["tags"]["type"]="rem"
senddata["fields"]["duration"]=round(dREM/3600,1)
write_influxdb()
#senddata["tags"]["type"]="wakeup"
#senddata["fields"]["duration"]=round(dToWake/3600,2)
#write_influxdb()
#senddata["tags"]["type"]="tosleep"
#senddata["fields"]["duration"]=round(dToSleep/3600,2)
#write_influxdb()
senddata["tags"]["type"]="snoring"
senddata["fields"]["duration"]=round(dSnoring/3600,2)
write_influxdb()
senddata["tags"]["type"]="woke"
senddata["fields"]["duration"]=round(dWokeup/3600,2)
write_influxdb()
senddata["tags"]["type"]="sleep"
senddata["fields"]["duration"]=round(dSleep/3600,2)
write_influxdb()
senddata["tags"]["type"]="inbed"
senddata["fields"]["duration"]=round(dInBed/3600,2)
write_influxdb()
senddata["tags"]["type"]="awake"
senddata["fields"]["duration"]=round(dAwake/3600,2)
write_influxdb()
del senddata["fields"]["duration"]
del senddata["tags"]["mode"]
senddata["tags"]["type"]="sleep"
senddata["fields"]["latency"]=round(lSleep/3600,2)
write_influxdb()
senddata["tags"]["type"]="wakeup"
senddata["fields"]["latency"]=round(lWakeup/3600,2)
write_influxdb()
del senddata["fields"]["latency"]
senddata["tags"]["type"]="snoring"
senddata["fields"]["count"]=int(nSnore)
write_influxdb()
senddata["tags"]["type"]="rem"
senddata["fields"]["count"]=int(nREM)
write_influxdb()
senddata["tags"]["type"]="wakeup"
senddata["fields"]["count"]=int(nWakeup)
write_influxdb()
senddata["tags"]["type"]="outofbed"
senddata["fields"]["count"]=int(nOutBed)
write_influxdb()
del senddata["fields"]["count"]
senddata["tags"]["type"]="score"
senddata["fields"]["percent"]=round(float(score),2)
write_influxdb()
senddata["tags"]["type"]="efficiency"
senddata["fields"]["percent"]=round(float(efficiency),2)
write_influxdb()
del senddata["fields"]["percent"]
# get sleep
# note need modified api to access all fields
print("Getting sleep raw...")
sleepRaw = read_api.sleep_get(
data_fields=GetSleepField,
startdate=ago,
enddate=now,
)
# pass sleep
senddata={}
senddata["tags"]={}
senddata["tags"]["source"]="docker withings-influxdbv2"
senddata["tags"]["origin"]="Withings"
senddata["fields"]={}
for serie in sleepRaw.series:
hrAvg=0
senddata["measurement"]="heart"
senddata["tags"]["type"]="sleeping"
for record in serie.hr:
if debug:
print(" ",record.timestamp," HR = ",record.value)
time = record.timestamp.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
senddata["time"]=time
senddata["tags"]["mode"]="raw"
senddata["fields"]["bpm"]=round(float(record.value),2)
write_influxdb()
for record in serie.sdnn_1:
if debug:
print(" ",record.timestamp," SD = ",record.value)
time = record.timestamp.strftime("%Y-%m-%dT%H:%M:%S.%f.%z")
senddata["time"]=time
senddata["tags"]["mode"]="sdev"
senddata["fields"]["bpm"]=round(float(record.value),2)
write_influxdb()
for record in serie.rmssd:
if debug:
print(" ",record.timestamp,"RMS = ",record.value)
time = record.timestamp.strftime("%Y-%m-%dT%H:%M:%S.%f.%z")
senddata["time"]=time
senddata["tags"]["mode"]="rms"
senddata["fields"]["bpm"]=round(float(record.value),2)
write_influxdb()
senddata["measurement"]="respiration"
senddata["tags"]["mode"]="raw"
for record in serie.rr:
if debug:
print(" ",record.timestamp," RR = ",record.value)
time = record.timestamp.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
senddata["time"]=time
senddata["fields"]["bpm"]=round(float(record.value),2)
write_influxdb()
del senddata["fields"]["bpm"]
senddata["measurement"]="sleep"
senddata["tags"]["type"]="snoring"
senddata["tags"]["mode"]="raw"
for record in serie.snoring:
if debug:
print(" ",record.timestamp," SN = ",record.value)
time = record.timestamp.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
senddata["time"]=time
senddata["fields"]["duration"]=round(record.value/3600,2)
write_influxdb()
del senddata["fields"]["duration"]
else:
print("Skipping sleep summary [",hour," != ",withings_sleep_at,"]")
# end
date=datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f%z')
print ( "date:",date )
print("Done")
exit(0)