-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_client.py
74 lines (70 loc) · 2.06 KB
/
ws_client.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
import asyncio
import websockets
import transport_pb2 as transport
from google.protobuf.json_format import MessageToJson
test_str="{\
name: 'Mic Pod',\
count: 2,\
maxCount: 7,\
img: 'assets/images/devices/pod.png',\
thumb: 'assets/images/dashboard/devices/mic-pod-skus-289.png',\
devices: [\
{\
id: '84938463',\
status: 'ok',\
connected: true,\
firmware: '6.3.7',\
lastUpdate: '2018-06-15T19:27:46.617Z',\
settings: {\
volume: getSlider(50),\
treble: getSlider(50),\
balance: getSlider(50),\
bass: getSlider(50),\
fader: getSlider(50),\
},\
},\
{\
id: '483729743',\
status: 'ok',\
connected: false,\
firmware: '1.01.1',\
lastUpdate: '2018-06-15T19:27:46.617Z',\
settings: {\
volume: getSlider(30),\
treble: getSlider(20),\
balance: getSlider(60),\
bass: getSlider(30),\
fader: getSlider(20),\
},\
},\
],\
}"
def parse_protobuf(msg):
msg_as_list = list(msg)
if msg_as_list[0] == 2:
print("This is a response messsage")
del msg_as_list[0]
response = transport.ResponseOK()
response.ParseFromString(bytes(msg_as_list))
print(response)
async def hello():
async with websockets.connect(
'ws://localhost:8765') as websocket:
while True:
await asyncio.sleep(1)
message=[0, 10, 14, 10, 9, 9, 0, 144, 105, 105, 166, 74, 118, 66, 18, 1, 49, 26, 2, 10, 0]
ba=bytes(message)
print(ba)
await websocket.send(ba)
print(f"> {ba}")
response = await websocket.recv()
parse_protobuf(response)
loop = asyncio.get_event_loop()
try:
asyncio.ensure_future(hello())
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
print("Closing Loop")
loop.close()