From f8cf6f2436c9d378dd54b632e08336b2018832d3 Mon Sep 17 00:00:00 2001 From: ctlove0523 <478309639@qq.com> Date: Tue, 30 Mar 2021 08:49:40 +0800 Subject: [PATCH] prepare for async --- async_device.go | 4 +-- device.go | 8 +++-- gateway.go | 10 +++--- {async => samples/async}/message/msg.go | 1 + samples/command/platform_command.go | 4 ++- samples/connect/connect_demo.go | 32 ++++++------------- samples/demo_utils.go | 19 +++++++++++ samples/devices/message/msg.go | 10 +++--- .../devices/properties/device_properties.go | 9 +++--- 9 files changed, 56 insertions(+), 41 deletions(-) rename {async => samples/async}/message/msg.go (99%) create mode 100644 samples/demo_utils.go diff --git a/async_device.go b/async_device.go index cb0206a..a1e01ed 100644 --- a/async_device.go +++ b/async_device.go @@ -8,8 +8,8 @@ import ( type AsyncDevice interface { BaseDevice - Gateway - SendMessage(message Message) BooleanAsyncResult + AsyncGateway + SendMessage(message Message) AsyncResult ReportProperties(properties DeviceProperties) AsyncResult BatchReportSubDevicesProperties(service DevicesService) AsyncResult QueryDeviceShadow(query DevicePropertyQueryRequest, handler DevicePropertyQueryResponseHandler) AsyncResult diff --git a/device.go b/device.go index 4c665ab..4bbf8ed 100644 --- a/device.go +++ b/device.go @@ -11,7 +11,7 @@ type Device interface { Gateway SendMessage(message Message) bool ReportProperties(properties DeviceProperties) bool - BatchReportSubDevicesProperties(service DevicesService) + BatchReportSubDevicesProperties(service DevicesService) bool QueryDeviceShadow(query DevicePropertyQueryRequest, handler DevicePropertyQueryResponseHandler) UploadFile(filename string) bool DownloadFile(filename string) bool @@ -75,7 +75,7 @@ func (device *iotDevice) ReportProperties(properties DeviceProperties) bool { } return true } -func (device *iotDevice) BatchReportSubDevicesProperties(service DevicesService) { +func (device *iotDevice) BatchReportSubDevicesProperties(service DevicesService) bool { subDeviceCounts := len(service.Devices) @@ -100,8 +100,11 @@ func (device *iotDevice) BatchReportSubDevicesProperties(service DevicesService) 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 } } + + return true } func (device *iotDevice) QueryDeviceShadow(query DevicePropertyQueryRequest, handler DevicePropertyQueryResponseHandler) { @@ -456,6 +459,7 @@ func CreateIotDeviceWitConfig(config DeviceConfig) Device { device.fileUrls = map[string]string{} device.qos = config.Qos + device.batchSubDeviceSize = 100 result := &iotDevice{ base: device, diff --git a/gateway.go b/gateway.go index 35e1090..0ad96bf 100644 --- a/gateway.go +++ b/gateway.go @@ -31,17 +31,17 @@ type AsyncGateway interface { baseGateway // 网关更新子设备状态 - UpdateSubDeviceState(subDevicesStatus SubDevicesStatus) BooleanAsyncResult + UpdateSubDeviceState(subDevicesStatus SubDevicesStatus) AsyncResult // 网关删除子设备 - DeleteSubDevices(deviceIds []string) BooleanAsyncResult + DeleteSubDevices(deviceIds []string) AsyncResult // 网关添加子设备 - AddSubDevices(deviceInfos []DeviceInfo) BooleanAsyncResult + AddSubDevices(deviceInfos []DeviceInfo) AsyncResult // 网关同步子设备列表,默认实现不指定版本 - SyncAllVersionSubDevices() BooleanAsyncResult + SyncAllVersionSubDevices() AsyncResult // 网关同步特定版本子设备列表 - SyncSubDevices(version int) BooleanAsyncResult + SyncSubDevices(version int) AsyncResult } diff --git a/async/message/msg.go b/samples/async/message/msg.go similarity index 99% rename from async/message/msg.go rename to samples/async/message/msg.go index a09c111..f6b5226 100644 --- a/async/message/msg.go +++ b/samples/async/message/msg.go @@ -10,6 +10,7 @@ import ( func main() { // 创建一个设备并初始化 device := iot.CreateAsyncIotDevice("5fdb75cccbfe2f02ce81d4bf_liqian", "123456789", "tls://iot-mqtts.cn-north-4.myhuaweicloud.com:8883") + device.Init() // 注册平台下发消息的callback,当收到平台下发的消息时,调用此callback. diff --git a/samples/command/platform_command.go b/samples/command/platform_command.go index f56d492..893298c 100644 --- a/samples/command/platform_command.go +++ b/samples/command/platform_command.go @@ -3,6 +3,7 @@ package main import ( "fmt" "github.com/ctlove0523/huaweicloud-iot-device-sdk-go" + "github.com/ctlove0523/huaweicloud-iot-device-sdk-go/samples" mqtt "github.com/eclipse/paho.mqtt.golang" "log" "os" @@ -16,7 +17,8 @@ func main() { mqtt.WARN = log.New(os.Stdout, "[WARN] ", 0) mqtt.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0) // 创建一个设备并初始化 - device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_chentong", "123456789", "tls://iot-mqtts.cn-north-4.myhuaweicloud.com:8883") + device := samples.CreateDevice() + device.Init() // 添加用于处理平台下发命令的callback diff --git a/samples/connect/connect_demo.go b/samples/connect/connect_demo.go index 9c8177e..b7a2ad8 100644 --- a/samples/connect/connect_demo.go +++ b/samples/connect/connect_demo.go @@ -1,35 +1,21 @@ package main import ( - iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go" - "strconv" - "time" + "fmt" + "github.com/ctlove0523/huaweicloud-iot-device-sdk-go/samples" ) -const password = "123456789" -const server = "tls://iot-mqtts.cn-north-4.myhuaweicloud.com:8883" -const produceId = "5fdb75cccbfe2f02ce81d4bf" - - func main() { - content := iot.Message{ - Content: "test content", - } - for i := 1; i <= 110; i++ { - go SendMessage(strconv.Itoa(i), content) - } + device := samples.CreateDevice() - time.Sleep(time.Hour) -} + initResult := device.Init() -func SendMessage(id string, message iot.Message) { - device := iot.CreateIotDevice(produceId+"_test-"+id, password, server) - device.Init() + fmt.Printf("device init %v\n", initResult) - for i := 0; i < 100; i++ { - device.SendMessage(message) - } + fmt.Printf("device connected to server %v\n", device.IsConnected()) - time.Sleep(time.Hour) + device.DisConnect() + + fmt.Printf("device connected to server %v\n", device.IsConnected()) } diff --git a/samples/demo_utils.go b/samples/demo_utils.go new file mode 100644 index 0000000..3f8b98a --- /dev/null +++ b/samples/demo_utils.go @@ -0,0 +1,19 @@ +package samples + +import iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go" + +const deviceId = "5fdb75cccbfe2f02ce81d4bf_liqian" +const devicePassword = "123456789" +const Server = "tls://iot-mqtts.cn-north-4.myhuaweicloud.com:8883" + +func CreateAsyncDevice() iot.AsyncDevice { + device := iot.CreateAsyncIotDevice(deviceId, devicePassword, Server) + + return device +} + +func CreateDevice() iot.Device { + device := iot.CreateIotDevice(deviceId, devicePassword, Server) + + return device +} diff --git a/samples/devices/message/msg.go b/samples/devices/message/msg.go index ad8de32..88eb39a 100644 --- a/samples/devices/message/msg.go +++ b/samples/devices/message/msg.go @@ -3,13 +3,13 @@ package main import ( "fmt" iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go" + "github.com/ctlove0523/huaweicloud-iot-device-sdk-go/samples" uuid "github.com/satori/go.uuid" - "time" ) func main() { // 创建一个设备并初始化 - device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_liqian", "123456789", "tls://iot-mqtts.cn-north-4.myhuaweicloud.com:8883") + device := samples.CreateDevice() device.Init() // 注册平台下发消息的callback,当收到平台下发的消息时,调用此callback. @@ -31,7 +31,9 @@ func main() { Id: uuid.NewV4().String(), Content: "Hello Huawei IoT Platform", } - device.SendMessage(message) - time.Sleep(2 * time.Minute) + + sendMsgResult:=device.SendMessage(message) + + fmt.Printf("send message %v",sendMsgResult) } diff --git a/samples/devices/properties/device_properties.go b/samples/devices/properties/device_properties.go index 8ad2782..653be30 100644 --- a/samples/devices/properties/device_properties.go +++ b/samples/devices/properties/device_properties.go @@ -3,12 +3,13 @@ package main import ( "fmt" iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go" + "github.com/ctlove0523/huaweicloud-iot-device-sdk-go/samples" "time" ) func main() { // 创建设备并初始化 - device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-mqtt", "123456789", "tls://iot-mqtts.cn-north-4.myhuaweicloud.com:8883") + device := samples.CreateDevice() device.Init() fmt.Printf("device connected: %v\n", device.IsConnected()) @@ -57,16 +58,16 @@ func main() { // 批量上报子设备属性 subDevice1 := iot.DeviceService{ - DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-1", + DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-1", Services: content, } subDevice2 := iot.DeviceService{ - DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-2", + DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-2", Services: content, } subDevice3 := iot.DeviceService{ - DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-3", + DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-3", Services: content, }