2020-12-20 15:08:43 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2020-12-20 15:53:53 +08:00
|
|
|
|
iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go"
|
2021-03-30 08:49:40 +08:00
|
|
|
|
"github.com/ctlove0523/huaweicloud-iot-device-sdk-go/samples"
|
2020-12-20 15:08:43 +08:00
|
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
// 创建一个设备并初始化
|
2021-03-30 08:49:40 +08:00
|
|
|
|
device := samples.CreateDevice()
|
2020-12-20 15:08:43 +08:00
|
|
|
|
device.Init()
|
|
|
|
|
|
|
|
|
|
// 注册平台下发消息的callback,当收到平台下发的消息时,调用此callback.
|
|
|
|
|
// 支持注册多个callback,并且按照注册顺序调用
|
|
|
|
|
device.AddMessageHandler(func(message iot.Message) bool {
|
|
|
|
|
fmt.Println("first handler called" + iot.Interface2JsonString(message))
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
device.AddMessageHandler(func(message iot.Message) bool {
|
|
|
|
|
fmt.Println("second handler called" + iot.Interface2JsonString(message))
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//向平台发送消息
|
|
|
|
|
message := iot.Message{
|
|
|
|
|
ObjectDeviceId: uuid.NewV4().String(),
|
|
|
|
|
Name: "Fist send message to platform",
|
|
|
|
|
Id: uuid.NewV4().String(),
|
|
|
|
|
Content: "Hello Huawei IoT Platform",
|
|
|
|
|
}
|
2021-03-30 08:49:40 +08:00
|
|
|
|
|
2021-04-13 08:23:09 +08:00
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
|
sendMsgResult := device.SendMessage(message)
|
|
|
|
|
fmt.Printf("send message %v", sendMsgResult)
|
|
|
|
|
}
|
2020-12-20 15:08:43 +08:00
|
|
|
|
|
|
|
|
|
}
|