Skip to content

Commit 14ee4d7

Browse files
committed
feat: robot character target validation
1 parent 39d5863 commit 14ee4d7

3 files changed

Lines changed: 55 additions & 26 deletions

File tree

butter/mas/clients/client.py

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def __init__(self, ip, port=3000, protocol='http'):
1515
protocol (str, optional): communication protocol. Defaults to "http".
1616
"""
1717
self._timeout = 40
18+
self._target = PacketBuilder.ANY_TARGET
19+
1820
self.ip = ip
1921
self.port = port
2022
self.protocol = protocol
@@ -43,6 +45,30 @@ def timeout(self, timeout):
4345

4446
self._timeout = timeout
4547

48+
@property
49+
def target(self):
50+
"""Get robot character target
51+
52+
Returns:
53+
integer: robot character target
54+
"""
55+
return self._target
56+
57+
@target.setter
58+
def target(self, target):
59+
"""Set robot character target
60+
61+
Args:
62+
target (str): robot character target
63+
64+
Raises:
65+
ValueError: if target is not a non empty string
66+
"""
67+
if (isinstance(target, str) or target.length == 0):
68+
raise ValueError("Robot character target most be a non empty string")
69+
70+
self._target = target
71+
4672
def assertLinkQuality(self, clientIp) -> Response:
4773
"""Validate robot connection and assert link quality
4874
This validation assets minimal lower bound link quality, and do not take worst case scenarios into account
@@ -54,7 +80,7 @@ def assertLinkQuality(self, clientIp) -> Response:
5480
Returns:
5581
Response: whether this machine is reachable within the defined link parameter
5682
"""
57-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
83+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
5884
.addCommand('network') \
5985
.addParameter('ping') \
6086
.addKeyValuePair('ip', clientIp) \
@@ -68,7 +94,7 @@ def getAvailableHandlers(self) -> Response:
6894
Returns:
6995
Response: response containing all the available robot handlers
7096
"""
71-
packet = PacketBuilder(self.ip, self.port, self.protocol).addCommand('list').build()
97+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol).addCommand('list').build()
7298

7399
return packet.send(self._timeout)
74100

@@ -81,7 +107,7 @@ def getAvailableAnimations(self, reload=False) -> Response:
81107
Returns:
82108
Response: response containing all the available (loaded) robot animations
83109
"""
84-
builder = PacketBuilder(self.ip, self.port, self.protocol).addCommand('animate')
110+
builder = PacketBuilder(self.ip, self.port, self.target, self.protocol).addCommand('animate')
85111

86112
if reload:
87113
builder.addParameter('reload')
@@ -99,7 +125,7 @@ def getAvailableSounds(self, reload=False) -> Response:
99125
Returns:
100126
Response: response containing all the available (loaded) robot sound assets
101127
"""
102-
builder = PacketBuilder(self.ip, self.port, self.protocol).addCommand('audio')
128+
builder = PacketBuilder(self.ip, self.port, self.target, self.protocol).addCommand('audio')
103129

