prepare for async

This commit is contained in:
ctlove0523 2021-03-30 21:51:28 +08:00
parent f8cf6f2436
commit 8ffe5260db
4 changed files with 5 additions and 63 deletions

View File

@ -4,27 +4,21 @@ import (
"fmt" "fmt"
"github.com/ctlove0523/huaweicloud-iot-device-sdk-go" "github.com/ctlove0523/huaweicloud-iot-device-sdk-go"
"github.com/ctlove0523/huaweicloud-iot-device-sdk-go/samples" "github.com/ctlove0523/huaweicloud-iot-device-sdk-go/samples"
mqtt "github.com/eclipse/paho.mqtt.golang"
"log"
"os"
"time" "time"
) )
// 处理平台下发的同步命令 // 处理平台下发的同步命令
func main() { 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 := samples.CreateDevice()
device.Init() device.Init()
// 添加用于处理平台下发命令的callback // 添加用于处理平台下发命令的callback
commandProcessResult:=false
device.AddCommandHandler(func(command iot.Command) bool { device.AddCommandHandler(func(command iot.Command) bool {
fmt.Println("I get command from platform") fmt.Println("I get command from platform")
time.Sleep(10 * time.Second) commandProcessResult = true
return true return true
}) })
time.Sleep(10 * time.Minute) time.Sleep(10 * time.Minute)

View File

@ -3,12 +3,13 @@ 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" "github.com/ctlove0523/huaweicloud-iot-device-sdk-go/samples"
"time" "time"
) )
func main() { 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) { 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")
@ -24,59 +25,6 @@ func main() {
}) })
device.Init() device.Init()
TestUpdateSubDeviceState(device)
time.Sleep(200 * time.Second) 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")
}
}