src/eric7/MicroPython/Devices/RP2040Devices.py

branch
mpy_network
changeset 9775
c6806d24468b
parent 9772
06ef28082c4d
child 9776
210bf87ae5c7
diff -r c7b712056146 -r c6806d24468b src/eric7/MicroPython/Devices/RP2040Devices.py
--- a/src/eric7/MicroPython/Devices/RP2040Devices.py	Sat Feb 18 09:21:42 2023 +0100
+++ b/src/eric7/MicroPython/Devices/RP2040Devices.py	Sat Feb 18 10:11:44 2023 +0100
@@ -8,6 +8,9 @@
 (e.g. Raspberry Pi Pico).
 """
 
+import ast
+import json
+
 from PyQt6.QtCore import QUrl, pyqtSlot
 from PyQt6.QtNetwork import QNetworkRequest
 from PyQt6.QtWidgets import QMenu
@@ -41,6 +44,20 @@
 
         self.__createRP2040Menu()
 
+        self.__statusTranslations = {
+            "picow": {
+                -3: self.tr('wrong password'),
+                -2: self.tr('no access point found'),
+                -1: self.tr('connection failed'),
+                0: self.tr('idle'),
+                1: self.tr('connecting'),
+                3: self.tr('connection successful'),
+            },
+            "pimoroni": {
+            # TODO: not yet implemented
+            },
+        }
+
     def setButtons(self):
         """
         Public method to enable the supported action buttons.
@@ -340,6 +357,113 @@
     rtc.datetime(rtc_time[:7] + (0,))
 """
 
+    ##################################################################
+    ## Methods below implement network related methods
+    ##################################################################
+
+    def hasWifi(self):
+        """
+        Public method to check the availability of WiFi.
+
+        @return tuple containing a flag indicating the availability of WiFi
+            and the WiFi type (picow or pimoroni)
+        @rtype tuple of (bool, str)
+        @exception OSError raised to indicate an issue with the device
+        """
+        command = """
+def has_wifi():
+    try:
+        import network
+        if hasattr(network, 'WLAN'):
+            return True, 'picow'
+    except ImportError:
+        try:
+            import picowireless
+            if picowireless.get_fw_version() != '':
+                return True, 'pimoroni'
+        except ImportError:
+            pass
+
+    return False, ''
+
+print(has_wifi())
+del has_wifi
+"""
+        out, err = self._interface.execute(command, timeout=10000)
+        if err:
+            raise OSError(self._shortError(err))
+        return ast.literal_eval(out.decode("utf-8"))
+
+    def getWifiData(self):
+        """
+        Public method to get data related to the current WiFi status
+
+        @return tuple of two dictionaries containing the WiFi status data
+            for the WiFi client and access point
+        @rtype tuple of (dict, dict)
+        """
+        if self._deviceData["wifi_type"] == "picow":
+            command = """
+def wifi_status():
+    import ubinascii
+    import ujson
+    import network
+
+    wifi = network.WLAN(network.STA_IF)
+    station = {
+        'active': wifi.active(),
+        'connected': wifi.isconnected(),
+        'status': wifi.status(),
+        'ifconfig': wifi.ifconfig(),
+        'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(),
+        'channel': wifi.config('channel'),
+        'txpower': wifi.config('txpower'),
+    }
+    print(ujson.dumps(station))
+
+    wifi = network.WLAN(network.AP_IF)
+    ap = {
+        'active': wifi.active(),
+        'connected': wifi.isconnected(),
+        'status': wifi.status(),
+        'ifconfig': wifi.ifconfig(),
+        'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(),
+        'channel': wifi.config('channel'),
+        'txpower': wifi.config('txpower'),
+        'essid': wifi.config('essid'),
+    }
+    print(ujson.dumps(ap))
+
+wifi_status()
+del wifi_status
+"""
+        elif self._deviceData["wifi_type"] == "pimoroni":
+            # TODO: not yet implemented
+            pass
+        else:
+            return super().getWifiData()
+
+        out, err = self._interface.execute(command, timeout=10000)
+        if err:
+            raise OSError(self._shortError(err))
+
+        stationStr, apStr = out.decode("utf-8").splitlines()
+        station = json.loads(stationStr)
+        ap = json.loads(apStr)
+        try:
+            station["status"] = self.__statusTranslations[
+                self._deviceData["wifi_type"]
+            ][station["status"]]
+        except KeyError:
+            station["status"] = str(station["status"])
+        try:
+            ap["status"] = self.__statusTranslations[
+                self._deviceData["wifi_type"]
+            ][ap["status"]]
+        except KeyError:
+            ap["status"] = str(ap["status"])
+        return station, ap
+
 
 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber):
     """

eric ide

mercurial