-
-
Notifications
You must be signed in to change notification settings - Fork 502
/
ttn_data_storage.py
196 lines (176 loc) · 6.97 KB
/
ttn_data_storage.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# coding=utf-8
import datetime
import logging
import requests
from flask_babel import lazy_gettext
from mycodo.databases.models import Conversion
from mycodo.databases.models import DeviceMeasurements
from mycodo.inputs.base_input import AbstractInput
from mycodo.inputs.sensorutils import calculate_altitude
from mycodo.inputs.sensorutils import calculate_dewpoint
from mycodo.inputs.sensorutils import calculate_vapor_pressure_deficit
from mycodo.utils.database import db_retrieve_table_daemon
from mycodo.utils.influx import parse_measurement
from mycodo.utils.influx import write_influxdb_value
# Measurements
measurements_dict = {
0: {
'measurement': 'humidity',
'unit': 'percent'
},
1: {
'measurement': 'temperature',
'unit': 'C'
},
2: {
'measurement': 'pressure',
'unit': 'Pa'
},
3: {
'measurement': 'dewpoint',
'unit': 'C'
},
4: {
'measurement': 'altitude',
'unit': 'm'
},
5: {
'measurement': 'vapor_pressure_deficit',
'unit': 'Pa'
}
}
# Input information
INPUT_INFORMATION = {
'input_name_unique': 'TTN_DATA_STORAGE',
'input_manufacturer': 'The Things Network',
'input_name': 'TTN Integration: Data Storage',
'measurements_name': 'Temperature/Humidity',
'measurements_dict': measurements_dict,
'options_enabled': [
'custom_options',
'measurements_select',
'period',
'pre_output'
],
'options_disabled': ['interface'],
'interfaces': ['Mycodo'],
'custom_options': [
{
'id': 'application_id',
'type': 'text',
'default_value': '',
'name': lazy_gettext('Application ID'),
'phrase': lazy_gettext('The Things Network Application ID')
},
{
'id': 'app_api_key',
'type': 'text',
'default_value': '',
'name': lazy_gettext('App API Key'),
'phrase': lazy_gettext('The Things Network Application API Key')
},
{
'id': 'device_id',
'type': 'text',
'default_value': '',
'name': lazy_gettext('Device ID'),
'phrase': lazy_gettext('The Things Network Device ID')
}
]
}
class InputModule(AbstractInput):
""" A sensor support class that retrieves stored data from The Things Network """
def __init__(self, input_dev, testing=False):
super(InputModule, self).__init__()
self.logger = logging.getLogger("mycodo.inputs.ttn_data_storage")
if not testing:
self.logger = logging.getLogger(
"mycodo.ttn_data_storage_{id}".format(id=input_dev.unique_id.split('-')[0]))
self.unique_id = input_dev.unique_id
self.interface = input_dev.interface
self.period = input_dev.period
self.first_run = True
self.device_measurements = db_retrieve_table_daemon(
DeviceMeasurements).filter(
DeviceMeasurements.device_id == input_dev.unique_id)
if input_dev.custom_options:
for each_option in input_dev.custom_options.split(';'):
option = each_option.split(',')[0]
value = each_option.split(',')[1]
if option == 'application_id':
self.application_id = value
elif option == 'app_api_key':
self.app_api_key = value
elif option == 'device_id':
self.device_id = value
# Get all the data the past 7 days when first started
self.get_new_data(604800) # 604800 seconds = 7 days (longest Data Storage Integration stores for)
def get_new_data(self, past_seconds):
if self.first_run:
self.first_run = False
return
endpoint = "https://{app}.data.thethingsnetwork.org/api/v2/query/{dev}?last={time}".format(
app=self.application_id, dev=self.device_id, time="{}s".format(past_seconds))
headers = {"Authorization": "key {k}".format(k=self.app_api_key)}
response = requests.get(endpoint, headers=headers)
try:
for each_resp in response.json():
datetime_ts = datetime.datetime.strptime(each_resp['time'][:-7], '%Y-%m-%dT%H:%M:%S.%f')
measurements = {
0: {
'measurement': 'humidity',
'unit': 'percent',
'value': each_resp['humidity']
},
1: {
'measurement': 'temperature',
'unit': 'C',
'value': each_resp['temperature']
},
2: {
'measurement': 'pressure',
'unit': 'Pa',
'value': each_resp['pressure']
},
3: {
'measurement': 'dewpoint',
'unit': 'C',
'value': calculate_dewpoint(
each_resp['temperature'], each_resp['humidity'])
},
4: {
'measurement': 'altitude',
'unit': 'm',
'value': calculate_altitude(each_resp['pressure'])
},
5: {
'measurement': 'vapor_pressure_deficit',
'unit': 'Pa',
'value': calculate_vapor_pressure_deficit(
each_resp['temperature'], each_resp['humidity'])
}
}
for channel in measurements:
if self.is_enabled(channel):
measurement = self.device_measurements.filter(
DeviceMeasurements.channel == channel).first()
conversion = db_retrieve_table_daemon(
Conversion, unique_id=measurement.conversion_id)
measurement_single = parse_measurement(
conversion,
measurement,
measurements,
measurement.channel,
measurements[channel])
write_influxdb_value(
self.unique_id,
measurement_single[channel]['unit'],
value=measurement_single[channel]['value'],
measure=measurement_single[channel]['measurement'],
channel=channel,
timestamp=datetime_ts)
except:
self.logger.exception("Error acquiring and/or storing measurements")
def get_measurement(self):
""" Gets the data """
self.get_new_data(int(self.period))