Skip to content

Commit 4c9a6d3

Browse files
simplify dog
1 parent 2de1ed7 commit 4c9a6d3

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

doggo.py

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ def __init__(self, dog, name, description, file):
5757
async def act(self, params):
5858
pass
5959

60+
async def call_robot(self, api_id, params=None):
61+
args = {'api_id': api_id}
62+
if params:
63+
args['parameter'] = params
64+
await self.dog.maybe_reconnect()
65+
await self.dog.robot.datachannel.pub_sub.publish_request_new(
66+
RTC_TOPIC["SPORT_MOD"], args
67+
)
68+
6069
def tool(self):
6170
return Tool(
6271
name=self.name,
@@ -72,76 +81,55 @@ def __init__(self, dog):
7281
super().__init__(dog, "stand_up", "Make the dog stand up", "tools/empty.json")
7382

7483
async def act(self, params):
75-
await self.dog.maybe_reconnect()
76-
await self.dog.robot.datachannel.pub_sub.publish_request_new(
77-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["StandUp"]}
78-
)
84+
await self.call_robot(SPORT_CMD["StandUp"])
7985
return "Doggo is now standing up"
8086

8187
class Damp(Trick):
8288
def __init__(self, dog):
8389
super().__init__(dog, "lie_down", "Make the dog lie down", "tools/empty.json")
8490

8591
async def act(self, params):
86-
await self.dog.maybe_reconnect()
87-
await self.dog.robot.datachannel.pub_sub.publish_request_new(
88-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Damp"]}
89-
)
92+
await self.call_robot(SPORT_CMD["Damp"])
9093
return "Doggo is now damping"
9194

9295
class Hello(Trick):
9396
def __init__(self, dog):
9497
super().__init__(dog, "hello", "Make the dog say hello", "tools/empty.json")
9598

9699
async def act(self, params):
97-
await self.dog.maybe_reconnect()
98-
await self.dog.robot.datachannel.pub_sub.publish_request_new(
99-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Hello"]}
100-
)
100+
await self.call_robot(SPORT_CMD["Hello"])
101101
return "Doggo is now saying hello"
102102

103103
class Move(Trick):
104104
def __init__(self, dog):
105105
super().__init__(dog, "move", "Make the dog move", "tools/move.json")
106106

107107
async def act(self, params):
108-
await self.dog.maybe_reconnect()
109-
await self.dog.robot.datachannel.pub_sub.publish_request_new(
110-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Move"], "parameter": params}
111-
)
108+
await self.call_robot(SPORT_CMD["Move"], params)
112109
return "Doggo is now moving"
113110

114111
class Stop(Trick):
115112
def __init__(self, dog):
116113
super().__init__(dog, "stop", "Make the dog stop", "tools/empty.json")
117114

118115
async def act(self, params):
119-
await self.dog.maybe_reconnect()
120-
await self.dog.robot.datachannel.pub_sub.publish_request_new(
121-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Stop"]}
122-
)
116+
await self.call_robot(SPORT_CMD["Stop"])
123117
return "Doggo is now stopping"
124118

125119
class Dance(Trick):
126120
def __init__(self, dog):
127121
super().__init__(dog, "dance", "Make the dog dance", "tools/empty.json")
128122

129123
async def act(self, params):
130-
await self.dog.maybe_reconnect()
131-
await self.dog.robot.datachannel.pub_sub.publish_request_new(
132-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Dance1"]}
133-
)
124+
await self.call_robot(SPORT_CMD["Dance1"])
134125
return "Doggo is now dancing"
135126

136127
class Jump(Trick):
137128
def __init__(self, dog):
138129
super().__init__(dog, "jump", "Make the dog jump", "tools/empty.json")
139130

140131
async def act(self, params):
141-
await self.dog.maybe_reconnect()
142-
await self.dog.robot.datachannel.pub_sub.publish_request_new(
143-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["FrontJump"]}
144-
)
132+
await self.call_robot(SPORT_CMD["FrontJump"])
145133
return "Doggo is now jumping"
146134

147135
def trick_tools(dog):

0 commit comments

Comments
 (0)