def register() -> None: """Registers device with Google IoT. Stores registration data in local files.""" logger.debug("Registering device with iot") # Check if already registed if is_registered(): logger.error("Unable to register, already registered") return # Check network is connected if not network.is_connected(): logger.warning("Unable to register, network is not connected") return # Build commands make_directory_command = ["mkdir", "-p", REGISTRATION_DATA_DIR] register_command = [REGISTER_SCRIPT_PATH, REGISTRATION_DATA_DIR] # Execute commands try: subprocess.run(make_directory_command) subprocess.run(register_command) except Exception as e: message = "Unable to register, unhandled exception: {}".format(type(e)) logger.exception(message)
def disable_raspi_access_point(self) -> Tuple[str, int]: """Disables raspberry pi access point.""" self.logger.debug("Disabling raspberry pi access point") try: self._disable_raspi_access_point() except Exception as e: message = "Unable to disable raspi access point, unhandled exception: {}".format( type(e)) self.logger.exception(message) return message, 500 # Wait for internet connection to be established timeout = 60 # seconds start_time = time.time() while not network_utilities.is_connected(): self.logger.debug("Waiting for network connection") # Check for timeout if time.time() - start_time > timeout: message = "Did not connect to internet within {} ".format( timeout) message += "seconds of joining wifi, recheck if internet is connected" self.logger.warning(message) return message, 202 # Recheck if internet is connected every second time.sleep(2) # Update connection state self.is_connected = True # Succesfully joined wifi self.logger.debug("Successfully disabled raspberry pi access point") return "Successfully disabled raspberry pi access point", 200
def delete_wifis(self) -> Tuple[str, int]: """ Deletes wifi.""" self.logger.debug("Deleting wifis") # Join wifi try: network_utilities.delete_wifis() except Exception as e: message = "Unable to delete wifi, unhandled exception: {}".format(type(e)) self.logger.exception(message) return message, 500 # Wait for internet to be disconnected timeout = 10 # seconds start_time = time.time() while network_utilities.is_connected(): self.logger.debug("Waiting for internet to disconnect") # Check for timeout if time.time() - start_time > timeout: message = "Did not disconnect from internet within {} ".format(timeout) message += "seconds of deleting wifis" self.logger.warning(message) return message, 202 # Recheck if internet is connected every second time.sleep(2) # Update connection state self.is_connected = False # Succesfully deleted wifi self.logger.debug("Successfully deleted wifis") return "Successfully deleted wifis", 200
def update_connection(self) -> None: """Updates connection state.""" self.is_connected = network_utilities.is_connected() if self.is_connected: self.ip_address = network_utilities.get_ip_address() else: self.ip_address = "UNKNOWN" self.wifi_access_points = network_utilities.get_wifi_access_points()
def update_connection(self) -> None: """Updates connection state.""" self.is_connected = network_utilities.is_connected() self.logger.debug("Is connected: {}".format(self.is_connected)) if self.is_connected: self.ip_address = network_utilities.get_ip_address() else: self.ip_address = "UNKNOWN" self.wifi_ssids = network_utilities.get_wifi_ssids()
def join_wifi_advanced(self, request: Dict[str, Any]) -> Tuple[str, int]: """ Joins wifi.""" self.logger.debug("Joining wifi advanced") # Get request parameters try: ssid_name = request["ssid_name"] passphrase = request["passphrase"] hidden_ssid = request["hidden_ssid"] security = request["security"] eap = request["eap"] identity = request["identity"] phase2 = request["phase2"] except KeyError as e: message = "Unable to join wifi advanced, invalid parameter `{}`".format( e) return message, 400 # Join wifi advanced self.logger.debug("Sending join wifi request to network utility") try: network_utilities.join_wifi_advanced(ssid_name, passphrase, hidden_ssid, security, eap, identity, phase2) except Exception as e: message = "Unable to join wifi advanced, unhandled exception: {}".format( type(e)) self.logger.exception(message) return message, 500 # Wait for internet connection to be established timeout = 60 # seconds start_time = time.time() while not network_utilities.is_connected(): self.logger.debug("Waiting for network to connect") # Check for timeout if time.time() - start_time > timeout: message = "Did not connect to internet within {} ".format( timeout) message += "seconds of joining wifi" self.logger.warning(message) return message, 202 # Recheck if internet is connected every second time.sleep(2) # Update connection state self.is_connected = True # Succesfully joined wifi advanced self.logger.debug("Successfully joined wifi advanced") return "Successfully joined wifi advanced", 200
def join_wifi(self, request: Dict[str, Any]) -> Tuple[str, int]: """ Joins wifi.""" self.logger.debug("Joining wifi") # Get request parameters try: wifi_ssid = request["wifi_ssid"] wifi_password = request["wifi_password"] except KeyError as e: message = "Unable to join wifi, invalid parameter {}".format(e) return message, 400 # Join wifi self.logger.debug("Sending join wifi request") try: network_utilities.join_wifi(wifi_ssid, wifi_password) except Exception as e: message = "Unable to join wifi, unhandled exception: {}".format( type(e)) self.logger.exception(message) return message, 500 # Wait for internet connection to be established timeout = 60 # seconds start_time = time.time() while not network_utilities.is_connected(): self.logger.debug("Waiting for network connection") # Check for timeout if time.time() - start_time > timeout: message = "Did not connect to internet within {} ".format( timeout) message += "seconds of joining wifi" self.logger.warning(message) # Remove failed wifi entry and re-enable raspi access point if "raspberry-pi" in str(os.getenv("PLATFORM")): network_utilities.remove_raspi_prev_wifi_entry() self._enable_raspi_access_point() return message, 202 # Recheck if internet is connected every second time.sleep(2) # Update connection state self.is_connected = True # Succesfully joined wifi self.logger.debug("Successfully joined wifi") return "Successfully joined wifi", 200