prepare for async
This commit is contained in:
parent
f8cf6f2436
commit
8ffe5260db
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue