-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
67 lines (55 loc) · 1.85 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
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
import asyncio
from datetime import datetime, timezone
import logging
import platform
import sys
import time
from config import Config
from influx import InfluxConnector
from myair import MyAirConnector
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
last_report_time: str = None
SUPPORTED_PYTHON_MAJOR = 3
SUPPORTED_PYTHON_MINOR = 11
if sys.version_info < (SUPPORTED_PYTHON_MAJOR, SUPPORTED_PYTHON_MINOR):
raise Exception(
f"Python version {SUPPORTED_PYTHON_MAJOR}.{SUPPORTED_PYTHON_MINOR} or later required. Current version: {platform.python_version()}."
)
try:
config = Config("config.toml", "myair_influx").load()
main_conf = config["main"]
logging.getLogger().setLevel(logging.getLevelName(main_conf["logverbosity"]))
sleep_time = main_conf["loop_minutes"] * 60
logging.debug(f"CONFIG: {config}")
my_air_conf = config["resmed"]
my_air = MyAirConnector(my_air_conf)
influx_conf = config["influx"]
influxConnector = InfluxConnector(
influx_conf["bucket"],
influx_conf["token"],
influx_conf["org"],
influx_conf["url"],
influx_conf["measurement"],
)
while True:
try:
to_time = datetime.now(timezone.utc)
from_time = influxConnector.get_last_recorded_time(
my_air_conf["max_days"], to_time
)
ret = asyncio.run(
my_air.get_samples(
last_report_time, from_time, to_time, influxConnector.measurement
)
)
if ret:
influxConnector.add_samples(ret[1])
last_report_time = ret[0]
except Exception as e:
logging.exception(e)
if not sleep_time:
exit(0)
time.sleep(sleep_time)
except Exception as e:
logging.exception(e)
exit(1)