-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflaskapp.py
72 lines (61 loc) · 1.84 KB
/
flaskapp.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
import sys
import os
import StringIO
import signal
import json
from flask import Flask
app = Flask(__name__)
from pymongo import MongoClient
mongo=None
db=None
@app.route('/')
def hello_world():
global mongo
global db
if not db:
mongo=MongoClient('ds011412.mlab.com',11412)
mongo.lora.authenticate("lora_ingester", "lora")
db=mongo["lora"]
deveuis=db.uplinks.find({},{"_id": False, "deveui": True}).distinct("deveui")
output = StringIO.StringIO()
cnt=db.uplinks.find().count()
print >>output, 'Hello World!'+" %d messages"%cnt
for deveui in deveuis:
nr=db.uplinks.find({"deveui": deveui}, {"_id":False, "deveui": True}).count()
last=db.uplinks.find({"deveui":deveui},{"_id":0, "payload_fields":1, "metadata.time": { "$slice": 1}}).sort([("_id", -1)])
if "payload_fields" in last[0]:
print "[[",last[0]["payload_fields"], "]]"
last=last[0]
lastdata=None
lastdata=last["payload_fields"]
# try:
# lastdata=json.loads(last["payload_fields"])
# except ValueError:
# pass
try:
lasttime=last["metadata"]["gateways"][0]["time"]
except KeyError:
lasttime="-"
try:
lastservertime=last["metadata"]["time"]
except KeyError:
lastservertime="-"
print >>output, "<p/>";
print >>output, "<table>"
print >>output, "<tr>";
print >>output, "<td>",deveui,"</td><td>",nr ,"</td>","<td>",lasttime,"</td>"
print >>output, "<td>",lastservertime,"</td>"
if lastdata:
if "t" in lastdata:
print >>output,"<td>",lastdata["t"],"</td>"
else:
print >>output, "</td>"
print >>output, "</tr>";
print >>output, "</table>"
return output.getvalue()
def handler(signum, frame):
print 'Here you go'
if __name__ == '__main__':
# signal.signal(signal.SIGINT, handler)
app.run(host='0.0.0.0', port=8080)
# print hello_world()