-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
76 lines (55 loc) · 2.44 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
68
69
70
71
72
73
74
75
76
from graphqlclient import GraphQLClient
from influxdb import InfluxDBClient
from threading import Timer
import yaml
import logging
import signal
import os
import time
from connectors import AwairConnector, InfluxConnector, AwairException
# TODO: set this via an argument
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
def main(config):
"""Accepts configuration and connects to services"""
# Run until we get a KeyboardInterrupt
while True:
logging.info("Checking for new data from Awair")
# Setup a GraphQL client to connect to Awair
awairClient = GraphQLClient(config["awair"]["endpoint"])
awairClient.inject_token("Bearer " + config["awair"]["token"])
# Setup an InfluxDB client
influxClient = InfluxDBClient(**config["influx"])
# Setup our Awair and Influx connectors and inject the clients
awairConnector = AwairConnector(awairClient)
influxConnector = InfluxConnector(influxClient)
# Find any devices
devices = awairConnector.get_devices()
logging.info("Found %s Awair devices", len(devices))
# Check each device
for device in devices:
logging.info(" - Looking up data for device %(name)s with UUID %(uuid)s", device)
# Find the last data we have for it, if any, in InfluxDB
last_data_at = influxConnector.get_last_recorded_time(device)
# Fetch sensor readings from Awair
samples = awairConnector.get_sensor_readings(device, last_data_at)
# Add samples to InfluxDB
influxConnector.add_samples(device, samples)
# TODO: we should calculate the next time there will be data
wait_for_seconds = 15 * 60
logging.info("Checking again in %s seconds...", wait_for_seconds)
# Sleep until we need to do another check
time.sleep(wait_for_seconds)
# Load and parse the configuration
try:
with open("config.yaml") as configFile:
config = yaml.load(configFile)
if ("influx" not in config or "awair" not in config):
logging.error("Invalid config.yaml file, please use config.example.yaml as a guide")
else:
main(config)
except FileNotFoundError as e:
logging.error("Please add a config.yaml file")
except AwairException as e:
logging.error("Awair server returned unexpected results", e)
except KeyboardInterrupt as e:
logging.warn("Stopping...")