diff --git a/README.md b/README.md index 15990d4..ca49d69 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,11 @@ huaweicloud-iot-device-sdk-go提供设备接入华为云IoT物联网平台的Go * [设备连接鉴权](#设备连接鉴权) -* [设备命令](#设备命令) +* [设备命令](#设备命令) * [设备消息](#设备消息) -* [设备属性](#设备属性) +* [设备属性](#设备属性) * [文件上传/下载管理](#文件上传/下载管理) @@ -20,13 +20,10 @@ huaweicloud-iot-device-sdk-go提供设备接入华为云IoT物联网平台的Go * [设备日志收集](#设备日志收集) - - ## 版本说明 当前稳定版本:v1.0.0 - ## 安装和构建 安装和构建的过程取决于你是使用go的 [modules](https://golang.org/ref/mod)(推荐) 还是还是`GOPATH` @@ -472,7 +469,7 @@ device.Init() device.UploadFile("D/software/mqttfx/chentong.txt") ~~~ -### 网关与子设备管理 +### 网关与子设备管理 > 当前SDK没有内置mqtt broker模块,对mqtt broker的支持正在开发中 @@ -502,8 +499,6 @@ device.SetSubDevicesDeleteHandler(func(devices iot.SubDeviceInfo) { device.Init() ~~~ - - #### 网关同步子设备列表 * 同步所有版本的子设备 @@ -530,8 +525,6 @@ device.Init() result:= device.AddSubDevices(deviceInfos) // deviceInfos 的类型为[]DeviceInfo ``` - - #### 网关删除子设备 ```go @@ -540,8 +533,6 @@ device.Init() result:= device.DeleteSubDevices(deviceIds) // deviceIds的类型为[]string ``` - - #### 网关更新子设备状态 ```go @@ -550,9 +541,7 @@ device.Init() result:= device.UpdateSubDeviceState(subDevicesStatus) //subDevicesStatus的类型SubDevicesStatus ``` - - -### 设备信息上报 +### 设备信息上报 设备可以向平台上报SDK版本、软固件版本信息,其中SDK的版本信息SDK自动填充 @@ -563,8 +552,6 @@ device.Init() device.ReportDeviceInfo("1.0", "2.0") ~~~ - - ### 设备日志收集 设备日志功能主要包括:平台下发日志收集命令,设备上报平台指定时间段内的日志;设备调用接口主动上报日志。 @@ -621,8 +608,6 @@ type HttpDevice interface { ~~~ ~~~ - - ## 报告bugs 如果你在使用过程中遇到任何问题或bugs,请通过issue的方式上报问题或bug,我们将会在第一时间内答复。上报问题或bugs时请尽量提供以下内容: diff --git a/async.go b/async.go index 69d9d4e..c11b7c4 100644 --- a/async.go +++ b/async.go @@ -14,7 +14,6 @@ func (err *DeviceError) Error() string { } type AsyncResult interface { - Wait() bool WaitTimeout(time.Duration) bool @@ -102,9 +101,9 @@ func (bar *BooleanAsyncResult) completeError(err error) { bar.complete <- struct{}{} } -func NewBooleanAsyncResult() *BooleanAsyncResult{ - asyncResult:=&BooleanAsyncResult{ - baseAsyncResult:baseAsyncResult{ +func NewBooleanAsyncResult() *BooleanAsyncResult { + asyncResult := &BooleanAsyncResult{ + baseAsyncResult: baseAsyncResult{ complete: make(chan struct{}), }, } diff --git a/base_device.go b/base_device.go index bbc66fc..9e377ad 100644 --- a/base_device.go +++ b/base_device.go @@ -14,41 +14,40 @@ import ( const ( // 平台下发消息topic - MessageDownTopic string = "$oc/devices/{device_id}/sys/messages/down" + MessageDownTopic string = "$oc/devices/{device_id}/sys/messages/down" // 设备上报消息topic - MessageUpTopic string = "$oc/devices/{device_id}/sys/messages/up" + MessageUpTopic string = "$oc/devices/{device_id}/sys/messages/up" // 平台下发命令topic CommandDownTopic string = "$oc/devices/{device_id}/sys/commands/#" // 设备响应平台命令 - CommandResponseTopic string = "$oc/devices/{device_id}/sys/commands/response/request_id=" + CommandResponseTopic string = "$oc/devices/{device_id}/sys/commands/response/request_id=" // 设备上报属性 - PropertiesUpTopic string = "$oc/devices/{device_id}/sys/properties/report" + PropertiesUpTopic string = "$oc/devices/{device_id}/sys/properties/report" //平台设置属性topic - PropertiesSetRequestTopic string = "$oc/devices/{device_id}/sys/properties/set/#" + PropertiesSetRequestTopic string = "$oc/devices/{device_id}/sys/properties/set/#" // 设备响应平台属性设置topic - PropertiesSetResponseTopic string = "$oc/devices/{device_id}/sys/properties/set/response/request_id=" + PropertiesSetResponseTopic string = "$oc/devices/{device_id}/sys/properties/set/response/request_id=" // 平台查询设备属性 - PropertiesQueryRequestTopic string = "$oc/devices/{device_id}/sys/properties/get/#" + PropertiesQueryRequestTopic string = "$oc/devices/{device_id}/sys/properties/get/#" // 设备响应平台属性查询 - PropertiesQueryResponseTopic string = "$oc/devices/{device_id}/sys/properties/get/response/request_id=" + PropertiesQueryResponseTopic string = "$oc/devices/{device_id}/sys/properties/get/response/request_id=" // 设备侧获取平台的设备影子数据 - DeviceShadowQueryRequestTopic string = "$oc/devices/{device_id}/sys/shadow/get/request_id=" + DeviceShadowQueryRequestTopic string = "$oc/devices/{device_id}/sys/shadow/get/request_id=" // 设备侧响应获取平台设备影子 - DeviceShadowQueryResponseTopic string = "$oc/devices/{device_id}/sys/shadow/get/response/#" + DeviceShadowQueryResponseTopic string = "$oc/devices/{device_id}/sys/shadow/get/response/#" // 网关批量上报子设备属性 - GatewayBatchReportSubDeviceTopic string = "$oc/devices/{device_id}/sys/gateway/sub_devices/properties/report" - + GatewayBatchReportSubDeviceTopic string = "$oc/devices/{device_id}/sys/gateway/sub_devices/properties/report" // 平台下发文件上传和下载URL FileActionUpload string = "upload" @@ -443,8 +442,8 @@ func (device *baseIotDevice) subscribeDefaultTopics() { func (device *baseIotDevice) handlePlatformToDeviceData() func(client mqtt.Client, message mqtt.Message) { handler := func(client mqtt.Client, message mqtt.Message) { data := &Data{} - err:=json.Unmarshal(message.Payload(), data) - if err!= nil { + err := json.Unmarshal(message.Payload(), data) + if err != nil { fmt.Println(err) return } diff --git a/device_test.go b/device_test.go index 2801fde..3e9224c 100644 --- a/device_test.go +++ b/device_test.go @@ -42,7 +42,7 @@ func TestIotDevice_ReportProperties(t *testing.T) { Services: content, } - reportResult:=device.ReportProperties(services) + reportResult := device.ReportProperties(services) if !reportResult { t.Error("device report property failed") } diff --git a/http_device.go b/http_device.go index a0fed15..99600c2 100644 --- a/http_device.go +++ b/http_device.go @@ -58,6 +58,8 @@ func (device *restyHttpDevice) init() { Password: hmacSha256(device.Password, "2019120219"), } + fmt.Println(Interface2JsonString(accessTokenBody)) + response, err := device.client.R(). SetBody(accessTokenBody). Post(fmt.Sprintf("%s%s", device.Servers, "/v5/device-auth")) @@ -69,7 +71,7 @@ func (device *restyHttpDevice) init() { tokenResponse := &accessTokenResponse{} err = json.Unmarshal(response.Body(), tokenResponse) if err != nil { - fmt.Println("json unmarshal failed") + fmt.Printf("json unmarshal failed %v", err) return } @@ -89,7 +91,7 @@ type accessTokenRequest struct { Password string `json:"password"` } -func CreateHttpDevice(id, password, server string) HttpDevice { +func CreateHttpDevice(config HttpDeviceConfig) HttpDevice { c := resty.New() c.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) c.SetTimeout(30 * time.Second) @@ -99,10 +101,18 @@ func CreateHttpDevice(id, password, server string) HttpDevice { return response.StatusCode() == http.StatusForbidden }) + connsPerHost := 10 + if config.MaxConnsPerHost != 0 { + connsPerHost = config.MaxConnsPerHost + } + c.SetTransport(&http.Transport{ + MaxConnsPerHost: connsPerHost, + }) + device := &restyHttpDevice{ - Id: id, - Password: password, - Servers: server, + Id: config.Id, + Password: config.Password, + Servers: config.Server, client: c, lock: sync.RWMutex{}, } @@ -124,3 +134,11 @@ func CreateHttpDevice(id, password, server string) HttpDevice { return device } + +type HttpDeviceConfig struct { + Id string + Password string + Server string // https://iot-mqtts.cn-north-4.myhuaweicloud.com:443 + MaxConnsPerHost int + MaxIdleConns int +} diff --git a/options.go b/options.go index 4c2e8ca..58545b2 100644 --- a/options.go +++ b/options.go @@ -298,7 +298,7 @@ type ReportDeviceInfoEventParas struct { // 上报设备日志请求 type ReportDeviceLogRequest struct { - Services []ReportDeviceLogServiceEvent `json:"services,omitempty"` + Services []ReportDeviceLogServiceEvent `json:"services,omitempty"` } type ReportDeviceLogServiceEvent struct { diff --git a/samples/async/message/msg.go b/samples/async/message/msg.go index f6b5226..2544162 100644 --- a/samples/async/message/msg.go +++ b/samples/async/message/msg.go @@ -32,8 +32,8 @@ func main() { Id: uuid.NewV4().String(), Content: "Hello Huawei IoT Platform", } - asyncResult:=device.SendMessage(message) - if asyncResult.Wait() && asyncResult.Error()!= nil { + asyncResult := device.SendMessage(message) + if asyncResult.Wait() && asyncResult.Error() != nil { fmt.Println("async send message failed") } else { fmt.Println("async send message success") diff --git a/samples/command/platform_command.go b/samples/command/platform_command.go index d45c0cf..75fd39c 100644 --- a/samples/command/platform_command.go +++ b/samples/command/platform_command.go @@ -15,7 +15,7 @@ func main() { device.Init() // 添加用于处理平台下发命令的callback - commandProcessResult:=false + commandProcessResult := false device.AddCommandHandler(func(command iot.Command) bool { fmt.Println("I get command from platform") commandProcessResult = true diff --git a/samples/file/upload_file.go b/samples/file/upload_file.go index 0633c0d..2920045 100644 --- a/samples/file/upload_file.go +++ b/samples/file/upload_file.go @@ -12,4 +12,3 @@ func main() { device.UploadFile("D/software/mqttfx/chentong.txt") device.DownloadFile("D/software/mqttfx/chentong.txt") } - diff --git a/samples/demo_utils.go b/samples/samples_utils.go similarity index 55% rename from samples/demo_utils.go rename to samples/samples_utils.go index bc334b0..06d968f 100644 --- a/samples/demo_utils.go +++ b/samples/samples_utils.go @@ -2,7 +2,7 @@ package samples import iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go" -const deviceId = "5fdb75cccbfe2f02ce81d4bf_log" +const deviceId = "611d13360ad1ed028658e089_http_report" const devicePassword = "123456789" const Server = "tls://iot-mqtts.cn-north-4.myhuaweicloud.com:8883" @@ -18,7 +18,14 @@ func CreateDevice() iot.Device { return device } -func CreateHttpDevice() iot.HttpDevice{ - return iot.CreateHttpDevice(deviceId, devicePassword, "https://iot-mqtts.cn-north-4.myhuaweicloud.com:443") +func CreateHttpDevice() iot.HttpDevice { + config := iot.HttpDeviceConfig{ + Id: deviceId, + Password: devicePassword, + Server: "https://iot-mqtts.cn-north-4.myhuaweicloud.com:443", + MaxConnsPerHost: 2, + MaxIdleConns: 0, + } + return iot.CreateHttpDevice(config) }