async device
This commit is contained in:
parent
19d8a79cf1
commit
f0962d61d0
36
device.go
36
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);
|
||||
|
|
10
options.go
10
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
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue