src/eric7/MicroPython/WifiDialogs/WifiController.py

branch
mpy_network
changeset 9776
210bf87ae5c7
child 9779
8d3c7c991085
equal deleted inserted replaced
9775:c6806d24468b 9776:210bf87ae5c7
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the WiFi related functionality.
8 """
9
10 from PyQt6.QtCore import QObject, pyqtSlot
11 from PyQt6.QtWidgets import QDialog, QMenu
12
13 from eric7 import Preferences
14 from eric7.EricWidgets import EricMessageBox
15
16
17 class WifiController(QObject):
18 """
19 Class implementing the WiFi related functionality.
20 """
21
22 def __init__(self, microPython, parent=None):
23 """
24 Constructor
25
26 @param microPython reference to the MicroPython widget
27 @type MicroPythonWidgep
28 @param parent reference to the parent object (defaults to None)
29 @type QObject (optional)
30 """
31 super().__init__(parent)
32
33 self.__mpy = microPython
34
35 def createMenu(self, menu):
36 """
37 Public method to create the WiFi submenu.
38
39 @param menu reference to the parent menu
40 @type QMenu
41 @return reference to the created menu
42 @rtype QMenu
43 """
44 wifiMenu = QMenu(self.tr("WiFi Functions"), menu)
45 wifiMenu.addAction(self.tr("Show WiFi Status"), self.__showWifiStatus)
46 wifiMenu.addSeparator()
47 wifiMenu.addAction(self.tr("Connect WiFi"), self.__connectWifi)
48 wifiMenu.addAction(self.tr("Disconnect WiFi"), self.__disconnectWifi)
49
50 # add device specific entries (if there are any)
51 self.__device.addDeviceWifiEntries(wifiMenu)
52
53 return wifiMenu
54
55 @pyqtSlot()
56 def __showWifiStatus(self):
57 """
58 Private slot to show a dialog with the WiFi status of the current device.
59 """
60 from .WifiStatusDialog import WifiStatusDialog
61
62 try:
63 clientStatus, apStatus = self.__mpy.getDevice().getWifiData()
64
65 dlg = WifiStatusDialog(clientStatus, apStatus)
66 dlg.exec()
67 except Exception as exc:
68 self.__showError("getWifiData()", str(exc))
69
70 @pyqtSlot()
71 def __connectWifi(self):
72 """
73 Private slot to connect the current device to a WiFi network.
74 """
75 from .WifiConnectionDialog import WifiConnectionDialog
76
77 dlg = WifiConnectionDialog()
78 if dlg.exec() == QDialog.DialogCode.Accepted:
79 ssid, password, remember = dlg.getConnectionParameters()
80 if remember:
81 # save the parameters to the preferences
82 Preferences.setMicroPython("WifiName", ssid)
83 Preferences.setMicroPython("WifiPassword", password)
84 success, error = self.__mpy.getDevice().connectWifi(ssid, password)
85 if success:
86 EricMessageBox.information(
87 None,
88 self.tr("Connect WiFi"),
89 self.tr(
90 "<p>The device was connected to <b>{0}</b> successfully.</p>"
91 ).format(ssid),
92 )
93 else:
94 EricMessageBox.critical(
95 None,
96 self.tr("Connect WiFi"),
97 self.tr(
98 "<p>The device could not connect to <b>{0}</b>.</p>"
99 "<p>Reason: {1}</p>"
100 ).format(ssid, error if error else self.tr("unknown")),
101 )
102
103 @pyqtSlot()
104 def __disconnectWifi(self):
105 """
106 Private slot to disconnect the current device from the WiFi network.
107 """
108 success, error = self.__mpy.getDevice().disconnectWifi()
109 if success:
110 EricMessageBox.information(
111 None,
112 self.tr("Disconnect WiFi"),
113 self.tr(
114 "<p>The device was disconnected from the WiFi network.</p>"
115 ),
116 )
117 else:
118 EricMessageBox.critical(
119 None,
120 self.tr("Disconnect WiFi"),
121 self.tr(
122 "<p>The device could not be disconnected.</p>"
123 "<p>Reason: {0}</p>"
124 ).format(error if error else self.tr("unknown")),
125 )

eric ide

mercurial