-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.py
42 lines (35 loc) · 1.14 KB
/
main.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
from inkbird.client import InkBirdClient
import bluepy
import time
import os
import logging
logger = logging.getLogger("inkbird")
logger.setLevel(logging.INFO)
logging.basicConfig(
level=logging.INFO, format="%(asctime)s %(name)-15s %(levelname)-8s %(message)s"
)
MAX_BACKOFF = 60
BACKOFF = 1
INITIAL_BACKOFF = 1
if __name__ == "__main__":
address = os.environ.get("INKBIRD_ADDRESS")
client = InkBirdClient(address=address)
while True:
try:
logger.info(f"Connecting to {address}")
client.connect()
client.login()
client.enable_data()
client.enable_battery()
logger.debug("Starting Loop")
BACKOFF = INITIAL_BACKOFF
while True:
try:
if client.client.waitForNotifications(1.0):
continue
except bluepy.btle.BTLEInternalError:
pass
except bluepy.btle.BTLEDisconnectError:
time.sleep(min(BACKOFF, MAX_BACKOFF))
BACKOFF *= 2
logger.info(f"Reconnecting to {address}")