8 """ |
8 """ |
9 |
9 |
10 from PyQt6.QtCore import QObject, pyqtSlot |
10 from PyQt6.QtCore import QObject, pyqtSlot |
11 from PyQt6.QtWidgets import QDialog, QMenu |
11 from PyQt6.QtWidgets import QDialog, QMenu |
12 |
12 |
13 from eric7 import Preferences |
|
14 from eric7.EricGui.EricOverrideCursor import EricOverrideCursor |
13 from eric7.EricGui.EricOverrideCursor import EricOverrideCursor |
15 from eric7.EricWidgets import EricMessageBox |
14 from eric7.EricWidgets import EricMessageBox |
16 |
15 |
17 |
16 |
18 class WifiController(QObject): |
17 class WifiController(QObject): |
71 wifiMenu.addAction( |
70 wifiMenu.addAction( |
72 self.tr("Deactivate Access Point Interface"), |
71 self.tr("Deactivate Access Point Interface"), |
73 lambda: self.__deactivateInterface("AP"), |
72 lambda: self.__deactivateInterface("AP"), |
74 ) |
73 ) |
75 |
74 |
|
75 if self.__mpy.getDevice().hasNetworkTime(): |
|
76 wifiMenu.addSeparator() |
|
77 wifiMenu.addAction(self.tr("Set Network Time"), self.__setNetworkTime) |
|
78 |
76 # add device specific entries (if there are any) |
79 # add device specific entries (if there are any) |
77 self.__mpy.getDevice().addDeviceWifiEntries(wifiMenu) |
80 self.__mpy.getDevice().addDeviceWifiEntries(wifiMenu) |
78 |
81 |
79 return wifiMenu |
82 return wifiMenu |
80 |
83 |
100 """ |
103 """ |
101 from .WifiConnectionDialog import WifiConnectionDialog |
104 from .WifiConnectionDialog import WifiConnectionDialog |
102 |
105 |
103 dlg = WifiConnectionDialog() |
106 dlg = WifiConnectionDialog() |
104 if dlg.exec() == QDialog.DialogCode.Accepted: |
107 if dlg.exec() == QDialog.DialogCode.Accepted: |
105 ssid, password, remember = dlg.getConnectionParameters() |
108 ssid, password = dlg.getConnectionParameters() |
106 if remember: |
|
107 # save the parameters to the preferences |
|
108 Preferences.setMicroPython("WifiName", ssid) |
|
109 Preferences.setMicroPython("WifiPassword", password) |
|
110 success, error = self.__mpy.getDevice().connectWifi(ssid, password) |
109 success, error = self.__mpy.getDevice().connectWifi(ssid, password) |
111 if success: |
110 if success: |
112 EricMessageBox.information( |
111 EricMessageBox.information( |
113 None, |
112 None, |
114 self.tr("Connect WiFi"), |
113 self.tr("Connect WiFi"), |
193 """ |
192 """ |
194 from .WifiConnectionDialog import WifiConnectionDialog |
193 from .WifiConnectionDialog import WifiConnectionDialog |
195 |
194 |
196 dlg = WifiConnectionDialog() |
195 dlg = WifiConnectionDialog() |
197 if dlg.exec() == QDialog.DialogCode.Accepted: |
196 if dlg.exec() == QDialog.DialogCode.Accepted: |
198 ssid, password, remember = dlg.getConnectionParameters() |
197 ssid, password = dlg.getConnectionParameters() |
199 if remember: |
|
200 # save the parameters to the preferences |
|
201 Preferences.setMicroPython("WifiName", ssid) |
|
202 Preferences.setMicroPython("WifiPassword", password) |
|
203 success, error = self.__mpy.getDevice().writeCredentials(ssid, password) |
198 success, error = self.__mpy.getDevice().writeCredentials(ssid, password) |
204 if success: |
199 if success: |
205 EricMessageBox.information( |
200 EricMessageBox.information( |
206 None, |
201 None, |
207 self.tr("Write WiFi Credentials"), |
202 self.tr("Write WiFi Credentials"), |
268 """ |
263 """ |
269 from .WifiApConfigDialog import WifiApConfigDialog |
264 from .WifiApConfigDialog import WifiApConfigDialog |
270 |
265 |
271 dlg = WifiApConfigDialog(withIP=withIP) |
266 dlg = WifiApConfigDialog(withIP=withIP) |
272 if dlg.exec() == QDialog.DialogCode.Accepted: |
267 if dlg.exec() == QDialog.DialogCode.Accepted: |
273 ssid, password, security, remember, ifconfig = dlg.getApConfig() |
268 ssid, password, security, ifconfig = dlg.getApConfig() |
274 |
|
275 if remember: |
|
276 Preferences.setMicroPython("WifiApName", ssid) |
|
277 Preferences.setMicroPython("WifiApPassword", password) |
|
278 Preferences.setMicroPython("WifiApAuthMode", security) |
|
279 if withIP: |
|
280 Preferences.setMicroPython("WifiApAddress", ifconfig[0]) |
|
281 Preferences.setMicroPython("WifiApNetmask", ifconfig[1]) |
|
282 Preferences.setMicroPython("WifiApGateway", ifconfig[2]) |
|
283 Preferences.setMicroPython("WifiApDNS", ifconfig[3]) |
|
284 |
269 |
285 ok, err = self.__mpy.getDevice().startAccessPoint( |
270 ok, err = self.__mpy.getDevice().startAccessPoint( |
286 ssid, |
271 ssid, |
287 security=security, |
272 security=security, |
288 password=password, |
273 password=password, |
376 self.tr("The WiFi interface was deactivated successfully."), |
361 self.tr("The WiFi interface was deactivated successfully."), |
377 ) |
362 ) |
378 else: |
363 else: |
379 msg = self.tr("<p>The WiFi interface could not be deactivated.</p>") |
364 msg = self.tr("<p>The WiFi interface could not be deactivated.</p>") |
380 if err: |
365 if err: |
381 msg += self.tr("<p>Reason: {0}").format(err) |
366 msg += self.tr("<p>Reason: {0}</p>").format(err) |
382 EricMessageBox.critical( |
367 EricMessageBox.critical( |
383 None, |
368 None, |
384 self.tr("Deactivate WiFi Interface"), |
369 self.tr("Deactivate WiFi Interface"), |
385 msg, |
370 msg, |
386 ) |
371 ) |
|
372 |
|
373 def __setNetworkTime(self): |
|
374 """ |
|
375 Private method to synchronize the device clock to network time. |
|
376 """ |
|
377 from ..NtpParametersDialog import NtpParametersDialog |
|
378 |
|
379 dlg = NtpParametersDialog(self.__mpy) |
|
380 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
381 server, tzOffset, isDst, timeout = dlg.getParameters() |
|
382 if isDst: |
|
383 tzOffset += 1 |
|
384 |
|
385 ok, err = self.__mpy.getDevice().setNetworkTime( |
|
386 server=server, tzOffset=tzOffset, timeout=timeout |
|
387 ) |
|
388 if ok: |
|
389 EricMessageBox.information( |
|
390 None, |
|
391 self.tr("Set Network Time"), |
|
392 self.tr("The device time was synchronized successfully."), |
|
393 ) |
|
394 else: |
|
395 if err: |
|
396 msg = self.tr( |
|
397 "<p>The device time could not be synchronized.</p>" |
|
398 "<p>Reason: {0}</p>" |
|
399 ).format(err) |
|
400 else: |
|
401 msg = self.tr( |
|
402 "<p>The device time could not be synchronized. Is the device" |
|
403 " connected to a WiFi network?</p>" |
|
404 ) |
|
405 EricMessageBox.critical( |
|
406 None, |
|
407 self.tr("Set Network Time"), |
|
408 msg, |
|
409 ) |