-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflaskServer.py
executable file
·461 lines (315 loc) · 10.2 KB
/
flaskServer.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/python
import sys, os, time, tempfile, datetime
from flask import Flask, request, json
from flask_cors import CORS, cross_origin
app = Flask(__name__)
CORS(app)
# Callback function to call
ecfp = None # executeCommandFromParams
su = None # scheduleUpdate
tse = None # thingSpeakEnable (setServiceThingSpeak)
rdff = None # readDeviceFromFile
rsd = None # readServiceData
rd = None # removeDevice
rtff = None # readTriggersFromFile
wttf = None # writeTriggerToFile
rsff = None # readScheduleFromFile
jsonObject = {
"status" : "Ok",
"devices": [
{
"name": "Arduino-1",
"hardware": "Arduino",
"technology": "nRF24"
}
]
}
@app.route("/getDevices.py", methods = ['GET'])
def get_devices():
print "---Get Devices---"
filename = "devs.json"
print "-Opening device file"
#with open(filename) as data_file:
# devs = json.load(data_file)
devs = rdff()
print "-Read devices"
jsonCurrent = {}
jsonCurrent["status"] = "Ok"
jsonCurrent["devices"] = devs
print "-Returning"
return json.dumps(jsonCurrent)
@app.route("/getLastValue.py", methods = ['GET', 'POST'])
def get_last_value():
print "---Get Last Value of Service"
data = request.get_json()
deviceName = data["device_name"]
serviceName = data["service_name"]
service_data = rsd()
values = []
for el in service_data:
if el["device_name"] == deviceName and el["service_name"] == serviceName:
values = el["values"]
jsonCurrent = {}
jsonCurrent["device_name"] = deviceName
jsonCurrent["service_name"] = serviceName
if values == [] or len(values) == 0:
jsonCurrent["available"] = False
else:
values = sorted(values, key=lambda k: k['timestamp'])
jsonCurrent["available"] = True
jsonCurrent["value"] = values[-1]["value"]
jsonCurrent["timestamp"] = datetime.datetime.fromtimestamp(long(values[-1]["timestamp"])/1000).strftime("%Y-%m-%d %H:%M")
return json.dumps(jsonCurrent)
@app.route("/getDeviceInfo.py", methods = ['GET', 'POST'])
def get_device_info():
print "---Get Device Info---"
err = open("/tmp/err.out", "w")
original_stderr = sys.stderr
sys.stderr = err
string = "Ok"
filename = "devs.json"
data = request.get_json()
device_name = data["device_name"]
with open(filename) as data_file:
devs = json.load(data_file)
d = None
for elem in devs:
if elem["device_name"] == device_name:
d = elem
jsonCurrent = {}
jsonCurrent["device_name"] = d["device_name"]
jsonCurrent["status"] = "Ok"
jsonCurrent["services"] = d["services"]
print "-Returning"
return json.dumps(jsonCurrent)
@app.route("/executeCommand.py", methods = ['GET', 'POST'])
def execute_command():
print "---Execute Command---"
err = open("/tmp/err.out", "w")
original_stderr = sys.stderr
sys.stderr = err
data = request.get_json()
result = "Error"
devfilename = "devs.json"
with open(devfilename) as data_file:
devs = json.load(data_file)
if data is None:
string = "Data is None"
else:
string = str(len(data))
device_name = data["device_name"]
service_name = data["service_name"]
command = data["command"]
argument = None
if "argument" in data.keys():
#print "Got argument"
argument = data["argument"]
dev = {}
for elem in devs:
if elem["device_name"] == device_name:
dev = elem
tech = dev["technology"]
address = dev["address"]
if argument == None:
commandString = device_name + '!' + tech + '!' + str(address) + '!' + service_name + '!' + command + '!'
else:
commandString = device_name + '!' + tech + '!' + str(address) + '!' + service_name + '!' + command + '!' + argument
#print commandString
result = ecfp(device_name, tech, address, service_name, command, argument, False)
print "Returned " + result
millis = str(int(round(time.time() * 1000)))
jsonObject = {
"device_name": device_name,
"result": result,
"timestamp": millis,
"timestamp_str": datetime.datetime.fromtimestamp(long(millis)/1000).strftime("%Y-%m-%d %H:%M")
}
return json.dumps(jsonObject)
@app.route("/getSchedule.py", methods = ['GET', 'POST'])
def get_schedule():
print "---Get Schedule--"
data = request.get_json()
err = open("/tmp/err.out", "w")
original_stderr = sys.stderr
sys.stderr = err
device_name = data["device_name"]
service_name = data["service_name"]
schedList = []
schedJson = rsff()
for el in schedJson:
if el["device_name"] == device_name and el["service_name"] == service_name:
schedList.append(el)
jsonObj = {}
jsonObj["schedule"] = schedList
return json.dumps(jsonObj)
@app.route("/handleSchedule.py", methods = ['GET', 'POST'])
def handle_schedule():
print "Inizio..."
data = request.get_json()
print data
err = open("/tmp/err.out", "w")
original_stderr = sys.stderr
sys.stderr = err
result = "Error"
schedule_command = data["schedule_command"]
device_name = data["device_name"]
service_name = data["service_name"]
command = data["command"]
if "argument" in data.keys():
argument = data["argument"]
else:
argument = ""
if "frequency" in data.keys():
frequency = str(data["frequency"])
print "Frequency schedule - " + frequency
su(schedule_command, device_name, service_name, command, argument, "F", frequency)
elif "time" in data.keys():
sched_time = data["time"]
print "Should add schedule at " + sched_time + "!"
su(schedule_command, device_name, service_name, command, argument, "T", sched_time)
else:
frequency = ""
schedId = data["id"]
#su(schedule_command, device_name, service_name, command, '', '', '')
su(schedule_command, '', '', '', schedId, '', '')
jsonObject = {
"result": "Ok"
}
return json.dumps(jsonObject)
@app.route("/handleTrigger.py", methods = ['GET', 'POST'])
def handle_trigger():
data = request.get_json()
print data
err = open("/tmp/err.out", "w")
original_stderr = sys.stderr
sys.stderr = err
request_type = data["request"]
# Reply with available commands
if request_type == "commands":
filename = "devs.json"
with open(filename) as data_file:
devs = json.load(data_file)
jsonObj = {}
jsonObj["commands"] = devs
return json.dumps(jsonObj)
# Reply with active triggers
if request_type == "triggers":
trigs = rtff()
jsonObj = {}
jsonObj["triggers"] = trigs
return json.dumps(jsonObj)
# Add trigger event
elif request_type == 'A':
print "Adding..."
trigs = rtff()
print trigs
print "Conditions"
print data["conditions"]
# Fields: type, device_name, service_name, command, mail_address, gcm_id
then = json.loads(data["then"])
print then
trigEl = {}
trigEl["then"] = then
trigEl["conditions"] = data["conditions"]
trigEl["id"] = len(trigs) + 1
trigs.append(trigEl)
print trigs
wttf(trigs)
return json.dumps(then)
# Remove trigger event
elif request_type == 'R':
trigs = rtff()
idToRemove = int(data["id"])
res = "No"
for i, el in enumerate(trigs):
if el["id"] == idToRemove:
del trigs[i]
res = "Si"
wttf(trigs)
response = {}
response["status"] = res
return json.dumps(response)
@app.route("/getServiceData.py", methods = ['GET', 'POST'])
def get_service_data():
err = open("/tmp/err.out", "w")
original_stderr = sys.stderr
sys.stderr = err
data = request.get_json()
device_name = data["device_name"]
service_name = data["service_name"]
startMillis = data["start_time"]
endMillis = data["end_time"]
start_date = datetime.datetime.fromtimestamp(long(startMillis)/1000).strftime("%Y-%m-%d %H:%M")
end_date = datetime.datetime.fromtimestamp(long(endMillis)/1000).strftime("%Y-%m-%d %H:%M")
#filename = "service_data.json"
#with open(filename) as data_file:
# service_data = json.load(data_file)
service_data = rsd()
values = []
for el in service_data:
if el["device_name"] == device_name and el["service_name"] == service_name:
values = el["values"]
values = sorted(values, key=lambda k: k['timestamp'])
filtered_data = []
for el in values:
if long(el['timestamp']) >= long(startMillis) and long(el['timestamp']) <= long(endMillis):
filtered_data.append(el)
for el in filtered_data:
#el["timestamp"] = datetime.datetime.fromtimestamp(long(el["timestamp"])/1000).strftime("%Y-%m-%d %H:%M")
el["timestamp"] = long(el["timestamp"])
jo = {
"status" : "Ok",
"service_name": service_name,
"device_name": device_name,
"start_date" : startMillis,
"end_date" : end_date,
"values": filtered_data
}
# Return Json #
return json.dumps(jo)
@app.route("/setThingSpeak.py", methods = ['POST'])
def set_thingspeak():
print "---Set ThingSpeak---"
print request
data = request.get_json()
print data
devName = str(data["device_name"])
servName = str(data["service_name"])
command = str(data["command"])
print devName + " " + servName + " " + command
res = tse(devName, servName, command)
print res
jsonCurrent = {}
if command == "GET":
print "Outside Get"
if res == True:
jsonCurrent["result"] = "ON"
else:
jsonCurrent["result"] = "OFF"
else:
if res == 1:
jsonCurrent["result"] = command
else:
jsonCurrent["result"] = False
return json.dumps(jsonCurrent)
@app.route("/removeDevice.py", methods = ['POST'])
def remove_device():
print "---Remove Device---"
data = request.get_json()
devName = str(data["device_name"])
res = rd(devName)
jsonCurrent = {}
jsonCurrent["result"] = res
return json.dumps(jsonCurrent)
def startFlaskServer(f1, f2, f3, f4, f5, f6, f7, f8, f9):
global ecfp, su, tse, rdff, rsd, rd, rtff, wttf, rsff
ecfp = f1
su = f2
tse = f3
rdff = f4
rsd = f5
rd = f6
rtff = f7
wttf = f8
rsff = f9
app.run(host='0.0.0.0', port=5555, debug=True, use_reloader=False, threaded=True)