MicroPython mpy_network

Fri, 24 Feb 2023 18:36:43 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 24 Feb 2023 18:36:43 +0100
branch
mpy_network
changeset 9798
4402d76c5fa9
parent 9797
3be7b2326e2c
child 9799
a79430a8811d

MicroPython
- some corrections, little extension and code style changes to the WiFi related parts

src/eric7/MicroPython/Devices/DeviceBase.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/Devices/EspDevices.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/Devices/RP2040Devices.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/WifiDialogs/WifiApConfigDialog.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/WifiDialogs/WifiController.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/WifiDialogs/WifiStatusDialog.py file | annotate | diff | comparison | revisions
--- a/src/eric7/MicroPython/Devices/DeviceBase.py	Fri Feb 24 14:11:20 2023 +0100
+++ b/src/eric7/MicroPython/Devices/DeviceBase.py	Fri Feb 24 18:36:43 2023 +0100
@@ -1013,7 +1013,7 @@
 except AttributeError:
     res['flash_info_available'] = False
 
-import ubinascii as __ba_
+import binascii as __ba_
 try:
     import machine as __mc_
     if isinstance(__mc_.freq(), tuple):
@@ -1203,11 +1203,11 @@
         """
         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)
+        @return tuple of three dictionaries containing the WiFi status data
+            for the WiFi client, access point and overall data
+        @rtype tuple of (dict, dict, dict)
         """
-        return {}, {}
+        return {}, {}, {}
 
     def connectWifi(self, ssid, password):
         """
--- a/src/eric7/MicroPython/Devices/EspDevices.py	Fri Feb 24 14:11:20 2023 +0100
+++ b/src/eric7/MicroPython/Devices/EspDevices.py	Fri Feb 24 18:36:43 2023 +0100
@@ -665,9 +665,9 @@
         """
         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)
+        @return tuple of three dictionaries containing the WiFi status data
+            for the WiFi client, access point and overall data
+        @rtype tuple of (dict, dict, dict)
         @exception OSError raised to indicate an issue with the device
         """
         command = """
@@ -708,6 +708,11 @@
             pass
     print(ujson.dumps(ap))
 
+    overall = {
+        'active': station['active'] or ap['active']
+    }
+    print(ujson.dumps(overall))
+
 wifi_status()
 del wifi_status
 """
@@ -716,9 +721,10 @@
         if err:
             raise OSError(self._shortError(err))
 
-        stationStr, apStr = out.decode("utf-8").splitlines()
+        stationStr, apStr, overallStr = out.decode("utf-8").splitlines()
         station = json.loads(stationStr)
         ap = json.loads(apStr)
+        overall = json.loads(overallStr)
         try:
             station["status"] = self.__statusTranslations[station["status"]]
         except KeyError:
@@ -727,7 +733,7 @@
             ap["status"] = self.__statusTranslations[ap["status"]]
         except KeyError:
             ap["status"] = str(ap["status"])
-        return station, ap
+        return station, ap, overall
 
     def connectWifi(self, ssid, password):
         """
@@ -760,9 +766,9 @@
 connect_wifi({0}, {1})
 del connect_wifi
 """.format(
-                repr(ssid),
-                repr(password if password else ""),
-            )
+            repr(ssid),
+            repr(password if password else ""),
+        )
 
         with EricOverrideCursor():
             out, err = self._interface.execute(command, timeout=15000)
@@ -831,7 +837,9 @@
 
 save_wifi_creds({0}, {1})
 del save_wifi_creds
-""".format(repr(ssid), repr(password) if password else "''")
+""".format(
+            repr(ssid), repr(password) if password else "''"
+        )
         bootCommand = """
 def modify_boot():
     add = True
@@ -1010,8 +1018,8 @@
 deactivate()
 del deactivate
 """.format(
-                interface
-            )
+            interface
+        )
 
         out, err = self._interface.execute(command)
         if err:
@@ -1058,8 +1066,8 @@
 start_ap({0}, {1}, {2}, {3})
 del start_ap
 """.format(
-                repr(ssid), security, repr(password), ifconfig
-            )
+            repr(ssid), security, repr(password), ifconfig
+        )
 
         out, err = self._interface.execute(command, timeout=15000)
         if err:
--- a/src/eric7/MicroPython/Devices/RP2040Devices.py	Fri Feb 24 14:11:20 2023 +0100
+++ b/src/eric7/MicroPython/Devices/RP2040Devices.py	Fri Feb 24 18:36:43 2023 +0100
@@ -423,9 +423,9 @@
         """
         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)
+        @return tuple of three dictionaries containing the WiFi status data
+            for the WiFi client, access point and overall data
+        @rtype tuple of (dict, dict, dict)
         @exception OSError raised to indicate an issue with the device
         """
         if self._deviceData["wifi_type"] == "picow":
@@ -463,6 +463,11 @@
     }
     print(ujson.dumps(ap))
 
+    overall = {
+        'active': station['active'] or ap['active']
+    }
+    print(ujson.dumps(overall))
+
 wifi_status()
 del wifi_status
 """
@@ -476,9 +481,10 @@
         if err:
             raise OSError(self._shortError(err))
 
-        stationStr, apStr = out.decode("utf-8").splitlines()
+        stationStr, apStr, overallStr = out.decode("utf-8").splitlines()
         station = json.loads(stationStr)
         ap = json.loads(apStr)
+        overall = json.loads(overallStr)
         try:
             station["status"] = self.__statusTranslations[
                 self._deviceData["wifi_type"]
@@ -491,7 +497,7 @@
             ]
         except KeyError:
             ap["status"] = str(ap["status"])
-        return station, ap
+        return station, ap, overall
 
     def connectWifi(self, ssid, password):
         """
--- a/src/eric7/MicroPython/WifiDialogs/WifiApConfigDialog.py	Fri Feb 24 14:11:20 2023 +0100
+++ b/src/eric7/MicroPython/WifiDialogs/WifiApConfigDialog.py	Fri Feb 24 18:36:43 2023 +0100
@@ -127,15 +127,16 @@
             and a flag indicating to save the parameters to the settings
         @rtype tuple of (str, str, int, bool)
         """
-        if self.__withIP:
-            ifconfig = (
+        ifconfig = (
+            (
                 self.addressEdit.text(),
                 self.netmaskEdit.text(),
                 self.gatewayEdit.text(),
                 self.dnsEdit.text(),
             )
-        else:
-            ifconfig = ("", "", "", "")
+            if self.__withIP
+            else ("", "", "", "")
+        )
 
         return (
             self.apSsidEdit.text(),
--- a/src/eric7/MicroPython/WifiDialogs/WifiController.py	Fri Feb 24 14:11:20 2023 +0100
+++ b/src/eric7/MicroPython/WifiDialogs/WifiController.py	Fri Feb 24 18:36:43 2023 +0100
@@ -86,9 +86,9 @@
         from .WifiStatusDialog import WifiStatusDialog
 
         try:
-            clientStatus, apStatus = self.__mpy.getDevice().getWifiData()
+            clientStatus, apStatus, overallStatus = self.__mpy.getDevice().getWifiData()
 
-            dlg = WifiStatusDialog(clientStatus, apStatus)
+            dlg = WifiStatusDialog(clientStatus, apStatus, overallStatus)
             dlg.exec()
         except Exception as exc:
             self.__mpy.showError("getWifiData()", str(exc))
--- a/src/eric7/MicroPython/WifiDialogs/WifiStatusDialog.py	Fri Feb 24 14:11:20 2023 +0100
+++ b/src/eric7/MicroPython/WifiDialogs/WifiStatusDialog.py	Fri Feb 24 18:36:43 2023 +0100
@@ -19,7 +19,7 @@
     Class implementing a dialog to show the WiFi status of the connected device.
     """
 
-    def __init__(self, clientStatus, apStatus, parent=None):
+    def __init__(self, clientStatus, apStatus, overallStatus, parent=None):
         """
         Constructor
 
@@ -29,6 +29,8 @@
         @param apStatus dictionary containing the WiFi status data of the
             access point interface
         @type dict
+        @param overallStatus dictionary containing the overall WiFi status data
+        @type dict
         @param parent reference to the parent widget (defaults to None)
         @type QWidget (optional)
         """
@@ -37,6 +39,15 @@
 
         self.statusTree.setColumnCount(2)
 
+        # overall status
+        QTreeWidgetItem(
+            self.statusTree,
+            [
+                self.tr("Active"),
+                self.tr("Yes") if overallStatus["active"] else self.tr("No"),
+            ],
+        )
+
         # client interface
         if clientStatus:
             header = self.__createHeader(self.tr("Client"))
@@ -55,7 +66,12 @@
                         self.tr("Yes") if clientStatus["connected"] else self.tr("No"),
                     ],
                 )
-                QTreeWidgetItem(header, [self.tr("Status"), clientStatus["status"]])
+                with contextlib.suppress(KeyError):
+                    QTreeWidgetItem(header, [self.tr("Status"), clientStatus["status"]])
+                with contextlib.suppress(KeyError):
+                    QTreeWidgetItem(
+                        header, [self.tr("Hostname"), clientStatus["hostname"]]
+                    )
                 QTreeWidgetItem(
                     header, [self.tr("IPv4 Address"), clientStatus["ifconfig"][0]]
                 )
@@ -102,16 +118,23 @@
                         self.tr("Yes") if apStatus["connected"] else self.tr("No"),
                     ],
                 )
-                QTreeWidgetItem(header, [self.tr("Status"), apStatus["status"]])
+                with contextlib.suppress(KeyError):
+                    QTreeWidgetItem(header, [self.tr("Status"), apStatus["status"]])
+                with contextlib.suppress(KeyError):
+                    QTreeWidgetItem(header, [self.tr("Hostname"), apStatus["hostname"]])
                 QTreeWidgetItem(
                     header, [self.tr("IPv4 Address"), apStatus["ifconfig"][0]]
                 )
                 QTreeWidgetItem(header, [self.tr("Netmask"), apStatus["ifconfig"][1]])
                 QTreeWidgetItem(header, [self.tr("Gateway"), apStatus["ifconfig"][2]])
                 QTreeWidgetItem(header, [self.tr("DNS"), apStatus["ifconfig"][3]])
-                QTreeWidgetItem(header, [self.tr("SSID"), apStatus["essid"]])
+                with contextlib.suppress(KeyError):
+                    QTreeWidgetItem(header, [self.tr("SSID"), apStatus["essid"]])
                 QTreeWidgetItem(header, [self.tr("MAC-Address"), apStatus["mac"]])
-                QTreeWidgetItem(header, [self.tr("Channel"), str(apStatus["channel"])])
+                with contextlib.suppress(KeyError):
+                    QTreeWidgetItem(
+                        header, [self.tr("Channel"), str(apStatus["channel"])]
+                    )
                 with contextlib.suppress(KeyError):
                     QTreeWidgetItem(header, [self.tr("Country"), apStatus["country"]])
                 with contextlib.suppress(KeyError):

eric ide

mercurial