-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.py
executable file
·313 lines (253 loc) · 10.3 KB
/
server.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python
import os, sys, signal
import json
import redis
import time
import mosquitto
from threading import Thread
from configobj import ConfigObj
from gevent import monkey
monkey.patch_all()
from flask import Flask, Response, request, redirect, url_for, session
from flask.ext.socketio import SocketIO, emit
from pyfina import pyfina
pyfina = pyfina("/home/pi/data/store/")
r = redis.Redis(host='localhost', port=6379, db=0)
app = Flask(__name__)
app.debug = False
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
mqtt_thread = None
basedir = "/home/pi/emonview/"
conffile = basedir+"emonhub.conf"
@app.route('/')
def index():
session['valid'] = session.get('valid',0)
if session['valid']:
return redirect(url_for('client'))
else:
with open(basedir+"login.html", 'rb') as f:
content = f.read()
return content
@app.route('/login',methods = ['POST','GET'])
def login():
settings = ConfigObj(conffile, file_error=True)
if request.form['username']==settings['auth']['username'] and request.form['password']==settings['auth']['password']:
session['valid'] = session.get('valid',0)
session['valid'] = True
return redirect("/")
@app.route('/logout',methods = ['POST','GET'])
def logout():
session.clear()
return redirect("/")
@app.route('/client')
def client():
if not session['valid']:
return redirect("/")
with open(basedir+"client.html", 'rb') as f:
content = f.read()
return content
@app.route('/test')
def test():
if not session['valid']:
return redirect("/")
with open(basedir+"test.html", 'rb') as f:
content = f.read()
return content
# ------------------------------------------------------------------------------------
# HTTP API
# ------------------------------------------------------------------------------------
@app.route('/api',defaults={'path': ''},methods = ['POST','GET'])
@app.route('/api/<path:path>',methods = ['POST','GET'])
def api(path):
if not session['valid']:
return redirect("/")
settings = ConfigObj(conffile, file_error=True)
config = settings["nodes"]
nodes = r.get("nodes")
if nodes is not None:
nodes = json.loads(nodes)
else:
nodes = {}
result = "False"
nodeid = False
varid = False
prop = False
if path is not "":
url = path.split("/")
if len(url)>0 and url[0].isnumeric():
nodeid = url[0]
if nodeid not in config:
nodeid = False
if len(url)>1 and url[1].isnumeric():
varid = int(url[1])
if nodeid is False and varid is False and len(url)>0:
prop = url[0]
if nodeid is not False and varid is False and len(url)>1:
prop = url[1]
if nodeid is not False and varid is not False and len(url)>2:
prop = url[2]
# POST
if request.method == "POST":
inputstr = request.data
if nodeid is False and varid is False and prop is not False:
if prop=="config":
r.set("config",inputstr)
if nodeid is not False and varid is False and prop is not False:
if prop=="values":
now = time.time()
values = inputstr.split(",")
nodes = json.loads(r.get("nodes"))
if not nodeid in nodes: nodes[nodeid] = {}
nodes[nodeid]["time"] = now
nodes[nodeid]["values"] = values
r.set("nodes",json.dumps(nodes))
# for varid in values:
# store(nodeid,varid,time,values[i])
else:
settings = ConfigObj(conffile, file_error=True)
config = settings["nodes"]
if config is None: config = {}
if not nodeid in config: config[nodeid] = {}
if prop=="nodename": config[nodeid]["nodename"] = inputstr
if prop=="hardware": config[nodeid]["hardware"] = inputstr
if prop=="firmware": config[nodeid]["firmware"] = inputstr
if prop=="names": config[nodeid]["names"] = inputstr.split(",")
if prop=="units": config[nodeid]["units"] = inputstr.split(",")
r.set("config",json.dumps(config))
result = config
if nodeid is not False and varid is not False and prop is not False:
settings = ConfigObj(conffile, file_error=True)
config = settings["nodes"]
if config is None: config = {}
if not nodeid in config: config[nodeid] = {}
if prop=="name": config[nodeid]["names"][varid] = inputstr
if prop=="unit": config[nodeid]["units"][varid] = inputstr
r.set("config",json.dumps(config))
result = config
# GET
if request.method == "GET":
# GET ALL NODES
if nodeid is False and varid is False and prop is False:
for nid in config:
for key in config[nid]:
if not nid in nodes: nodes[nid] = {}
nodes[nid][key] = config[nid][key]
result = nodes
# GET NODE
if nodeid is not False and varid is False and prop is False:
node = config[nodeid]
node["time"] = nodes[nodeid]["time"]
node["values"] = nodes[nodeid]["values"]
result = node
# GET NODE:PROPERTY
if nodeid is not False and varid is False and prop is not False:
if prop=="nodename": result = config[nodeid]["nodename"]
if prop=="firmware": result = config[nodeid]["firmware"]
if prop=="hardware": result = config[nodeid]["hardware"]
if prop=="names": result = config[nodeid]["names"]
if prop=="units": result = config[nodeid]["units"]
if prop=="values": result = nodes[nodeid]["values"]
# GET NODE:VAR
if nodeid is not False and varid is not False and prop is False:
result = {}
if len(config[nodeid]["names"])>varid:
result["name"] = config[nodeid]["names"][varid]
if len(nodes[nodeid]["values"])>varid:
result["value"] = nodes[nodeid]["values"][varid]
if len(config[nodeid]["units"])>varid:
result["unit"] = config[nodeid]["units"][varid]
if not len(result):
result = "False"
# GET NODE:VAR:PROPERTY
if nodeid is not False and varid is not False and prop is not False:
if prop=="name" and len(config[nodeid]["names"])>varid:
result = config[nodeid]["names"][varid]
if prop=="unit" and len(config[nodeid]["units"])>varid:
result = config[nodeid]["units"][varid]
if prop=="value" and len(nodes[nodeid]["values"])>varid:
result = nodes[nodeid]["values"][varid]
if prop=="meta":
filename = str(nodeid)+"_"+str(varid)
meta = pyfina.get_meta(filename)
meta["npoints"] = pyfina.get_npoints(filename)
result = meta
if prop=="data":
filename = str(nodeid)+"_"+str(varid)
start = request.args.get('start')
end = request.args.get('end')
interval = request.args.get('interval')
data = json.dumps(pyfina.data(filename,start,end,interval))
result = data
if type(result) is dict:
result = json.dumps(result)
if type(result) is list:
result = json.dumps(result)
return Response(result, mimetype='text/plain')
@app.route('/conf',methods = ['POST','GET'])
def conf():
if not session['valid']:
return redirect("/")
if request.method == 'POST':
# might be good to do some input checking/sanitization here!
with open(conffile,'w') as f:
f.write(request.data)
return "ok"
else:
with open(conffile, 'rb') as f:
content = f.read()
return Response(content, mimetype='text/plain')
# ------------------------------------------------------------------------------------
# MQTT
# ------------------------------------------------------------------------------------
class MQTT_Thread(Thread):
def __init__(self):
Thread.__init__(self)
self.stop = False
def run(self):
while not self.stop and mqttc.loop() == 0:
pass
def signal_handler(signal, frame):
print "Exit on Ctrl-C"
mqtt_thread.stop = True
sys.exit(0)
def on_message(mosq, obj, msg):
key = msg.topic.split("/")
if key[0]=="api" and key[2]=="values":
nodeid = str(key[1])
now = int(time.time())
values = msg.payload.split(",")
nodes = r.get("nodes")
if nodes is not None:
nodes = json.loads(nodes)
else:
nodes = {}
nodes[nodeid] = {"time":now, "values":values}
r.set("nodes",json.dumps(nodes))
socketio.emit('mqttrelay',{'topic':msg.topic,'payload':msg.payload},namespace='/test')
# ------------------------------------------------------------------------------------
# SOCKET IO
# ------------------------------------------------------------------------------------
@socketio.on('my event', namespace='/test')
def test_message(message):
pass
@socketio.on('connect', namespace='/test')
def test_connect():
pass
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
pass
# ------------------------------------------------------------------------------------
# MAIN PROGRAM
# ------------------------------------------------------------------------------------
# Start MQTT (Mosquitto)
mqttc = mosquitto.Mosquitto()
mqttc.on_message = on_message
mqttc.connect("127.0.0.1",1883, 60, True)
mqttc.subscribe("log", 0)
mqttc.subscribe("api/#", 0)
signal.signal(signal.SIGINT, signal_handler)
mqtt_thread = MQTT_Thread()
if __name__ == '__main__':
mqtt_thread.start()
socketio.run(app,host='0.0.0.0',port=8000)