-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
227 lines (174 loc) · 6.54 KB
/
app.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
""" smariot : barebones REST App for IoT """
import os
import datetime
import json
import base64
import struct
from time import gmtime, strftime
import dateutil.parser
from flask import Flask, render_template, request, jsonify, abort
from flask_sqlalchemy import SQLAlchemy
from flask_socketio import SocketIO, emit
RELOAD_INTERVAL = 3000 # in seconds
VIZ_DATA_POINTS = 50 # default data points for the chart
REC_FETCH_COUNT = '1' # default records to fetch (must be a string)
app = Flask(__name__)
socketio = SocketIO(app)
# DB config settings, the second one is to supress a warning
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Get API Key for env vars (can be set via Heroku Dashboard)
API_KEY = os.environ['API_KEY']
class SensorData(db.Model): # pylint: disable=too-few-public-methods
""" ORM class for sensor readings """
__tablename__ = 'sensor_data'
id = db.Column(db.Integer, primary_key=True)
timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow)
hw_id = db.Column(db.String)
msg = db.Column(db.String)
def __init__(self, hw_id, msg):
self.hw_id = hw_id
self.msg = msg
def __str__(self):
return "SensorData({},{},{})".format(self.timestamp, self.hw_id, self.msg)
class DeviceData(db.Model): # pylint: disable=too-few-public-methods
""" ORM class for device info """
__tablename__ = 'device_data'
id = db.Column(db.Integer, primary_key=True)
timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow)
hw_id = db.Column(db.String)
def __init__(self, hw_id):
self.hw_id = hw_id
def __str__(self):
return "DeviceData({},{})".format(self.timestamp, self.hw_id)
@app.route("/")
def default_handler():
"""handler for / endpoint"""
return render_template('index.html')
@app.route("/req")
def req_handler():
"""handler for request iframe"""
return render_template('req.html', refresh=RELOAD_INTERVAL)
@app.route("/data", methods=['GET', 'POST'])
def data_handler():
"""handler for /data endpoint"""
if request.method == 'POST':
key = request.headers['x-api-key']
if not key == API_KEY:
abort(401)
else:
try:
save_and_emit(request.get_json(force=True))
return jsonify({'result': 'success'})
except:
abort(400)
elif request.method == 'GET':
return db_fetch_handler()
else:
abort(400)
@app.route("/db")
@app.route("/db/<count>")
def db_fetch_handler(count=REC_FETCH_COUNT):
""" handler for /db endpoint -- fetch data from DB"""
dat = db.session.query(SensorData).order_by(SensorData.id.desc()).limit(count)
ret_list = list()
for item in dat:
ret_list.append({'hw_id':item.hw_id,
'timestamp': item.timestamp,
'data':json.loads(item.msg)})
return jsonify(ret_list)
@app.route("/viz")
@app.route("/viz/<dev_id>")
def viz_handler(dev_id=''):
""" handler for the viz endpoint """
if not dev_id:
return render_template('viz.html', dev_list=get_dev_list())
else:
viz = get_viz_data(dev_id)
return render_template('viz_chart.html', refresh=RELOAD_INTERVAL, viz_data=viz)
@socketio.on('connect', namespace='/live')
def client_connect():
""" socketio client connect handler """
emit('my response', {'data': 'Connected'})
def get_timestamp():
"""returns UTC time in readable format"""
return strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime())
def save_and_emit(data):
""" save POSTed data to DB and emit to socketio """
try:
readings = msg_get_value(data)
hwid = msg_get_hw_id(data)
# parse sensor data and add to DB
sensor_data = SensorData(hwid, json.dumps(readings))
db.session.add(sensor_data)
# parse Hardware ID and add to DB, if not existing
dev_data = get_or_create(db.session, DeviceData, hw_id=hwid)
if dev_data:
db.session.add(dev_data)
# save changes to DB
db.session.commit()
except:
pass
# emit raw JSON to socketio (so that it shows up on homepage)
disp_json = censor_downlink_URL(data)
socketio.emit('data', {'timestamp': get_timestamp(), 'value': json.dumps(disp_json)},
namespace='/live')
def get_viz_data(device_id, count=VIZ_DATA_POINTS):
""" fetch data from DB and parse for visualization"""
return parse_db_data(device_id, count)
def parse_db_data(device_id, count):
""" parses stored JSON and returns plottable data (timestamp vs sensor value) """
dat = db.session.query(SensorData).filter(SensorData.hw_id.like(device_id)) \
.order_by(SensorData.id.desc()).limit(count)
dat_list = list()
for item in dat:
timestamp = item.timestamp
val = json.loads(item.msg)
dat_list.append((timestamp, val))
return list(reversed(dat_list))
def msg_get_timestamp(raw_json):
""" extract timestamp from JSON """
return dateutil.parser.parse(raw_json['metadata']['time']).strftime("%d/%m/%y %H:%M:%S")
def msg_get_value(raw_json):
""" extract sensor reading from JSON """
return msg_parse_val(raw_json['payload_raw'])
def msg_parse_val(raw_val):
""" parse JSON from TTN and return actual sensor value """
ret_val = 0
try:
# extract sensor values (2x floats)
byte_arr = base64.b64decode(raw_val)
ret_val = struct.unpack('ff', byte_arr)
except:
pass
return ret_val
def msg_get_hw_id(raw_json):
""" extract hardware ID from JSON """
return raw_json['hardware_serial']
# see https://stackoverflow.com/q/2546207
def get_or_create(session, model, **kwargs):
""" helper method to insert in DB if not exist """
instance = session.query(model).filter_by(**kwargs).first()
if instance:
return instance
else:
instance = model(**kwargs)
session.add(instance)
session.commit()
return instance
def get_dev_list():
""" get list of hardware IDs in DB"""
dat = db.session.query(DeviceData).order_by(DeviceData.id.desc())
dat_list = list()
for item in dat:
dat_list.append(str(item.hw_id))
return list(reversed(dat_list))
def censor_downlink_URL(raw_json):
try:
raw_json['downlink_url'] = 'http://example.com/'
except:
pass
return raw_json
if __name__ == '__main__':
socketio.run(app)