|
@@ -1,11 +1,24 @@
|
|
|
import asyncio
|
|
import asyncio
|
|
|
import websockets
|
|
import websockets
|
|
|
import threading
|
|
import threading
|
|
|
|
|
+import json
|
|
|
|
|
|
|
|
class Websocket(threading.Thread):
|
|
class Websocket(threading.Thread):
|
|
|
- def __init__(self):
|
|
|
|
|
|
|
+ def __init__(self, add_question, handle_diapo, handle_rec, toggle, handle_video):
|
|
|
super(Websocket, self).__init__()
|
|
super(Websocket, self).__init__()
|
|
|
self.start_server = websockets.serve(self.handle_input, '127.0.0.1', 5678)
|
|
self.start_server = websockets.serve(self.handle_input, '127.0.0.1', 5678)
|
|
|
|
|
+ self.callbacks={
|
|
|
|
|
+ "add_preg": add_question,
|
|
|
|
|
+ "diapo": add_question,
|
|
|
|
|
+ "startrec":handle_rec,
|
|
|
|
|
+ "startrectrat":handle_rec,
|
|
|
|
|
+ "stoprec":handle_rec,
|
|
|
|
|
+ "toggle":toggle,
|
|
|
|
|
+ "video":handle_video
|
|
|
|
|
+ }
|
|
|
|
|
+ print(type(self.callbacks))
|
|
|
|
|
+ self.valid_data_types=["add_preg", "diapo", "startrec","startrectrat","stoprec","toggle","video"]
|
|
|
|
|
+
|
|
|
#asyncio.get_event_loop().run_until_complete(start_server)
|
|
#asyncio.get_event_loop().run_until_complete(start_server)
|
|
|
#asyncio.get_event_loop().run_forever()
|
|
#asyncio.get_event_loop().run_forever()
|
|
|
self.loop = asyncio.get_event_loop()
|
|
self.loop = asyncio.get_event_loop()
|
|
@@ -19,7 +32,21 @@ class Websocket(threading.Thread):
|
|
|
while True:
|
|
while True:
|
|
|
try:
|
|
try:
|
|
|
data = await websocket.recv()
|
|
data = await websocket.recv()
|
|
|
- print("{}".format(data))
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ data=json.loads(data)
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(e)
|
|
|
|
|
+ print(data)
|
|
|
|
|
+ print("No pude hacer loads")
|
|
|
|
|
+ continue
|
|
|
|
|
+ print(data)
|
|
|
|
|
+ if not "type" in data:
|
|
|
|
|
+ print("data!= type?")
|
|
|
|
|
+ continue
|
|
|
|
|
+ #add preg, diapo, start|stop rec, switch mode, add video
|
|
|
|
|
+ if data["type"] in self.valid_data_types:
|
|
|
|
|
+ print("{}".format(data))
|
|
|
|
|
+ self.callbacks[data["type"]]()
|
|
|
except websockets.exceptions.ConnectionClosed as c:
|
|
except websockets.exceptions.ConnectionClosed as c:
|
|
|
print( "client dc'd")
|
|
print( "client dc'd")
|
|
|
break
|
|
break
|