class IotDevice extends EventEmitter {
constructor({serverAddress = “127.0.0.1:8883”, productName, deviceName, secret, clientID, storePath} = {}) {
super();
this.serverAddress = mqtts://${serverAddress}
…
}
handleCommand({commandName, requestID, encoding, payload, expiresAt, commandType = “cmd”}) {
…
var self = this
var respondCommand = function (respData) {
var topic = ${commandType}_resp/${self.productName}/${self.deviceName}/${commandName}/${requestID}/${new ObjectId().toHexString()}
self.client.publish(topic, respData, {
qos: 1
})
module.exports = IotDevice;
在下面的另一个文件中引用上面模块中的handleCommand的内嵌函数respondCommand,这个方法对不对,如果可以请讲一下,怎样的情况下能这样引用?
var IotDevice = require("…/sdk/iot_device") require(‘dotenv’).config() var path = require(‘path’);
var device = new IotDevice({
productName: process.env.PRODUCT_NAME,
…
})
device.on(“command”, function (command, data, respondCommand) {
if (command == “ping”) {
console.log(get ping with: ${data.readUInt32BE(0)}
)
…
respondCommand(buf)
}