def initialize_configuration(self, maker_id): Logger.info(LOCATION, 'Initializing configuration...') public_key, private_key = KeyGenerator().generate_key() device_id = self.key_generator.generate_uuid() #initialize the alternative id. aid = 0 # Option for Multi pairing # If the option is yes, then alternative id needed print('Enable Multi pair(yes/no)') status = input() if (status == 'yes'): device_status = DeviceStatus.MULTIPAIR.value print('Enter your alternativeID:') aid = input() else: device_status = DeviceStatus.NEW.value print('Enable Bluetooth (yes/no; default is yes)') bluetooth = input() if bluetooth == 'no': bluetooth_enabled = False else: bluetooth_enabled = True # Added alternative id as an argument to initializing the configuration self.configuration.initialize(maker_id, device_id, device_status, bluetooth_enabled, aid, public_key, private_key) self.configuration_store.save(self.configuration) self.generate_qr_code() Logger.success(LOCATION, 'Configuration successfully initialized.')
def generate_qr_code(self): Logger.info(LOCATION, 'Generating QR Code for alternative pairing...') device_information = self.configuration.get_device_information() image = qrcode.make(json.dumps(device_information), image_factory=PymagingImage) Store.save_qrcode(image) Logger.success(LOCATION, 'QR Code successfully generated')
def activate(self): try: self.bot_service.post(RESOURCE, {DEVICE_ID: self.device_id}) Logger.success(LOCATION, 'Device successfully activated. Triggering actions enabled.') return True except: Logger.error(LOCATION, 'Failed to activate device.') return False
def _get_action(self, action_id): actions = self.get_actions() for action in actions: if action[ACTION_ID] == action_id: Logger.success(LOCATION, 'Action found') return action Logger.error(LOCATION, 'Action not found') raise falcon.HTTPNotFound(description='Action not found')
def initialize_configuration(self, maker_id): Logger.info(LOCATION, 'Initializing configuration...') public_key, private_key = KeyGenerator().generate_key() device_id = self.key_generator.generate_uuid() device_status = DeviceStatus.NEW.value self.configuration.initialize(maker_id, device_id, device_status, public_key, private_key) self.configuration_store.save(self.configuration) self.generate_qr_code() Logger.success(LOCATION, 'Configuration successfully initialized.')
def pair(self): try: response = self.bot_service.get(RESOURCE) except: Logger.error(LOCATION, 'Failed pairing attempt.') return False if response['status'] is True: Logger.success(LOCATION, 'Device successfully paired.') return True else: Logger.error(LOCATION, 'Failed pairing attempt.') return False
def get_actions(self): Logger.info(LOCATION, 'Retrieving actions...') try: actions = self.bot_service.get(ACTIONS_ENDPOINT) Logger.success(LOCATION, 'Successfully retrieved ' + str(len(actions)) + ' action(s) from server') Store.set_actions(actions) return actions except falcon.HTTPServiceUnavailable: Logger.warning(LOCATION, 'Unable to retrieve actions from server. Loading locally stored action(s)...') actions = self.store.get_actions() Logger.success(LOCATION, 'Successfully loaded ' + str(len(actions)) + ' cached action(s)') return actions
def remove_configuration(): try: if Store.has_configuration(): os.remove(_configuration_file_path) os.remove(_qr_image_path) Logger.success(LOCATION, 'Successfully removed configuration file') else: Logger.warning(LOCATION, 'Could not reset, no configuration available') except IOError as io_error: Logger.error(LOCATION, io_error.message) raise io_error
def pair(self): try: response = self.bot_service.get(RESOURCE) Logger.info(LOCATION, 'Pairing Response: ' + str(response)) # TODO : Make exception more specific except: Logger.error(LOCATION, 'Failed pairing attempt.') return False if response['status'] is True: Logger.success(LOCATION, 'Device successfully paired.') return True else: Logger.error(LOCATION, 'Failed pairing attempt.') return False
def trigger(self, action_id, value=None): Logger.info(LOCATION, 'Triggering action: ' + action_id) action = self._get_action(action_id) self._validate_frequency(action) Logger.success(LOCATION, 'Action valid') data = self.create_trigger_body(action_id, value) try: self.bot_service.post(ACTIONS_ENDPOINT, data) Logger.success(LOCATION, 'Successfully triggered action: ' + action_id) self.store.set_last_triggered(action_id, time.time()) return True except: Logger.error(LOCATION, 'Unable to trigger action: ' + action_id) return False
def remove_configuration(): try: if Store.has_configuration(): os.remove(_configuration_file_path) if os.path.isfile(_qr_image_path): os.remove(_qr_image_path) if os.path.isfile(_saved_actions_path): os.remove(_saved_actions_path) if os.path.isfile(_last_triggered_path): os.remove(_last_triggered_path) Logger.success(LOCATION, 'Successfully reset device configuration') else: Logger.warning(LOCATION, 'Could not reset, no configuration available') except IOError as io_error: Logger.error(LOCATION, io_error.message) raise io_error