src/eric7/MicroPython/WifiDialogs/WifiController.py

branch
mpy_network
changeset 9776
210bf87ae5c7
child 9779
8d3c7c991085
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/eric7/MicroPython/WifiDialogs/WifiController.py	Sat Feb 18 18:12:32 2023 +0100
@@ -0,0 +1,125 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing the WiFi related functionality.
+"""
+
+from PyQt6.QtCore import QObject, pyqtSlot
+from PyQt6.QtWidgets import QDialog, QMenu
+
+from eric7 import Preferences
+from eric7.EricWidgets import EricMessageBox
+
+
+class WifiController(QObject):
+    """
+    Class implementing the WiFi related functionality.
+    """
+
+    def __init__(self, microPython, parent=None):
+        """
+        Constructor
+
+        @param microPython reference to the MicroPython widget
+        @type MicroPythonWidgep
+        @param parent reference to the parent object (defaults to None)
+        @type QObject (optional)
+        """
+        super().__init__(parent)
+
+        self.__mpy = microPython
+
+    def createMenu(self, menu):
+        """
+        Public method to create the WiFi submenu.
+
+        @param menu reference to the parent menu
+        @type QMenu
+        @return reference to the created menu
+        @rtype QMenu
+        """
+        wifiMenu = QMenu(self.tr("WiFi Functions"), menu)
+        wifiMenu.addAction(self.tr("Show WiFi Status"), self.__showWifiStatus)
+        wifiMenu.addSeparator()
+        wifiMenu.addAction(self.tr("Connect WiFi"), self.__connectWifi)
+        wifiMenu.addAction(self.tr("Disconnect WiFi"), self.__disconnectWifi)
+
+        # add device specific entries (if there are any)
+        self.__device.addDeviceWifiEntries(wifiMenu)
+
+        return wifiMenu
+
+    @pyqtSlot()
+    def __showWifiStatus(self):
+        """
+        Private slot to show a dialog with the WiFi status of the current device.
+        """
+        from .WifiStatusDialog import WifiStatusDialog
+
+        try:
+            clientStatus, apStatus = self.__mpy.getDevice().getWifiData()
+
+            dlg = WifiStatusDialog(clientStatus, apStatus)
+            dlg.exec()
+        except Exception as exc:
+            self.__showError("getWifiData()", str(exc))
+
+    @pyqtSlot()
+    def __connectWifi(self):
+        """
+        Private slot to connect the current device to a WiFi network.
+        """
+        from .WifiConnectionDialog import WifiConnectionDialog
+
+        dlg = WifiConnectionDialog()
+        if dlg.exec() == QDialog.DialogCode.Accepted:
+            ssid, password, remember = dlg.getConnectionParameters()
+            if remember:
+                # save the parameters to the preferences
+                Preferences.setMicroPython("WifiName", ssid)
+                Preferences.setMicroPython("WifiPassword", password)
+            success, error = self.__mpy.getDevice().connectWifi(ssid, password)
+            if success:
+                EricMessageBox.information(
+                    None,
+                    self.tr("Connect WiFi"),
+                    self.tr(
+                        "<p>The device was connected to <b>{0}</b> successfully.</p>"
+                    ).format(ssid),
+                )
+            else:
+                EricMessageBox.critical(
+                    None,
+                    self.tr("Connect WiFi"),
+                    self.tr(
+                        "<p>The device could not connect to <b>{0}</b>.</p>"
+                        "<p>Reason: {1}</p>"
+                    ).format(ssid, error if error else self.tr("unknown")),
+                )
+
+    @pyqtSlot()
+    def __disconnectWifi(self):
+        """
+        Private slot to disconnect the current device from the WiFi network.
+        """
+        success, error = self.__mpy.getDevice().disconnectWifi()
+        if success:
+            EricMessageBox.information(
+                None,
+                self.tr("Disconnect WiFi"),
+                self.tr(
+                    "<p>The device was disconnected from the WiFi network.</p>"
+                ),
+            )
+        else:
+            EricMessageBox.critical(
+                None,
+                self.tr("Disconnect WiFi"),
+                self.tr(
+                    "<p>The device could not be disconnected.</p>"
+                    "<p>Reason: {0}</p>"
+                ).format(error if error else self.tr("unknown")),
+            )

eric ide

mercurial