-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_mongo.py
47 lines (42 loc) · 1.31 KB
/
websocket_mongo.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
import asyncio
import websockets
import json
import signal
import sys
import mongo_handler as mdb
def signal_handler(signal, frame):
print('You pressed Ctrl+C!')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
async def request_handler(websocket, path):
async for message in websocket:
if(path == "/measures"):
request = json.loads(message)
r_type = request["request"]["type"]
if(r_type == "update"):
print("update")
elif(r_type == "nodesinfo"):
response = mdb.get_nodesinfo()
elif(r_type == "kill"):
print("kill")
sys.exit(0)
else:
print("unsupported command: ", r_type)
response = {
"id": 1,
"complete":{
"14":{
"temperature":{
"Times":[0],
"Values":[33]
}
}
}
}
await websocket.send(message)
config = json.load(open('config.json'))
print("Entering webscoket loop")
asyncio.get_event_loop().run_until_complete(
websockets.serve(request_handler,config["websocket"]["url"],config["websocket"]["port"]))
asyncio.get_event_loop().run_forever()