From 8ffe5260db869a6e6e751664e763f43ae6b45bdb Mon Sep 17 00:00:00 2001 From: ctlove0523 <478309639@qq.com> Date: Tue, 30 Mar 2021 21:51:28 +0800 Subject: [PATCH] prepare for async --- samples/command/platform_command.go | 10 +--- samples/gateway/gateway_update_status.go | 58 +------------------ samples/{devices => }/message/msg.go | 0 .../properties/device_properties.go | 0 4 files changed, 5 insertions(+), 63 deletions(-) rename samples/{devices => }/message/msg.go (100%) rename samples/{devices => }/properties/device_properties.go (100%) diff --git a/samples/command/platform_command.go b/samples/command/platform_command.go index 893298c..d45c0cf 100644 --- a/samples/command/platform_command.go +++ b/samples/command/platform_command.go @@ -4,27 +4,21 @@ 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" "time" ) // 处理平台下发的同步命令 func main() { - mqtt.ERROR = log.New(os.Stdout, "[ERROR] ", 0) - mqtt.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0) - mqtt.WARN = log.New(os.Stdout, "[WARN] ", 0) - mqtt.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0) // 创建一个设备并初始化 device := samples.CreateDevice() device.Init() // 添加用于处理平台下发命令的callback + commandProcessResult:=false device.AddCommandHandler(func(command iot.Command) bool { fmt.Println("I get command from platform") - time.Sleep(10 * time.Second) + commandProcessResult = true return true }) time.Sleep(10 * time.Minute) diff --git a/samples/gateway/gateway_update_status.go b/samples/gateway/gateway_update_status.go index ccc48e8..a64c14d 100644 --- a/samples/gateway/gateway_update_status.go +++ b/samples/gateway/gateway_update_status.go @@ -3,12 +3,13 @@ package main import ( "fmt" iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go" - "strconv" + "github.com/ctlove0523/huaweicloud-iot-device-sdk-go/samples" "time" ) func main() { - device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-sdk", "123456789", "tls://iot-mqtts.cn-north-4.myhuaweicloud.com:8883") + device := samples.CreateDevice() + device.SetSubDevicesAddHandler(func(devices iot.SubDeviceInfo) { for _, info := range devices.Devices { fmt.Println("handle device add") @@ -24,59 +25,6 @@ func main() { }) device.Init() - TestUpdateSubDeviceState(device) time.Sleep(200 * time.Second) } - -func TestUpdateSubDeviceState(device iot.Device) { - - var devicesStatus []iot.DeviceStatus - for i := 0; i < 200; i++ { - subDevice := iot.DeviceStatus{ - DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-" + strconv.Itoa(i), - Status: "ONLINE", - } - - devicesStatus = append(devicesStatus, subDevice) - } - - ok := device.UpdateSubDeviceState(iot.SubDevicesStatus{ - DeviceStatuses: devicesStatus, - }) - if ok { - fmt.Println("gateway update sub devices status success") - } else { - fmt.Println("gateway update sub devices status failed") - } -} - -func TestDeleteSubDevices(device iot.Device) { - ok := device.DeleteSubDevices([]string{"5fdb75cccbfe2f02ce81d4bf_sub-device-3"}) - if ok { - fmt.Println("gateway send sub devices request success.") - } else { - fmt.Println("gateway send sub devices request failed.") - } -} - -func TestAddSubDevices(device iot.Device) { - subDevices := []iot.DeviceInfo{{ - NodeId: "sub-device-3", - ProductId: "5fdb75cccbfe2f02ce81d4bf", - }, { - NodeId: "sub-device-4", - ProductId: "5fdb75cccbfe2f02ce81d4bf", - }, { - NodeId: "sub-device-5", - ProductId: "5fdb75cccbfe2f02ce81d4bf", - }} - - ok := device.AddSubDevices(subDevices) - if ok { - fmt.Println("gateway add sub-devices success") - } else { - fmt.Println("gateway add sub-devices failed") - } - -} diff --git a/samples/devices/message/msg.go b/samples/message/msg.go similarity index 100% rename from samples/devices/message/msg.go rename to samples/message/msg.go diff --git a/samples/devices/properties/device_properties.go b/samples/properties/device_properties.go similarity index 100% rename from samples/devices/properties/device_properties.go rename to samples/properties/device_properties.go