OTA-远程升级组件(阿里云物联网平台)

更新时间:2023-04-04 05:42:40

本文介绍OTA-远程升级组件所提供的相关方法说明,支持的硬件平台列表以及参考示例代码。

硬件平台

产品型号

是否支持

硬件类型

固件版本号

产品型号

是否支持

硬件类型

固件版本号

HaaS510

DTU

4.0.1

HaaS520

DTU

4.0.1

HaaS530

DTU

4.0.1

HaaS531

DTU

4.0.1

HaaS632-LT32V

蜂窝模组

4.0.1

HaaS600-EC600S

蜂窝模组

4.0.1

HaaS600-EC600N

蜂窝模组

4.0.1

HaaS600-EC600U

蜂窝模组

4.0.1

HaaS600-N715

蜂窝模组

4.0.1

open(Object option)

创建一个OTA实例。

入参

属性

类型

是否必填

描述

属性

类型

是否必填

描述

iot

Text

传入IoT组件的device handle

返回

OTA实例,返回空表示失败。

report(Object option)

上报版本号。

入参

属性

类型

是否必填

描述

属性

类型

是否必填

描述

device_handle

Number

传入IoT组件的device handle指针。

product_key

String

设备的产品密钥。

device_name

String

设备名称。

module_name

String

模块名称。

version

String

模块的版本号。

返回

无。

OTA触发事件: new

上报属性

属性

类型

是否必填

描述

属性

类型

是否必填

描述

url

String

下载文件的URL。

length

Number

固件的大小。

module_name

String

模块名称。

version

String

固件的版本号。

hash

String

Hash值。

hash_type

String

Hash类型。

dowload(Object option, Function callback)

下载文件。

入参

属性

类型

是否必填

描述

属性

类型

是否必填

描述

url

String

下载文件的URL。

store_path

String

存储下载文件的路径包括文件名称。

callback

Function

下载完后的回调函数。

返回

无。

verify(Object option, Function callback)

校验下载的文件。

入参

属性

类型

是否必填

描述

属性

类型

是否必填

描述

length

String

下载文件的长度。

hash_type

String

Hash类型。

  • md5。

  • sha256。

store_path

String

存储下载文件的路径。

callback

Functiom

校验结果的回调函数。

返回

无。

upgrade(Object option, Function callback)

新脚本覆盖和加载。

入参

属性

类型

是否必填

描述

属性

类型

是否必填

描述

length

String

下载文件的长度。

store_path

String

存储下载文件的路径。

install_path

String

安装脚本的路径。

callback

Function

升级成功的回调函数。

返回

无。

sysUpgrade(Object option, Function callback)

系统固件升级,当module_name = system时调用此函数。

入参

属性

类型

是否必填

描述

属性

类型

是否必填

描述

length

String

下载文件的长度。

hash_type

String

Hash类型。

  • md5。

  • sha256。

hash

String

固件Hash值。

module_name

Function

模块名称。

version

String

模块的版本号。

url

Function

下载文件的URL。

返回

升级成功设备会重启,失败会返回非0值。

unpack(Object option, Function callback)

资源文件升级解包。

入参

属性

类型

是否必填

描述

属性

类型

是否必填

描述

length

String

下载文件的长度。

store_path

String

存储文件的路径。

unpack_path

String

要解压文件的路径。

callback

Functiom

解压完成的回调函数。

返回

无。

failreport(Object option)

升级过程异常时,上报云端。

入参

属性

类型

是否必填

描述

属性

类型

是否必填

描述

device_handle

Number

下载文件的长度。

product_key

String

设备的产品密钥。

device_name

String

设备名称。

module_name

String

模块名称。

msg

String

异常原因。

返回

  • 0:表示成功。

  • 其他:表示失败。

示例代码

import * as network from 'network';
import * as iot from 'iot';
import * as appota from 'appota'
import * as fs from 'fs'

/* device info */
var productKey = '';      /* your productKey */
var deviceName = '';      /* your deviceName */
var deviceSecret = '';  /* your deviceSecret */
var app_module_name = 'default';
var default_ver = '1.0.0';
var resource_module_name = 'resource';
var resource_ver = '0.0.0';
var resource_ver_file_path = 'jsamp/resource.json';

var ota;
var device;
var status;

/* download info */
var info = {
  url: '',
  store_path:'jsamp/pack.bin', 
  install_path:'jsamp/', 
  unpack_path:'jsamp/',
  length: 0,
  hashType: '',
  hash: ''
}