104130
if reload:
105131
builder.addParameter('reload')
@@ -118,7 +144,7 @@ def getAvailableMotorRegisters(self, motorName, readableOnly=False) -> Response:
118144
Returns:
119145
Response: response containing all the available motor registers
120146
"""
121-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
147+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
122148
.addCommand('dxl') \
123149
.addArguments('get', motorName) \
124150
.addParameter('list') \
@@ -137,7 +163,7 @@ def getMotorRegister(self, motorName, registerName) -> Response:
137163
Returns:
138164
Response: response containing register value
139165
"""
140-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
166+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
141167
.addCommand('dxl') \
142168
.addArguments('get', motorName, registerName) \
143169
.build()
@@ -154,7 +180,7 @@ def getMotorRegisterRange(self, motorName, registerName) -> Response:
154180
Returns:
155181
Response: response containing register range value
156182
"""
157-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
183+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
158184
.addCommand('dxl') \
159185
.addArguments('get', motorName, registerName) \
160186
.addParameter('range') \
@@ -173,7 +199,7 @@ def setMotorRegister(self, motorName, registerName, value) -> Response:
173199
Returns:
174200
Response: response containing execution result
175201
"""
176-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
202+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
177203
.addCommand('dxl') \
178204
.addArguments('set', motorName, registerName, value) \
179205
.build()
@@ -193,7 +219,7 @@ def moveMotorToPosition(self, motorName, position, velocity=None, acceleration=N
193219
Returns:
194220
Response: response containing execution result
195221
"""
196-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
222+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
197223
.addCommand('move').addArguments(motorName, position) \
198224
.addKeyValuePair('velocity', velocity) \
199225
.addKeyValuePair('acceleration', acceleration) \
@@ -214,7 +240,7 @@ def moveMotorInTime(self, motorName, position, duration, units=RotationUnits.RAD
214240
Returns:
215241
Response: response containing execution result
216242
"""
217-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
243+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
218244
.addCommand('move') \
219245
.addArguments(motorName, position) \
220246
.addKeyValuePair('duration', str(duration)) \
@@ -236,7 +262,7 @@ def moveMotorInDirection(self, motorName, direction, velocity=None, units=Rotati
236262
Response: response containing execution result
237263
"""
238264
direction_code = -1 if direction.lower() == 'left' else 1 if direction.lower() == 'right' else 0
239-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
265+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
240266
.addCommand('move') \
241267
.addArguments(motorName, direction_code) \
242268
.addKeyValuePair('velocity', velocity) \
@@ -261,7 +287,7 @@ def moveMotorInDirection(self, motorName, direction, velocity=None, units=Rotati
261287
# Response: response containing execution result
262288
# """
263289
# direction_code = -1 if direction.lower() == 'left' else 1 if direction.lower() == 'right' else 0
264-
# packet = PacketBuilder(self.ip, self.port, self.protocol) \
290+
# packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
265291
# .addCommand('move') \
266292
# .addArguments(motorName, direction_code) \
267293
# .addKeyValuePair('steps', steps) \
@@ -296,7 +322,7 @@ def playAnimation(self, animationName, lenient=False, relative=False) -> Respons
296322
Returns:
297323
Response: response containing execution result
298324
"""
299-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
325+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
300326
.addCommand('animate') \
301327
.addArgument(animationName) \
302328
.addKeyValuePair("lenient", lenient) \
@@ -314,13 +340,13 @@ def observeAnimation(self, animationName=None) -> Response:
314340
Returns:
315341
Response: response containing execution result
316342
"""
317-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
343+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
318344
.addCommand('animate') \
319345
.addArgument(animationName) \
320346
.addParameter('status') \
321347
.build() \
322348
if animationName is not None else \
323-
PacketBuilder(self.ip, self.port, self.protocol) \
349+
PacketBuilder(self.ip, self.port, self.target, self.protocol) \
324350
.addCommand('animate') \
325351
.addParameter('status') \
326352
.build()
@@ -333,7 +359,7 @@ def pauseAnimation(self) -> Response:
333359
Returns:
334360
Response: response containing execution result
335361
"""
336-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
362+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
337363
.addCommand('animate') \
338364
.addParameter('pause') \
339365
.build()
@@ -346,7 +372,7 @@ def resumeAnimation(self) -> Response:
346372
Returns:
347373
Response: response containing execution result
348374
"""
349-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
375+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
350376
.addCommand('animate') \
351377
.addParameter('resume') \
352378
.build()
@@ -359,7 +385,7 @@ def stopAnimation(self) -> Response:
359385
Returns:
360386
Response: response containing execution result
361387
"""
362-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
388+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
363389
.addCommand('animate') \
364390
.addParameter('stop') \
365391
.build()
@@ -372,7 +398,7 @@ def clearAnimation(self) -> Response:
372398
Returns:
373399
Response: response containing execution result
374400
"""
375-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
401+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
376402
.addCommand('animate') \
377403
.addParameter('clear') \
378404
.build()
@@ -388,7 +414,7 @@ def playAudio(self, fileName) -> Response:
388414
Returns:
389415
Response: response containing execution result
390416
"""
391-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
417+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
392418
.addCommand('audio') \
393419
.addArgument(fileName) \
394420
.build()
@@ -401,7 +427,7 @@ def pauseAudio(self) -> Response:
401427
Returns:
402428
Response: response containing execution result
403429
"""
404-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
430+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
405431
.addCommand('audio') \
406432
.addParameter('pause') \
407433
.build()
@@ -414,7 +440,7 @@ def resumeAudio(self) -> Response:
414440
Returns:
415441
Response: response containing execution result
416442
"""
417-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
443+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
418444
.addCommand('audio') \
419445
.addParameter('resume') \
420446
.build()
@@ -427,7 +453,7 @@ def stopAudio(self) -> Response:
427453
Returns:
428454
Response: response containing execution result
429455
"""
430-
packet = PacketBuilder(self.ip, self.port, self.protocol) \
456+
packet = PacketBuilder(self.ip, self.port, self.target, self.protocol) \
431457
.addCommand('audio') \
432458
.addParameter('stop') \
433459
.build()

butter/mas/environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = '2.3.0'
1+
__version__ = '2.5.0'
22
app_name = 'butter.mas-api'

butter/mas/packets/packet_builder.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44

55
class PacketBuilder:
66
""" Builds a command packet using the builder design pattern """
7+
ANY_TARGET = '*'
78

8-
def __init__(self, ip, port, protocol="http"):
9+
def __init__(self, ip, port, target, protocol="http"):
910
"""Initialize builder
1011
1112
Args:
1213
ip (str): robot IP
1314
port (int): robot port
15+
target (string): robot character target
1416
protocol (str, optional): communication protocol. defaults to "http".
1517
"""
1618
self.ip = ip
1719
self.port = port
20+
self.target = target
1821

1922
packetFactory = PacketFactory()
2023
self.packet = packetFactory.getPacketClass(protocol)
@@ -141,7 +144,7 @@ def build(self) -> Packet:
141144
keys = list(map(lambda key: "%s=%s" % (key, self.keys[key]), self.keys))
142145
query = "%s%s" % (query, '&'.join(keys))
143146

144-
uri = '/'.join(['api', 'robots', 'any', 'command'])
147+
uri = '/'.join(['api', 'robots', self.target, 'command'])
145148
uri = "%s/%s" % (uri, query.strip('&'))
146149

147150
return self.packet(self.ip, self.port, uri)

0 commit comments

Comments
 (0)