Skip to content

Latest commit

 

History

History
142 lines (111 loc) · 8.74 KB

网关子设备固件升级.md

File metadata and controls

142 lines (111 loc) · 8.74 KB

OTA网关子设备固件升级

网关子设备固件升级简介

设备固件升级又称 OTA,是物联网通信服务的重要组成部分。当物联设备有新功能或者需要修复漏洞时,设备可以通过 OTA 服务快速进行固件升级。请参考官网文档 控制台使用手册 固件升级

体验网关子设备固件升级需要在控制台中添加新的固件,请参考官网文档 开发者手册 设备固件升级

由于子设备无法直接和云端建立连接,网关设备延用设备OTA升级方式对子设备进行固件升级,支持网关子设备单台升级和批量升级。

编译运行示例程序

下载Hub JAVA SDK Demo示例代码

示例代码及源码在 hub-device-java 的module下。

准备开发环境

本示例使用的开发环境如下:

检查SDK的依赖关系

示例工程中使用的是源码依赖,也可以在maven中添加依赖,请参考 README-工程配置

填写认证连接设备的参数

请在 GatewaySampleTest.java 文件中填入所需的参数。

public class GatewaySampleTest {
    private static String mProductID = "YOUR_PRODUCT_ID"; //网关产品ID
	private static String mDevName = "YOUR_DEVICE_NAME"; //网关设备名称
	private static String mDevPSK = "YOUR_DEV_PSK"; //网关设备密钥(密钥认证)
	private static String mSubProductID = "YOUR_SUB_PRODUCT_ID"; //子设备产品ID
	private static String mSubDevName = "YOUR_SUB_DEV_NAME"; //子设备名称
	private static String mSubDevProductKey = "YOUR_SUB_DEV_PSK"; //子设备密钥(密钥认证)
}

运行Demo认证连接并执行子设备固件升级

当 MQTT 认证方式为密钥认证时,在 MqttConnectOptions 中不需要做 SSL 配置,走TCP,当您使用的sdk版本为3.3.0及以下时,密钥认证需要在 MqttOptions 中做 SSL 配置 options.setSocketFactory(AsymcSslUtils.getSocketFactory());

当 MQTT 认证方式为证书认证时,在 MqttConnectOptions 中添加 SSL 配置 options.setSocketFactory(AsymcSslUtils.getSocketFactoryByFile(workDir + mCertFilePath, workDir + mPrivKeyFilePath));

运行 GatewaySampleTest.java ,将 网关设备 进行 MQTT 认证连接,认证连接后,初始化OTA并使网关设备和子设备都订阅 Topic $ota/update/${productId}/${deviceName}。示例代码如下:

public static void main(String[] args) {
    mqttconnection = new TXMqttConnection(mProductID, mDevName, mDevPSK, new callBack());
    mqttconnection.setSubDevName(mSubDevName);  //设置子设备名称
    mqttconnection.setSubDevProductKey(mSubDevProductKey);  //设置子设备密钥(密钥认证)
    mqttconnection.setSubProductID(mSubProductID);  //设置子设备产品ID
    mqttconnection.connect(options, null);  //进行 MQTT 认证连接
}

public static class callBack extends TXMqttActionCallBack {

    @Override
    public void onConnectCompleted(Status status, boolean reconnect, Object userContext, String msg) {  //进行 MQTT 认证连接回调
        mqttconnection.initOTA(path2Store, oTACallBack);  //初始化OTA
    }

