def connect_wifi_using_config()

in deepracer_systems_pkg/deepracer_systems_pkg/network_monitor_module/network_monitor_node.py [0:0]


    def connect_wifi_using_config(self, config_dict, connection_report):
        """Helper method to attempt to connect to WiFi using the details passed in the
           config_dict and append the results of the execution ot the connection report.

        Args:
            config_dict (dict): Dictionary with the SSID, Password and connection name details.
            connection_report (list): Report that is passed by reference to which the connection
                                      results are appended.

        Returns:
            bool: True if the connection was successful else False.
        """
        # Extract configuration fields.
        ssid = config_dict.get(network_config.WiFiConfigKeys.SSID)
        password = \
            config_dict.get(network_config.WiFiConfigKeys.PASSWORD,
                            network_config.WIFI_CONFIG_DEFAULTS[network_config.WiFiConfigKeys.PASSWORD])
        connection_name = \
            config_dict.get(network_config.WiFiConfigKeys.CONNECTION_NAME,
                            network_config.WIFI_CONFIG_DEFAULTS[network_config.WiFiConfigKeys.CONNECTION_NAME])
        self.get_logger().info(f"{ssid}, {password}, {connection_name}")

        # Ignore if already connected to the same.
        if (ssid == network_utils.get_current_SSID()):
            self.get_logger().info("No change in configuration found, ignoring.")
            connection_report.append("No change in configuration found, ignoring.")
            return True

        # No need to clear the previous connections, just Attempt to connect to the new SSID.
        # If new SSID fails,Ubuntu will revert to the previous SSID
        # Else will connect to the new SSID
        connection_successful, connection_response = wifi_config_utils.connect_wifi(ssid, password)
        self.get_logger().error(f"First attempt at connection: {connection_successful} {connection_response}")
        connected = connection_successful
        if not connection_successful:
            self.get_logger().error(f"Connection was not successful: {connection_response}")
            connection_report.extend(connection_response)
            # Verify whether we failed because of the wrong password.
            bad_pass = ("secrets were required" in line for line in connection_response)

            # Match the input SSID to one of the available networks.
            if not bad_pass:
                # Get the list of active networks.
                networks = wifi_config_utils.get_available_wifi_networks()
                matched_network = None
                self.get_logger().info(f"Available networks: {networks}")
                for network, _ in networks:
                    try:
                        # Convert the names to ASCII.
                        a_ssid = unidecode.unidecode(ssid.decode("utf-8"))
                        if a_ssid == unidecode.unidecode(network.decode("utf-8")):
                            matched_network = network
                            break
                    except Exception as ex:
                        pass
                if matched_network is not None:
                    self.get_logger().info(f"Second attempt to connect to a matched network: "
                                           f"{matched_network}")
                    connection_report.append("Second attempt to connect to a matched network: "
                                             f"{matched_network}")
                    connection_successful, connection_response = \
                        wifi_config_utils.connect_wifi(matched_network, password)
                    self.get_logger().error("Second attempt at connection: "
                                            f"{connection_successful} {connection_response}")
                    connected = connection_successful
                    connection_report.extend(connection_response)
                else:
                    self.get_logger().error("Could not find a matching network for: "
                                            f"{ssid}")
                    connection_report.append("Could not find a matching network for: "
                                             f"{ssid}")

        else:
            self.get_logger().info(f"Successfully connected to: {ssid}")
            connection_report.extend(connection_response)
        return connected