From fd6ffc19b9906bff8ca69660dcebdd889fa087f4 Mon Sep 17 00:00:00 2001 From: ctlove0523 <478309639@qq.com> Date: Mon, 1 Mar 2021 23:51:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=82=E6=AD=A5=E5=A4=84=E7=90=86=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iot_device.go | 42 ++++++++++++++--------------- samples/command/platform_command.go | 1 + 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/iot_device.go b/iot_device.go index 54a6410..9330ef2 100644 --- a/iot_device.go +++ b/iot_device.go @@ -366,28 +366,28 @@ func (device *iotDevice) createMessageMqttHandler() func(client mqtt.Client, mes func (device *iotDevice) createCommandMqttHandler() func(client mqtt.Client, message mqtt.Message) { commandHandler := func(client mqtt.Client, message mqtt.Message) { - command := &Command{} - if json.Unmarshal(message.Payload(), command) != nil { - glog.Warningf("unmarshal platform command failed,device id = %s,message = %s", device.Id, message) - } - - handleFlag := true - for _, handler := range device.commandHandlers { - handleFlag = handleFlag && handler(*command) - } - var res string - if handleFlag { - glog.Infof("device %s handle command success", device.Id) - res = Interface2JsonString(CommandResponse{ - ResultCode: 0, - }) - } else { - glog.Warningf("device %s handle command failed", device.Id) - res = Interface2JsonString(CommandResponse{ - ResultCode: 1, - }) - } go func() { + command := &Command{} + if json.Unmarshal(message.Payload(), command) != nil { + glog.Warningf("unmarshal platform command failed,device id = %s,message = %s", device.Id, message) + } + + handleFlag := true + for _, handler := range device.commandHandlers { + handleFlag = handleFlag && handler(*command) + } + var res string + if handleFlag { + glog.Infof("device %s handle command success", device.Id) + res = Interface2JsonString(CommandResponse{ + ResultCode: 0, + }) + } else { + glog.Warningf("device %s handle command failed", device.Id) + res = Interface2JsonString(CommandResponse{ + ResultCode: 1, + }) + } if token := device.client.Publish(FormatTopic(CommandResponseTopic, device.Id)+GetTopicRequestId(message.Topic()), 1, false, res); token.Wait() && token.Error() != nil { glog.Infof("device %s send command response failed", device.Id) diff --git a/samples/command/platform_command.go b/samples/command/platform_command.go index e8b2dda..f56d492 100644 --- a/samples/command/platform_command.go +++ b/samples/command/platform_command.go @@ -22,6 +22,7 @@ func main() { // 添加用于处理平台下发命令的callback device.AddCommandHandler(func(command iot.Command) bool { fmt.Println("I get command from platform") + time.Sleep(10 * time.Second) return true }) time.Sleep(10 * time.Minute)