src/eric7/MicroPython/WifiDialogs/WifiController.py

branch
mpy_network
changeset 9868
467288cffee2
parent 9798
4402d76c5fa9
child 9870
0399d3607829
diff -r e69788b71c00 -r 467288cffee2 src/eric7/MicroPython/WifiDialogs/WifiController.py
--- a/src/eric7/MicroPython/WifiDialogs/WifiController.py	Thu Mar 09 16:59:39 2023 +0100
+++ b/src/eric7/MicroPython/WifiDialogs/WifiController.py	Fri Mar 10 18:04:06 2023 +0100
@@ -10,7 +10,6 @@
 from PyQt6.QtCore import QObject, pyqtSlot
 from PyQt6.QtWidgets import QDialog, QMenu
 
-from eric7 import Preferences
 from eric7.EricGui.EricOverrideCursor import EricOverrideCursor
 from eric7.EricWidgets import EricMessageBox
 
@@ -73,6 +72,10 @@
             lambda: self.__deactivateInterface("AP"),
         )
 
+        if self.__mpy.getDevice().hasNetworkTime():
+            wifiMenu.addSeparator()
+            wifiMenu.addAction(self.tr("Set Network Time"), self.__setNetworkTime)
+
         # add device specific entries (if there are any)
         self.__mpy.getDevice().addDeviceWifiEntries(wifiMenu)
 
@@ -102,11 +105,7 @@
 
         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)
+            ssid, password = dlg.getConnectionParameters()
             success, error = self.__mpy.getDevice().connectWifi(ssid, password)
             if success:
                 EricMessageBox.information(
@@ -195,11 +194,7 @@
 
         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)
+            ssid, password = dlg.getConnectionParameters()
             success, error = self.__mpy.getDevice().writeCredentials(ssid, password)
             if success:
                 EricMessageBox.information(
@@ -270,17 +265,7 @@
 
         dlg = WifiApConfigDialog(withIP=withIP)
         if dlg.exec() == QDialog.DialogCode.Accepted:
-            ssid, password, security, remember, ifconfig = dlg.getApConfig()
-
-            if remember:
-                Preferences.setMicroPython("WifiApName", ssid)
-                Preferences.setMicroPython("WifiApPassword", password)
-                Preferences.setMicroPython("WifiApAuthMode", security)
-                if withIP:
-                    Preferences.setMicroPython("WifiApAddress", ifconfig[0])
-                    Preferences.setMicroPython("WifiApNetmask", ifconfig[1])
-                    Preferences.setMicroPython("WifiApGateway", ifconfig[2])
-                    Preferences.setMicroPython("WifiApDNS", ifconfig[3])
+            ssid, password, security, ifconfig = dlg.getApConfig()
 
             ok, err = self.__mpy.getDevice().startAccessPoint(
                 ssid,
@@ -378,9 +363,47 @@
         else:
             msg = self.tr("<p>The WiFi interface could not be deactivated.</p>")
             if err:
-                msg += self.tr("<p>Reason: {0}").format(err)
+                msg += self.tr("<p>Reason: {0}</p>").format(err)
             EricMessageBox.critical(
                 None,
                 self.tr("Deactivate WiFi Interface"),
                 msg,
             )
+
+    def __setNetworkTime(self):
+        """
+        Private method to synchronize the device clock to network time.
+        """
+        from ..NtpParametersDialog import NtpParametersDialog
+        
+        dlg = NtpParametersDialog(self.__mpy)
+        if dlg.exec() == QDialog.DialogCode.Accepted:
+            server, tzOffset, isDst, timeout = dlg.getParameters()
+            if isDst:
+                tzOffset += 1
+
+            ok, err = self.__mpy.getDevice().setNetworkTime(
+                server=server, tzOffset=tzOffset, timeout=timeout
+            )
+            if ok:
+                EricMessageBox.information(
+                    None,
+                    self.tr("Set Network Time"),
+                    self.tr("The device time was synchronized successfully."),
+                )
+            else:
+                if err:
+                    msg = self.tr(
+                        "<p>The device time could not be synchronized.</p>"
+                        "<p>Reason: {0}</p>"
+                    ).format(err)
+                else:
+                    msg = self.tr(
+                        "<p>The device time could not be synchronized. Is the device"
+                        " connected to a WiFi network?</p>"
+                    )
+                EricMessageBox.critical(
+                    None,
+                    self.tr("Set Network Time"),
+                    msg,
+                )

eric ide

mercurial