    private TXOTACallBack oTACallBack = new TXOTACallBack() {

        @Override
        public void onReportFirmwareVersion(int resultCode, String version, String resultMsg) { // 上报固件版本回调
        }

        @Override
        public boolean onLastestFirmwareReady(String url, String md5, String version) {
        	System.out.println("onLastestFirmwareReady url=" + url + " version " + version);
        	mqttconnection.gatewayDownSubdevApp(url, path2Store + "/" + md5, md5, version);
        	return true; // false 自动触发下载升级文件  true 需要手动触发下载升级文件
        }

        @Override
        public void onDownloadProgress(int percent, String version) { // OTA升级包下载进度回调
        	mqttconnection.gatewaySubdevReportProgress(percent, version);
        }

        @Override
        public void onDownloadCompleted(String outputFile, String version) { // OTA升级包下载完成回调
        	mqttconnection.gatewaySubdevReportStart(version);
        	mqttconnection.gatewaySubdevReportSuccess(version);
        }

        @Override
        public void onDownloadFailure(int errCode, String version) { // OTA升级包下载失败回调
        	mqttconnection.gatewaySubdevReportFail(errCode, "", version);
        }
    };
    @Override
    public void onSubscribeCompleted(Status status, IMqttToken asyncActionToken, Object userContext, String errMsg) {
        for (String topicEls : asyncActionToken.getTopics()) {
            if (topicEls.startsWith("$ota/update/")) {
                mqttconnection.gatewaySubdevReportVer("0.0"); // 上报当前的设备固件的版本号为0.0。
            }
        }
    ...
}

以下是子设备成功订阅 OTA 升级的 Topic 主题 和 上报当前版本号 的IDE控制台日志,示例中的版本号version传入的值为0.0。

TXMqttConnection connect 297  - Start connecting to ssl://AP9ZLEVFKT.iotcloud.tencentdevices.com:8883
TXMqttConnection onSuccess 268  - onSuccess!
ffffffffffff msg connected to ssl://AP9ZLEVFKT.iotcloud.tencentdevices.com:8883
TXMqttConnection subscribe 633  - Starting subscribe topic: $ota/update/AP9ZLEVFKT/gateway1
TXMqttConnection subscribe 633  - Starting subscribe topic: $ota/update/9RW4A8OOFK/door1
onSubscribeCompleted, status[OK], topics[[$ota/update/AP9ZLEVFKT/gateway1]], userContext[], errMsg[subscribe success]
onSubscribeCompleted, status[OK], topics[[$ota/update/9RW4A8OOFK/door1]], userContext[], errMsg[subscribe success]
TXMqttConnection publish 451  - Starting publish topic: $ota/report/9RW4A8OOFK/door1 Message: {"report":{"version":"0.0"},"type":"report_version"}
onPublishCompleted, status[OK], topics[[$ota/report/9RW4A8OOFK/door1]],   errMsg[publish success]

当在控制台中触发固件升级操作后,设备端会通过订阅的 OTA 升级的 Topic $ota/update/${productID}/${deviceName} 收到固件升级的消息

TXMqttConnection messageArrived 879  - Received topic: $ota/update/9RW4A8OOFK/door1, id: 0, message: {"file_size":234775,"md5sum":"f2f1b3317d4f1ef7f512bfae5050563b","type":"update_firmware","url":"https://ota-1255858890.cos.ap-guangzhou.myqcloud.com/100012619289_9RW4A8OOFK_0.0.1?sign=q-sign-algorithm%3Dsha1%26q-ak%3DAKIDdO8ldrUa0Uts4H5Gzx6FZ9nfedjpiCd7%26q-sign-time%3D1603710048%3B1603796448%26q-key-time%3D1603710048%3B1603796448%26q-header-list%3D%26q-url-param-list%3D%26q-signature%3Deb248051f6bef7756462b7f833f0608c81281cc1","version":"0.0.1"}
TXOTAImpl run 501  - fileLength 234775 bytes
TXOTAImpl run 510  - connect: https://ota-1255858890.cos.ap-guangzhou.myqcloud.com/100012619289_9RW4A8OOFK_0.0.1?sign=q-sign-algorithm%3Dsha1%26q-ak%3DAKIDdO8ldrUa0Uts4H5Gzx6FZ9nfedjpiCd7%26q-sign-time%3D1603710048%3B1603796448%26q-key-time%3D1603710048%3B1603796448%26q-header-list%3D%26q-url-param-list%3D%26q-signature%3Deb248051f6bef7756462b7f833f0608c81281cc1
TXOTAImpl checkServerTrusted 434  - checkServerTrusted OK!!
TXOTAImpl run 520  - totalLength 234775 bytes
TXOTAImpl run 543  - download 7789 bytes. percent:3
TXMqttConnection publish 451  - Starting publish topic: $ota/report/AP9ZLEVFKT/gateway1 Message: {"report":{"progress":{"result_msg":"","result_code":"0","state":"downloading","percent":"3"},"version":"0.0.1"},"type":"report_progress"}
onPublishCompleted, status[OK], topics[[$ota/report/9RW4A8OOFK/door1]],   errMsg[publish success]
...
TXOTAImpl run 543  - download 234775 bytes. percent:100
TXMqttConnection publish 451  - Starting publish topic: $ota/report/9RW4A8OOFK/door1 Message: {"report":{"progress":{"result_msg":"","result_code":"0","state":"done"},"version":"0.0.1"},"type":"report_progress"}
onPublishCompleted, status[OK], topics[[$ota/report/9RW4A8OOFK/door1]],   errMsg[publish success]

以上日志为 收到固件升级的消息 ,下载并显示下载进度,下载新版本固件成功后,上报最新的版本号,此时示例中上报的最新版本号version为0.0.1。