Skip to content
This repository was archived by the owner on Sep 8, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions mycroft/client/enclosure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def process(self, data):
self.ws.emit(Message(data))

if "Command: system.version" in data:
self.ws.emit(Message("enclosure.start"))
# This happens in response to the "system.version" message
# sent during the construction of Enclosure()
self.ws.emit(Message("enclosure.started"))

if "mycroft.stop" in data:
create_signal('buttonPress')
Expand Down Expand Up @@ -224,29 +226,55 @@ def __init__(self):
self.__init_serial()
self.reader = EnclosureReader(self.serial, self.ws)
self.writer = EnclosureWriter(self.serial, self.ws)

# Send a message to the Arduino across the serial line asking
# for a reply with version info.
self.writer.write("system.version")
self.ws.on("enclosure.start", self.start)
self.started = False
Timer(5, self.stop).start() # WHY? This at least
# needs an explanation, this is non-obvious behavior
# When the Arduino responds, it will generate this message
self.ws.on("enclosure.started", self.on_arduino_responded)

self.arduino_responded = False

# Start a 5 second timer. If the serial port hasn't received
# any acknowledgement of the "system.version" within those
# 5 seconds, assume there is nothing on the other end (e.g.
# we aren't running a Mark 1 with an Arduino)
Timer(5, self.check_for_response).start()

# Notifications from mycroft-core
self.ws.on("enclosure.notify.no_internet", self.on_no_internet)
self.last_internet_notification = 0

def start(self, event=None):
def on_arduino_responded(self, event=None):
self.eyes = EnclosureEyes(self.ws, self.writer)
self.mouth = EnclosureMouth(self.ws, self.writer)
self.system = EnclosureArduino(self.ws, self.writer)
self.weather = EnclosureWeather(self.ws, self.writer)
self.__register_events()
self.__reset()
self.started = True
self.arduino_responded = True

# verify internet connection and prompt user on bootup if needed
if not connected():
self.on_no_internet()
# We delay this for several seconds to ensure that the other
# clients are up and connected to the messagebus in order to
# receive the "speak". This was sometimes happening too
# quickly and the user wasn't notified what to do.
Timer(5, self.on_no_internet).start()

def on_no_internet(self, event=None):
# TODO: This should go into EnclosureMark1 subclass of Enclosure
if connected():
# One last check to see if connection was established
return

if time.time()-self.last_internet_notification < 30:
# don't bother the user with multiple notifications with 30 secs
return

self.last_internet_notification = time.time()

# TODO: This should go into EnclosureMark1 subclass of Enclosure.
# Handle the translation within that code.
self.ws.emit(Message("speak", {
'utterance': "This device is not connected to the Internet. "
"Either plug in a network cable or hold the button "
Expand Down Expand Up @@ -305,8 +333,10 @@ def run(self):
LOG.error("Error: {0}".format(e))
self.stop()

def stop(self):
if not self.started:
def check_for_response(self):
if not self.arduino_responded:
# There is nothing on the other end of the serial port
# close these serial-port readers and this process
self.writer.stop()
self.reader.stop()
self.serial.close()
Expand Down