-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.py
789 lines (731 loc) · 31.5 KB
/
sensor.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
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
import os, sys, time
import logging
import atexit, requests, json, datetime, base64
from newrelic_telemetry_sdk import (
MetricBatch,
MetricClient,
Harvester,
Event,
EventClient,
)
_LOGGER = logging.getLogger()
_LOGGER.setLevel(logging.INFO)
handler = logging.FileHandler("owlet.log")
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
_LOGGER.addHandler(handler)
metric_client = MetricClient(os.environ["NEW_RELIC_INSERT_KEY"])
event_client = EventClient(os.environ["NEW_RELIC_INSERT_KEY"])
metric_batch = MetricBatch()
metric_harvester = Harvester(metric_client, metric_batch)
metric_harvester.harvest_interval = 5
class OwletAPI:
def __init__(self):
"""Initialize API Token."""
self.__client = requests.session()
self.__JWT = ""
self.__MINI_TOKEN = ""
self.__OWLET_USER = os.environ["OWLET_USER"]
self.__OWLET_PASS = os.environ["OWLET_PASS"]
self.__OWLET_REGION = os.environ["OWLET_REGION"]
self.__OWLET_TOKEN = None
self.__OWLET_REGION_CONFIG = {
"world": {
"url_mini": "https://ayla-sso.owletdata.com/mini/",
"url_signin": "https://user-field-1a2039d9.aylanetworks.com/api/v1/token_sign_in",
"url_base": "https://ads-field-1a2039d9.aylanetworks.com/apiv1",
"apiKey": "AIzaSyCsDZ8kWxQuLJAMVnmEhEkayH1TSxKXfGA",
"app_id": "sso-prod-3g-id",
"app_secret": "sso-prod-UEjtnPCtFfjdwIwxqnC0OipxRFU",
},
"europe": {
"url_mini": "https://ayla-sso.eu.owletdata.com/mini/",
"url_signin": "https://user-field-eu-1a2039d9.aylanetworks.com/api/v1/token_sign_in",
"url_base": "https://ads-field-eu-1a2039d9.aylanetworks.com/apiv1",
"apiKey": "AIzaSyDm6EhV70wudwN3iOSq3vTjtsdGjdFLuuM",
"app_id": "OwletCare-Android-EU-fw-id",
"app_secret": "OwletCare-Android-EU-JKupMPBoj_Npce_9a95Pc8Qo0Mw",
},
}
self.__API_KEY = self.__OWLET_REGION_CONFIG[self.__OWLET_REGION]["apiKey"]
self.__API_URL = self.__OWLET_REGION_CONFIG[self.__OWLET_REGION]["url_base"]
self.__OWLET_TOKEN_EXPIRE_TIME = None
self.__OWLET_TOKEN_REAUTH_ATTEMPTS = 0
self.__OWLET_TOKEN_REAUTH_MAX_ATTEMPTS = 10
self.__OWLET_TOKEN = ""
self.__devices = None
self.__token = None
def authenticate(self, reauthenticate=False, check=False):
# authenticate against Firebase, get the JWT.
# need to pass the X-Android-Package and X-Android-Cert headers because
# the API key is restricted to the Owlet Android app
# https://cloud.google.com/docs/authentication/api-keys#api_key_restrictions
if reauthenticate:
if (
self.__OWLET_TOKEN_REAUTH_ATTEMPTS
<= self.__OWLET_TOKEN_REAUTH_MAX_ATTEMPTS
):
self.__OWLET_TOKEN_REAUTH_ATTEMPTS = (
self.__OWLET_TOKEN_REAUTH_ATTEMPTS + 1
)
_LOGGER.info(
"Reauthticate Attempt: %s" % self.__OWLET_TOKEN_REAUTH_ATTEMPTS
)
else:
return self.__OWLET_TOKEN, None
if check:
_LOGGER.info(time.time() + 1800)
if time.time() + 1800 >= self.__OWLET_TOKEN_EXPIRE_TIME:
self.__OWLET_TOKEN_REAUTH_ATTEMPTS = 1
else:
return self.__OWLET_TOKEN, None
_LOGGER.info("Logging into Owlet API via Firebase as %s" % self.__OWLET_USER)
try:
r = requests.post(
f"https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key={self.__API_KEY}",
data=json.dumps(
{
"email": self.__OWLET_USER,
"password": self.__OWLET_PASS,
"returnSecureToken": True,
}
),
headers={
"X-Android-Package": "com.owletcare.owletcare",
"X-Android-Cert": "2A3BC26DB0B8B0792DBE28E6FFDC2598F9B12B74",
},
)
r.raise_for_status()
except requests.exceptions.RequestException as e:
_LOGGER.error("Owlet API Could not Authenticate against Firebase: %s" % e)
return False, e
self.__JWT = r.json()["idToken"]
# authenticate against owletdata.com, get the mini_token
try:
r = requests.get(
self.__OWLET_REGION_CONFIG[self.__OWLET_REGION]["url_mini"],
headers={"Authorization": self.__JWT},
)
r.raise_for_status()
except requests.exceptions.RequestException as e:
_LOGGER.error(
"Owlet API Could not Authenticate against owletdata.com: %s" % e
)
return False, e
self.__MINI_TOKEN = r.json()["mini_token"]
# authenticate against Ayla, get the access_token
try:
r = requests.post(
self.__OWLET_REGION_CONFIG[self.__OWLET_REGION]["url_signin"],
json={
"app_id": self.__OWLET_REGION_CONFIG[self.__OWLET_REGION]["app_id"],
"app_secret": self.__OWLET_REGION_CONFIG[self.__OWLET_REGION][
"app_secret"
],
"provider": "owl_id",
"token": self.__MINI_TOKEN,
},
)
r.raise_for_status()
except requests.exceptions.RequestException as e:
_LOGGER.error("Owlet API Could not Authenticate against Firebase: %s" % e)
return False, e
self.__OWLET_TOKEN = r.json()["access_token"]
# we will re-auth 60 seconds before the token expires
self.__OWLET_TOKEN_EXPIRE_TIME = time.time() + r.json()["expires_in"] - 60
_LOGGER.info("Owlet API Token for %s acquired" % self.__OWLET_USER)
_LOGGER.info("Owlet API Token acquired: %s" % self.__OWLET_TOKEN)
self.__OWLET_TOKEN_REAUTH_ATTEMPTS = 1
return self.__OWLET_TOKEN, None
def api_get(self, route):
return self.__client.get(
self.__API_URL + route,
headers={"Authorization": "auth_token " + self.__OWLET_TOKEN},
)
def api_post(self, route, json_body):
return self.__client.post(
self.__API_URL + route,
json=json_body,
headers={"Authorization": "auth_token " + self.__OWLET_TOKEN},
)
@property
def token(self):
if (
(self.__OWLET_TOKEN is None)
or (self.__OWLET_TOKEN_EXPIRE_TIME is None)
or (time.time() >= self.__OWLET_TOKEN_EXPIRE_TIME)
):
self.__OWLET_TOKEN = self.authenticate()
return self.__OWLET_TOKEN
def get_devices(self):
try:
_LOGGER.info("Getting list of Owlet Devices")
r = self.api_get("/devices.json")
r.raise_for_status()
except requests.exceptions.RequestException as e:
_LOGGER.error("Owlet Network error while retrieving devices: %s" % e)
time.sleep(5)
self.__client = requests.session()
return []
self.__devices = r.json()
self.__device_serial_numbers = []
if len(self.__devices) < 1:
_LOGGER.info("No Owlet Smart Sock Found")
# Allow for multiple devices
else:
for device in self.__devices:
if device["device"]["dsn"] not in self.__device_serial_numbers:
self.__device_serial_numbers.append(device["device"]["dsn"])
_LOGGER.info(
"Found Owlet Smart Sock with Serial Number %s"
% device["device"]["dsn"]
)
return self.__device_serial_numbers
@property
def devices(self):
if self.__devices is None or self.__device_serial_numbers is None:
self.get_devices()
return self.__device_serial_numbers
def activate_sock(self, dsn):
try:
r = self.api_post(
"/dsns/%s/properties/APP_ACTIVE/datapoints.json" % dsn,
{"datapoint": {"metadata": {}, "value": 1}},
)
r.raise_for_status()
return True
except requests.exceptions.RequestException as e:
_LOGGER.error("Owlet Network error while activating Sock %s: %s" % (dsn, e))
time.sleep(5)
self.__client = requests.session()
return False
def sock_properties(self, dsn):
if self.activate_sock(dsn):
device_props = {"DSN": dsn}
try:
r = self.api_get("/dsns/%s/properties.json" % dsn)
timestamp = datetime.date.today().isoformat()
r.raise_for_status()
except requests.exceptions.RequestException as e:
_LOGGER.error(
"Owlet Network error while retrieving data for Sock %s: %s"
% (dsn, e)
)
time.sleep(5)
self.__client = requests.session()
return False, False
response_body = r.json()
for prop in response_body:
device_props[prop["property"]["name"]] = prop["property"]
return device_props, timestamp
else:
return False, False
def vitals(self, dsn):
p, timestamp = self.sock_properties(dsn)
if p is not False:
if "REAL_TIME_VITALS" in p:
# Sock is a Smart Sock 3
rt_vitals = json.loads(p["REAL_TIME_VITALS"]["value"])
_LOGGER.info(rt_vitals)
return {
"dsn": p["DSN"],
"charge_status": rt_vitals["chg"],
"heart_rate": "%d" % rt_vitals["hr"],
"base_station_on": rt_vitals["bso"],
"oxygen_saturation": "%d" % rt_vitals["ox"],
"movement": "%d" % rt_vitals["mv"],
"battery": "%d" % rt_vitals["bat"],
"ble_rssi": "%d" % rt_vitals["rsi"],
"LOW_INTEG_READ": bool(p["LOW_INTEG_READ"]["value"]),
"LOW_BATT_ALRT": bool(p["LOW_BATT_ALRT"]["value"]),
"HIGH_HR_ALRT": bool(p["HIGH_HR_ALRT"]["value"]),
"LOW_HR_ALRT": bool(p["LOW_HR_ALRT"]["value"]),
"LOW_OX_ALRT": bool(p["LOW_OX_ALRT"]["value"]),
"SOCK_DISCON_ALRT": bool(p["SOCK_DISCON_ALRT"]["value"]),
"RED_ALERT_SUMMARY": "%s" % p["RED_ALERT_SUMMARY"]["value"],
"error": False,
}
elif "CHARGE_STATUS" in p:
# Sock is a Smart Sock 2
return {
"dsn": p["DSN"],
"charge_status": p["CHARGE_STATUS"]["value"],
"heart_rate": "%d" % p["HEART_RATE"]["value"],
"base_station_on": p["BASE_STATION_ON"]["value"],
"oxygen_saturation": "%d" % p["OXYGEN_LEVEL"]["value"],
"movement": "%d" % p["MOVEMENT"]["value"],
"battery": "%d" % p["BATT_LEVEL"]["value"],
"ble_rssi": "%d" % p["BLE_RSSI"]["value"],
"LOW_INTEG_READ": bool(p["LOW_INTEG_READ"]["value"]),
"LOW_BATT_ALRT": bool(p["LOW_BATT_ALRT"]["value"]),
"HIGH_HR_ALRT": bool(p["HIGH_HR_ALRT"]["value"]),
"LOW_HR_ALRT": bool(p["LOW_HR_ALRT"]["value"]),
"LOW_OX_ALRT": bool(p["LOW_OX_ALRT"]["value"]),
"SOCK_DISCON_ALRT": bool(p["SOCK_DISCON_ALRT"]["value"]),
"RED_ALERT_SUMMARY": "",
"error": False,
}
return {"dsn": dsn, "error": True}
def version(self, dsn):
p, timestamp = self.sock_properties(dsn)
if p is not False:
if "oem_sock_version" in p:
oem_sock_version = json.loads(p["oem_sock_version"]["value"])
_LOGGER.info(oem_sock_version)
return {
"dsn": p["DSN"],
"app": "%s" % oem_sock_version["app"],
"bl": "%s" % oem_sock_version["bl"],
"rev": "%s" % oem_sock_version["rev"],
"error": False,
}
return {"dsn": dsn, "error": True}
def Initialize():
"""Set up the sensor platform."""
Owlet = OwletAPI()
if not Owlet.authenticate():
_LOGGER.error("Unable to get Owlet API Token")
return
SockDSN = []
SockOBJ = []
for SmartSockDSN in Owlet.devices:
_LOGGER.info("Adding Owlet Smart Sock %s" % SmartSockDSN)
OwletSmartSock(SmartSockDSN, Owlet).version()
SockDSN.append(SmartSockDSN)
for DSN in SockDSN:
SockOBJ.append(OwletSmartSock(DSN, Owlet))
while True:
try:
for sock in SockOBJ:
Owlet.authenticate(False, True)
sock.update()
time.sleep(10)
except:
_LOGGER.error("Loop broken")
raise
class OwletSmartSock:
"""An Owlet Smart Sock Sensor."""
def __init__(
self,
DSN,
Owlet,
):
"""Initialize the Smart Sock Sensor."""
self.__DSN = DSN
self.__Owlet = Owlet
self.__state = None
self.__attributes = None
self._setup_device_attributes()
self._setup_state()
self._setup_attributes()
@property
def name(self):
"""Return the name of the sensor."""
return "Owlet Smart Sock %s" % self.__DSN
@property
def state(self):
"""Return the Socks Currently Measured Vitals."""
return self.__state
@property
def device_state_attributes(self):
"""Return device specific state attributes."""
return self.__attributes
def _setup_device_attributes(self):
self.__attributes = {
"dsn": self.__DSN,
"app": "%d" % 0,
"bl": "%d" % 0,
"rev": "%d" % 0,
}
def _setup_state(self):
self.__state = "Connnected"
def _setup_attributes(self):
self.__attributes = {
"dsn": self.__DSN,
"charge_status": "%d" % 0,
"heart_rate": "%d" % 0,
"base_station_on": 0,
"oxygen_saturation": "%d" % 0,
"movement": "%d" % 0,
"battery": "%d" % 0,
"ble_rssi": "%d" % 0,
"active": False,
"LOW_INTEG_READ": False,
"LOW_BATT_ALRT": False,
"HIGH_HR_ALRT": False,
"LOW_HR_ALRT": False,
"LOW_OX_ALRT": False,
"SOCK_DISCON_ALRT": False,
"RED_ALERT_SUMMARY": "",
}
def version(self):
"""Fetch hw versions from the Owlet API"""
if self.__state == "Disconnected":
self.__Owlet.authenticate(True)
info = self.__Owlet.version(self.__DSN)
if info["error"] != False:
self.__state = "Disconnected"
self.__attributes["app"] = None
self.__attributes["bl"] = None
self.__attributes["rev"] = None
else:
self.__state = "Connected"
self.__attributes["app"] = info["app"]
self.__attributes["bl"] = info["bl"]
self.__attributes["rev"] = info["rev"]
event = Event(
"SOCK_VERSION",
{
"description": "OEM Sock Version",
"app": self.__attributes["app"],
"bl": self.__attributes["bl"],
"rev": self.__attributes["rev"],
"dsn": self.__attributes["dsn"],
"active": True,
},
)
event_client.send(event)
def update(self):
"""Fetch latest vital signs from the Owlet API"""
if self.__state == "Disconnected":
self.__Owlet.authenticate(True)
state = self.__Owlet.vitals(self.__DSN)
if state["error"] != False:
self.__state = "Disconnected"
self.__attributes["dsn"] = None
self.__attributes["charge_status"] = None
self.__attributes["heart_rate"] = None
self.__attributes["base_station_on"] = None
self.__attributes["oxygen_saturation"] = None
self.__attributes["movement"] = None
self.__attributes["battery"] = None
self.__attributes["ble_rssi"] = None
self.__attributes["LOW_INTEG_READ"] = True
self.__attributes["LOW_BATT_ALRT"] = False
self.__attributes["HIGH_HR_ALRT"] = False
self.__attributes["LOW_HR_ALRT"] = False
self.__attributes["LOW_OX_ALRT"] = False
self.__attributes["SOCK_DISCON_ALRT"] = True
self.__attributes["RED_ALERT_SUMMARY"] = ""
self.__attributes["active"] = False
else:
if "heart_rate" in state and int(state["heart_rate"]) > 0:
self.__state = "Connected"
self.__attributes["dsn"] = state["dsn"]
self.__attributes["charge_status"] = state["charge_status"]
self.__attributes["heart_rate"] = state["heart_rate"]
self.__attributes["base_station_on"] = state["base_station_on"]
self.__attributes["oxygen_saturation"] = state["oxygen_saturation"]
self.__attributes["movement"] = state["movement"]
self.__attributes["battery"] = state["battery"]
self.__attributes["ble_rssi"] = state["ble_rssi"]
self.__attributes["LOW_INTEG_READ"] = state["LOW_INTEG_READ"]
self.__attributes["LOW_BATT_ALRT"] = state["LOW_BATT_ALRT"]
self.__attributes["HIGH_HR_ALRT"] = state["HIGH_HR_ALRT"]
self.__attributes["LOW_HR_ALRT"] = state["LOW_HR_ALRT"]
self.__attributes["LOW_OX_ALRT"] = state["LOW_OX_ALRT"]
self.__attributes["SOCK_DISCON_ALRT"] = state["SOCK_DISCON_ALRT"]
self.__attributes["RED_ALERT_SUMMARY"] = state["RED_ALERT_SUMMARY"]
self.__attributes["active"] = (
bool(state["base_station_on"])
and not bool(state["SOCK_DISCON_ALRT"])
and not bool(state["charge_status"])
and not bool(state["LOW_INTEG_READ"])
)
for key in self.__attributes:
_LOGGER.info("key: %s , value: %s" % (key, self.__attributes[key]))
metric_batch.record_gauge(
"charge_status",
int(self.__attributes["charge_status"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
metric_batch.record_gauge(
"heart_rate",
int(self.__attributes["heart_rate"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
metric_batch.record_gauge(
"base_station_on",
int(self.__attributes["base_station_on"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
metric_batch.record_gauge(
"oxygen_saturation",
int(self.__attributes["oxygen_saturation"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
metric_batch.record_gauge(
"movement",
int(self.__attributes["movement"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
metric_batch.record_gauge(
"battery",
int(self.__attributes["battery"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
metric_batch.record_gauge(
"ble_rssi",
int(self.__attributes["ble_rssi"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
if state["LOW_INTEG_READ"]:
event = Event(
"LOW_INTEG_READ",
{
"description": "Low Integrity Read",
"value": self.__attributes["LOW_INTEG_READ"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if state["LOW_BATT_ALRT"]:
event = Event(
"LOW_BATT_ALRT",
{
"description": "Low Battery Alert",
"value": self.__attributes["LOW_BATT_ALRT"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if state["HIGH_HR_ALRT"]:
event = Event(
"HIGH_HR_ALRT",
{
"description": "High HR Alert",
"value": self.__attributes["HIGH_HR_ALRT"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if state["LOW_HR_ALRT"]:
event = Event(
"LOW_HR_ALRT",
{
"description": "Low HR Alert",
"value": self.__attributes["LOW_HR_ALRT"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if state["LOW_OX_ALRT"]:
event = Event(
"LOW_OX_ALRT",
{
"description": "Low Oxygen Alert",
"value": self.__attributes["LOW_OX_ALRT"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if state["SOCK_DISCON_ALRT"]:
event = Event(
"SOCK_DISCON_ALRT",
{
"description": "Sock Disconnect Alert",
"value": self.__attributes["SOCK_DISCON_ALRT"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if state["RED_ALERT_SUMMARY"] is None:
event = Event(
"RED_ALERT_SUMMARY",
{
"description": "Red Alert Summary",
"value": self.__attributes["RED_ALERT_SUMMARY"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
else:
self.__state = "Connected"
self.__attributes["dsn"] = state["dsn"]
self.__attributes["charge_status"] = state["charge_status"]
self.__attributes["heart_rate"] = state["heart_rate"]
self.__attributes["base_station_on"] = state["base_station_on"]
self.__attributes["oxygen_saturation"] = state["oxygen_saturation"]
self.__attributes["movement"] = state["movement"]
self.__attributes["battery"] = state["battery"]
self.__attributes["ble_rssi"] = state["ble_rssi"]
self.__attributes["LOW_INTEG_READ"] = state["LOW_INTEG_READ"]
self.__attributes["LOW_BATT_ALRT"] = state["LOW_BATT_ALRT"]
self.__attributes["HIGH_HR_ALRT"] = state["HIGH_HR_ALRT"]
self.__attributes["LOW_HR_ALRT"] = state["LOW_HR_ALRT"]
self.__attributes["LOW_OX_ALRT"] = state["LOW_OX_ALRT"]
self.__attributes["SOCK_DISCON_ALRT"] = state["SOCK_DISCON_ALRT"]
self.__attributes["RED_ALERT_SUMMARY"] = ""
self.__attributes["active"] = False
for key in self.__attributes:
_LOGGER.info("key: %s , value: %s" % (key, self.__attributes[key]))
metric_batch.record_gauge(
"charge_status",
int(self.__attributes["charge_status"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": False,
},
)
metric_batch.record_gauge(
"heart_rate",
int(self.__attributes["heart_rate"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": False,
},
)
metric_batch.record_gauge(
"base_station_on",
int(self.__attributes["base_station_on"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
metric_batch.record_gauge(
"oxygen_saturation",
int(self.__attributes["oxygen_saturation"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
metric_batch.record_gauge(
"movement",
int(self.__attributes["movement"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
metric_batch.record_gauge(
"battery",
int(self.__attributes["battery"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
metric_batch.record_gauge(
"ble_rssi",
int(self.__attributes["ble_rssi"]),
{
"state": "Connected",
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
if state["LOW_INTEG_READ"]:
event = Event(
"LOW_INTEG_READ",
{
"description": "Low Integrity Read",
"value": self.__attributes["LOW_INTEG_READ"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if state["LOW_BATT_ALRT"]:
event = Event(
"LOW_BATT_ALRT",
{
"description": "Low Battery Alert",
"value": self.__attributes["LOW_BATT_ALRT"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if state["HIGH_HR_ALRT"]:
event = Event(
"HIGH_HR_ALRT",
{
"description": "High HR Alert",
"value": self.__attributes["HIGH_HR_ALRT"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if state["LOW_HR_ALRT"]:
event = Event(
"LOW_HR_ALRT",
{
"description": "Low HR Alert",
"value": self.__attributes["LOW_HR_ALRT"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if state["LOW_OX_ALRT"]:
event = Event(
"LOW_OX_ALRT",
{
"description": "Low Oxygen Alert",
"value": self.__attributes["LOW_OX_ALRT"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if state["SOCK_DISCON_ALRT"]:
event = Event(
"SOCK_DISCON_ALRT",
{
"description": "Sock Disconnect Alert",
"value": self.__attributes["SOCK_DISCON_ALRT"],
"dsn": self.__attributes["dsn"],
"active": self.__attributes["active"],
},
)
event_client.send(event)
if __name__ == "__main__":
"""Set up the sensor platform."""
metric_harvester.start()
Initialize()
atexit.register(metric_harvester.stop)