14 from PyQt6.QtCore import QUrl, pyqtSlot |
14 from PyQt6.QtCore import QUrl, pyqtSlot |
15 from PyQt6.QtNetwork import QNetworkRequest |
15 from PyQt6.QtNetwork import QNetworkRequest |
16 from PyQt6.QtWidgets import QMenu |
16 from PyQt6.QtWidgets import QMenu |
17 |
17 |
18 from eric7 import Globals, Preferences |
18 from eric7 import Globals, Preferences |
|
19 from eric7.EricGui.EricOverrideCursor import EricOverrideCursor |
19 from eric7.EricWidgets import EricMessageBox |
20 from eric7.EricWidgets import EricMessageBox |
20 from eric7.EricWidgets.EricApplication import ericApp |
21 from eric7.EricWidgets.EricApplication import ericApp |
21 |
22 |
22 from ..MicroPythonWidget import HAS_QTCHART |
23 from ..MicroPythonWidget import HAS_QTCHART |
23 from . import FirmwareGithubUrls |
24 from . import FirmwareGithubUrls |
49 -3: self.tr('wrong password'), |
50 -3: self.tr('wrong password'), |
50 -2: self.tr('no access point found'), |
51 -2: self.tr('no access point found'), |
51 -1: self.tr('connection failed'), |
52 -1: self.tr('connection failed'), |
52 0: self.tr('idle'), |
53 0: self.tr('idle'), |
53 1: self.tr('connecting'), |
54 1: self.tr('connecting'), |
|
55 2: self.tr('getting IP address'), |
54 3: self.tr('connection successful'), |
56 3: self.tr('connection successful'), |
55 }, |
57 }, |
56 "pimoroni": { |
58 "pimoroni": { |
57 # TODO: not yet implemented |
59 # TODO: not yet implemented |
58 }, |
60 }, |
441 # TODO: not yet implemented |
443 # TODO: not yet implemented |
442 pass |
444 pass |
443 else: |
445 else: |
444 return super().getWifiData() |
446 return super().getWifiData() |
445 |
447 |
446 out, err = self._interface.execute(command, timeout=10000) |
448 out, err = self._interface.execute(command)##, timeout=10000) |
447 if err: |
449 if err: |
448 raise OSError(self._shortError(err)) |
450 raise OSError(self._shortError(err)) |
449 |
451 |
450 stationStr, apStr = out.decode("utf-8").splitlines() |
452 stationStr, apStr = out.decode("utf-8").splitlines() |
451 station = json.loads(stationStr) |
453 station = json.loads(stationStr) |
462 ][ap["status"]] |
464 ][ap["status"]] |
463 except KeyError: |
465 except KeyError: |
464 ap["status"] = str(ap["status"]) |
466 ap["status"] = str(ap["status"]) |
465 return station, ap |
467 return station, ap |
466 |
468 |
|
469 def connectWifi(self, ssid, password): |
|
470 """ |
|
471 Public method to connect a device to a WiFi network. |
|
472 |
|
473 @param ssid name (SSID) of the WiFi network |
|
474 @type str |
|
475 @param password password needed to connect |
|
476 @type str |
|
477 @return tuple containing the connection status and an error string |
|
478 @rtype tuple of (bool, str) |
|
479 """ |
|
480 if self._deviceData["wifi_type"] == "picow": |
|
481 command = """ |
|
482 def connect_wifi(ssid, password): |
|
483 import network |
|
484 from time import sleep |
|
485 import ujson |
|
486 |
|
487 wifi = network.WLAN(network.STA_IF) |
|
488 wifi.active(False) |
|
489 wifi.active(True) |
|
490 wifi.connect(ssid, password) |
|
491 max_wait = 100 |
|
492 while max_wait: |
|
493 if wifi.status() < 0 or wifi.status() >= 3: |
|
494 break |
|
495 max_wait -= 1 |
|
496 sleep(0.1) |
|
497 status = wifi.status() |
|
498 print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) |
|
499 |
|
500 connect_wifi({0}, {1}) |
|
501 del connect_wifi |
|
502 """.format(repr(ssid), repr(password if password else "")) |
|
503 elif self._deviceData["wifi_type"] == "pimoroni": |
|
504 # TODO: not yet implemented |
|
505 pass |
|
506 else: |
|
507 return super().connectWifi(ssid, password) |
|
508 |
|
509 with EricOverrideCursor(): |
|
510 out, err = self._interface.execute(command, timeout=15000) |
|
511 if err: |
|
512 return False, err |
|
513 |
|
514 result = json.loads(out.decode("utf-8").strip()) |
|
515 if result["connected"]: |
|
516 error = "" |
|
517 else: |
|
518 try: |
|
519 error = self.__statusTranslations[ |
|
520 self._deviceData["wifi_type"] |
|
521 ][result["status"]] |
|
522 except KeyError: |
|
523 error = str(result["status"]) |
|
524 |
|
525 return result["connected"], error |
|
526 |
|
527 def disconnectWifi(self): |
|
528 """ |
|
529 Public method to disconnect a device from the WiFi network. |
|
530 |
|
531 @return tuple containing a flag indicating success and an error string |
|
532 @rtype tuple of (bool, str) |
|
533 """ |
|
534 if self._deviceData["wifi_type"] == "picow": |
|
535 command = """ |
|
536 def disconnect_wifi(): |
|
537 import network |
|
538 from time import sleep |
|
539 |
|
540 wifi = network.WLAN(network.STA_IF) |
|
541 wifi.disconnect() |
|
542 wifi.active(False) |
|
543 sleep(0.1) |
|
544 print(not wifi.isconnected()) |
|
545 |
|
546 disconnect_wifi() |
|
547 del disconnect_wifi |
|
548 """ |
|
549 elif self._deviceData["wifi_type"] == "pimoroni": |
|
550 # TODO: not yet implemented |
|
551 pass |
|
552 else: |
|
553 return super().disconnectWifi() |
|
554 |
|
555 out, err = self._interface.execute(command) |
|
556 if err: |
|
557 return False, err |
|
558 |
|
559 return out.decode("utf-8").strip() == "True", "" |
|
560 |
467 |
561 |
468 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
562 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
469 """ |
563 """ |
470 Function to instantiate a MicroPython device object. |
564 Function to instantiate a MicroPython device object. |
471 |
565 |