-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.py
More file actions
105 lines (84 loc) · 2.83 KB
/
client.py
File metadata and controls
105 lines (84 loc) · 2.83 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import asyncio
import websockets
from websockets.exceptions import ConnectionClosedOK
import json
import asyncio
import nest_asyncio
from dandy.common import *
from dandy.dandynodes import *
from dandy.image import make_b64image
async def send_data_async(data):
try:
url = f"ws://localhost:{DANDY_WS_PORT}"
async with websockets.connect(url, max_size=MAX_DANDY_SOCKET_MSG) as websocket:
dandy_token = dandy_token_store.token
# print('DandyServicesClient :: get_service_id ')
get_service_id_msg = json.dumps({
"command": "get_service_id",
"dandy_token": dandy_token
})
await websocket.send(get_service_id_msg)
response = await websocket.recv()
# print('DandyServicesClient :: get_service_id response: ' + response)
o = json.loads(response)
pyc = o['service_id']
out_data = {
'py_client': pyc,
'dandy_token': dandy_token,
**data
}
s = json.dumps(out_data)
# print('DandyServicesClient :: send_message(): ' + s[:200])
await websocket.send(s)
response = await websocket.recv()
# print('DandyServicesClient :: response: ' + response[:200])
return json.loads(response)
except ConnectionClosedOK:
print("DandyServicesClient :: websocket closed")
abort_abort_abort()
class DandyServicesClient:
def __init__(self):
pass
def send_data(self, data):
nest_asyncio.apply()
async def f():
return await send_data_async(data)
return asyncio.get_event_loop().run_until_complete(f())
def request_captures(self, js_client,
int, float, boolean, string,
positive, negative, b64images, b64masks):
x = {
'command': 'request_captures',
'js_client': js_client,
'int': int,
'float': float,
'boolean': boolean,
'string': string,
'positive': positive,
'negative': negative,
'image': b64images,
'mask': b64masks
}
#print('DandyClient, sending: ' + str(x))
o = self.send_data(x)
if (o['command'] == 'delivering_captures'):
return o
return None
def request_string(self, js_client):
# print("DandyServicesClient :: request_prompt")
o = self.send_data({
'command': 'request_string',
'js_client': js_client
})
if (o['command'] == 'delivering_string'):
# print("DandyServicesClient :: delivering_prompt")
return o['string']
print("DandyServicesClient :: NO PROMPT")
return None
def send_input(self, js_client, input):
# print("DandyServicesClient :: send_inputs")
return self.send_data({
'command': 'sending_input',
'js_client': js_client,
'input': input
})