This commit is contained in:
ctlove0523 2021-03-19 19:52:53 +08:00
parent 35ccbedd66
commit 713d46c94c
4 changed files with 113 additions and 41 deletions

8
go.sum
View File

@ -1,5 +1,7 @@
github.com/eclipse/paho.mqtt.golang v1.3.0 h1:MU79lqr3FKNKbSrGN7d7bNYqh8MwWW7Zcx0iG+VIw9I= github.com/eclipse/paho.mqtt.golang v1.3.0 h1:MU79lqr3FKNKbSrGN7d7bNYqh8MwWW7Zcx0iG+VIw9I=
github.com/eclipse/paho.mqtt.golang v1.3.0/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc= github.com/eclipse/paho.mqtt.golang v1.3.0/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc=
github.com/go-resty/resty/v2 v2.4.0 h1:s6TItTLejEI+2mn98oijC5w/Rk2YU+OA6x0mnZN6r6k=
github.com/go-resty/resty/v2 v2.4.0/go.mod h1:B88+xCTEwvfD94NOuE6GS1wMlnoKNY8eEiNizfNwOwA=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
@ -14,8 +16,14 @@ github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdh
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U= golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

View File

@ -70,6 +70,7 @@ type iotDevice struct {
deviceUpgradeHandler DeviceUpgradeHandler deviceUpgradeHandler DeviceUpgradeHandler
fileUrls map[string]string fileUrls map[string]string
qos byte qos byte
batchSubDeviceSize int
} }
func (device *iotDevice) DisConnect() () { func (device *iotDevice) DisConnect() () {
@ -202,22 +203,43 @@ func (device *iotDevice) DeleteSubDevices(deviceIds []string) bool {
func (device *iotDevice) UpdateSubDeviceState(subDevicesStatus SubDevicesStatus) bool { func (device *iotDevice) UpdateSubDeviceState(subDevicesStatus SubDevicesStatus) bool {
glog.Infof("begin to update sub-devices status") glog.Infof("begin to update sub-devices status")
requestEventService := DataEntry{ subDeviceCounts := len(subDevicesStatus.DeviceStatuses)
ServiceId: "$sub_device_manager",
EventType: "sub_device_update_status", batchUpdateSubDeviceState := 0
EventTime: GetEventTimeStamp(), if subDeviceCounts%device.batchSubDeviceSize == 0 {
Paras: subDevicesStatus, batchUpdateSubDeviceState = subDeviceCounts / device.batchSubDeviceSize
} else {
batchUpdateSubDeviceState = subDeviceCounts/device.batchSubDeviceSize + 1
} }
request := Data{ for i := 0; i < batchUpdateSubDeviceState; i++ {
ObjectDeviceId: device.Id, begin := i * device.batchSubDeviceSize
Services: []DataEntry{requestEventService}, end := (i + 1) * device.batchSubDeviceSize
} if end > subDeviceCounts {
end = subDeviceCounts
}
if token := device.client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), device.qos, false, Interface2JsonString(request)); sds := SubDevicesStatus{
token.Wait() && token.Error() != nil { DeviceStatuses: subDevicesStatus.DeviceStatuses[begin:end],
glog.Warningf("gateway %s update sub devices status failed", device.Id) }
return false
requestEventService := DataEntry{
ServiceId: "$sub_device_manager",
EventType: "sub_device_update_status",
EventTime: GetEventTimeStamp(),
Paras: sds,
}
request := Data{
ObjectDeviceId: device.Id,
Services: []DataEntry{requestEventService},
}
if token := device.client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), device.qos, false, Interface2JsonString(request));
token.Wait() && token.Error() != nil {
glog.Warningf("gateway %s update sub devices status failed", device.Id)
return false
}
} }
glog.Info("gateway update sub devices status failed", device.Id) glog.Info("gateway update sub devices status failed", device.Id)
@ -603,9 +625,31 @@ func (device *iotDevice) ReportProperties(properties DeviceProperties) bool {
} }
func (device *iotDevice) BatchReportSubDevicesProperties(service DevicesService) { func (device *iotDevice) BatchReportSubDevicesProperties(service DevicesService) {
if token := device.client.Publish(FormatTopic(GatewayBatchReportSubDeviceTopic, device.Id), device.qos, false, Interface2JsonString(service));
token.Wait() && token.Error() != nil { subDeviceCounts := len(service.Devices)
glog.Warningf("device %s batch report sub device properties failed", device.Id)
batchReportSubDeviceProperties := 0
if subDeviceCounts%device.batchSubDeviceSize == 0 {
batchReportSubDeviceProperties = subDeviceCounts / device.batchSubDeviceSize
} else {
batchReportSubDeviceProperties = subDeviceCounts/device.batchSubDeviceSize + 1
}
for i := 0; i < batchReportSubDeviceProperties; i++ {
begin := i * device.batchSubDeviceSize
end := (i + 1) * device.batchSubDeviceSize
if end > subDeviceCounts {
end = subDeviceCounts
}
sds := DevicesService{
Devices: service.Devices[begin:end],
}
if token := device.client.Publish(FormatTopic(GatewayBatchReportSubDeviceTopic, device.Id), device.qos, false, Interface2JsonString(sds));
token.Wait() && token.Error() != nil {
glog.Warningf("device %s batch report sub device properties failed", device.Id)
}
} }
} }
@ -645,20 +689,39 @@ func (device *iotDevice) SetPropertyQueryHandler(handler DevicePropertyQueryHand
} }
func CreateIotDevice(id, password, servers string) Device { func CreateIotDevice(id, password, servers string) Device {
return CreateIotDeviceWithQos(id, password, servers, 0) config := DeviceConfig{
Id: id,
Password: password,
Servers: servers,
Qos: 0,
}
return CreateIotDeviceWitConfig(config)
} }
func CreateIotDeviceWithQos(id, password, servers string, qos byte) Device { func CreateIotDeviceWithQos(id, password, servers string, qos byte) Device {
config := DeviceConfig{
Id: id,
Password: password,
Servers: servers,
Qos: qos,
}
return CreateIotDeviceWitConfig(config)
}
func CreateIotDeviceWitConfig(config DeviceConfig) Device {
device := &iotDevice{} device := &iotDevice{}
device.Id = id device.Id = config.Id
device.Password = password device.Password = config.Password
device.Servers = servers device.Servers = config.Servers
device.messageHandlers = []MessageHandler{} device.messageHandlers = []MessageHandler{}
device.commandHandlers = []CommandHandler{} device.commandHandlers = []CommandHandler{}
device.fileUrls = map[string]string{} device.fileUrls = map[string]string{}
device.qos = qos device.qos = config.Qos
device.batchSubDeviceSize = config.BatchSubDeviceSize
return device return device
} }

