forked from developius/piometer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
48 lines (37 loc) · 1.21 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
#!/usr/bin/python3
# import the necessary components
from flask import Flask, request, jsonify
from servo_move import pushToServos
app = Flask(__name__)
# define a dictionary to store our information in
info = {}
# listen for data at /data
@app.route("/data", methods=["GET", "POST"])
def api():
# convert the data to a dict
data = request.get_json(silent=True)
# pretty debug
print("%s sent: %s" % (data['uuid'], data['stats']))
# replace the old data with the new data
info[data['uuid']] = {'cpu': data['stats']['cpu'], 'mem': data['stats']['memory']}
# keep count of the CPU and memory across all devices
totalCpu = 0
totalMem = 0
# loop through all devices
for key, stats in info.items():
# add the CPU and memory to their respective counters
totalCpu += stats['cpu']
totalMem += stats['mem']
# calculate means
meanCpu = totalCpu / len(info)
meanMem = totalMem / len(info)
# more nice debugging
print("Mean CPU: %s" % meanCpu)
print("Mean memory: %s" % meanMem)
pushToServos(meanCpu, meanMem)
# tell the client that all is well
return(jsonify({"message": "ok"}))
# if file is called directly...
if __name__ == "__main__":
# ...start the server on port 8742
app.run(port=8742, host="0.0.0.0")