def check_and_resume_configuration(): configuration = ConfigurationStore().get() device_status = configuration.get_device_status() system_platform = platform.system() Logger.info(LOCATION, "Detected Platform System: " + system_platform) if device_status is DeviceStatus.ACTIVE: Logger.info(LOCATION, "Device is already active, no need to further configure") Logger.info(LOCATION, "Server is waiting for requests to serve...") Logger.info( LOCATION, "Supported Endpoints: /qrcode /actions /pairing /activate" ) elif device_status is DeviceStatus.PAIRED: Logger.info(LOCATION, "Device state is PAIRED, resuming the configuration") ConfigurationService().resume_configuration() elif device_status is DeviceStatus.MULTIPAIR and PairingService().pair(): Logger.info( LOCATION, "Device is paired as MULTIPAIR, Server is waiting for requests to serve..." ) else: Logger.info( LOCATION, "Pair the device either using QRCode or Bluetooth Service through FINN Mobile App" ) if system_platform != 'Darwin' and configuration.is_bluetooth_enabled( ): from bot_python_sdk.bluetooth_service import BluetoothService # Handle BLE specific events and callbacks BluetoothService().initialize() ConfigurationService().resume_configuration()
class ActivationResource: def __init__(self): self.configuration_service = ConfigurationService() self.configuration_store = ConfigurationStore() def on_get(self, request, response): Logger.info(LOCATION, "Serving Activation Request...") configuration = self.configuration_store.get() if configuration.get_device_status() is DeviceStatus.ACTIVE: error = 'Device is already activated' Logger.error(LOCATION, error) raise falcon.HTTPBadRequest(description=error) else: self.configuration_service.resume_configuration()
import os import subprocess import sys import re from bot_python_sdk.configuration_service import ConfigurationService from bot_python_sdk.store import Store from bot_python_sdk.logger import Logger configuration_service = ConfigurationService() store = Store() LOCATION = 'Server' # Function to validate the given IP Address def is_valid(ip): regex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)''' return re.search(regex, ip) if not store.has_configuration(): if len(sys.argv) <= 1: exit('Please add your makerID to configure the SDK: "make server makerID=YOUR_MAKER_ID"') maker_id = sys.argv[1] # 1 -> First argument after server.py configuration_service.initialize_configuration(maker_id) # If OS is windows based, it doesn't support gunicorn so we run waitress
def __init__(self): self.configuration_service = ConfigurationService()
class ActivationResource: def __init__(self): self.configuration_service = ConfigurationService() def on_get(self): self.configuration_service.resume_configuration()
Logger.info(LOCATION, INCOMING_REQUEST + METHOD_GET + ' ' + PAIRING_ENDPOINT) configuration = self.configuration_store.get() if configuration.get_device_status() is not DeviceStatus.NEW: error = 'Device is already paired.' Logger.error(LOCATION, error) raise falcon.HTTPForbidden(description=error) device_information = configuration.get_device_information() response.media = json.dumps(device_information) subprocess.Popen(['make', 'pair']) class ActivationResource: def __init__(self): self.configuration_service = ConfigurationService() def on_get(self): self.configuration_service.resume_configuration() api = application = falcon.API() api.add_route(ACTIONS_ENDPOINT, ActionsResource()) api.add_route(PAIRING_ENDPOINT, PairingResource()) api.add_route(ACTIVATION_ENDPOINT, ActivationResource()) ConfigurationService().resume_configuration() #Initialize the Bluetooth service class to process #handle BLE specific envents and callbacks BluetoothService().initialize()
if configuration.get_device_status() is not DeviceStatus.NEW: error = 'Device is already paired.' Logger.error(LOCATION, error) raise falcon.HTTPForbidden(description=error) device_information = configuration.get_device_information() response.media = json.dumps(device_information) subprocess.Popen(['make', 'pair']) class ActivationResource: def __init__(self): self.configuration_service = ConfigurationService() def on_get(self): self.configuration_service.resume_configuration() api = application = falcon.API() api.add_route(ACTIONS_ENDPOINT, ActionsResource()) api.add_route(PAIRING_ENDPOINT, PairingResource()) api.add_route(ACTIVATION_ENDPOINT, ActivationResource()) ConfigurationService().resume_configuration() if ConfigurationService().configuration.is_bluetooth_enabled(): from bot_python_sdk.bluetooth_service import BluetoothService #Initialize the Bluetooth service class to process #handle BLE specific envents and callbacks BluetoothService().initialize()