-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·66 lines (48 loc) · 1.87 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
#! /usr/bin/env python3
import time
import yaml
import logging
import platform
import sys
from influx import InfluxConnector
from iqair import IqAirConnector
from pathlib import Path
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO)
def get_config():
CONFIG_FILE = "config.yaml"
try:
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", "iqair", "main"}:
if name not in config:
raise ValueError(f"Invalid {CONFIG_FILE}: missing section {name}.")
return config
except FileNotFoundError as e:
logging.error(f"Missing {e.filename}.")
exit(2)
SUPPORTED_PYTHON_MAJOR = 3
SUPPORTED_PYTHON_MINOR = 9
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 = get_config()
main_conf = config["main"]
logging.getLogger().setLevel(logging.getLevelName(main_conf["logverbosity"]))
loop_seconds: int = main_conf["loop_seconds"]
influx_conf = config["influx"]
influxConnector = InfluxConnector(influx_conf["bucket"], influx_conf["token"], influx_conf["org"], influx_conf["url"])
iqAirConnector = IqAirConnector(config["iqair"])
while True:
try:
records = iqAirConnector.fetch_data()
influxConnector.add_samples(records)
except Exception as e:
logging.exception(e)
if not loop_seconds:
exit(0)
time.sleep(loop_seconds)
except Exception as e:
logging.exception(e)
exit(1)