-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·62 lines (46 loc) · 1.86 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
import time
import yaml
import logging
from datetime import datetime
from influx import InfluxConnector
from weewx import WeewxConnector
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", "weewx", "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"]
influxConnector = InfluxConnector(influx_conf["bucket"], influx_conf["token"], influx_conf["org"], influx_conf["url"])
weewx_conf = config["weewx"]
weewxConnector = WeewxConnector(weewx_conf["db"], influx_conf["measurement"], weewx_conf["metric"])
max_hours: int = weewx_conf["max_hours"]
while True:
try:
from_time = influxConnector.get_last_recorded_time(weewxConnector.measurement, max_hours, datetime.utcnow())
records = weewxConnector.fetch_data(from_time)
influxConnector.add_samples(records)
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)