diff --git a/async_device.go b/async_device.go index d16f49e..abd1cc2 100644 --- a/async_device.go +++ b/async_device.go @@ -120,7 +120,7 @@ func (device *asyncDevice) SendMessage(message Message) AsyncResult { glog.Info("begin async send message") messageData := Interface2JsonString(message) - topic := FormatTopic(MessageUpTopic, device.base.Id) + topic := formatTopic(MessageUpTopic, device.base.Id) glog.Infof("async send message topic is %s", topic) token := device.base.Client.Publish(topic, device.base.qos, false, messageData) if token.Wait() && token.Error() != nil { @@ -139,7 +139,7 @@ func (device *asyncDevice) ReportProperties(properties DeviceProperties) AsyncRe go func() { glog.Info("begin to report properties") propertiesData := Interface2JsonString(properties) - if token := device.base.Client.Publish(FormatTopic(PropertiesUpTopic, device.base.Id), device.base.qos, false, propertiesData); + if token := device.base.Client.Publish(formatTopic(PropertiesUpTopic, device.base.Id), device.base.qos, false, propertiesData); token.Wait() && token.Error() != nil { glog.Warningf("device %s async report properties failed", device.base.Id) asyncResult.completeError(token.Error()) @@ -176,7 +176,7 @@ func (device *asyncDevice) BatchReportSubDevicesProperties(service DevicesServic Devices: service.Devices[begin:end], } - if token := device.base.Client.Publish(FormatTopic(GatewayBatchReportSubDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(sds)); + if token := device.base.Client.Publish(formatTopic(GatewayBatchReportSubDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(sds)); token.Wait() && token.Error() != nil { glog.Warningf("device %s batch report sub device properties failed", device.base.Id) loopResult = false @@ -199,7 +199,7 @@ func (device *asyncDevice) QueryDeviceShadow(query DevicePropertyQueryRequest, h go func() { requestId := uuid.NewV4() - if token := device.base.Client.Publish(FormatTopic(DeviceShadowQueryRequestTopic, device.base.Id)+requestId.String(), device.base.qos, false, Interface2JsonString(query)); + if token := device.base.Client.Publish(formatTopic(DeviceShadowQueryRequestTopic, device.base.Id)+requestId.String(), device.base.qos, false, Interface2JsonString(query)); token.Wait() && token.Error() != nil { glog.Warningf("device %s query device shadow data failed,request id = %s", device.base.Id, requestId) asyncResult.completeError(token.Error()) @@ -232,7 +232,7 @@ func (device *asyncDevice) UploadFile(filename string) AsyncResult { Services: services, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); token.Wait() && token.Error() != nil { glog.Warningf("publish file upload request url failed") asyncResult.completeError(&DeviceError{ @@ -265,7 +265,7 @@ func (device *asyncDevice) UploadFile(filename string) AsyncResult { } glog.Infof("file upload url is %s", device.base.fileUrls[filename+FileActionUpload]) - //filename = SmartFileName(filename) + //filename = smartFileName(filename) uploadFlag := CreateHttpClient().UploadFile(filename, device.base.fileUrls[filename+FileActionUpload]) if !uploadFlag { glog.Errorf("upload file failed") @@ -277,7 +277,7 @@ func (device *asyncDevice) UploadFile(filename string) AsyncResult { response := CreateFileUploadDownLoadResultResponse(filename, FileActionUpload, uploadFlag) - token := device.base.Client.Publish(FormatTopic(PlatformEventToDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(response)) + token := device.base.Client.Publish(formatTopic(PlatformEventToDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(response)) if token.Wait() && token.Error() != nil { glog.Error("report file upload file result failed") asyncResult.completeError(token.Error()) @@ -310,7 +310,7 @@ func (device *asyncDevice) DownloadFile(filename string) AsyncResult { Services: services, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); token.Wait() && token.Error() != nil { glog.Warningf("publish file download request url failed") asyncResult.completeError(&DeviceError{ @@ -352,7 +352,7 @@ func (device *asyncDevice) DownloadFile(filename string) AsyncResult { response := CreateFileUploadDownLoadResultResponse(filename, FileActionDownload, downloadFlag) - token := device.base.Client.Publish(FormatTopic(PlatformEventToDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(response)) + token := device.base.Client.Publish(formatTopic(PlatformEventToDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(response)) if token.Wait() && token.Error() != nil { glog.Error("report file upload file result failed") asyncResult.completeError(token.Error()) @@ -386,7 +386,7 @@ func (device *asyncDevice) ReportDeviceInfo(swVersion, fwVersion string) AsyncRe Services: []ReportDeviceInfoServiceEvent{event}, } - token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)) + token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)) if token.Wait() && token.Error() != nil { asyncResult.completeError(token.Error()) } else { @@ -423,7 +423,7 @@ func (device *asyncDevice) ReportLogs(logs []DeviceLogEntry) AsyncResult { fmt.Println(Interface2JsonString(request)) - topic := FormatTopic(DeviceToPlatformTopic, device.base.Id) + topic := formatTopic(DeviceToPlatformTopic, device.base.Id) token := device.base.Client.Publish(topic, 1, false, Interface2JsonString(request)) @@ -484,7 +484,7 @@ func (device *asyncDevice) UpdateSubDeviceState(subDevicesStatus SubDevicesStatu Services: []DataEntry{requestEventService}, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); token.Wait() && token.Error() != nil { glog.Warningf("gateway %s update sub devices status failed", device.base.Id) asyncResult.completeError(token.Error()) @@ -522,7 +522,7 @@ func (device *asyncDevice) DeleteSubDevices(deviceIds []string) AsyncResult { Services: []DataEntry{requestEventService}, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); token.Wait() && token.Error() != nil { glog.Warningf("gateway %s delete sub devices request send failed", device.base.Id) asyncResult.completeError(token.Error()) @@ -558,7 +558,7 @@ func (device *asyncDevice) AddSubDevices(deviceInfos []DeviceInfo) AsyncResult { Services: []DataEntry{requestEventService}, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); token.Wait() && token.Error() != nil { glog.Warningf("gateway %s add sub devices request send failed", device.base.Id) asyncResult.completeError(token.Error()) @@ -591,7 +591,7 @@ func (device *asyncDevice) SyncAllVersionSubDevices() AsyncResult { Services: dataEntries, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(data)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(data)); token.Wait() && token.Error() != nil { asyncResult.completeError(token.Error()) } else { @@ -626,7 +626,7 @@ func (device *asyncDevice) SyncSubDevices(version int) AsyncResult { Services: dataEntries, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(data)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(data)); token.Wait() && token.Error() != nil { glog.Errorf("send sync sub device request failed") asyncResult.completeError(token.Error()) diff --git a/base_device.go b/base_device.go index 28b8ff2..4334b63 100644 --- a/base_device.go +++ b/base_device.go @@ -161,7 +161,7 @@ func (device *baseIotDevice) Init() bool { options.AddBroker(device.Servers) options.SetClientID(assembleClientId(device)) options.SetUsername(device.Id) - options.SetPassword(HmacSha256(device.Password, TimeStamp())) + options.SetPassword(hmacSha256(device.Password, timeStamp())) options.SetKeepAlive(250 * time.Second) options.SetAutoReconnect(true) options.SetConnectRetry(true) @@ -252,7 +252,7 @@ func assembleClientId(device *baseIotDevice) string { segments[0] = device.Id segments[1] = "0" segments[2] = "0" - segments[3] = TimeStamp() + segments[3] = timeStamp() return strings.Join(segments, "_") } @@ -291,7 +291,7 @@ func (device *baseIotDevice) createCommandMqttHandler() func(client mqtt.Client, ResultCode: 1, }) } - if token := device.Client.Publish(FormatTopic(CommandResponseTopic, device.Id)+GetTopicRequestId(message.Topic()), 1, false, res); + if token := device.Client.Publish(formatTopic(CommandResponseTopic, device.Id)+getTopicRequestId(message.Topic()), 1, false, res); token.Wait() && token.Error() != nil { glog.Infof("device %s send command response failed", device.Id) } @@ -329,7 +329,7 @@ func (device *baseIotDevice) createPropertiesSetMqttHandler() func(client mqtt.C response.ResultDesc = "Set properties failed." res = Interface2JsonString(response) } - if token := device.Client.Publish(FormatTopic(PropertiesSetResponseTopic, device.Id)+GetTopicRequestId(message.Topic()), device.qos, false, res); + if token := device.Client.Publish(formatTopic(PropertiesSetResponseTopic, device.Id)+getTopicRequestId(message.Topic()), device.qos, false, res); token.Wait() && token.Error() != nil { glog.Warningf("unmarshal platform properties set request failed,device id = %s,message = %s", device.Id, message) } @@ -366,7 +366,7 @@ func (device *baseIotDevice) createPropertiesQueryMqttHandler() func(client mqtt queryResult := device.propertyQueryHandler(*propertiesQueryRequest) responseToPlatform := Interface2JsonString(queryResult) - if token := device.Client.Publish(FormatTopic(PropertiesQueryResponseTopic, device.Id)+GetTopicRequestId(message.Topic()), device.qos, false, responseToPlatform); + if token := device.Client.Publish(formatTopic(PropertiesQueryResponseTopic, device.Id)+getTopicRequestId(message.Topic()), device.qos, false, responseToPlatform); token.Wait() && token.Error() != nil { glog.Warningf("device %s send properties query response failed.", device.Id) } @@ -390,7 +390,7 @@ func (device *baseIotDevice) createPropertiesQueryResponseMqttHandler() func(cli func (device *baseIotDevice) subscribeDefaultTopics() { // 订阅平台命令下发topic - topic := FormatTopic(CommandDownTopic, device.Id) + topic := formatTopic(CommandDownTopic, device.Id) if token := device.Client.Subscribe(topic, device.qos, device.createCommandMqttHandler()); token.Wait() && token.Error() != nil { glog.Warningf("device %s subscribe platform send command topic %s failed", device.Id, topic) @@ -398,7 +398,7 @@ func (device *baseIotDevice) subscribeDefaultTopics() { } // 订阅平台消息下发的topic - topic = FormatTopic(MessageDownTopic, device.Id) + topic = formatTopic(MessageDownTopic, device.Id) if token := device.Client.Subscribe(topic, device.qos, device.createMessageMqttHandler()); token.Wait() && token.Error() != nil { glog.Warningf("device % subscribe platform send message topic %s failed.", device.Id, topic) @@ -406,7 +406,7 @@ func (device *baseIotDevice) subscribeDefaultTopics() { } // 订阅平台设置设备属性的topic - topic = FormatTopic(PropertiesSetRequestTopic, device.Id) + topic = formatTopic(PropertiesSetRequestTopic, device.Id) if token := device.Client.Subscribe(topic, device.qos, device.createPropertiesSetMqttHandler()); token.Wait() && token.Error() != nil { glog.Warningf("device %s subscribe platform set properties topic %s failed", device.Id, topic) @@ -414,7 +414,7 @@ func (device *baseIotDevice) subscribeDefaultTopics() { } // 订阅平台查询设备属性的topic - topic = FormatTopic(PropertiesQueryRequestTopic, device.Id) + topic = formatTopic(PropertiesQueryRequestTopic, device.Id) if token := device.Client.Subscribe(topic, device.qos, device.createPropertiesQueryMqttHandler()) token.Wait() && token.Error() != nil { glog.Warningf("device %s subscriber platform query device properties topic failed %s", device.Id, topic) @@ -422,7 +422,7 @@ func (device *baseIotDevice) subscribeDefaultTopics() { } // 订阅查询设备影子响应的topic - topic = FormatTopic(DeviceShadowQueryResponseTopic, device.Id) + topic = formatTopic(DeviceShadowQueryResponseTopic, device.Id) if token := device.Client.Subscribe(topic, device.qos, device.createPropertiesQueryResponseMqttHandler()); token.Wait() && token.Error() != nil { glog.Warningf("device %s subscribe query device shadow topic %s failed", device.Id, topic) @@ -430,7 +430,7 @@ func (device *baseIotDevice) subscribeDefaultTopics() { } // 订阅平台下发到设备的事件 - topic = FormatTopic(PlatformEventToDeviceTopic, device.Id) + topic = formatTopic(PlatformEventToDeviceTopic, device.Id) if token := device.Client.Subscribe(topic, device.qos, device.handlePlatformToDeviceData()); token.Wait() && token.Error() != nil { glog.Warningf("device %s subscribe query device shadow topic %s failed", device.Id, topic) @@ -598,7 +598,7 @@ func (device *baseIotDevice) reportLogs(logs []DeviceLogEntry) { } reportedLog := Interface2JsonString(data) - device.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), 0, false, reportedLog) + device.Client.Publish(formatTopic(DeviceToPlatformTopic, device.Id), 0, false, reportedLog) } func (device *baseIotDevice) reportVersion() { @@ -620,7 +620,7 @@ func (device *baseIotDevice) reportVersion() { Services: []DataEntry{dataEntry}, } - device.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), device.qos, false, Interface2JsonString(data)) + device.Client.Publish(formatTopic(DeviceToPlatformTopic, device.Id), device.qos, false, Interface2JsonString(data)) } func (device *baseIotDevice) upgradeDevice(upgradeType byte, upgradeInfo *UpgradeInfo) { @@ -636,7 +636,7 @@ func (device *baseIotDevice) upgradeDevice(upgradeType byte, upgradeInfo *Upgrad Services: []DataEntry{dataEntry}, } - if token := device.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), device.qos, false, Interface2JsonString(data)); + if token := device.Client.Publish(formatTopic(DeviceToPlatformTopic, device.Id), device.qos, false, Interface2JsonString(data)); token.Wait() && token.Error() != nil { glog.Errorf("device %s upgrade failed,type %d", device.Id, upgradeType) } diff --git a/device.go b/device.go index 23791fb..26c042e 100644 --- a/device.go +++ b/device.go @@ -80,7 +80,7 @@ func (device *iotDevice) ReportLogs(logs []DeviceLogEntry) bool { fmt.Println(Interface2JsonString(request)) - topic := FormatTopic(DeviceToPlatformTopic, device.base.Id) + topic := formatTopic(DeviceToPlatformTopic, device.base.Id) token := device.base.Client.Publish(topic, 1, false, Interface2JsonString(request)) @@ -94,7 +94,7 @@ func (device *iotDevice) ReportLogs(logs []DeviceLogEntry) bool { 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); + if token := device.base.Client.Publish(formatTopic(MessageUpTopic, device.base.Id), device.base.qos, false, messageData); token.Wait() && token.Error() != nil { glog.Warningf("device %s send message failed", device.base.Id) return false @@ -104,7 +104,7 @@ func (device *iotDevice) SendMessage(message Message) bool { func (device *iotDevice) ReportProperties(properties DeviceProperties) bool { propertiesData := Interface2JsonString(properties) - if token := device.base.Client.Publish(FormatTopic(PropertiesUpTopic, device.base.Id), device.base.qos, false, propertiesData); + if token := device.base.Client.Publish(formatTopic(PropertiesUpTopic, device.base.Id), device.base.qos, false, propertiesData); token.Wait() && token.Error() != nil { glog.Warningf("device %s report properties failed", device.base.Id) return false @@ -133,7 +133,7 @@ func (device *iotDevice) BatchReportSubDevicesProperties(service DevicesService) Devices: service.Devices[begin:end], } - if token := device.base.Client.Publish(FormatTopic(GatewayBatchReportSubDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(sds)); + if token := device.base.Client.Publish(formatTopic(GatewayBatchReportSubDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(sds)); token.Wait() && token.Error() != nil { glog.Warningf("device %s batch report sub device properties failed", device.base.Id) return false @@ -146,7 +146,7 @@ func (device *iotDevice) BatchReportSubDevicesProperties(service DevicesService) func (device *iotDevice) QueryDeviceShadow(query DevicePropertyQueryRequest, handler DevicePropertyQueryResponseHandler) { device.base.propertiesQueryResponseHandler = handler requestId := uuid.NewV4() - if token := device.base.Client.Publish(FormatTopic(DeviceShadowQueryRequestTopic, device.base.Id)+requestId.String(), device.base.qos, false, Interface2JsonString(query)); + if token := device.base.Client.Publish(formatTopic(DeviceShadowQueryRequestTopic, device.base.Id)+requestId.String(), device.base.qos, false, Interface2JsonString(query)); token.Wait() && token.Error() != nil { glog.Warningf("device %s query device shadow data failed,request id = %s", device.base.Id, requestId) } @@ -171,7 +171,7 @@ func (device *iotDevice) UploadFile(filename string) bool { Services: services, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); token.Wait() && token.Error() != nil { glog.Warningf("publish file upload request url failed") return false @@ -198,7 +198,7 @@ BreakPoint: } glog.Infof("file upload url is %s", device.base.fileUrls[filename+FileActionUpload]) - //filename = SmartFileName(filename) + //filename = smartFileName(filename) uploadFlag := CreateHttpClient().UploadFile(filename, device.base.fileUrls[filename+FileActionUpload]) if !uploadFlag { glog.Errorf("upload file failed") @@ -207,7 +207,7 @@ BreakPoint: response := CreateFileUploadDownLoadResultResponse(filename, FileActionUpload, uploadFlag) - token := device.base.Client.Publish(FormatTopic(PlatformEventToDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(response)) + token := device.base.Client.Publish(formatTopic(PlatformEventToDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(response)) if token.Wait() && token.Error() != nil { glog.Error("report file upload file result failed") return false @@ -235,7 +235,7 @@ func (device *iotDevice) DownloadFile(filename string) bool { Services: services, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); token.Wait() && token.Error() != nil { glog.Warningf("publish file download request url failed") return false @@ -268,7 +268,7 @@ BreakPoint: response := CreateFileUploadDownLoadResultResponse(filename, FileActionDownload, downloadFlag) - token := device.base.Client.Publish(FormatTopic(PlatformEventToDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(response)) + token := device.base.Client.Publish(formatTopic(PlatformEventToDeviceTopic, device.base.Id), device.base.qos, false, Interface2JsonString(response)) if token.Wait() && token.Error() != nil { glog.Error("report file upload file result failed") return false @@ -296,7 +296,7 @@ func (device *iotDevice) ReportDeviceInfo(swVersion, fwVersion string) { Services: []ReportDeviceInfoServiceEvent{event}, } - device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)) + device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)) } func (device *iotDevice) SetSubDevicesAddHandler(handler SubDevicesAddHandler) { @@ -358,7 +358,7 @@ func (device *iotDevice) UpdateSubDeviceState(subDevicesStatus SubDevicesStatus) Services: []DataEntry{requestEventService}, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); token.Wait() && token.Error() != nil { glog.Warningf("gateway %s update sub devices status failed", device.base.Id) return false @@ -390,7 +390,7 @@ func (device *iotDevice) DeleteSubDevices(deviceIds []string) bool { Services: []DataEntry{requestEventService}, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); token.Wait() && token.Error() != nil { glog.Warningf("gateway %s delete sub devices request send failed", device.base.Id) return false @@ -419,7 +419,7 @@ func (device *iotDevice) AddSubDevices(deviceInfos []DeviceInfo) bool { Services: []DataEntry{requestEventService}, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(request)); token.Wait() && token.Error() != nil { glog.Warningf("gateway %s add sub devices request send failed", device.base.Id) return false @@ -445,7 +445,7 @@ func (device *iotDevice) SyncAllVersionSubDevices() { Services: dataEntries, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(data)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(data)); token.Wait() && token.Error() != nil { glog.Errorf("send sub device sync request failed") } @@ -472,7 +472,7 @@ func (device *iotDevice) SyncSubDevices(version int) { Services: dataEntries, } - if token := device.base.Client.Publish(FormatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(data)); + if token := device.base.Client.Publish(formatTopic(DeviceToPlatformTopic, device.base.Id), device.base.qos, false, Interface2JsonString(data)); token.Wait() && token.Error() != nil { glog.Errorf("send sync sub device request failed") } diff --git a/http_client.go b/http_client.go index 9d1707d..4f171f1 100644 --- a/http_client.go +++ b/http_client.go @@ -20,7 +20,7 @@ type httpClient struct { func (client *httpClient) DownloadFile(fileName, downloadUrl string) bool { glog.Infof("begin to download file %s, url = %s", fileName, downloadUrl) - fileName = SmartFileName(fileName) + fileName = smartFileName(fileName) out, err := os.Create(fileName) if err != nil { glog.Errorf("create file in os failed ,file name %s", fileName) @@ -53,7 +53,7 @@ func (client *httpClient) DownloadFile(fileName, downloadUrl string) bool { } func (client *httpClient) UploadFile(filename, uri string) bool { - filename = SmartFileName(filename) + filename = smartFileName(filename) fileBytes, err := ioutil.ReadFile(filename) if err != nil { diff --git a/util.go b/util.go index 52981d8..6c24725 100644 --- a/util.go +++ b/util.go @@ -10,7 +10,7 @@ import ( ) // 时间戳:为设备连接平台时的UTC时间,格式为YYYYMMDDHH,如UTC 时间2018/7/24 17:56:20 则应表示为2018072417。 -func TimeStamp() string { +func timeStamp() string { strFormatTime := time.Now().Format("2006-01-02 15:04:05") strFormatTime = strings.ReplaceAll(strFormatTime, "-", "") strFormatTime = strings.ReplaceAll(strFormatTime, " ", "") @@ -25,7 +25,7 @@ func GetEventTimeStamp() string { return now.Format("20060102T150405Z") } -func HmacSha256(data string, secret string) string { +func hmacSha256(data string, secret string) string { h := hmac.New(sha256.New, []byte(secret)) h.Write([]byte(data)) return hex.EncodeToString(h.Sum(nil)) @@ -42,16 +42,16 @@ func Interface2JsonString(v interface{}) string { return string(byteData) } -func GetTopicRequestId(topic string) string { +func getTopicRequestId(topic string) string { return strings.Split(topic, "=")[1] } -func FormatTopic(topic, deviceId string) string { +func formatTopic(topic, deviceId string) string { return strings.ReplaceAll(topic, "{device_id}", deviceId) } // 根据当前运行的操作系统重新修改文件路径以适配操作系统 -func SmartFileName(filename string) string { +func smartFileName(filename string) string { // Windows操作系统适配 if strings.Contains(OsName(), "windows") { pathParts := strings.Split(filename, "/") diff --git a/util_test.go b/util_test.go index 54c29e6..9d59c60 100644 --- a/util_test.go +++ b/util_test.go @@ -1,12 +1,11 @@ package iot import ( - "fmt" "testing" ) func TestTimeStamp(t *testing.T) { - timeStamp := TimeStamp() + timeStamp := timeStamp() if len(timeStamp) != 10 { t.Error(`Time Stamp length must be 10`) } @@ -20,8 +19,8 @@ func TestDataCollectionTime(t *testing.T) { func TestHmacSha256(t *testing.T) { encodedPassword := "c0fefa1341fb0647290e93f641a9bcea74cd32111668cdc5f7418553640a55cc" - if HmacSha256("123456789", "202012222200") != encodedPassword { - t.Errorf("encoded password must be %s but is %s", encodedPassword, HmacSha256("123456789", "202012222200")) + if hmacSha256("123456789", "202012222200") != encodedPassword { + t.Errorf("encoded password must be %s but is %s", encodedPassword, hmacSha256("123456789", "202012222200")) } } @@ -33,7 +32,7 @@ func TestInterface2JsonString(t *testing.T) { func TestGetTopicRequestId(t *testing.T) { topic := "$os/device/down/request=123456789" - if GetTopicRequestId(topic) != "123456789" { + if getTopicRequestId(topic) != "123456789" { t.Errorf("topic request id must be %s", "123456789") } } @@ -41,9 +40,9 @@ func TestGetTopicRequestId(t *testing.T) { func TestFormatTopic(t *testing.T) { topic := "$os/device/{device_id}/up" deviceId := "123" - formatTopic := "$os/device/123/up" - if formatTopic != FormatTopic(topic, deviceId) { - t.Errorf("formated topic must be %s", formatTopic) + formatTopicName := "$os/device/123/up" + if formatTopicName != formatTopic(topic, deviceId) { + t.Errorf("formated topic must be %s", formatTopicName) } } @@ -51,10 +50,9 @@ func TestFormatTopic(t *testing.T) { // 仅适用于windows系统 func TestSmartFileName(t *testing.T) { fileName := "D/go/sdk/test.log" - smartFileName := "D:\\go\\sdk\\test.log" - fmt.Println(SmartFileName(fileName)) + name := "D:\\go\\sdk\\test.log" - if smartFileName != SmartFileName(fileName) { - t.Errorf("in windows file system,smart file name must be %s", smartFileName) + if name != smartFileName(fileName) { + t.Errorf("in windows file system,smart file name must be %s", name) } }