-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_client.py
31 lines (28 loc) · 1.06 KB
/
socket_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
import socketio, time
class socket_client:
@staticmethod
def __connect_eventhandler(sio, state, sent):
sio.emit('bot-light', state)
sent["state"] = True
time.sleep(0.2)
sio.disconnect()
@staticmethod
def __connect_error_eventhandler(sio, err):
sio.disconnect()
@staticmethod
def switch_light(server_address, state: int = 0):
sent = {"state":False}
state = min(max(int(state), 0), 1)
sio = socketio.Client()
sio.on("connect", lambda : socket_client.__connect_eventhandler(sio, state, sent))
sio.on("connect_error", lambda err: socket_client.__connect_error_eventhandler(sio, err))
try:
sio.connect(server_address)
sio.wait()
finally:
return sent["state"]
if __name__ == "__main__":
import sys
server_address = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080"
light_state = int(sys.argv[2]) if len(sys.argv) > 2 else 0
socket_client.switch_light(server_address, light_state)