-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
101 lines (80 loc) · 2.71 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
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
#!/usr/bin/python3
from flask import Flask, request, redirect, abort, jsonify, render_template
from datetime import timedelta
import json
import os
import storage
from storage import Mode
import common
from hardware import Client, Sensor
import validator
app = Flask(__name__, template_folder=common.get_abs_path('templates'))
LOCAL_ENV = os.getenv('ENVIRONMENT', '') == 'local'
SECRET = common.read_line_from('secret.txt')
if LOCAL_ENV: print('Running in local/test mode...')
@app.route('/', methods = ['GET'])
def home():
return redirect('/get', code=302)
@app.route('/update', methods = ['POST'])
def update():
if 'secret' not in request.json or request.json['secret'] != SECRET:
return abort(403)
values = []
client = None
for rasp in Client.items:
for sensor in rasp.sensors:
name = sensor.name
if name not in request.json: break
if not validator.is_sane(sensor, request.json[name]):
print('Got weird value for sensor %s: %s' % \
(name, request.json[name]))
break
values.append(request.json[name])
else:
if len(values) == len(rasp.sensors):
client = rasp
if not client:
return abort(400)
storage.put(LOCAL_ENV, client, values)
return 'success', 202
def get(mode=Mode.avg):
client = Client.from_str(request.args.get('client', default='', type=str))
hours = request.args.get('hours', default=0, type=int)
days = request.args.get('days', default=0, type=int)
weeks = request.args.get('weeks', default=0, type=int)
json = 'json' in request.args
clients = [ client ] if client else Client.items
data = storage.get(LOCAL_ENV, timedelta(
hours=hours,
days=days,
weeks=weeks,
),
mode,
clients
)
if json:
return jsonify(data)
else:
return render_template('get.html',
readouts=data,
header=','.join([ c['client'] for c in data ]),
units=Sensor.get_map(),
)
@app.route('/get', methods = ['GET'])
def get_default():
return get()
@app.route('/get/avg', methods = ['GET'])
def get_avg():
return get(mode=Mode.avg)
@app.route('/get/min', methods = ['GET'])
def get_min():
return get(mode=Mode.min)
@app.route('/get/max', methods = ['GET'])
def get_max():
return get(mode=Mode.max)
if __name__ == '__main__':
# This is used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
app.run(host='127.0.0.1', port=8080, debug=True)
# [END gae_python37_app]