9
iot_device_config.go Normal file
View File

@ -0,0 +1,9 @@
package iot
type DeviceConfig struct {
Id string
Password string
Servers string
Qos byte
BatchSubDeviceSize int
}

View File

@ -3,11 +3,12 @@ package main
import ( import (
"fmt" "fmt"
iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go" iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go"
"strconv"
"time" "time"
) )
func main() { func main() {
device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-mqtt", "123456789", "tls://iot-mqtts.cn-north-4.myhuaweicloud.com:8883") device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-sdk", "123456789", "tls://iot-mqtts.cn-north-4.myhuaweicloud.com:8883")
device.SetSubDevicesAddHandler(func(devices iot.SubDeviceInfo) { device.SetSubDevicesAddHandler(func(devices iot.SubDeviceInfo) {
for _, info := range devices.Devices { for _, info := range devices.Devices {
fmt.Println("handle device add") fmt.Println("handle device add")
@ -23,31 +24,22 @@ func main() {
}) })
device.Init() device.Init()
TestDeleteSubDevices(device) TestUpdateSubDeviceState(device)
time.Sleep(2* time.Second) time.Sleep(200 * time.Second)
//device.SyncAllVersionSubDevices()
time.Sleep(time.Hour)
} }
func TestUpdateSubDeviceState(device iot.Device) { func TestUpdateSubDeviceState(device iot.Device) {
subDevice1 := iot.DeviceStatus{
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-1",
Status: "OFFLINE",
}
subDevice2 := iot.DeviceStatus{
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-2",
Status: "OFFLINE",
}
subDevice3 := iot.DeviceStatus{ var devicesStatus []iot.DeviceStatus
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-3", for i := 0; i < 200; i++ {
Status: "ONLINE", subDevice := iot.DeviceStatus{
} DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-" + strconv.Itoa(i),
Status: "ONLINE",
}
devicesStatus := []iot.DeviceStatus{subDevice1, subDevice2, subDevice3} devicesStatus = append(devicesStatus, subDevice)
}
ok := device.UpdateSubDeviceState(iot.SubDevicesStatus{ ok := device.UpdateSubDeviceState(iot.SubDevicesStatus{
DeviceStatuses: devicesStatus, DeviceStatuses: devicesStatus,