设备接入 IoTDA-快速接入:命令下发

时间:2025-09-18 09:37:22

命令下发

设置命令监听器用来接收平台下发的命令,在回调接口里,将对命令进行处理,并上报响应。

  1. ./samples/command/platform_command.go是一个处理平台命令下发的例子。使用CommandHandler函数作为命令监听器,即:
     1 2 3 4 5 6 7 8 910
        mqttDevice.Client.AddCommandHandler(func(command model.Command) (bool, interface{}) {glog.Infof("I get command from platform")glog.Infof("command device id is %s", command.ObjectDeviceId)glog.Infof("command name is %s", command.CommandName)glog.Infof("command serviceId is %s", command.ServiceId)glog.Infof("command params is %v", command.Paras)return true, map[string]interface{}{"cost_time": 12,}})
  2. 当device收到命令时将自动调用CommandHandler函数,并将函数响应结果返回给平台。
  3. 执行main方法,在平台给设备下发命令,查看代码日志如下。
    图11 go语言命令下发
support.huaweicloud.com/sdkreference-iothub/iot_02_00925.html