在这一节我们开始来实现 OTA 升级的设备端功能。
上报升级进度
首先我们使用一个类来封装上报升级进度的操作:
//IotHub_Device/sdk/ota_progress.js
const ObjectId = require('bson').ObjectID;
class OTAProgress {
constructor({productName, deviceName, mqttClient, version, type}) {
this.productName = productName
this.deviceName = deviceName
this.mqttClient = mqttClient
this.version = version
this.type = type
}
sendProgress(progress) {
var meta = {
version: this.version,
type: this.type
}
var topic = `update_ota_status/${this.productName}/${this.deviceName}/${new ObjectId().toHexString()}`
this.mqttClient.publish(topic, JSON.stringify({...meta, ...progress}), {qos: 1})
}
download(percent, desc = "download") {
this.sendProgress({progress: percent, desc: desc})
}
downloadError(desc = "download error")