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
| #!flask/bin/python
#
# Environmental Data Sensing and Storing
# mscs crysanthus@gmail.com
#
import datetime
from flask import Flask, abort, request, make_response, jsonify, render_template, send_from_directory
# db
import sqlite3
# graph
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
# WSGI Server
from gevent.wsgi import WSGIServer
# create db and table in init case
try:
conn = sqlite3.connect('homestats.db')
cursr = conn.cursor()
cursr.execute('''CREATE TABLE IF NOT EXISTS weather_data
(iid integer primary key, date text, temp text, humd text, lumn text)''')
conn.commit()
except:
abort(500)
app = Flask(__name__, static_url_path='')
# serve images directly, no process
@app.route('/favicon.ico')
def send_favicon():
return send_from_directory('templates/images', 'favicon.ico')
# serve images directly, no process
@app.route('/images/<path:path>')
def send_images(path):
return send_from_directory('templates/images', path)
# serve graph directly, no process
@app.route('/graphs/<path:path>')
def send_graphs(path):
return send_from_directory('templates/graphs', path)
# save data
# save GET data -- working part --
@app.route('/api/addstatsg', methods=['GET'])
def add_stats_get():
try:
# check to see if API token is correct -- TODO --
if not request.args['cert']:
abort(417)
if request.args['cert'] != 'xyz':
abort(417)
# check to see if API token is correct -- TODO --
# chk to see if the field have correct values
if not request.args['temp'] or not request.args['temp'].isnumeric():
abort(404)
if not request.args['humd'] or not request.args['humd'].isnumeric():
abort(404)
if not request.args['lumn'] or not request.args['lumn'].isnumeric():
abort(404)
stats = {'date': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'temp': request.args['temp'],
'humd': request.args['humd'],
'lumn': request.args['lumn']
}
except:
abort(501)
try:
conn = sqlite3.connect('homestats.db')
cursr = conn.cursor()
cursr.execute('''INSERT INTO weather_data
VALUES(NULL, :date, :temp, :humd, :lumn)''', stats)
conn.commit()
except:
abort(500)
return jsonify({'homestats':'data saved'}), 201
# create graph and serve stat html
@app.route('/')
def index():
# do the graph
plt.ylabel('Units')
plt.xlabel('Date/Time')
# get data
conn = sqlite3.connect('homestats.db')
cursr = conn.cursor()
cursr.execute('''SELECT date, temp, humd, lumn FROM weather_data;''')
xv = [] # x axis
yv1 = [] # y axis - tempreture in red
yv2 = [] # y axis - humidity in green
yv3 = [] # y axis - ambient light in yellow
# add data to graph
for row in cursr:
xv.append(datetime.datetime.strptime(row[0],"%Y-%m-%d %H:%M:%S"))
yv1.append(myFloat(row[1]))
yv2.append(myFloat(row[2]))
yv3.append(myFloat(row[3]))
# legends
plt.plot(xv, yv1, '-r', label='Tempreture', linewidth=0.50)
plt.plot(xv, yv2, '-g', label='Humidity', linewidth=0.75)
plt.plot(xv, yv3, '-y', label='Light', linewidth=0.80)
# beautify the x-labels
plt.gcf().autofmt_xdate()
plt.legend()
# save graph as a .png file to serve via html
plt.savefig('templates/graphs/statistic.png')
plt.close()
# -- send html out with graph --
return render_template('stats.html')
# http errors
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
@app.errorhandler(500)
def not_found(error):
return make_response(jsonify({'error': 'Server Internal error'}), 500)
@app.errorhandler(501)
def not_found(error):
return make_response(jsonify({'error': 'Not Implemented'}), 501)
@app.errorhandler(417)
def not_found(error):
return make_response(jsonify({'error': 'Token error'}), 417)
# chk to see if a string is float-able
def myFloat(s):
try:
return float(s)
except ValueError:
return 0.0
if __name__ == '__main__':
http_server = WSGIServer(('0.0.0.0', 5000), app)
http_server.serve_forever()
|