支持设备查询设备影子数据

This commit is contained in:
ctlove0523 2020-12-20 00:14:13 +08:00
parent 85be1101ee
commit 8588cd9d49
6 changed files with 95 additions and 11 deletions

6
go.mod
View File

@ -2,4 +2,8 @@ module huaweicloud-iot-device-sdk-go
go 1.15
require github.com/eclipse/paho.mqtt.golang v1.3.0
require (
github.com/eclipse/paho.mqtt.golang v1.3.0
github.com/satori/go.uuid v1.2.0
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

9
go.sum
View File

@ -2,9 +2,18 @@ github.com/eclipse/paho.mqtt.golang v1.3.0 h1:MU79lqr3FKNKbSrGN7d7bNYqh8MwWW7Zcx
github.com/eclipse/paho.mqtt.golang v1.3.0/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

View File

@ -87,5 +87,24 @@ func FailedPropertiesSetResponse() IotDevicePropertyDownResponse {
type IotDevicePropertyQueryHandler func(query IotDevicePropertyQueryRequest) IotServicePropertyEntry
type IotDevicePropertyQueryRequest struct {
ObjectDeviceId string `json:"object_device_id"`
ServiceId string `json:"service_id"`
ServiceId string `json:"service_id"`
}
// 设备获取设备影子数据
type IotDevicePropertyQueryResponseHandler func(response IotDevicePropertyQueryResponse)
type IotDevicePropertyQueryResponse struct {
ObjectDeviceId string `json:"object_device_id"`
Shadow []IotDeviceShadowData `json:"shadow"`
}
type IotDeviceShadowData struct {
ServiceId string `json:"service_id"`
Desired IotDeviceShadowPropertiesData `json:"desired"`
Reported IotDeviceShadowPropertiesData `json:"reported"`
Version int `json:"version"`
}
type IotDeviceShadowPropertiesData struct {
Properties interface{} `json:"properties"`
EventTime string `json:"event_time"`
}

View File

@ -27,4 +27,10 @@ const (
PropertiesQueryRequestTopic string = "$oc/devices/{device_id}/sys/properties/get/#"
PropertiesQueryResponseTopicName string = "propertiesQueryResponseTopicName"
PropertiesQueryResponseTopic string = "$oc/devices/{device_id}/sys/properties/get/response/request_id="
// 设备侧获取平台的设备影子数据
DeviceShadowQueryRequestTopicName string = "deviceShadowQueryRequestTopicName"
DeviceShadowQueryRequestTopic string = "$oc/devices/{device_id}/sys/shadow/get/request_id="
DeviceShadowQueryResponseTopicName string = "deviceShadowQueryResponseTopicName"
DeviceShadowQueryResponseTopic string = "$oc/devices/{device_id}/sys/shadow/get/response/#"
)

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/satori/go.uuid"
"huaweicloud-iot-device-sdk-go/handlers"
"strings"
)
@ -13,22 +14,25 @@ type IotDevice interface {
IsConnected() bool
SendMessage(message handlers.IotMessage) bool
ReportProperties(properties handlers.IotServiceProperty) bool
QueryDeviceShadow(query handlers.IotDevicePropertyQueryRequest, handler handlers.IotDevicePropertyQueryResponseHandler)
AddMessageHandler(handler handlers.IotMessageHandler)
AddCommandHandler(handler handlers.IotCommandHandler)
AddPropertiesSetHandler(handler handlers.IotDevicePropertiesSetHandler)
SetPropertyQueryHandler(handler handlers.IotDevicePropertyQueryHandler)
//SetPropertiesQueryResponseHandler(handler handlers.IotDevicePropertyQueryResponseHandler)
}
type iotDevice struct {
Id string
Password string
Servers string
client mqtt.Client
commandHandlers []handlers.IotCommandHandler
messageHandlers []handlers.IotMessageHandler
propertiesSetHandlers []handlers.IotDevicePropertiesSetHandler
propertyQueryHandler handlers.IotDevicePropertyQueryHandler
topics map[string]string
Id string
Password string
Servers string
client mqtt.Client
commandHandlers []handlers.IotCommandHandler
messageHandlers []handlers.IotMessageHandler
propertiesSetHandlers []handlers.IotDevicePropertiesSetHandler
propertyQueryHandler handlers.IotDevicePropertyQueryHandler
propertiesQueryResponseHandler handlers.IotDevicePropertyQueryResponseHandler
topics map[string]string
}
func (device *iotDevice) createMessageMqttHandler() func(client mqtt.Client, message mqtt.Message) {
@ -117,6 +121,18 @@ func (device *iotDevice) createPropertiesQueryMqttHandler() func(client mqtt.Cli
return propertiesQueryHandler
}
func (device *iotDevice) createPropertiesQueryResponseMqttHandler() func(client mqtt.Client, message mqtt.Message) {
propertiesQueryResponseHandler := func(client mqtt.Client, message mqtt.Message) {
propertiesQueryResponse := &handlers.IotDevicePropertyQueryResponse{}
if json.Unmarshal(message.Payload(), propertiesQueryResponse) != nil {
fmt.Println("unmarshal failed")
}
device.propertiesQueryResponseHandler(*propertiesQueryResponse)
}
return propertiesQueryResponseHandler
}
func (device *iotDevice) Init() bool {
options := mqtt.NewClientOptions()
options.AddBroker(device.Servers)
@ -165,6 +181,14 @@ func (device *iotDevice) subscribeDefaultTopics() {
fmt.Println("subscribe properties query topic failed")
panic(0)
}
// 订阅查询设备影子响应的topic
if token := device.client.Subscribe(device.topics[DeviceShadowQueryResponseTopicName], 2, device.createPropertiesQueryResponseMqttHandler());
token.Wait() && token.Error() != nil {
fmt.Println("subscribe query device shadow datafailed")
panic(0)
}
}
func (device *iotDevice) IsConnected() bool {
@ -195,6 +219,16 @@ func (device *iotDevice) ReportProperties(properties handlers.IotServiceProperty
return true
}
func (device *iotDevice) QueryDeviceShadow(query handlers.IotDevicePropertyQueryRequest, handler handlers.IotDevicePropertyQueryResponseHandler) {
device.propertiesQueryResponseHandler = handler
requestId:= uuid.NewV4()
fmt.Println(requestId)
if token := device.client.Publish(device.topics[DeviceShadowQueryRequestTopicName]+requestId.String(), 2, false, JsonString(query));
token.Wait() && token.Error() != nil {
fmt.Println("query device shadow data failed")
}
}
func (device *iotDevice) AddMessageHandler(handler handlers.IotMessageHandler) {
if handler == nil {
return
@ -221,6 +255,10 @@ func (device *iotDevice) SetPropertyQueryHandler(handler handlers.IotDevicePrope
device.propertyQueryHandler = handler
}
//func (device *iotDevice) SetPropertiesQueryResponseHandler(handler handlers.IotDevicePropertyQueryResponseHandler) {
// device.propertiesQueryResponseHandler = handler
//}
func assembleClientId(device *iotDevice) string {
segments := make([]string, 4)
segments[0] = device.Id
@ -250,5 +288,7 @@ func CreateIotDevice(id, password, servers string) IotDevice {
device.topics[PropertiesSetResponseTopicName] = TopicFormat(PropertiesSetResponseTopic, id)
device.topics[PropertiesQueryRequestTopicName] = TopicFormat(PropertiesQueryRequestTopic, id)
device.topics[PropertiesQueryResponseTopicName] = TopicFormat(PropertiesQueryResponseTopic, id)
device.topics[DeviceShadowQueryRequestTopicName] = TopicFormat(DeviceShadowQueryRequestTopic, id)
device.topics[DeviceShadowQueryResponseTopicName] = TopicFormat(DeviceShadowQueryResponseTopic, id)
return device
}

View File

@ -59,6 +59,12 @@ func main() {
Services: content,
}
device.ReportProperties(services)
device.QueryDeviceShadow(handlers.IotDevicePropertyQueryRequest{
ServiceId: "value",
}, func(response handlers.IotDevicePropertyQueryResponse) {
fmt.Println(response.Shadow)
})
time.Sleep(time.Hour)
}