class ConfigurationService: def __init__(self): self.configuration_store = ConfigurationStore() self.configuration = self.configuration_store.get() self.key_generator = KeyGenerator() 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() def generate_qr_code(self): try: device_information = self.configuration.get_device_information() qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) qr.add_data(json.dumps(device_information)) qr.print_ascii(invert=True) except Exception as exception: raise exception
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 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()
class ConfigurationService: def __init__(self): self.configuration_store = ConfigurationStore() self.configuration = self.configuration_store.get() self.key_generator = KeyGenerator() 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 resume_configuration(self): device_status = self.configuration.get_device_status() Logger.info(LOCATION, 'DeviceStatus = ' + device_status.value) if device_status == DeviceStatus.NEW: self.pair() if device_status == DeviceStatus.PAIRED: self.activate() def pair(self): success = PairingService().run() if success: self.configuration.set_device_status(DeviceStatus.PAIRED.value) self.configuration_store.save(self.configuration) self.activate() def activate(self): success = ActivationService().run() if success: self.configuration.set_device_status(DeviceStatus.ACTIVE.value) self.configuration_store.save(self.configuration) def generate_qr_code(self): try: 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') except Exception as exception: Logger.error(LOCATION, 'QR Code not generated') raise exception
def __init__(self): self.configuration_store = ConfigurationStore() self.configuration = self.configuration_store.get() self.key_generator = KeyGenerator()
class ConfigurationService: def __init__(self): self.configuration_store = ConfigurationStore() self.configuration = self.configuration_store.get() self.key_generator = KeyGenerator() 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 # Added alternative id as an argument to initializing the configuration self.configuration.initialize(maker_id, device_id, device_status, aid, public_key, private_key) self.configuration_store.save(self.configuration) self.generate_qr_code() Logger.success(LOCATION, 'Configuration successfully initialized.') def resume_configuration(self): device_status = self.configuration.get_device_status() Logger.info(LOCATION, 'DeviceStatus = ' + device_status.value) if device_status == DeviceStatus.NEW: self.pair() if device_status == DeviceStatus.PAIRED: self.activate() def pair(self): success = PairingService().run() if success: self.configuration.set_device_status(DeviceStatus.PAIRED.value) self.configuration_store.save(self.configuration) self.activate() def activate(self): success = ActivationService().run() if success: self.configuration.set_device_status(DeviceStatus.ACTIVE.value) self.configuration_store.save(self.configuration) def generate_qr_code(self): try: 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') except Exception as exception: Logger.error(LOCATION, 'QR Code not generated') raise exception
def __init__(self): self.configuration = ConfigurationStore().get() self.bot_service = BoTService() self.key_generator = KeyGenerator() self.store = Store()
class ActionService: def __init__(self): self.configuration = ConfigurationStore().get() self.bot_service = BoTService() self.key_generator = KeyGenerator() self.store = Store() 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 trigger(self, action_id, value=None, alternative_id=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, alternative_id) 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 # TODO : Make exception more specific except: Logger.error(LOCATION, 'Unable to trigger action: ' + action_id) return False def _validate_frequency(self, action): last_triggered = self.store.get_last_triggered(action[ACTION_ID]) if last_triggered is None: return # It was never triggered, so it is valid, unless we ever introduce Frequency: 'never' frequency = action[FREQUENCY] if frequency not in FrequenciesInSeconds.keys(): self._handle_unsupported_frequency(frequency) if FrequenciesInSeconds[frequency] > time.time() - last_triggered: self._handle_maximum_frequency(frequency) 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 _create_trigger_body(self, action_id, value, alternative_id): data = { ACTION_ID: action_id, DEVICE_ID: self.configuration.get_device_id(), QUEUE_ID: self.key_generator.generate_uuid() } if alternative_id is not None: data[ALTERNATIVE_ID] = str(alternative_id) if value is not None: data[VALUE] = str(value) return data @staticmethod def _handle_unsupported_frequency(frequency): error = 'Frequency not supported: ' + frequency Logger.error(LOCATION, error) raise falcon.HTTPBadRequest(description=error) @staticmethod def _handle_maximum_frequency(frequency): error = 'Maximum ' + frequency + ' triggers reached' Logger.error(LOCATION, error) raise falcon.HTTPBadRequest(description=error)
def test_generate_uuid(): uuid = KeyGenerator.generate_uuid() assert len(str(uuid)) == 36