-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (47 loc) · 1.96 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
#!/usr/bin/python3
from awair import AwairConnector
from datetime import datetime, timezone
from influx import InfluxConnector
import logging
from pathlib import Path
import time
import yaml
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO)
def main(config) -> None:
influx_conf = config["influx"]
awair_conf = config["awair"]
measurement = influx_conf["measurement"]
influxConnector = InfluxConnector(influx_conf["bucket"], influx_conf["token"], influx_conf["org"], influx_conf["url"])
try:
to_time = datetime.now(timezone.utc)
from_time = influxConnector.get_last_recorded_time(measurement, awair_conf["maxhours"], to_time)
awairConnector = AwairConnector(awair_conf["token"], awair_conf["records"])
records = awairConnector.fetch_data(from_time, to_time, measurement)
try:
influxConnector.add_samples(records, len(records))
except Exception as e:
logging.error(f"Exception importing records")
logging.exception(e)
except Exception as e:
logging.error(f"Exception while querying records")
logging.exception(e)
CONFIG_FILE = "config.yaml"
try:
while True:
with open(Path(__file__).with_name(CONFIG_FILE)) as config_file:
config = yaml.safe_load(config_file)
if not config:
raise ValueError(f"Invalid {CONFIG_FILE}. See template.{CONFIG_FILE}.")
for name in {"influx", "awair", "main"}:
if name not in config:
raise ValueError(f"Invalid {CONFIG_FILE}: missing section {name}.")
main_conf = config["main"]
logging.getLogger().setLevel(logging.getLevelName(main_conf["logverbosity"]))
main(config)
time.sleep(main_conf["loop_seconds"])
except FileNotFoundError as e:
logging.error(f"Missing {e.filename}.")
exit(2)
except Exception as e:
logging.exception(e)
exit(1)