-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·60 lines (44 loc) · 1.73 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
#!/usr/bin/python3
import logging
import time
import yaml
from influx import InfluxConnector
from gqgmc import GqGmcConnector
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", "gqgmc", "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)
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"]
measurement: str = influx_conf["measurement"]
influxConnector = InfluxConnector(influx_conf["bucket"], influx_conf["token"], influx_conf["org"], influx_conf["url"])
gq_conf = config["gqgmc"]
gcmConnector = GqGmcConnector(gq_conf["port"], gq_conf["baudrate"], gq_conf["timeout"], gq_conf["check"], gq_conf["version"])
while True:
try:
record = gcmConnector.fetch_data(measurement)
influxConnector.add_samples(record)
if not loop_seconds:
exit(0)
time.sleep(loop_seconds)
except Exception as e:
logging.exception(e)
except Exception as e:
logging.exception(e)
exit(1)