src/eric7/MicroPython/Devices/RP2Devices.py

branch
eric7
changeset 11263
28f0ead11a82
parent 11254
cdb56075b4bc
child 11266
97311a9616e3
equal deleted inserted replaced
11262:07d9cc8d773c 11263:28f0ead11a82
609 wifi = network.WLAN(network.STA_IF) 609 wifi = network.WLAN(network.STA_IF)
610 station = { 610 station = {
611 'active': wifi.active(), 611 'active': wifi.active(),
612 'connected': wifi.isconnected(), 612 'connected': wifi.isconnected(),
613 'status': wifi.status(), 613 'status': wifi.status(),
614 'ifconfig': wifi.ifconfig(),
615 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), 614 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(),
616 'channel': wifi.config('channel'), 615 'channel': wifi.config('channel'),
616 'essid': wifi.config('essid'),
617 'txpower': wifi.config('txpower'), 617 'txpower': wifi.config('txpower'),
618 } 618 }
619 try:
620 station['ifconfig'] = (
621 wifi.ipconfig('addr4') + (wifi.ipconfig('gw4'), network.ipconfig('dns'))
622 )
623 except AttributeError:
624 station['ifconfig'] = wifi.ifconfig()
625 try:
626 station['ipv6_addr'] = [a[0] for a in wifi.ipconfig('addr6')]
627 except ValueError:
628 station['ipv6_addr'] = []
619 print(ujson.dumps(station)) 629 print(ujson.dumps(station))
620 630
621 wifi = network.WLAN(network.AP_IF) 631 wifi = network.WLAN(network.AP_IF)
622 ap = { 632 ap = {
623 'active': wifi.active(), 633 'active': wifi.active(),
624 'connected': wifi.isconnected(), 634 'connected': wifi.isconnected(),
625 'status': wifi.status(), 635 'status': wifi.status(),
626 'ifconfig': wifi.ifconfig(),
627 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), 636 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(),
628 'channel': wifi.config('channel'), 637 'channel': wifi.config('channel'),
629 'txpower': wifi.config('txpower'), 638 'txpower': wifi.config('txpower'),
630 'essid': wifi.config('essid'), 639 'essid': wifi.config('essid'),
631 'ap_security': security_str(wifi.config('security')), 640 'ap_security': security_str(wifi.config('security')),
632 } 641 }
642 try:
643 ap['ifconfig'] = (
644 wifi.ipconfig('addr4') + (wifi.ipconfig('gw4'), network.ipconfig('dns'))
645 )
646 except AttributeError:
647 ap['ifconfig'] = wifi.ifconfig()
648 try:
649 ap['ipv6_addr'] = [a[0] for a in wifi.ipconfig('addr6')]
650 except ValueError:
651 ap['ipv6_addr'] = []
633 print(ujson.dumps(ap)) 652 print(ujson.dumps(ap))
634 653
635 overall = { 654 overall = {
636 'active': station['active'] or ap['active'] 655 'active': station['active'] or ap['active']
637 } 656 }
641 overall['country'] = rp2.country() 660 overall['country'] = rp2.country()
642 try: 661 try:
643 overall['hostname'] = network.hostname() 662 overall['hostname'] = network.hostname()
644 except AttributeError: 663 except AttributeError:
645 pass 664 pass
665 try:
666 overall['prefer'] = network.ipconfig('prefer')
667 except ValueError:
668 overall['prefer'] = 4
646 print(ujson.dumps(overall)) 669 print(ujson.dumps(overall))
647 670
648 wifi_status() 671 wifi_status()
649 del wifi_status 672 del wifi_status
650 """ 673 """
660 683
661 station = { 684 station = {
662 'active': pw.get_connection_status() not in (0, 7, 8, 9), 685 'active': pw.get_connection_status() not in (0, 7, 8, 9),
663 'connected': pw.get_connection_status() == 3, 686 'connected': pw.get_connection_status() == 3,
664 'status': pw.get_connection_status(), 687 'status': pw.get_connection_status(),
688 'mac': ubinascii.hexlify(pw.get_mac_address(), ':').decode(),
665 'ifconfig': ( 689 'ifconfig': (
666 ip_str(pw.get_ip_address()), 690 ip_str(pw.get_ip_address()),
667 ip_str(pw.get_subnet_mask()), 691 ip_str(pw.get_subnet_mask()),
668 ip_str(pw.get_gateway_ip()), 692 ip_str(pw.get_gateway_ip()),
669 '0.0.0.0' 693 '0.0.0.0'
670 ), 694 ),
671 'mac': ubinascii.hexlify(pw.get_mac_address(), ':').decode(), 695 'ipv6_addr': [],
672 } 696 }
673 if station['connected']: 697 if station['connected']:
674 station.update({ 698 station.update({
675 'ap_ssid': pw.get_current_ssid(), 699 'ap_ssid': pw.get_current_ssid(),
676 'ap_bssid': ubinascii.hexlify(pw.get_current_bssid(), ':'), 700 'ap_bssid': ubinascii.hexlify(pw.get_current_bssid(), ':'),
683 'active': pw.get_connection_status() in (7, 8, 9), 707 'active': pw.get_connection_status() in (7, 8, 9),
684 'connected': pw.get_connection_status() == 8, 708 'connected': pw.get_connection_status() == 8,
685 'status': pw.get_connection_status(), 709 'status': pw.get_connection_status(),
686 'mac': ubinascii.hexlify(pw.get_mac_address(), ':').decode(), 710 'mac': ubinascii.hexlify(pw.get_mac_address(), ':').decode(),
687 'ap_security': pw.get_current_encryption_type(), 711 'ap_security': pw.get_current_encryption_type(),
712 'ipv6_addr': [],
688 } 713 }
689 if ap['active']: 714 if ap['active']:
690 ap['essid'] = pw.get_current_ssid() 715 ap['essid'] = pw.get_current_ssid()
691 ap['ifconfig'] = ( 716 ap['ifconfig'] = (
692 ip_str(pw.get_ip_address()), 717 ip_str(pw.get_ip_address()),
695 '0.0.0.0' 720 '0.0.0.0'
696 ) 721 )
697 print(ujson.dumps(ap)) 722 print(ujson.dumps(ap))
698 723
699 overall = { 724 overall = {
700 'active': pw.get_connection_status() != 0 725 'active': pw.get_connection_status() != 0,
726 'prefer': 4,
701 } 727 }
702 print(ujson.dumps(overall)) 728 print(ujson.dumps(overall))
703 729
704 wifi_status() 730 wifi_status()
705 del wifi_status 731 del wifi_status
1856 def getEthernetStatus(self): 1882 def getEthernetStatus(self):
1857 """ 1883 """
1858 Public method to get Ethernet status data of the connected board. 1884 Public method to get Ethernet status data of the connected board.
1859 1885
1860 @return list of tuples containing the translated status data label and 1886 @return list of tuples containing the translated status data label and
1861 the associated value 1887 the associated value and a dictionary with keys 'ipv4' and 'ipv6'
1862 @rtype list of tuples of (str, str) 1888 containing the respective address information
1889 @rtype tuple of list of tuples of (str, str) and dict
1863 @exception OSError raised to indicate an issue with the device 1890 @exception OSError raised to indicate an issue with the device
1864 """ 1891 """
1865 command = """{0} 1892 command = """{0}
1866 def ethernet_status(): 1893 def ethernet_status():
1867 import network 1894 import network
1872 1899
1873 res = {{ 1900 res = {{
1874 'active': nic.active(), 1901 'active': nic.active(),
1875 'connected': nic.isconnected(), 1902 'connected': nic.isconnected(),
1876 'status': nic.status(), 1903 'status': nic.status(),
1877 'ifconfig': nic.ifconfig(),
1878 'mac': ubinascii.hexlify(nic.config('mac'), ':').decode(), 1904 'mac': ubinascii.hexlify(nic.config('mac'), ':').decode(),
1879 }} 1905 }}
1906 try:
1907 res['ipv4_addr'] = (
1908 nic.ipconfig('addr4') + (nic.ipconfig('gw4'), network.ipconfig('dns'))
1909 )
1910 except AttributeError:
1911 res['ipv4_addr'] = nic.ifconfig()
1912 try:
1913 res['ipv6_addr'] = [a[0] for a in nic.ipconfig('addr6')]
1914 except ValueError:
1915 res['ipv6_addr'] = []
1880 try: 1916 try:
1881 res['hostname'] = network.hostname() 1917 res['hostname'] = network.hostname()
1882 except AttributeError: 1918 except AttributeError:
1883 res['hostname'] = '' 1919 res['hostname'] = ''
1920 try:
1921 res['prefer'] = network.ipconfig('prefer')
1922 except ValueError:
1923 res['prefer'] = 4
1884 print(ujson.dumps(res)) 1924 print(ujson.dumps(res))
1885 1925
1886 ethernet_status() 1926 ethernet_status()
1887 del ethernet_status, w5x00_init 1927 del ethernet_status, w5x00_init
1888 """.format( 1928 """.format(
1891 1931
1892 out, err = self.executeCommands(command, mode=self._submitMode) 1932 out, err = self.executeCommands(command, mode=self._submitMode)
1893 if err: 1933 if err:
1894 raise OSError(self._shortError(err)) 1934 raise OSError(self._shortError(err))
1895 1935
1896 status = []
1897 ethStatus = json.loads(out.decode("utf-8")) 1936 ethStatus = json.loads(out.decode("utf-8"))
1898 status.append((self.tr("Active"), self.bool2str(ethStatus["active"]))) 1937 status = [
1899 status.append((self.tr("Connected"), self.bool2str(ethStatus["connected"]))) 1938 (self.tr("Active"), self.bool2str(ethStatus["active"])),
1900 status.append( 1939 (self.tr("Connected"), self.bool2str(ethStatus["connected"])),
1901 ( 1940 (
1902 self.tr("Status"), 1941 self.tr("Status"),
1903 self.__statusTranslations["picowiz"][ethStatus["status"]], 1942 self.__statusTranslations["picowiz"][ethStatus["status"]],
1904 ) 1943 ),
1905 )
1906 status.append(
1907 ( 1944 (
1908 self.tr("Hostname"), 1945 self.tr("Hostname"),
1909 ethStatus["hostname"] if ethStatus["hostname"] else self.tr("unknown"), 1946 ethStatus["hostname"] if ethStatus["hostname"] else self.tr("unknown"),
1910 ) 1947 ),
1911 ) 1948 (self.tr("MAC-Address"), ethStatus["mac"]),
1912 status.append((self.tr("IPv4 Address"), ethStatus["ifconfig"][0])) 1949 ]
1913 status.append((self.tr("Netmask"), ethStatus["ifconfig"][1])) 1950 addressInfo = {
1914 status.append((self.tr("Gateway"), ethStatus["ifconfig"][2])) 1951 "ipv4": ethStatus["ipv4_addr"],
1915 status.append((self.tr("DNS"), ethStatus["ifconfig"][3])) 1952 "ipv6": ethStatus["ipv6_addr"],
1916 status.append((self.tr("MAC-Address"), ethStatus["mac"])) 1953 }
1917 1954
1918 return status 1955 return status, addressInfo
1919 1956
1920 def connectToLan(self, config, hostname): 1957 def connectToLan(self, config, hostname):
1921 """ 1958 """
1922 Public method to connect the connected device to the LAN. 1959 Public method to connect the connected device to the LAN.
1923 1960

eric ide

mercurial