def __init__(self):
        super().__init__()

        self.adapter_home_path = get_adapter_home_path()
        self.python_path = get_python3_path()
        self.env_manage = EnvManage(self.logger)
        self.jupyter_proc = None
Пример #2
0
    def connect(self, id, **kwargs):
        self.port = id
        # self.node_instance.logger.debug(f"args: {kwargs}")
        if not kwargs.get("baudrate", None):
            kwargs["baudrate"] = 115200
        # if not kwargs.get("timeout", None):
        timeout = kwargs["timeout"]  # client输入
        kwargs["timeout"] = 1  # 串口超时时间
        ser = serial.Serial(self.port, **kwargs)
        self.thing = ser
        firmware_path = str(get_adapter_home_path() / "src" /
                            "usb_Microbit_firmware_4v1v2.hex")

        t1 = time.time()
        # time.sleep(0.1)
        while self.node_instance._running:
            if time.time() - t1 >= timeout / 2:
                # 超时没收到version信息, 可能是第一次烧录
                self.node_instance.logger.error("get version timeout")
                ser.close()
                self.node_instance.pub_notification("正在刷入固件...", type="INFO")
                flash_makecode_file(firmware_path)  # todo flash_hex_file
                time.sleep(10)
                return
            # self.send_command(payload="__version__", msgid="query version")
            self.send_command()
            # ser.write(b"version\n")  # 没写进去,microbit没有反应
            data = self.uart_helper()  # timeout, 为何read没东西? 第一次
            self.node_instance.logger.debug(f"version query reply: {data}")
            if data:
                if data.get("version"):
                    self.node_instance.logger.debug(
                        f"usb microbit firmware -> {data['version']}")
                    version = data.get("version")
                    if version >= "0.4":  # todo 0.5 set radio_03 其他不要动,固件变了
                        self.is_connected = True  # 连接成功
                        self.thing = ser
                        self.node_instance.pub_notification("micro:bit 已连接",
                                                            type="SUCCESS")
                        self.run_task_forever()
                        return "ok"
                    else:
                        self.thing.close()
                        self.node_instance.pub_notification("正在刷入固件...",
                                                            type="INFO")
                        flash_makecode_file(firmware_path)
                        time.sleep(10)
                        # self.node_instance.pub_notification(self.flash_finished_notification, type="SUCCESS")
                    return  # 只有串口数据是 version_xx才退出循环
    def connect(self, id, **kwargs):
        self.port = id
        # self.node_instance.logger.debug(f"args: {kwargs}")
        if not kwargs.get("baudrate", None):
            kwargs["baudrate"] = 115200
        # if not kwargs.get("timeout", None):
        timeout = kwargs["timeout"]  # client输入
        kwargs["timeout"] = 0.5  # 串口超时时间
        ser = serial.Serial(self.port, **kwargs)

        firmware_path = str(get_adapter_home_path() / "src" /
                            "makecode_radio_adapter.hex")
        t1 = time.time()
        # time.sleep(0.1)
        while self.node_instance._running:
            if time.time() - t1 >= timeout / 2:
                # 超时没收到version信息, 可能是第一次烧录
                self.node_instance.logger.error("get version timeout")
                ser.close()
                self.node_instance.pub_notification("正在刷入固件...", type="INFO")
                flash_makecode_file(firmware_path)
                time.sleep(10)
                return
            ser.write(b"version\n")  # 没写进去,microbit没有反应
            data = ser.readline()  # timeout
            if data:
                self.node_instance.logger.debug(f"version query reply: {data}")
                data = data.decode().strip()
                if data.startswith("version_"):
                    version = data.split('version_')[-1]
                    self.node_instance.logger.debug(
                        f"makecode radio firmware -> {version}")
                    if version >= "0.5":  # todo 0.5 set radio_03 其他不要动,固件变了
                        self.is_connected = True  # 连接成功
                        self.thing = ser
                        if self.thing.name:
                            self.node_instance.pub_notification(
                                "micro:bit 已连接", type="SUCCESS")
                            self.run_task_forever()
                            return "ok"
                    else:
                        self.thing.close()
                        self.node_instance.pub_notification("正在刷入固件...",
                                                            type="INFO")
                        flash_makecode_file(firmware_path)
                        time.sleep(10)
                        # self.node_instance.pub_notification(self.flash_finished_notification, type="SUCCESS")
                    return  # 只有串口数据是 version_xx才退出循环
