Skip to content

Commit

Permalink
代码重构
Browse files Browse the repository at this point in the history
  • Loading branch information
ctlove0523 committed Dec 26, 2020
1 parent 74d8421 commit 9c5af1c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
17 changes: 6 additions & 11 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,30 @@ const (
PropertiesUpTopic string = "$oc/devices/{device_id}/sys/properties/report"

//平台设置属性topic
//PropertiesSetRequestTopicName string = "propertiesSetRequestTopicName"
PropertiesSetRequestTopic string = "$oc/devices/{device_id}/sys/properties/set/#"

// 设备响应平台属性设置topic
//PropertiesSetResponseTopicName string = "propertiesSetResponseTopicName"
PropertiesSetResponseTopic string = "$oc/devices/{device_id}/sys/properties/set/response/request_id="

// 平台查询设备属性
PropertiesQueryRequestTopicName string = "propertiesQueryRequestTopicName"
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/#"

// 网关批量上报子设备属性
GatewayBatchReportSubDeviceTopicName string = "gatewayBatchReportSubDeviceTopicName"
//GatewayBatchReportSubDeviceTopicName string = "gatewayBatchReportSubDeviceTopicName"
GatewayBatchReportSubDeviceTopic string = "$oc/devices/{device_id}/sys/gateway/sub_devices/properties/report"

// 文件上传请求:获取上传和下载URL,上报结果
FileRequestTopicName string = "fileUploadRequestTopicName"
FileRequestTopic string = "$oc/devices/{device_id}/sys/events/up"

// 平台下发文件上传和下载URL
FileResponseTopicName string = "FileUploadResultTopic"
//FileResponseTopicName string = "FileUploadResultTopic"

FileActionUpload string = "upload"
FileActionDownload string = "download"
Expand Down
35 changes: 13 additions & 22 deletions iotdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ type iotDevice struct {
propertiesQueryResponseHandler DevicePropertyQueryResponseHandler
subDevicesAddHandler SubDevicesAddHandler
subDevicesDeleteHandler SubDevicesDeleteHandler
topics map[string]string
fileUrls map[string]string
}

Expand Down Expand Up @@ -228,7 +227,7 @@ func (device *iotDevice) DownloadFile(filename string) bool {
Services: services,
}

if token := device.client.Publish(device.topics[FileRequestTopicName], 1, false, Interface2JsonString(request));
if token := device.client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), 1, false, Interface2JsonString(request));
token.Wait() && token.Error() != nil {
glog.Warningf("publish file download request url failed")
return false
Expand Down Expand Up @@ -261,7 +260,7 @@ ENDFOR:

response := CreateFileUploadDownLoadResultResponse(filename, FileActionDownload, downloadFlag)

token := device.client.Publish(device.topics[FileResponseTopicName], 1, false, Interface2JsonString(response))
token := device.client.Publish(FormatTopic(PlatformEventToDeviceTopic, device.Id), 1, false, Interface2JsonString(response))
if token.Wait() && token.Error() != nil {
glog.Error("report file upload file result failed")
return false
Expand Down Expand Up @@ -289,7 +288,7 @@ func (device *iotDevice) UploadFile(filename string) bool {
Services: services,
}

if token := device.client.Publish(device.topics[FileRequestTopicName], 1, false, Interface2JsonString(request));
if token := device.client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), 1, false, Interface2JsonString(request));
token.Wait() && token.Error() != nil {
glog.Warningf("publish file upload request url failed")
return false
Expand Down Expand Up @@ -325,7 +324,7 @@ ENDFOR:

response := CreateFileUploadDownLoadResultResponse(filename, FileActionUpload, uploadFlag)

token := device.client.Publish(device.topics[FileResponseTopicName], 1, false, Interface2JsonString(response))
token := device.client.Publish(FormatTopic(PlatformEventToDeviceTopic, device.Id), 1, false, Interface2JsonString(response))
if token.Wait() && token.Error() != nil {
glog.Error("report file upload file result failed")
return false
Expand Down Expand Up @@ -462,7 +461,7 @@ func (device *iotDevice) createPropertiesQueryMqttHandler() func(client mqtt.Cli

queryResult := device.propertyQueryHandler(*propertiesQueryRequest)
responseToPlatform := Interface2JsonString(queryResult)
if token := device.client.Publish(device.topics[PropertiesQueryResponseTopicName]+GetTopicRequestId(message.Topic()), 1, false, responseToPlatform);
if token := device.client.Publish(FormatTopic(PropertiesQueryResponseTopic, device.Id)+GetTopicRequestId(message.Topic()), 1, false, responseToPlatform);
token.Wait() && token.Error() != nil {
glog.Warningf("device %s send properties query response failed.", device.Id)
}
Expand Down Expand Up @@ -534,7 +533,7 @@ func (device *iotDevice) ReportProperties(properties ServiceProperty) bool {
}

func (device *iotDevice) BatchReportSubDevicesProperties(service DevicesService) {
if token := device.client.Publish(device.topics[GatewayBatchReportSubDeviceTopicName], 2, false, Interface2JsonString(service));
if token := device.client.Publish(FormatTopic(GatewayBatchReportSubDeviceTopic, device.Id), 2, false, Interface2JsonString(service));
token.Wait() && token.Error() != nil {
glog.Warningf("device %s batch report sub device properties failed", device.Id)
}
Expand All @@ -543,7 +542,7 @@ func (device *iotDevice) BatchReportSubDevicesProperties(service DevicesService)
func (device *iotDevice) QueryDeviceShadow(query DevicePropertyQueryRequest, handler DevicePropertyQueryResponseHandler) {
device.propertiesQueryResponseHandler = handler
requestId := uuid.NewV4()
if token := device.client.Publish(device.topics[DeviceShadowQueryRequestTopicName]+requestId.String(), 2, false, Interface2JsonString(query));
if token := device.client.Publish(FormatTopic(DeviceShadowQueryRequestTopic, device.Id)+requestId.String(), 2, false, Interface2JsonString(query));
token.Wait() && token.Error() != nil {
glog.Warningf("device %s query device shadow data failed,request id = %s", device.Id, requestId)
}
Expand Down Expand Up @@ -585,16 +584,6 @@ func CreateIotDevice(id, password, servers string) Device {

device.fileUrls = map[string]string{}

// 初始化设备相关的所有topic
device.topics = make(map[string]string)
//device.topics[PropertiesSetRequestTopicName] = FormatTopic(PropertiesSetRequestTopic, id)
//device.topics[PropertiesSetResponseTopicName] = FormatTopic(PropertiesSetResponseTopic, id)
device.topics[PropertiesQueryRequestTopicName] = FormatTopic(PropertiesQueryRequestTopic, id)
device.topics[PropertiesQueryResponseTopicName] = FormatTopic(PropertiesQueryResponseTopic, id)
device.topics[DeviceShadowQueryRequestTopicName] = FormatTopic(DeviceShadowQueryRequestTopic, id)
device.topics[DeviceShadowQueryResponseTopicName] = FormatTopic(DeviceShadowQueryResponseTopic, id)
device.topics[GatewayBatchReportSubDeviceTopicName] = FormatTopic(GatewayBatchReportSubDeviceTopic, id)
device.topics[FileRequestTopicName] = FormatTopic(FileRequestTopic, id)
return device
}

Expand Down Expand Up @@ -644,16 +633,18 @@ func (device *iotDevice) subscribeDefaultTopics() {
}

// 订阅平台查询设备属性的topic
if token := device.client.Subscribe(device.topics[PropertiesQueryRequestTopicName], 2, device.createPropertiesQueryMqttHandler());
topic = FormatTopic(PropertiesQueryRequestTopic, device.Id)
if token := device.client.Subscribe(topic, 2, device.createPropertiesQueryMqttHandler())
token.Wait() && token.Error() != nil {
glog.Warningf("device %s subscriber platform query device properties topic failed %s", device.Id, device.topics[PropertiesQueryRequestTopicName])
glog.Warningf("device %s subscriber platform query device properties topic failed %s", device.Id, topic)
panic(0)
}

// 订阅查询设备影子响应的topic
if token := device.client.Subscribe(device.topics[DeviceShadowQueryResponseTopicName], 2, device.createPropertiesQueryResponseMqttHandler());
topic = FormatTopic(DeviceShadowQueryResponseTopic, device.Id)
if token := device.client.Subscribe(topic, 2, device.createPropertiesQueryResponseMqttHandler());
token.Wait() && token.Error() != nil {
glog.Warningf("device %s subscribe query device shadow topic %s failed", device.Id, device.topics[DeviceShadowQueryResponseTopicName])
glog.Warningf("device %s subscribe query device shadow topic %s failed", device.Id, topic)
panic(0)
}

Expand Down
2 changes: 1 addition & 1 deletion samples/file/upload_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func main() {
device.Init()

device.UploadFile("D/software/mqttfx/chentong.txt")
//device.DownloadFile("D/software/mqttfx/chentong.txt")
device.DownloadFile("D/software/mqttfx/chentong.txt")
}

0 comments on commit 9c5af1c

Please sign in to comment.