支持处理来自平台的命令和响应平台的命令

This commit is contained in:
ctlove0523 2020-12-19 00:51:55 +08:00
parent d30f372762
commit 4a324b4a0f
3 changed files with 32 additions and 16 deletions

12
iotdevice/constants.go Normal file
View File

@ -0,0 +1,12 @@
package iotdevice
const (
MessageDownTopic string = "$oc/devices/{device_id}/sys/messages/down"
MessageDownTopicName string = "messageDownTopicName"
CommandDownTopicName string = "commandDownTopicName"
CommandDownTopic string = "$oc/devices/{device_id}/sys/commands/#"
CommandResponseTopicName string = "commandResponseTopicName"
CommandResponseTopic string = "$oc/devices/{device_id}/sys/commands/response/request_id="
)

View File

@ -20,20 +20,17 @@ type IotMessageHandler interface {
Handle(message IotMessage) Handle(message IotMessage)
} }
type iotDevice struct { type iotDevice struct {
Id string Id string
Password string Password string
Servers string Servers string
commandHandlers []handlers.IotCommandHandler commandHandlers []handlers.IotCommandHandler
client mqtt.Client client mqtt.Client
messageHandlers []IotMessageHandler messageHandlers []IotMessageHandler
messageDownTopic string topics map[string]string
commandDownTopic string
commandResponseTopic string
} }
func (device *iotDevice) createCommandMqttHandler() func(client mqtt.Client, message mqtt.Message) { func (device *iotDevice) createCommandMqttHandler() func(client mqtt.Client, message mqtt.Message) {
commandHandler := func(client mqtt.Client, message mqtt.Message) { commandHandler := func(client mqtt.Client, message mqtt.Message) {
command := &handlers.IotCommand{} command := &handlers.IotCommand{}
if json.Unmarshal(message.Payload(), command) != nil { if json.Unmarshal(message.Payload(), command) != nil {
@ -50,7 +47,7 @@ func (device *iotDevice) createCommandMqttHandler() func(client mqtt.Client, mes
} else { } else {
res = JsonString(handlers.FailedIotCommandResponse()) res = JsonString(handlers.FailedIotCommandResponse())
} }
if token := device.client.Publish(device.commandResponseTopic+CommandRequestId(message.Topic()), 1, false, res); if token := device.client.Publish(device.topics[CommandResponseTopicName]+CommandRequestId(message.Topic()), 1, false, res);
token.Wait() && token.Error() != nil { token.Wait() && token.Error() != nil {
fmt.Println("send command response success") fmt.Println("send command response success")
} }
@ -73,7 +70,7 @@ func (device *iotDevice) Init() bool {
return false return false
} }
subscirbeToken := device.client.Subscribe(device.commandDownTopic, 2, device.createCommandMqttHandler()) subscirbeToken := device.client.Subscribe(device.topics[CommandDownTopicName], 2, device.createCommandMqttHandler())
if subscirbeToken.Wait() && subscirbeToken.Error() != nil { if subscirbeToken.Wait() && subscirbeToken.Error() != nil {
fmt.Println(len(subscirbeToken.Error().Error())) fmt.Println(len(subscirbeToken.Error().Error()))
fmt.Println("subscribe failed") fmt.Println("subscribe failed")
@ -139,9 +136,12 @@ func CreateIotDevice(id, password, servers string) IotDevice {
device.Servers = servers device.Servers = servers
device.messageHandlers = []IotMessageHandler{} device.messageHandlers = []IotMessageHandler{}
device.commandHandlers = []handlers.IotCommandHandler{} device.commandHandlers = []handlers.IotCommandHandler{}
device.messageDownTopic = strings.ReplaceAll("$oc/devices/{device_id}/sys/messages/down", "{device_id}", id)
device.commandDownTopic = strings.ReplaceAll("$oc/devices/{device_id}/sys/commands/#", "{device_id}", id) // 初始化设备相关的所有topic
device.commandResponseTopic = strings.ReplaceAll("$oc/devices/{device_id}/sys/commands/response/request_id=", "{device_id}", id) device.topics = make(map[string]string)
device.topics[MessageDownTopicName] = TopicFormat(MessageDownTopic, id)
device.topics[CommandDownTopicName] = TopicFormat(CommandDownTopic, id)
device.topics[CommandResponseTopicName] = TopicFormat(CommandResponseTopic, id)
return device return device
} }

View File

@ -34,3 +34,7 @@ func JsonString(v interface{}) string {
func CommandRequestId(topic string) string { func CommandRequestId(topic string) string {
return strings.Split(topic, "=")[1] return strings.Split(topic, "=")[1]
} }
func TopicFormat(topic, deviceId string) string {
return strings.ReplaceAll(topic, "{device_id}", deviceId)
}