-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
127 lines (82 loc) · 2.43 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
import ast
from flask import Flask, request
import pymongo
import json
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
app = Flask(__name__)
my_client = pymongo.MongoClient("mongodb://localhost:27017/")
my_db = my_client["local"]
my_collection = my_db["devices4"]
@app.route("/")
def hello():
return "Hello World!"
@app.route('/get_data', methods=['GET'])
def get_data():
if request.method == 'GET':
return_string = ''
for x in my_collection.find():
return_string += str(x) + '\n\n'
return return_string
@app.route('/post_data', methods=['POST'])
def post_data():
if request.method == 'POST':
data = dict(request.form)
for k in data:
data = k
break
data = json.loads(data)
return_string = ''
arr = []
for device in data:
for timestamp in data[device]:
inner_dict = {'device': device, 'timestamp': timestamp}
return_string += device + ', ' + timestamp + ', '
for k in data[device][timestamp]:
if data[device][timestamp][k] and k == 'warning':
return_string += '****' + (str(data[device][timestamp][k])).upper() + '****, '
else:
return_string += str(data[device][timestamp][k]) + ', '
inner_dict[k] = data[device][timestamp][k]
return_string += '\n'
arr.append(inner_dict)
x = my_collection.insert_many(arr)
print(return_string)
return return_string
if __name__ == "__main__":
# app.run()
app.run(host='0.0.0.0')
'''
{
"device1": {
"2019-11-18T23:31:16": {
"topic": "Normal message",
"warning": false,
"data": 1.5e6
},
"2019-11-18T23:42:56": {
"topic": "Normal message",
"warning": false,
"data": 1400000
},
"2019-11-18T23:45:02": {
"topic": "Normal message",
"warning": false,
"data": 1520000
}
},
"device2": {
"2019-11-18T02:00:07": {
"topic": "Backup message",
"warning": false,
"data": 1500000
},
"2019-11-18T03:00:21": {
"topic": "Backup message",
"warning": false,
"data": 1400000
}
}
}
'''