#9 Http类型设备支持设置连接参数
This commit is contained in:
parent
c39d21b67a
commit
5fec9688af
15
README.md
15
README.md
|
@ -20,13 +20,10 @@ huaweicloud-iot-device-sdk-go提供设备接入华为云IoT物联网平台的Go
|
|||
|
||||
* [设备日志收集](#设备日志收集)
|
||||
|
||||
|
||||
|
||||
## 版本说明
|
||||
|
||||
当前稳定版本:v1.0.0
|
||||
|
||||
|
||||
## 安装和构建
|
||||
|
||||
安装和构建的过程取决于你是使用go的 [modules](https://golang.org/ref/mod)(推荐) 还是还是`GOPATH`
|
||||
|
@ -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,8 +541,6 @@ 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时请尽量提供以下内容:
|
||||
|
|
7
async.go
7
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{}),
|
||||
},
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -298,7 +298,7 @@ type ReportDeviceInfoEventParas struct {
|
|||
|
||||
// 上报设备日志请求
|
||||
type ReportDeviceLogRequest struct {
|
||||
Services []ReportDeviceLogServiceEvent `json:"services,omitempty"`
|
||||
Services []ReportDeviceLogServiceEvent `json:"services,omitempty"`
|
||||
}
|
||||
|
||||
type ReportDeviceLogServiceEvent struct {
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -12,4 +12,3 @@ func main() {
|
|||
device.UploadFile("D/software/mqttfx/chentong.txt")
|
||||
device.DownloadFile("D/software/mqttfx/chentong.txt")
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
}
|
Loading…
Reference in New Issue