-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.py
56 lines (50 loc) · 1.51 KB
/
script.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
import socket
import os
import json
import pprint
import time
import sys
socket_path = 'm1.socket'
def send_request(action, content=None):
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client.connect(os.path.realpath(socket_path))
message = {"action": action}
if content:
message["content"] = content
client.sendall(json.dumps(message).encode())
response = client.recv(2048).decode()
client.close()
print(response)
return json.loads(response)
def subscribe_messages():
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
client.connect(os.path.realpath(socket_path))
print("Subscribed to messages")
client.sendall(json.dumps({"action": "/subscribe_messages"}).encode())
while True:
time.sleep(0.05)
response = client.recv(1024).decode()
if response:
print(response)
def main():
pass
#robot = send_request("/me")
#print('=== /me ===')
#pprint.pprint(robot)
local_devices = send_request("/local_robots")
print('=== /local_robots ===')
pprint.pprint(local_devices)
exit(0)
#subscribe_messages()
#while True:
# inp = sys.stdin.read(1)
# print(inp)
# #res = send_request("/send_message", "w")
# #print(res)
def press(key):
print(key)
res =send_request("/send_message", key)
print(res)
if __name__=='__main__':
main()
subscribe_messages()