Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
blacks all python files
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Oung committed Aug 30, 2021
1 parent 50ca857 commit 996657b
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 180 deletions.
16 changes: 4 additions & 12 deletions pytemi/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@


def _on_connect(client, userdata, flags, rc):
"""Connect to MQTT broker and subscribe to topics
"""
"""Connect to MQTT broker and subscribe to topics"""
print(
"[STATUS] Connected to: {} (rc:{})".format(
client._client_id.decode("ascii"), str(rc)
Expand All @@ -27,9 +25,7 @@ def _on_connect(client, userdata, flags, rc):


def _on_disconnect(client, userdata, rc):
"""Disconnect from MQTT broker
"""
"""Disconnect from MQTT broker"""
print(
"[STATUS] Disconnected from: {} (rc:{})".format(
client._client_id.decode("ascii"), str(rc)
Expand All @@ -39,16 +35,12 @@ def _on_disconnect(client, userdata, rc):


def _on_message(client, userdata, msg):
"""Print out any topics that have no callbacks
"""
"""Print out any topics that have no callbacks"""
print("[{}][SUB] {} {}".format(now(), msg.topic, str(msg.payload)))


def connect(host, port, username=None, password=None):
"""Connect to MQTT broker
"""
"""Connect to MQTT broker"""
client_id = socket.gethostname() + "-" + datetime.now().strftime("%Y%m%d%H%M%S")

# create a new MQTT client instance
Expand Down
115 changes: 47 additions & 68 deletions pytemi/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,76 @@

from datetime import datetime

def now():
"""Return time in string format

"""
def now():
"""Return time in string format"""
return datetime.now().strftime("%H:%M:%S")


def _on_status(client, userdata, msg):
d = json.loads(msg.payload)
userdata['locations'] = d['waypoint_list']
userdata['battery']['percentage'] = d['battery_percentage']
userdata["locations"] = d["waypoint_list"]
userdata["battery"]["percentage"] = d["battery_percentage"]


def _on_battery(client, userdata, msg):
print("[{}] [SUB] [BATTERY] {}".format(now(), str(msg.payload)))
d = json.loads(msg.payload)
userdata['battery']['percentage'] = d['percentage']
userdata['battery']['is_charging'] = d['is_charging']
userdata["battery"]["percentage"] = d["percentage"]
userdata["battery"]["is_charging"] = d["is_charging"]


def _on_goto(client, userdata, msg):
d = json.loads(msg.payload)
userdata['goto']['location'] = d['location']
userdata['goto']['status'] = d['status']
userdata["goto"]["location"] = d["location"]
userdata["goto"]["status"] = d["status"]


def _on_user(client, userdata, msg):
print("[{}] [SUB] [USER] {}".format(now(), str(msg.payload)))
userdata['user'] = json.loads(msg.payload)
userdata["user"] = json.loads(msg.payload)


class Robot:
"""Robot Class
"""
"""Robot Class"""

def __init__(self, mqtt_client, temi_serial, silent=True):
"""Constructor
"""
"""Constructor"""
self.client = mqtt_client
self.id = temi_serial
self.silent = silent

# set user data
self.state = {
'locations': [],
'battery': {},
'goto': {},
'user': {}
}
self.state = {"locations": [], "battery": {}, "goto": {}, "user": {}}
self.client.user_data_set(self.state)

# attach subscription callbacks
self.client.message_callback_add("temi/{}/status/info".format(temi_serial), _on_status)
self.client.message_callback_add("temi/{}/status/utils/battery".format(temi_serial), _on_battery)
self.client.message_callback_add("temi/{}/event/waypoint/goto".format(temi_serial), _on_goto)
self.client.message_callback_add("temi/{}/event/user/detection".format(temi_serial), _on_user)
self.client.message_callback_add(
"temi/{}/status/info".format(temi_serial), _on_status
)
self.client.message_callback_add(
"temi/{}/status/utils/battery".format(temi_serial), _on_battery
)
self.client.message_callback_add(
"temi/{}/event/waypoint/goto".format(temi_serial), _on_goto
)
self.client.message_callback_add(
"temi/{}/event/user/detection".format(temi_serial), _on_user
)

def rotate(self, angle):
"""Rotate
"""
"""Rotate"""
if not self.silent:
print("[CMD] Rotate: {} [deg]".format(angle))

if (angle != 0):
if angle != 0:
topic = "temi/" + self.id + "/command/move/turn_by"
payload = json.dumps({"angle": angle})

self.client.publish(topic, payload, qos=0)

def translate(self, value):
"""Translate
"""
"""Translate"""
if not self.silent:
print("[CMD] Translate: {} [unitless]".format(value))

Expand All @@ -91,9 +88,7 @@ def translate(self, value):
self.client.publish(topic, payload, qos=0)

def tilt(self, angle):
"""Tilt head (absolute angle)
"""
"""Tilt head (absolute angle)"""
if not self.silent:
print("[CMD] Tilt: {} [deg]".format(angle))

Expand All @@ -103,9 +98,7 @@ def tilt(self, angle):
self.client.publish(topic, payload, qos=0)

def tilt_by(self, angle):
"""Tilt head (relative angle)
"""
"""Tilt head (relative angle)"""
if not self.silent:
print("[CMD] Tilt By: {} [deg]".format(angle))

Expand All @@ -115,9 +108,7 @@ def tilt_by(self, angle):
self.client.publish(topic, payload, qos=0)

def stop(self):
"""Stop
"""
"""Stop"""
if not self.silent:
print("[CMD] Stop")

Expand All @@ -126,9 +117,7 @@ def stop(self):
self.client.publish(topic, "{}", qos=1)

def follow(self):
"""Follow
"""
"""Follow"""
if not self.silent:
print("[CMD] Follow")

Expand All @@ -137,9 +126,7 @@ def follow(self):
self.client.publish(topic, "{}", qos=1)

def goto(self, location_name):
"""Go to a saved location
"""
"""Go to a saved location"""
if not self.silent:
print("[CMD] Go-To: {}".format(location_name))

Expand All @@ -149,9 +136,7 @@ def goto(self, location_name):
self.client.publish(topic, payload, qos=1)

def tts(self, text):
"""Text-to-speech
"""
"""Text-to-speech"""
if not self.silent:
print("[CMD] TTS: {}".format(text))

Expand All @@ -172,9 +157,7 @@ def tts(self, text):
# self.client.publish(topic, payload, qos=1)

def video(self, url):
"""Play video
"""
"""Play video"""
if not self.silent:
print("[CMD] Play Video: {}".format(url))

Expand All @@ -196,9 +179,7 @@ def video(self, url):
# self.client.publish(topic, payload, qos=1)

def webview(self, url):
"""Show webview
"""
"""Show webview"""
if not self.silent:
print("[CMD] Show Webview: {}".format(url))

Expand All @@ -209,33 +190,31 @@ def webview(self, url):

@property
def locations(self):
"""Return a list of locations
"""
if 'locations' in self.state:
return self.state['locations']
"""Return a list of locations"""
if "locations" in self.state:
return self.state["locations"]
else:
return []

@property
def goto_status(self):
if 'status' in self.state['goto']:
return self.state['goto']['status']
if "status" in self.state["goto"]:
return self.state["goto"]["status"]
else:
return None

@property
def battery(self):
return self.state['battery']['percentage']
return self.state["battery"]["percentage"]

@property
def GOTO_START(self):
return "start"

@property
def GOTO_ABORT(self):
return "abort"

@property
def GOTO_GOING(self):
return "going"
Expand Down
36 changes: 18 additions & 18 deletions sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,46 @@
# --------------------------------------------------------------
# TEXT-TO-SPEECH COMMANDS
# --------------------------------------------------------------
robot.tts("Hello World!") # command the robot to speak
time.sleep(1) # wait some time for action to complete
robot.tts("Hello World!") # command the robot to speak
time.sleep(1) # wait some time for action to complete


# --------------------------------------------------------------
# WAYPOINT COMMANDS
# --------------------------------------------------------------
robot.goto("entrance") # command the robot to go to a saved location
time.sleep(3) # wait some time for action to complete
robot.goto("entrance") # command the robot to go to a saved location
time.sleep(3) # wait some time for action to complete


# --------------------------------------------------------------
# MOVE COMMANDS
# --------------------------------------------------------------
# robot.tilt(+45) # tilt the robot's head (absolute angle)
# time.sleep(3) # wait some time for action to complete
robot.tilt(+45) # tilt the robot's head (absolute angle)
time.sleep(3) # wait some time for action to complete

# robot.tilt(-15) # tilt the robot's head (absolute angle)
# time.sleep(3) # wait some time for action to complete
robot.tilt(-15) # tilt the robot's head (absolute angle)
time.sleep(3) # wait some time for action to complete

# robot.tilt_by(+30) # tilt the robot's head (relative angle)
# time.sleep(3) # wait some time for action to complete
robot.tilt_by(+30) # tilt the robot's head (relative angle)
time.sleep(3) # wait some time for action to complete

# robot.tilt_by(-10) # tilt the robot's head (relative angle)
# time.sleep(3) # wait some time for action to complete
robot.tilt_by(-10) # tilt the robot's head (relative angle)
time.sleep(3) # wait some time for action to complete

# robot.rotate(90) # rotate the robot (relative angle)
# time.sleep(5) # wait some time for action to complete
robot.rotate(90) # rotate the robot (relative angle)
time.sleep(5) # wait some time for action to complete

# robot.rotate(-30) # rotate the robot (relative angle)
# time.sleep(5) # wait some time for action to complete
robot.rotate(-30) # rotate the robot (relative angle)
time.sleep(5) # wait some time for action to complete


# --------------------------------------------------------------
# MEDIA COMMANDS
# --------------------------------------------------------------
robot.video(
"https://roboteam-assets.s3.eu-central-1.amazonaws.com/ui/skills/tutorials/videos/intorducing+temi.mp4"
) # play online video by passing a URL
time.sleep(30) # wait some time for action to complete
) # play online video by passing a URL; make sure video is optimized for the web (this sample video is not)
time.sleep(30) # wait some time for action to complete

# show webview by passing a URL
robot.webview("https://www.google.com/")
Expand Down
Loading

0 comments on commit 996657b

Please sign in to comment.