diff --git a/base_device.go b/base_device.go index 053d44e..671bba1 100644 --- a/base_device.go +++ b/base_device.go @@ -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 diff --git a/samples/log/log_samples.go b/samples/log/log_samples.go index 6ec8daf..ade4b6c 100644 --- a/samples/log/log_samples.go +++ b/samples/log/log_samples.go @@ -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) }