From f0962d61d0c8c365af2bb0ede60108336ef20b86 Mon Sep 17 00:00:00 2001 From: ctlove0523 <478309639@qq.com> Date: Tue, 13 Apr 2021 08:23:09 +0800 Subject: [PATCH] async device --- device.go | 36 +++++++++++++++++++++++++++++++++ options.go | 10 ++++++++++ samples/log/log_samples.go | 41 ++++++++++++++++---------------------- samples/message/msg.go | 7 ++++--- 4 files changed, 67 insertions(+), 27 deletions(-) diff --git a/device.go b/device.go index 5f0e058..23791fb 100644 --- a/device.go +++ b/device.go @@ -1,6 +1,7 @@ package iot import ( + "fmt" "github.com/golang/glog" uuid "github.com/satori/go.uuid" "time" @@ -16,6 +17,7 @@ type Device interface { UploadFile(filename string) bool DownloadFile(filename string) bool ReportDeviceInfo(swVersion, fwVersion string) + ReportLogs(logs []DeviceLogEntry) bool } type iotDevice struct { @@ -56,6 +58,40 @@ func (device *iotDevice) SetPropertyQueryHandler(handler DevicePropertyQueryHand device.base.SetPropertyQueryHandler(handler) } +func (device *iotDevice) ReportLogs(logs []DeviceLogEntry) bool { + var services []ReportDeviceLogServiceEvent + + for _, logEntry := range logs { + service := ReportDeviceLogServiceEvent{ + BaseServiceEvent: BaseServiceEvent{ + ServiceId: "$log", + EventType: "log_report", + EventTime: GetEventTimeStamp(), + }, + Paras: logEntry, + } + + services = append(services, service) + } + + request := ReportDeviceLogRequest{ + Services: services, + } + + fmt.Println(Interface2JsonString(request)) + + topic := FormatTopic(DeviceToPlatformTopic, device.base.Id) + + token := device.base.Client.Publish(topic, 1, false, Interface2JsonString(request)) + + if token.Wait() && token.Error() != nil { + glog.Errorf("device %s report log failed", device.base.Id) + return false + } else { + return true + } +} + func (device *iotDevice) SendMessage(message Message) bool { messageData := Interface2JsonString(message) if token := device.base.Client.Publish(FormatTopic(MessageUpTopic, device.base.Id), device.base.qos, false, messageData); diff --git a/options.go b/options.go index 8eae98d..80c0e77 100644 --- a/options.go +++ b/options.go @@ -296,6 +296,16 @@ type ReportDeviceInfoEventParas struct { FwVersion string `json:"fw_version,omitempty"` } +// 上报设备日志请求 +type ReportDeviceLogRequest struct { + Services []ReportDeviceLogServiceEvent `json:"services,omitempty"` +} + +type ReportDeviceLogServiceEvent struct { + BaseServiceEvent + Paras DeviceLogEntry `json:"paras,omitempty"` +} + // 设备状态日志收集器 type DeviceStatusLogCollector func(endTime string) []DeviceLogEntry diff --git a/samples/log/log_samples.go b/samples/log/log_samples.go index ade4b6c..454922e 100644 --- a/samples/log/log_samples.go +++ b/samples/log/log_samples.go @@ -10,31 +10,24 @@ import ( 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 { - 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() + var 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) + } + + for i := 0; i < 100; i++ { + result := device.ReportLogs(entries) + fmt.Println(result) + + time.Sleep(2 * time.Second) + } time.Sleep(1 * time.Minute) } diff --git a/samples/message/msg.go b/samples/message/msg.go index 88eb39a..c9ed4c8 100644 --- a/samples/message/msg.go +++ b/samples/message/msg.go @@ -32,8 +32,9 @@ func main() { Content: "Hello Huawei IoT Platform", } - sendMsgResult:=device.SendMessage(message) - - fmt.Printf("send message %v",sendMsgResult) + for i := 0; i < 100; i++ { + sendMsgResult := device.SendMessage(message) + fmt.Printf("send message %v", sendMsgResult) + } }