-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_cycle.py
75 lines (60 loc) · 3.25 KB
/
read_cycle.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
import datetime
import configparser
from time import sleep
from persistance.filestore import FileStore
from datasource.am2302 import AM2302DataSource
from datasource.weather import OpenWeatherMapDataSource
from datasource.cpu import CPU
# from datasource.test_data_source import TestClimateDataSource
from persistance.influxDb import InfluxDBStore, TimeSeriesMeasurementEntry
# Setup
config = configparser.RawConfigParser()
config.read('config.properties')
deployment_name = config.get('Deploy', 'name')
location = config.get('Weather', 'location')
open_weather_map_app_id = config.get('Weather', 'open_weather_map_app_id')
weather_data_source = OpenWeatherMapDataSource(location, open_weather_map_app_id)
# test_data_source = TestClimateDataSource()
am2302_data_source = AM2302DataSource(pin=config.get('AM2302', 'pin'))
file_store = FileStore()
influx_db_store = InfluxDBStore('TelemetryHistory')
cpu = CPU()
#######
while True:
time = datetime.datetime.now()
wH, wT = weather_data_source.read()
# ghH, ghT = test_data_source.read()
ghH, ghT = am2302_data_source.read()
print("Greenhouse Temp: {0}, Weather Temp: {1}".format(ghT, wT))
greenhouse = TimeSeriesMeasurementEntry(measurement='am2302',
tags={"update": "whole",
"device": "am2302",
"deployment": deployment_name},
fields={"temp": float(ghT),
"humidity": float(ghH)})
greenhouse_data_persisted = influx_db_store.persist(greenhouse.to_record())
file_store.persist(time, ghH, ghT)
if wH is not None and wT is not None:
weather = TimeSeriesMeasurementEntry(measurement='weather',
tags={"update": "whole",
"device": "openweathermap",
"location": location,
"deployment": deployment_name},
fields={"temp": float(wT),
"humidity": float(wH)}
)
weather_data_persisted = influx_db_store.persist(weather.to_record())
else:
weather_data_persisted = False
cpu_data = TimeSeriesMeasurementEntry(measurement="cpu",
tags={"device": "cpu",
"deployment": deployment_name},
fields={"temp": cpu.temperature()})
cpu_data_persisted = influx_db_store.persist(cpu_data.to_record())
print("[{0}] Greenhouse: {1}, Weather: {2}, CPU: {3}".format(time,
("PERSISTED" if greenhouse_data_persisted else "ERR"),
("PERSISTED" if weather_data_source else "ERR"),
("PERSISTED" if cpu_data_persisted else "ERR"),
)
)
sleep(60)