-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
52 lines (41 loc) · 1.69 KB
/
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
from autobahn.asyncio.websocket import WebSocketClientProtocol, \
WebSocketClientFactory
import json
import random
class KommandozentraleClientProtocol(WebSocketClientProtocol):
def onOpen(self):
switch = random.choice(["NonExistingSwitch", "hauptraum/decke_rechts"])
method = random.choice(["on", "off", "getState"])
x = {"action":"call_method", "switch":switch,"method":method}
self.sendMessage(json.dumps(x).encode('utf8'))
if "data" in x:
print('{switch}: {method}({data})'.format(**x))
else:
print('{switch}: {method}()'.format(**x))
x = {"action":"get_config"}
self.sendMessage(json.dumps(x).encode('utf8'))
x = {"action":"get_state", "switch":switch}
self.sendMessage(json.dumps(x).encode('utf8'))
def onMessage(self, payload, isBinary):
if not isBinary:
res = json.loads(payload.decode('utf8'))
if res["result"] == "state":
print("State of {switch}: {state}".format(**res))
elif res["result"] == "config":
print(res)
self.sendClose()
elif res["result"] == "error":
print("An error occured: {0}".format(res["error"]))
else:
print(res)
def onClose(self, wasClean, code, reason):
loop.stop()
if __name__ == '__main__':
import asyncio
factory = WebSocketClientFactory(u"ws://localhost:9000")
factory.protocol = KommandozentraleClientProtocol
loop = asyncio.get_event_loop()
connection = loop.create_connection(factory, 'localhost', 9000)
loop.run_until_complete(connection)
loop.run_forever()
loop.close()