Example #1
0
    def _sub_callback_internal(self, topic, msg):
        try:
            message = msg.decode()
            _mqtt_log.info("Subscribe received, topic={}, message={}".format(
                topic.decode(), message))
            restart = False

            config_setting = ujson.loads(message)
            config_keys = config_setting.keys()
            for key in config_setting:
                config = config_setting[key]
                key_exist = self.bs_config.check_key_exist(key)
                if key_exist:
                    if not restart:
                        restart = self.bs_config.mqtt_check_key_restart(key)
                    result = self._handle_callback(key, config)
                    if result:
                        restart = True
            if restart:
                restart = False
                _mqtt_log.info(
                    "New configuration was received from mqtt, restarting system to take effect"
                )
                Power.powerRestart()
        except Exception as err:
            _mqtt_log.error(
                "Cannot handle subscribe callback for mqtt, the error is {}".
                format(err))
        finally:
            self._is_sub_callback_running = False
Example #2
0
    def _download_fota_file(self, download_url):
        file_name = self._download_one_file(download_url)
        file_exist = bluestone_common.BluestoneCommon.check_file_exist(file_name)
        if file_exist:
            fota.set_update_flag()
            utime.sleep_ms(1000)

            _fota_log.info("User application file was downloaded, restarting system to take effect")
            Power.powerRestart()
        else:
            _fota_log.error("Cannot download file from '{}'".format(download_url))
Example #3
0
    def _download_fota_app(self, download_url_list):
        fota = app_fota.new()
        download_list = self._get_download_list(download_url_list)
        if not download_list or len(download_list) <= 0:
            return
        
        result = fota.bulk_download(download_list)
        if result and len(result) > 0:
            _fota_log.error("Cannot download files, the error is {}".format(result))
            return
        
        fota.set_update_flag()
        utime.sleep_ms(1000)

        _fota_log.info("User application files were downloaded, restarting system to take effect")
        Power.powerRestart()
Example #4
0
def run():
    fota_obj = fota()  # 创建Fota对象
    file_size = uos.stat("FotaFile.bin")[6]  # 获取文件总字节数
    print(file_size)
    with open("FotaFile.bin", "rb") as f:  # rb模式打开.bin文件(需要制作升级包文件)
        while 1:
            c = f.read(1024)  # read
            if not c:
                break
            fota_obj.write(c, file_size)  # 写入.bin文件数据与文件总字节数

    fota_log.info("flush verify...")
    res = fota_obj.verify()  # 校验
    if res != 0:
        fota_log.error("verify error")
        return
    fota_log.info("flush power_reset...")
    utime.sleep(2)
    Power.powerRestart()  # 重启模块
def print_module_info(args):
    free_ram = gc.mem_free()
    free_rom = _thread.get_heap_size()
    vbat = Power.getVbatt()
    csq = net.csqQueryPoll()
    cdc_log.info('==================================================')
    cdc_log.info('free_ram : {} Bytes'.format(free_ram))
    cdc_log.info('free_rom : {} Bytes'.format(free_rom))
    cdc_log.info('vbat     : {} mV'.format(vbat))
    cdc_log.info('CSQ      : {} '.format(csq))
    cdc_log.info('==================================================')
Example #6
0
 def _download_fota(self, fireware_url):
     file_name = self._download_one_file(fireware_url)
     file_exist = bluestone_common.BluestoneCommon.check_file_exist(file_name)
     if not file_exist:
         _fota_log.error("Cannot download fireware file from '{}'".format(download_url))
         return
     
     fota_obj = fota()
     file_size = uos.stat(file_name)[6]
     with open(file_name, "rb") as f:
         while True:
             count = f.read(1024)
             if not count:
                 break
             fota_obj.write(count, file_size)
         
     _fota_log.info("Flush fireware and verify...")
     res = fota_obj.verify()
     if res != 0:
         _fota_log.error("Fireware verify failed ")
     
     _fota_log.info("Fireware was downloaded, restarting system to take effect")
     utime.sleep_ms(2000)
     Power.powerRestart()
Example #7
0
    def _uart_read(self, name, uart):
        _uart_log.info("UART {} start with {}".format(name, uart))
        config = None
        loop = True
        restart = False

        while loop:
            num = uart.any()
            utime.sleep_ms(50)
            num2 = uart.any()
            if num != num2:
                continue

            if num:
                _uart_log.info("UART ready data length is {}".format(num))
                msg = uart.read(num)

                # 初始数据是字节类型(bytes),将字节类型数据进行编码
                utf8_msg = msg.decode()
                if num < 1024:
                    _uart_log.info("UART read message is {}".format(utf8_msg))

                is_json = bluestone_common.BluestoneCommon.is_json(utf8_msg)
                if not is_json:
                    self.send_message(name, utf8_msg)
                    continue
                try:
                    config_setting = ujson.loads(utf8_msg)
                    config_keys = config_setting.keys()
                    if 'payload' in config_keys:  # uart write payload, ignore it
                        continue
                    for key in config_setting:
                        config = config_setting[key]
                        exist = self.bs_config.check_key_exist(key)
                        if exist:
                            self.bs_config.update_config(key, config)
                        self._handle_cmd(key, config)
                    if name in config_keys:
                        config = config_setting[name]  # 保证重启逻辑的数据正确性
                        loop = False
                        restart = False
                    else:
                        restart = self.bs_config.check_key_restart(key)
                        if restart:
                            loop = False
                    self.send_message(name, config_setting)
                except Exception as err:
                    _uart_log.error(
                        "Cannot handle read command for uart, the error is {}".
                        format(err))
            else:
                continue
            utime.sleep_ms(300)

        if restart:
            restart = False
            _uart_log.info(
                "New configuration was received from uart, restarting system to take effect"
            )
            Power.powerRestart()
        else:
            self.restart_uart(name, config)
Example #8
0
def CallBack(t):
    Power.powerDown()