src/eric7/MicroPython/Devices/RP2040Devices.py

branch
mpy_network
changeset 9781
3112f77f722b
parent 9779
8d3c7c991085
child 9782
67414f28db68
--- a/src/eric7/MicroPython/Devices/RP2040Devices.py	Sun Feb 19 14:45:16 2023 +0100
+++ b/src/eric7/MicroPython/Devices/RP2040Devices.py	Mon Feb 20 11:42:45 2023 +0100
@@ -9,6 +9,7 @@
 """
 
 import ast
+import binascii
 import json
 
 from PyQt6.QtCore import QUrl, pyqtSlot
@@ -47,18 +48,28 @@
 
         self.__statusTranslations = {
             "picow": {
-                -3: self.tr('wrong password'),
-                -2: self.tr('no access point found'),
+                -3: self.tr('authentication failed'),
+                -2: self.tr('no matching access point found'),
                 -1: self.tr('connection failed'),
                 0: self.tr('idle'),
                 1: self.tr('connecting'),
-                2: self.tr('getting IP address'),
-                3: self.tr('connection successful'),
+                2: self.tr('connected, waiting for IP address'),
+                3: self.tr('connected'),
             },
             "pimoroni": {
             # TODO: not yet implemented
             },
         }
+        self.__securityTranslations = {
+            0: self.tr("open", "open WiFi network"),
+            1: "WEP",
+            2: "WPA",
+            3: "WPA2",
+            4: "WPA/WPA2",
+            5: "WPA2 (CCMP)",
+            6: "WPA3",
+            7: "WPA2/WPA3"
+        }
 
     def setButtons(self):
         """
@@ -581,6 +592,101 @@
 
         return out.decode("utf-8").strip() == "True", ""
 
+    def checkInternet(self):
+        """
+        Public method to check, if the internet can be reached.
+
+        @return tuple containing a flag indicating reachability and an error string
+        @rtype tuple of (bool, str)
+        """
+        if self._deviceData["wifi_type"] == "picow":
+            command = """
+def check_internet():
+    import network
+    import socket
+
+    wifi = network.WLAN(network.STA_IF)
+    if wifi.isconnected():
+        s = socket.socket()
+        try:
+            s.connect(socket.getaddrinfo('google.com', 80)[0][-1])
+            s.close()
+            print(True)
+        except:
+            print(False)
+    else:
+        print(False)
+
+check_internet()
+del check_internet
+"""
+        elif self._deviceData["wifi_type"] == "pimoroni":
+            # TODO: not yet implemented
+            pass
+        else:
+            return super().checkInternet()
+
+        out, err = self._interface.execute(command)
+        if err:
+            return False, err
+
+        return out.decode("utf-8").strip() == "True", ""
+
+    def scanNetworks(self):
+        """
+        Public method to scan for available WiFi networks.
+
+        @return tuple containing the list of available networks as a tuple of 'Name',
+            'MAC-Address', 'channel', 'RSSI' and 'security' and an error string
+        @rtype tuple of (list of tuple of (str, str, int, int, str), str)
+        """
+        if self._deviceData["wifi_type"] == "picow":
+            command = """
+def scan_networks():
+    import network
+
+    wifi = network.WLAN(network.STA_IF)
+    active = wifi.active()
+    if not active:
+        wifi.active(True)
+    network_list = wifi.scan()
+    if not active:
+        wifi.active(False)
+    print(network_list)
+
+scan_networks()
+del scan_networks
+"""
+        elif self._deviceData["wifi_type"] == "pimoroni":
+            # TODO: not yet implemented
+            pass
+        else:
+            return super().checkInternet()
+
+        out, err = self._interface.execute(command, timeout=15000)
+        if err:
+            return [], err
+
+        networksList = ast.literal_eval(out.decode("utf-8"))
+        networks = []
+        for network in networksList:
+            if network[0]:
+                ssid = network[0].decode("utf-8")
+                mac = binascii.hexlify(network[1], ":").decode("utf-8")
+                channel = network[2]
+                rssi = network[3]
+                try:
+                    security = self.__securityTranslations[network[4]]
+                except KeyError:
+                    security = self.tr("unknown ({0})").format(network[4])
+                networks.append((ssid, mac, channel, rssi, security))
+
+        return networks, ""
+
+    ############################################################################
+    ## RP2 only methods below
+    ############################################################################
+
     @pyqtSlot()
     def __setCountry(self):
         """

eric ide

mercurial