|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Ethernet related functionality. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import QObject, pyqtSlot |
|
11 from PyQt6.QtWidgets import QDialog, QMenu |
|
12 |
|
13 from eric7.EricGui.EricOverrideCursor import EricOverrideCursor |
|
14 from eric7.EricWidgets import EricMessageBox |
|
15 |
|
16 |
|
17 class EthernetController(QObject): |
|
18 """ |
|
19 Class implementing the Ethernet 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 Ethernet 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 ethernetMenu = QMenu(self.tr("Ethernet Functions"), menu) |
|
45 ethernetMenu.setTearOffEnabled(True) |
|
46 ethernetMenu.addAction( |
|
47 self.tr("Show Ethernet Status"), self.__showEthernetStatus |
|
48 ) |
|
49 ethernetMenu.addSeparator() |
|
50 ethernetMenu.addAction(self.tr("Connect to LAN (DHCP)"), self.__connectLanDhcp) |
|
51 ethernetMenu.addAction( |
|
52 self.tr("Connect to LAN (fixed IP)"), self.__connectLanIp |
|
53 ) |
|
54 ethernetMenu.addAction( |
|
55 self.tr("Check Internet Connection"), self.__checkInternet |
|
56 ) |
|
57 ethernetMenu.addAction(self.tr("Disconnect from LAN"), self.__disconnectLan) |
|
58 ethernetMenu.addSeparator() |
|
59 ethernetMenu.addAction( |
|
60 self.tr("Write Auto-Connect Script"), self.__writeAutoConnect |
|
61 ) |
|
62 ethernetMenu.addAction( |
|
63 self.tr("Remove Auto-Connect Script"), self.__removeAutoConnect |
|
64 ) |
|
65 ethernetMenu.addSeparator() |
|
66 ethernetMenu.addAction( |
|
67 self.tr("Deactivate Ethernet Interface"), self.__deactivateEthernet |
|
68 ) |
|
69 ethernetMenu.addSeparator() |
|
70 ethernetMenu.addAction(self.tr("Set Network Time"), self.__setNetworkTime) |
|
71 |
|
72 # add device specific entries (if there are any) |
|
73 self.__mpy.getDevice().addDeviceEthernetEntries(ethernetMenu) |
|
74 |
|
75 return ethernetMenu |
|
76 |
|
77 @pyqtSlot() |
|
78 def __showEthernetStatus(self): |
|
79 """ |
|
80 Private slot to show a dialog with the WiFi status of the current device. |
|
81 """ |
|
82 from .EthernetStatusDialog import EthernetStatusDialog |
|
83 |
|
84 try: |
|
85 status = self.__mpy.getDevice().getEthernetStatus() |
|
86 # status is a list of user labels and associated values |
|
87 |
|
88 dlg = EthernetStatusDialog(status, self.__mpy) |
|
89 dlg.exec() |
|
90 except Exception as exc: |
|
91 self.__mpy.showError("getEthernetStatus()", str(exc)) |
|
92 |
|
93 @pyqtSlot() |
|
94 def __connectLanDhcp(self): |
|
95 """ |
|
96 Private slot to connect to the LAN with a dynamic IPv4 address (DHCP mode). |
|
97 """ |
|
98 self.__connectLan("dhcp") |
|
99 |
|
100 @pyqtSlot() |
|
101 def __connectLanIp(self): |
|
102 """ |
|
103 Private slot to connect to the LAN with a fixed IPv4 address (fixed address |
|
104 mode). |
|
105 """ |
|
106 from .IPv4AddressDialog import IPv4AddressDialog |
|
107 |
|
108 dlg = IPv4AddressDialog(withDhcp=False, parent=self.__mpy) |
|
109 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
110 ifconfig = dlg.getIPv4Address() |
|
111 self.__connectLan(ifconfig) |
|
112 |
|
113 def __connectLan(self, config): |
|
114 """ |
|
115 Private method to connect the connected device to the LAN. |
|
116 |
|
117 @param config configuration for the connection (either the string 'dhcp' |
|
118 for a dynamic address or a tuple of four strings with the IPv4 parameters. |
|
119 @type str of tuple of (str, str, str, str) |
|
120 """ |
|
121 success, error = self.__mpy.getDevice().connectToLan(config) |
|
122 if success: |
|
123 EricMessageBox.information( |
|
124 None, |
|
125 self.tr("Connect to LAN"), |
|
126 self.tr("<p>The device was connected to the LAN successfully.</p>"), |
|
127 ) |
|
128 else: |
|
129 EricMessageBox.critical( |
|
130 None, |
|
131 self.tr("Connect to LAN"), |
|
132 self.tr( |
|
133 "<p>The device could not connect to the LAN.</p><p>Reason: {0}</p>" |
|
134 ).format(error if error else self.tr("unknown")), |
|
135 ) |
|
136 |
|
137 @pyqtSlot() |
|
138 def __disconnectLan(self): |
|
139 """ |
|
140 Private slot to disconnect from the LAN. |
|
141 """ |
|
142 success, error = self.__mpy.getDevice().disconnectFromLan() |
|
143 if success: |
|
144 EricMessageBox.information( |
|
145 None, |
|
146 self.tr("Disconnect from LAN"), |
|
147 self.tr("<p>The device was disconnected from the LAN.</p>"), |
|
148 ) |
|
149 else: |
|
150 EricMessageBox.critical( |
|
151 None, |
|
152 self.tr("Disconnect from LAN"), |
|
153 self.tr( |
|
154 "<p>The device could not be disconnected from the LAN.</p>" |
|
155 "<p>Reason: {0}</p>" |
|
156 ).format(error if error else self.tr("unknown")), |
|
157 ) |
|
158 |
|
159 @pyqtSlot() |
|
160 def __checkInternet(self): |
|
161 """ |
|
162 Private slot to check the availability of an internet connection. |
|
163 """ |
|
164 success, error = self.__mpy.getDevice().checkInternetViaLan() |
|
165 if not error: |
|
166 msg = ( |
|
167 self.tr("<p>The internet connection is <b>available</b>.</p>") |
|
168 if success |
|
169 else self.tr("<p>The internet connection is <b>not available</b>.</p>") |
|
170 ) |
|
171 EricMessageBox.information( |
|
172 None, |
|
173 self.tr("Check Internet Connection"), |
|
174 msg, |
|
175 ) |
|
176 else: |
|
177 EricMessageBox.critical( |
|
178 None, |
|
179 self.tr("Check Internet Connection"), |
|
180 self.tr( |
|
181 "<p>The internet is not available.</p><p>Reason: {0}</p>" |
|
182 ).format(error if error else self.tr("unknown")), |
|
183 ) |
|
184 |
|
185 @pyqtSlot() |
|
186 def __writeAutoConnect(self): |
|
187 """ |
|
188 Private slot to generate a script and associated configuration to connect the |
|
189 device during boot time. |
|
190 |
|
191 This will also modify the boot script to perform the automatic connection. |
|
192 """ |
|
193 from .IPv4AddressDialog import IPv4AddressDialog |
|
194 |
|
195 dlg = IPv4AddressDialog(withDhcp=True, parent=self.__mpy) |
|
196 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
197 ifconfig = dlg.getIPv4Address() |
|
198 ok, err = self.__mpy.getDevice().writeLanAutoConnect(ifconfig) |
|
199 if ok: |
|
200 EricMessageBox.information( |
|
201 None, |
|
202 self.tr("Write Auto-Connect Script"), |
|
203 self.tr( |
|
204 "<p>The auto-connect script and associated configuration was" |
|
205 " saved on the device. The device will connect to the LAN at" |
|
206 " boot time.</p>" |
|
207 ), |
|
208 ) |
|
209 else: |
|
210 EricMessageBox.critical( |
|
211 None, |
|
212 self.tr("Write Auto-Connect Script"), |
|
213 self.tr( |
|
214 "<p>The auto-connect script and associated configuration could" |
|
215 " not be saved on the device.</p><p>Reason: {0}</p>" |
|
216 ).format(err if err else self.tr("unknown")), |
|
217 ) |
|
218 |
|
219 @pyqtSlot() |
|
220 def __removeAutoConnect(self): |
|
221 """ |
|
222 Private slot to remove the boot time connect capability. |
|
223 |
|
224 This will not remove the auto-connect part of the boot script. This needs to be |
|
225 done manually if desired. |
|
226 """ |
|
227 ok = EricMessageBox.yesNo( |
|
228 None, |
|
229 self.tr("Remove Auto-Connect Script"), |
|
230 self.tr( |
|
231 "Shall the saved IPv4 parameters really be removed from the connected" |
|
232 " device?" |
|
233 ), |
|
234 ) |
|
235 if ok: |
|
236 ok, err = self.__mpy.getDevice().removeLanAutoConnect() |
|
237 if ok: |
|
238 EricMessageBox.information( |
|
239 None, |
|
240 self.tr("Remove Auto-Connect Script"), |
|
241 self.tr( |
|
242 "<p>The IPv4 parameters were removed from the device. The" |
|
243 " device will not connect to the LAN at boot time anymore.</p>" |
|
244 ), |
|
245 ) |
|
246 else: |
|
247 EricMessageBox.critical( |
|
248 None, |
|
249 self.tr("Remove Auto-Connect Script"), |
|
250 self.tr( |
|
251 "<p>The IPv4 parameters could not be removed from the device." |
|
252 "</p><p>Reason: {0}</p>" |
|
253 ).format(err if err else self.tr("unknown")), |
|
254 ) |
|
255 |
|
256 @pyqtSlot() |
|
257 def __deactivateEthernet(self): |
|
258 """ |
|
259 Private slot to deactivate the Ethernet interface. |
|
260 """ |
|
261 ok, err = self.__mpy.getDevice().deactivateEthernet() |
|
262 if ok: |
|
263 EricMessageBox.information( |
|
264 None, |
|
265 self.tr("Deactivate Ethernet Interface"), |
|
266 self.tr("The Ethernet interface was deactivated successfully."), |
|
267 ) |
|
268 else: |
|
269 msg = self.tr("<p>The Ethernet interface could not be deactivated.</p>") |
|
270 if err: |
|
271 msg += self.tr("<p>Reason: {0}</p>").format(err) |
|
272 EricMessageBox.critical( |
|
273 None, |
|
274 self.tr("Deactivate Ethernet Interface"), |
|
275 msg, |
|
276 ) |
|
277 |
|
278 @pyqtSlot() |
|
279 def __setNetworkTime(self): |
|
280 """ |
|
281 Private slot to synchronize the device clock to network time. |
|
282 """ |
|
283 from ..NtpParametersDialog import NtpParametersDialog |
|
284 |
|
285 device = self.__mpy.getDevice() |
|
286 if not device.getDeviceData("ntp"): |
|
287 if device.hasCircuitPython(): |
|
288 EricMessageBox.warning( |
|
289 None, |
|
290 self.tr("Set Network Time"), |
|
291 self.tr( |
|
292 "<p>The device does not support network time synchronization." |
|
293 " The module <b>adafruit_ntp</b> is not installed.</p>" |
|
294 ), |
|
295 ) |
|
296 else: |
|
297 EricMessageBox.critical( |
|
298 None, |
|
299 self.tr("Set Network Time"), |
|
300 self.tr( |
|
301 "<p>The device does not support network time synchronization." |
|
302 " The module <b>ntptime</b> is not available.</p>" |
|
303 ), |
|
304 ) |
|
305 return |
|
306 |
|
307 dlg = NtpParametersDialog(self.__mpy) |
|
308 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
309 server, tzOffset, isDst, timeout = dlg.getParameters() |
|
310 if isDst: |
|
311 tzOffset += 1 |
|
312 |
|
313 ok, err = self.__mpy.getDevice().setNetworkTime( |
|
314 server=server, tzOffset=tzOffset, timeout=timeout |
|
315 ) |
|
316 if ok: |
|
317 EricMessageBox.information( |
|
318 None, |
|
319 self.tr("Set Network Time"), |
|
320 self.tr("The device time was synchronized successfully."), |
|
321 ) |
|
322 else: |
|
323 if err: |
|
324 msg = self.tr( |
|
325 "<p>The device time could not be synchronized.</p>" |
|
326 "<p>Reason: {0}</p>" |
|
327 ).format(err) |
|
328 else: |
|
329 msg = self.tr( |
|
330 "<p>The device time could not be synchronized. Is the device" |
|
331 " connected to a LAN?</p>" |
|
332 ) |
|
333 EricMessageBox.critical( |
|
334 None, |
|
335 self.tr("Set Network Time"), |
|
336 msg, |
|
337 ) |