function createDevice(){
  if (!(iot && iot.device)) {
    throw new Error("iot: [failed] require(\'iot\')");
  }
  console.log('iot: [success] require(\'iot\')');
  
  device = iot.device({
    productKey: productKey,
    deviceName: deviceName,
    deviceSecret: deviceSecret,
    region: 'cn-shanghai',
  });
  
  device.on('connect', function() {
    console.log('iot device connected success!');
    var iotDeviceHandle = device.getDeviceHandle();
    ota = appota.open(iotDeviceHandle);
    var res = ota.report({
      device_handle: iotDeviceHandle,
      product_key: productKey,
      device_name: deviceName,
      module_name: app_module_name,
      version: default_ver
    });
    console.log('report resource module ver!');
    var fsize = fs.fileSize(resource_ver_file_path);
    if (fsize <= 0) {
      resource_ver = '0.0.0';
    }
    else {
      console.log('==================');
      var resource_info_value = fs.readSync(resource_ver_file_path);
      var sver = JSON.parse(resource_info_value);
      console.log('resource ver ' + sver.version);
      resource_ver = sver.version;
    }
    var res = ota.report({
      device_handle: iotDeviceHandle,
      product_key: productKey,
      device_name: deviceName,
      module_name: resource_module_name,
      version: resource_ver
    });
    ota.on('new', function(res) {
      console.log('length is ' + res.length);
      console.log('module_name is ' + res.module_name);
      console.log('version is ' + res.version);
      console.log('url is ' + res.url);
      console.log('hash is ' + res.hash);
      console.log('hash_type is ' + res.hash_type);
      
      info.url = res.url;
      info.length = res.length;
      info.module_name = res.module_name;
      info.version = res.version;
      info.hash = res.hash;
      info.hashType = res.hash_type;
      
      if (info.module_name == 'system')
      {
        console.log('download system bin.');
        ota.sysUpgrade({
          length: info.length,
          hash_type: info.hashType,
          hash: info.hash,
          module_name: info.module_name,
          version: info.version,
          url: info.url
        }, function(res) {
          if (res >= 0) {
            console.log('system upgrade success');
          } else {
            console.log('system upgrade failed.');
          }
        })
      }
      else if (info.module_name == 'default') {
        ota.download({
          url: info.url,
          store_path: info.store_path
        }, function(res) {
          if (res >= 0) {
            console.log('verify start');
            console.log(info.hashType);
            ota.verify({
              length: info.length,
              hash_type: info.hashType,
              hash: info.hash,
              store_path: info.store_path
            }, function(res) {
              if (res >= 0) {
                ota.upgrade({
                  length: info.length,
                  store_path: info.store_path,
                  install_path: info.install_path
                }, function(res) {
                  if (res < 0) {
                    console.log('upgrade success')
                  }
                })
              } else {
                console.log('verify failed');
              }
            })
          } else {
            console.log('download failed');
          }
        });
      }
      else if (info.module_name == 'resource') {
        console.log('module name:  resource, begin download');
        ota.download({
          url: info.url,
          store_path: info.store_path
        }, function(res) {
          if (res >= 0) {
            console.log('resource verify start');
            console.log(info.hashType);
            ota.verify({
              length: info.length,
              hash_type: info.hashType,
              hash: info.hash,
              store_path: info.store_path
            }, function(res) {
              if (res >= 0) {
                ota.unpack({
                  length: info.length,
                  store_path: info.store_path,
                  unpack_path: info.unpack_path
                }, function(res) {
                  if (res >= 0) {
                    console.log('unpack success');
                    var new_fsize = fs.fileSize(resource_ver_file_path);
                    if (new_fsize <= 0) {
                      console.log('new resource file ' + resource_ver_file_path + 'size is 0');
                    }
                    else {
                      console.log('==================');
                      var new_info_value = fs.readSync(resource_ver_file_path);
                      console.log('new resource ' + new_info_value);
                      var new_ver = JSON.parse(new_info_value);
                      console.log('new resource ver ' + new_ver.version);
                      resource_ver = new_ver.version;
                    }
                    var res = ota.report({
                      device_handle: iotDeviceHandle,
                      product_key: productKey,
                      device_name: deviceName,
                      module_name: resource_module_name,
                      version: resource_ver
                    });
                  }
                  else {
                    console.log('unpack fail');
                  }
                })
              } else {
                console.log('resource verify failed');
              }
            });
          } else {
            console.log('resource download failed');
          }
        });
        console.log('resource download finish');
      }
    });
  })
}

var net=network.openNetWorkClient();
var status=net.getStatus();
console.log('net status is: '+status);

if (status=='connect') {
  createDevice();
}else{
  net.on('connect',function(){
    console.log('net is connected success!');
    createDevice();
  });
}
  • 本页导读 (0)
  • 硬件平台
  • open(Object option)
  • 入参
  • 返回
  • report(Object option)
  • 入参
  • 返回
  • OTA触发事件: new
  • 上报属性
  • dowload(Object option, Function callback)
  • 入参
  • 返回
  • verify(Object option, Function callback)
  • 入参
  • 返回
  • upgrade(Object option, Function callback)
  • 入参
  • 返回
  • sysUpgrade(Object option, Function callback)
  • 入参
  • 返回
  • unpack(Object option, Function callback)
  • 入参
  • 返回
  • failreport(Object option)
  • 入参
  • 返回
  • 示例代码