sync device

This commit is contained in:
ctlove0523 2021-04-08 07:47:50 +08:00
parent c3db4bbd64
commit 19d8a79cf1
2 changed files with 29 additions and 2 deletions

View File

@ -380,7 +380,7 @@ func (device *baseIotDevice) subscribeDefaultTopics() {
panic(0)
}
// 订阅平台下发的文件上传和下载URL topic
// 订阅平台下发到设备的事件
topic = FormatTopic(PlatformEventToDeviceTopic, device.Id)
if token := device.Client.Subscribe(topic, device.qos, device.handlePlatformToDeviceData());
token.Wait() && token.Error() != nil {
@ -448,6 +448,7 @@ func (device *baseIotDevice) handlePlatformToDeviceData() func(client mqtt.Clien
case "log_config":
// 平台下发日志收集通知
fmt.Println("platform send log collect command")
logConfig := &LogCollectionConfig{}
if json.Unmarshal([]byte(Interface2JsonString(entry.Paras)), logConfig) != nil {
continue

View File

@ -1,14 +1,40 @@
package main
import (
"fmt"
iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go"
"github.com/ctlove0523/huaweicloud-iot-device-sdk-go/samples"
"strconv"
"time"
)
func main() {
device := samples.CreateDevice()
device.AddMessageHandler(func(message iot.Message) bool {
fmt.Println(message)
return true
})
device.SetSubDevicesAddHandler(func(devices iot.SubDeviceInfo) {
fmt.Println(device)
})
device.SetSubDevicesDeleteHandler(func(devices iot.SubDeviceInfo) {
fmt.Println(device)
})
device.SetDeviceStatusLogCollector(func(endTime string) []iot.DeviceLogEntry {
return []iot.DeviceLogEntry{}
fmt.Println("begin to collect log")
entries := []iot.DeviceLogEntry{}
for i := 0; i < 10; i++ {
entry := iot.DeviceLogEntry{
Type: "DEVICE_MESSAGE",
Timestamp: iot.GetEventTimeStamp(),
Content: "message hello " + strconv.Itoa(i),
}
entries = append(entries, entry)
}
return entries
})
device.Init()
time.Sleep(1 * time.Minute)
}