prepare for async
This commit is contained in:
parent
9e8ef3208b
commit
f8cf6f2436
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
10
gateway.go
10
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
|
||||
}
|
||||
|
|
|
@ -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.
|
|
@ -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
|
||||
|
|
|
@ -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())
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue