-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
251 lines (215 loc) · 6.75 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import sys
import time
import socket
import json
import re
START_INITIALIZATAION = 'START-INITIALIZATION'
START_MISSION = 'START-MISSION'
INITIALIZING = 'INITIALIZING'
UNINITIALIZED = 'UNINITIALIZED'
WAITING = 'WAITING'
ACCEPTED = 'ACCEPTED'
place_count = 0
mission_parameter = {
"metadata": {
"container_id": None,
"item_ids": None
},
"item": {
"dimensions": None,
"weight": None
},
"pick": {
"location": {
"zone": None,
"container": {
"width": None,
"height": None,
"depth": None,
"subdivisions": {
"width": None,
"height": None
}
},
"position": {
"x": None,
"y": None,
"index": None
}
},
"qty": None
},
"identify": {
"location": {
"zone": None,
"container": {
"width": None,
"height": None,
"depth": None,
"subdivisions": {
"width": None,
"height": None
}
},
"position": {
"x": None,
"y": None,
"index": None
}
},
"pause": False
},
"place": {
"locations": [
{
"zone": None,
"container": {
"width": None,
"height": None,
"depth": None,
"subdivisions": {
"width": None,
"height": None
}
},
"position": {
"x": None,
"y": None,
"index": None
}
}
]
}
}
class MySocket:
"""demonstration class only
- coded for clarity, not efficiency
"""
def __init__(self, sock=None):
if sock is None:
self.sock = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
self.connect('localhost', 2687)
def connect(self, host, port):
self.sock.connect((host, port))
def mysend(self, msg):
sent = self.sock.send(msg + b'\n')
if sent == 0:
raise RuntimeError("socket connection broken")
def myreceive(self):
buffer = []
while True:
try:
reply = self.sock.recv(1)
if not reply:
break
if b'\n' == reply:
break
buffer.append(reply.decode())
except Exception as e:
print(e)
return buffer
def check_command_status(receive=None):
if receive is not None:
try:
return json.loads(receive[0])['command']['status']
except Exception as e:
print(e)
def check_system_status(receive=None):
if receive is not None:
try:
return json.loads(receive[0])['system']['status']
except Exception as e:
print(e)
def parse_parameter(param=None):
for argument in param:
sub_arguments = re.search('\((.*)\)', argument).group(1)
sub_sub_arguments = sub_arguments.split(',')
parent_key = argument[:argument.index("(")]
recursive_dict(mission_parameter[parent_key], sub_sub_arguments, 2)
def recursive_dict(param=None, sub_arguments=None, level=2):
global place_count
for k, v in param.items():
if type(v) == dict:
if level < 3:
level += 1
param[k] = recursive_dict(v, sub_arguments, level)
else:
param[k] = container_value_to_json(find_key(sub_arguments, k), k)
elif isinstance(v, list) == True and k == 'locations':
if level < 3:
level += 1
if place_count == len(param[k]):
param[k].append(recursive_dict(v[0], sub_arguments, level))
else:
param[k][place_count] = recursive_dict(v[0], sub_arguments, level)
place_count += 1
else:
param[k][place_count] = container_value_to_json(find_key(sub_arguments, k), k)
elif find_key(sub_arguments, k) != False:
param[k] = find_key(sub_arguments, k)
return param
def find_key(arguments=None, key=None):
for argument in arguments:
lists = argument.split('=')
if lists[0] == key:
if key == 'item_ids' or key == 'dimensions':
return [int(x) for x in lists[1].split('|')]
elif key == 'container' or key == 'position':
return lists[1]
elif key == 'pause':
return bool(int(lists[1]))
return int(lists[1])
return False
def container_value_to_json(data=None, key=None):
values = data.split('|')
if len(values) > 0 and key == 'container':
json = {}
json['width'] = int(re.search('\d+', values[0]).group())
if len(values) >= 2:
json['height'] = int(re.search('\d+', values[1]).group())
if len(values) >= 3:
json['depth'] = int(re.search('\d+', values[2]).group())
if len(values) >= 4:
json['subdivisions'] = {}
json['subdivisions']['width'] = int(re.search('\d+', values[3]).group())
if len(values) >= 5:
json['subdivisions']['height'] = int(re.search('\d+', values[4]).group())
return json
if len(values) > 0 and key == 'position':
json = {}
json['x'] = int(re.search('\d+', values[0]).group())
if len(values) >= 2:
json['y'] = int(re.search('\d+', values[1]).group())
if len(values) >= 3:
json['index'] = int(re.search('\d+', values[2]).group())
return json
if len(values) > 0 and key == 'item_ids':
return values
return None
if __name__ == "__main__":
socket = MySocket()
while True:
command = input(">> ")
arguments = command.split(' ')
if len(arguments) == 1 and arguments[0] == START_INITIALIZATAION:
receive = socket.myreceive()
print(''.join(receive))
if check_command_status(''.join(receive).split('STATUS-UPDATE')[1:]) == WAITING\
and check_system_status(''.join(receive).split('STATUS-UPDATE')[1:]) == UNINITIALIZED:
socket.mysend(command.encode())
time.sleep(5)
elif arguments[0] == START_MISSION:
while True:
receive = socket.myreceive()
print(''.join(receive))
if check_command_status(''.join(receive).split('STATUS-UPDATE')[1:]) == ACCEPTED \
and check_system_status(''.join(receive).split('STATUS-UPDATE')[1:]) == WAITING:
parse_parameter(arguments[1:])
socket.mysend(b'START-MISSION ' + json.dumps(mission_parameter).encode())
time.sleep(5)
break
else:
print('Invalid command')