forked from gunja/influx2-weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
69 lines (48 loc) · 1.65 KB
/
weather.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
import configparser
import requests
import importlib
from datetime import datetime
import json
import time
import datetime
from influxdb_client import InfluxDBClient, WritePrecision
from influxdb_client.client.write_api import ASYNCHRONOUS
from influxdb_client.client.write.point import Point
config = configparser.ConfigParser()
config.read('config.ini')
delay = float(config['GENERAL']['Delay'])
output = bool(config['GENERAL'].get('Output', fallback=True))
# print(output)
influxURL = config['INFLUXDB']['URL']
influxTOKEN = config['INFLUXDB']['token']
influxORG = config['INFLUXDB']['org']
influxBucket = config['INFLUXDB']['bucket']
Sources = json.loads(config['WEATHER'].get('Sources'))
influx_client = InfluxDBClient(url = influxURL, token = influxTOKEN,
org = influxORG)
write_api = influx_client.write_api(write_options=ASYNCHRONOUS)
#return a list of payloads to send to influxdb
def getSourceData(source):
lib = importlib.import_module(source)
sourceData = lib.main()
return sourceData
def sendInfluxData(data):
if output:
print(type(data))
try:
write_api.write(bucket = influxBucket, org = influxORG, record = data)
except Exception as e:
print('ERROR: Failed To Write To InfluxDB')
print(e)
if output:
print('Written To Influx: {}'.format(data))
def main():
while True:
for source in Sources:
sourceData = getSourceData(source)
#only send the data if there is non-null data to send
if sourceData is not None:
sendInfluxData(sourceData)
time.sleep(delay)
if __name__ == '__main__':
main()