def check_connection(): """ Check for network connection. If not paired trigger pairing. Runs as a Timer every second until connection is detected. """ if connected(): enclosure = EnclosureAPI(bus) if is_paired(): # Skip the sync message when unpaired because the prompt to go to # home.mycrof.ai will be displayed by the pairing skill enclosure.mouth_text(dialog.get("message_synching.clock")) # Force a sync of the local clock with the internet config = Configuration.get() platform = config['enclosure'].get("platform", "unknown") if platform in ['mycroft_mark_1', 'picroft']: bus.wait_for_response(Message('system.ntp.sync'), 'system.ntp.sync.complete', 15) if not is_paired(): try_update_system(platform) # Check if the time skewed significantly. If so, reboot skew = abs((time.monotonic() - start_ticks) - (time.time() - start_clock)) if skew > 60 * 60: # Time moved by over an hour in the NTP sync. Force a reboot to # prevent weird things from occcurring due to the 'time warp'. # data = {'utterance': dialog.get("time.changed.reboot")} bus.emit(Message("speak", data)) wait_while_speaking() # provide visual indicators of the reboot enclosure.mouth_text(dialog.get("message_rebooting")) enclosure.eyes_color(70, 65, 69) # soft gray enclosure.eyes_spin() # give the system time to finish processing enclosure messages time.sleep(1.0) # reboot bus.emit(Message("system.reboot")) return else: bus.emit(Message("enclosure.mouth.reset")) time.sleep(0.5) enclosure.eyes_color(189, 183, 107) # dark khaki enclosure.mouth_text(dialog.get("message_loading.skills")) bus.emit(Message('mycroft.internet.connected')) # check for pairing, if not automatically start pairing try: if not is_paired(ignore_errors=False): payload = {'utterances': ["pair my device"], 'lang': "en-us"} bus.emit(Message("recognizer_loop:utterance", payload)) else: from mycroft.api import DeviceApi api = DeviceApi() api.update_version() except BackendDown: data = {'utterance': dialog.get("backend.down")} bus.emit(Message("speak", data)) bus.emit(Message("backend.down")) else: thread = Timer(1, check_connection) thread.daemon = True thread.start()
class DevicePrimer(object): """Container handling the device preparation. Arguments: message_bus_client: Bus client used to interact with the system config (dict): Mycroft configuration """ def __init__(self, message_bus_client, config): self.bus = message_bus_client self.platform = config['enclosure'].get("platform", "unknown") self.enclosure = EnclosureAPI(self.bus) self.is_paired = False self.backend_down = False # Remember "now" at startup. Used to detect clock changes. def prepare_device(self): """Internet dependent updates of various aspects of the device.""" self._get_pairing_status() self._update_system_clock() self._update_system() # Above will block during update process and kill this instance if # new software is installed if self.backend_down: self._notify_backend_down() else: self._display_skill_loading_notification() self.bus.emit(Message('mycroft.internet.connected')) self._ensure_device_is_paired() self._update_device_attributes_on_backend() def _get_pairing_status(self): """Set an instance attribute indicating the device's pairing status""" try: self.is_paired = is_paired(ignore_errors=False) except BackendDown: LOG.error('Cannot complete device updates due to backend issues.') self.backend_down = True if self.is_paired: LOG.info('Device is paired') def _update_system_clock(self): """Force a sync of the local clock with the Network Time Protocol. The NTP sync is only forced on Raspberry Pi based devices. The assumption being that these devices are only running Mycroft services. We don't want to sync the time on a Linux desktop device, for example, because it could have a negative impact on other software running on that device. """ if self.platform in RASPBERRY_PI_PLATFORMS: LOG.info('Updating the system clock via NTP...') if self.is_paired: # Only display time sync message when paired because the prompt # to go to home.mycroft.ai will be displayed by the pairing # skill when pairing self.enclosure.mouth_text(dialog.get("message_synching.clock")) self.bus.wait_for_response(Message('system.ntp.sync'), 'system.ntp.sync.complete', 15) def _notify_backend_down(self): """Notify user of inability to communicate with the backend.""" self._speak_dialog(dialog_id="backend.down") self.bus.emit(Message("backend.down")) def _display_skill_loading_notification(self): """Indicate to the user that skills are being loaded.""" self.enclosure.eyes_color(189, 183, 107) # dark khaki self.enclosure.mouth_text(dialog.get("message_loading.skills")) def _ensure_device_is_paired(self): """Determine if device is paired, if not automatically start pairing. Pairing cannot be performed if there is no connection to the back end. So skip pairing if the backend is down. """ if not self.is_paired and not self.backend_down: LOG.info('Device not paired, invoking the pairing skill') payload = dict(utterances=["pair my device"], lang="en-us") self.bus.emit(Message("recognizer_loop:utterance", payload)) def _update_device_attributes_on_backend(self): """Communicate version information to the backend. The backend tracks core version, enclosure version, platform build and platform name for each device, if it is known. """ if self.is_paired: LOG.info('Sending updated device attributes to the backend...') try: api = DeviceApi() api.update_version() except Exception: self._notify_backend_down() def _update_system(self): """Emit an update event that will be handled by the admin service.""" if not self.is_paired: LOG.info('Attempting system update...') self.bus.emit(Message('system.update')) msg = Message('system.update', dict(paired=self.is_paired, platform=self.platform)) resp = self.bus.wait_for_response(msg, 'system.update.processing') if resp and (resp.data or {}).get('processing', True): self.bus.wait_for_response(Message('system.update.waiting'), 'system.update.complete', 1000) def _speak_dialog(self, dialog_id, wait=False): data = {'utterance': dialog.get(dialog_id)} self.bus.emit(Message("speak", data)) if wait: wait_while_speaking()
def check_connection(): """ Check for network connection. If not paired trigger pairing. Runs as a Timer every second until connection is detected. """ if connected(): enclosure = EnclosureAPI(bus) if is_paired(): # Skip the sync message when unpaired because the prompt to go to # home.mycrof.ai will be displayed by the pairing skill enclosure.mouth_text(dialog.get("message_synching.clock")) # Force a sync of the local clock with the internet config = Configuration.get() platform = config['enclosure'].get("platform", "unknown") if platform in ['mycroft_mark_1', 'picroft']: bus.emit(Message("system.ntp.sync")) time.sleep(15) # TODO: Generate/listen for a message response... # Check if the time skewed significantly. If so, reboot skew = abs((time.monotonic() - start_ticks) - (time.time() - start_clock)) if skew > 60 * 60: # Time moved by over an hour in the NTP sync. Force a reboot to # prevent weird things from occcurring due to the 'time warp'. # data = {'utterance': dialog.get("time.changed.reboot")} bus.emit(Message("speak", data)) wait_while_speaking() # provide visual indicators of the reboot enclosure.mouth_text(dialog.get("message_rebooting")) enclosure.eyes_color(70, 65, 69) # soft gray enclosure.eyes_spin() # give the system time to finish processing enclosure messages time.sleep(1.0) # reboot bus.emit(Message("system.reboot")) return else: bus.emit(Message("enclosure.mouth.reset")) time.sleep(0.5) bus.emit(Message('mycroft.internet.connected')) # check for pairing, if not automatically start pairing try: if not is_paired(ignore_errors=False): payload = { 'utterances': ["pair my device"], 'lang': "en-us" } bus.emit(Message("recognizer_loop:utterance", payload)) else: from mycroft.api import DeviceApi api = DeviceApi() api.update_version() except BackendDown: data = {'utterance': dialog.get("backend.down")} bus.emit(Message("speak", data)) bus.emit(Message("backend.down")) else: thread = Timer(1, check_connection) thread.daemon = True thread.start()