import time
import webbrowser
import requests

import bottle
from bottle import route, run, template, view, request

from codelab_adapter.core_extension import Extension
from codelab_adapter.utils import threaded, get_local_ip
from codelab_adapter_client.utils import get_adapter_home_path
# html 文件所在目录
PORT = 18081

bottle.TEMPLATE_PATH = [
    get_adapter_home_path() / "extensions",  # 优先搜索 html 模版所在目录
    get_adapter_home_path() / "src",
]


@route('/hi/<name>')
def index(name="world"):
    ip = get_local_ip()
    html = '''
    <p>IP: {{ip}}</p>
    <p>EIM point: http://{{ip}}:{{PORT}}/api/message/eim?message=1</p>
    '''
    return template(html, name=name, ip=ip, PORT=PORT)


@route('/api/message/eim', method='POST')
    def connect(self, port, firmware_type, **kwargs):
        '''
        firmware_type
            usb_microbit
            makecode_radio
        '''
        self.port = port
        self.logger.debug(f"args: {kwargs}")
        if self.ser:
            self.ser.close()
            time.sleep(0.2)
        # 默认支持microbit, timeout 默认不超过scratch的积木 5s (3)
        if not kwargs.get("baudrate", None):
            kwargs["baudrate"] = 115200
        if not kwargs.get("timeout", None):
            kwargs["timeout"] = 3
        _ser = serial.Serial(port, **kwargs)
        if firmware_type == "usb_microbit":
            self.send_command(ser = _ser, msgid="query version", payload="__version__") # 子类的方法
            firmware_path = str(get_adapter_home_path() / "src" /"usb_Microbit_firmware.py")
            try:
                data = self.get_response_from_microbit(_ser)
                self.extensionInstance.logger.debug(f"query version(reply) -> {data}")
                # none?
                if data.get("version") and data.get("version") >= "0.4":
                    self.extensionInstance.logger.debug(f"microbit firmware -> {data['version']}")   
                else:
                    self.extensionInstance.pub_notification("flashing new firmware...",type="INFO") 
                    _ser.close()
                    flash_usb_microbit(firmware_path)
                    # flash_usb_microbit是非阻塞的
                    # https://github.com/ntoll/uflash/blob/master/tests/test_uflash.py
                    # self.extensionInstance.pub_notification(self.flash_finished_notification, type="SUCCESS")
                    return
            except Exception as e:
                self.extensionInstance.logger.exception("!!!")
                _ser.close()
                self.extensionInstance.pub_notification("flashing firmware...",type="INFO")
                flash_usb_microbit(firmware_path)
                # self.extensionInstance.pub_notification(self.flash_finished_notification, type="SUCCESS")
                raise e

        if firmware_type == "makecode_radio":
            self.write("version\n",ser=_ser)
            firmware_path = str(get_adapter_home_path() / "src" /"makecode_radio_adapter.hex")
            try:
                data = self.readline(_ser)
                self.extensionInstance.logger.debug(f"makecode_radio uart reply: {data}")
                if type(data)==str and data >= "0.4": # todo 0.4 set radio_03 其他不要动,固件变了
                    self.extensionInstance.logger.debug(f"makecode radio firmware -> {data}")   
                else:
                    # flash
                    _ser.close()
                    self.extensionInstance.pub_notification("flashing firmware...",type="ERROR") 
                    flash_makecode_file(firmware_path)
                    # self.extensionInstance.pub_notification(self.flash_finished_notification, type="SUCCESS")
                    return
            except Exception as e:
                self.extensionInstance.logger.error(e)
                _ser.close()
                self.extensionInstance.pub_notification("flashing firmware...",type="ERROR") 
                flash_makecode_file(firmware_path)
                # self.extensionInstance.pub_notification(self.flash_finished_notification, type="SUCCESS")
                raise e
        
        self.ser = _ser
        # query version
        if self.ser.name:
            self.extensionInstance.pub_notification("micro:bit Connected!",
                                                    type="SUCCESS")
            return "ok"