def onWriteRequest(self, data, offset, without_response, callback): if offset: callback(Characteristic.RESULT_ATTR_NOT_LONG) else: callback(Characteristic.RESULT_SUCCESS) if offset > len(data): callback(bleno.Characteristic.RESULT_INVALID_OFFSET) Logger.error(LOCATION, 'Error in Characteristic') else: callback(Characteristic.RESULT_SUCCESS) #decode the byte sequence sent from the client and prepare a JSON structure details = json.loads(data.decode()) #skip wifi configuration skip_wifi_config = False try: skip_wifi_config = details['Skip'] except: Logger.info(LOCATION, 'Wifi Configuration is available ..') #wifi configuration is enabled from the client if skip_wifi_config == True: Logger.info( LOCATION, 'Connected device skipped Wifi setup. ' + 'Initializing pairing process...') PairingService().run() else: wifi_details = '' #if valid SSID provided then create the wpa supplicant configuration. if details['SSID'] != '': wifi_details = 'ctrl_interface=DIR=/var/run/wpa_supplicant' + \ ' GROUP=netdev\r\n update_config=1\r\n country=GB \r\n'+ \ 'network={ \r\n ssid="' + details['SSID'] + \ '" \r\n' + \ ' psk="' + details['PWD'] + \ '" \r\n ' + \ 'key_mgmt=WPA-PSK \r\n}' Logger.info( LOCATION, 'Wifi setup complete. Initializing pairing process...') PairingService().run() time.sleep(3) subprocess.run([ 'sudo echo \'' + wifi_details + '\' > ./wpa_supplicant.conf' ], shell=True) subprocess.run([ "sudo", "cp", "./wpa_supplicant.conf", "/etc/wpa_supplicant/" ]) Logger.info( LOCATION, 'Wifi configuration done! Device reboot in progress') # run the necessary command to update the wpa supplicant file with in /etc/ subprocess.run(["sudo", "rm", "./wpa_supplicant.conf"]) subprocess.run(["sudo", "sleep", "1"]) # reboot the device after successful update of wpa suppicant configuration subprocess.run(["sudo", "reboot"])
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()
def onWriteRequest(self, data, offset, withoutResponse, callback): if offset: callback(Characteristic.RESULT_ATTR_NOT_LONG) else: callback(Characteristic.RESULT_SUCCESS) if offset > len(data): callback(bleno.Characteristic.RESULT_INVALID_OFFSET) Logger.error(LOCATION, 'Error in Characteristic') else: callback(Characteristic.RESULT_SUCCESS, data[offset:]) details = json.loads(data) if details['Skip'] == True: Logger.info( LOCATION, 'Connected device skipped Wifi setup. ' + 'Initializing pairing process...') PairingService().run() else: wifiDetails = '' if details['SSID'] != '': wifiDetails = 'ctrl_interface=DIR=/var/run/wpa_supplicant' + \ ' GROUP=netdev\r\n update_config=1\r\n country=GB \r\n'+ \ 'network={ \r\n ssid="' + details['SSID'] + \ '" \r\n' + \ ' psk="' + details['PWD'] + \ '" \r\n ' + \ 'key_mgmt=WPA-PSK \r\n}' Logger.info( LOCATION, 'Wifi setup complete. Initializing pairing process...') PairingService().run() time.sleep(3) subprocess.run([ 'sudo echo \'' + wifiDetails + '\' > ./wpa_supplicant.conf' ], shell=True) subprocess.run([ "sudo", "cp", "./wpa_supplicant.conf", "/etc/wpa_supplicant/" ]) subprocess.run(["sudo", "rm", "./wpa_supplicant.conf"]) subprocess.run(["sudo", "sleep", "1", "&&", "reboot"])
def onReadRequest(self, offset, callback): if not offset: data = {'BoT': 'Configuration Done'} Logger.info( LOCATION, 'Connected device configuration complete. ' + 'Start pairing process...') self.configureData.extend(map(ord, json.dumps(data))) PairingService().run() callback(Characteristic.RESULT_SUCCESS, self.configureData[offset:])
def pair(self): success = PairingService().run() if success: self.configuration.set_device_status(DeviceStatus.PAIRED.value) self.configuration_store.save(self.configuration) self.activate()