本文介紹使用huaweicloud-iot-device-sdk-go 連接華為云IoT平臺,實現(xiàn)簡單的華為云文檔介紹的四個功能:設(shè)備連接鑒權(quán)、設(shè)備命令、設(shè)備消息和設(shè)備屬性。huaweicloud-iot-device-sdk-go提供設(shè)備接入華為云IoT物聯(lián)網(wǎng)平臺的Go版本的SDK,提供設(shè)備和平臺之間通訊能力,以及設(shè)備服務(wù)、網(wǎng)關(guān)服務(wù)、OTA等高級服務(wù)。IoT設(shè)備開發(fā)者使用SDK可以大大簡化開發(fā)復(fù)雜度,快速的接入平臺。
Gihub項目地址:huaweicloud-iot-device-sdk-go
安裝和構(gòu)建
安裝和構(gòu)建的過程取決于使用go的 modules(推薦) 還是還是GOPATH
Modules
如果你使用 modules 只需要導(dǎo)入包"github.com/ctlove0523/huaweicloud-iot-device-sdk-go"即可使用。當(dāng)你使用go build命令構(gòu)建項目時,依賴的包會自動被下載。注意使用go build命令構(gòu)建時會自動下載最新版本,最新版本還沒有達(dá)到release的標(biāo)準(zhǔn)可能存在一些尚未修復(fù)的bug。如果想使用穩(wěn)定的發(fā)布版本可以從release 獲取最新穩(wěn)定的版本號,并在go.mod文件中指定版本號。
module example
go 1.15
require github.com/ctlove0523/huaweicloud-iot-device-sdk-go v0.0.1-alpha
GOPATH
如果你使用GOPATH,下面的一條命令即可實現(xiàn)安裝
go get github.com/ctlove0523/huaweicloud-iot-device-sdk-go
使用API
創(chuàng)建設(shè)備并初始化
1、首先,在華為云IoT平臺創(chuàng)建一個設(shè)備,設(shè)備的信息如下:
設(shè)備ID:5fdb75cccbfe2f02ce81d4bf_go-mqtt
設(shè)備密鑰:123456789
2、使用SDK創(chuàng)建一個Device對象,并初始化Device。
// 創(chuàng)建一個設(shè)備并初始化
device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-mqtt", "123456789", "tcp://iot-mqtts.cn-north-4.myhuaweicloud.com:1883")
device.Init()
完整樣例
import (
"fmt"
"github.com/ctlove0523/huaweicloud-iot-device-sdk-go"
"time"
)
func main() {
// 創(chuàng)建一個設(shè)備并初始化
device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-mqtt", "123456789", "tcp://iot-mqtts.cn-north-4.myhuaweicloud.com:1883")
device.Init()
if device.IsConnected() {
fmt.Println("device connect huawei iot platform success")
} else {
fmt.Println("device connect huawei iot platform failed")
}
}
iot-mqtts.cn-north-4.myhuaweicloud.com為華為IoT平臺(基礎(chǔ)班)在華為云北京四的訪問端點,如果你購買了標(biāo)準(zhǔn)版或企業(yè)版,請將iot-mqtts.cn-north-4.myhuaweicloud.com更換為對應(yīng)的MQTT協(xié)議接入端點。
設(shè)備處理平臺下發(fā)的命令
1、首先,在華為云IoT平臺創(chuàng)建一個設(shè)備,設(shè)備的信息如下:
設(shè)備ID:5fdb75cccbfe2f02ce81d4bf_go-mqtt
設(shè)備密鑰:123456789
2、使用SDK創(chuàng)建一個Device對象,并初始化Device。
// 創(chuàng)建一個設(shè)備并初始化
device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-mqtt", "123456789", "tcp://iot-mqtts.cn-north-4.myhuaweicloud.com:1883")
device.Init()
if device.IsConnected() {
fmt.Println("device connect huawei iot platform success")
} else {
fmt.Println("device connect huawei iot platform failed")
}
3、注冊命令處理handler,支持注冊多個handler并且按照注冊的順序回調(diào)
// 添加用于處理平臺下發(fā)命令的callback
device.AddCommandHandler(func(command iot.Command) bool {
fmt.Println("First command handler begin to process command.")
return true
})
device.AddCommandHandler(func(command iot.Command) bool {
fmt.Println("Second command handler begin to process command.")
return true
})
4、通過應(yīng)用側(cè)API向設(shè)備下發(fā)一個命令,可以看到程序輸出如下:
device connect huawei iot platform success
First command handler begin to process command.
Second command handler begin to process command.
完整樣例
import (
"fmt"
"github.com/ctlove0523/huaweicloud-iot-device-sdk-go"
"time"
)
// 處理平臺下發(fā)的同步命令
func main() {
// 創(chuàng)建一個設(shè)備并初始化
device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-mqtt", "123456789", "tcp://iot-mqtts.cn-north-4.myhuaweicloud.com:1883")
device.Init()
if device.IsConnected() {
fmt.Println("device connect huawei iot platform success")
} else {
fmt.Println("device connect huawei iot platform failed")
}
// 添加用于處理平臺下發(fā)命令的callback
device.AddCommandHandler(func(command iot.Command) bool {
fmt.Println("First command handler begin to process command.")
return true
})
device.AddCommandHandler(func(command iot.Command) bool {
fmt.Println("Second command handler begin to process command.")
return true
})
time.Sleep(1 * time.Minute)
}
設(shè)備支持的命令定義在產(chǎn)品中
設(shè)備消息
1、首先,在華為云IoT平臺創(chuàng)建一個設(shè)備,設(shè)備的信息如下:
設(shè)備ID:5fdb75cccbfe2f02ce81d4bf_go-mqtt
設(shè)備密鑰:123456789
2、使用SDK創(chuàng)建一個Device對象,并初始化Device。
device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-mqtt", "123456789", "tcp://iot-mqtts.cn-north-4.myhuaweicloud.com:1883")
device.Init()
設(shè)備消息上報
message := iot.Message{
ObjectDeviceId: uuid.NewV4().String(),
Name: "Fist send message to platform",
Id: uuid.NewV4().String(),
Content: "Hello Huawei IoT Platform",
}
device.SendMessage(message)
平臺消息下發(fā)
接收平臺下發(fā)的消息,只需注冊消息處理handler,支持注冊多個handler并按照注冊順序回調(diào)。
// 注冊平臺下發(fā)消息的callback,當(dāng)收到平臺下發(fā)的消息時,調(diào)用此callback.
// 支持注冊多個callback,并且按照注冊順序調(diào)用
device.AddMessageHandler(func(message iot.Message) bool {
fmt.Println("first handler called" + iot.Interface2JsonString(message))
return true
})
device.AddMessageHandler(func(message iot.Message) bool {
fmt.Println("second handler called" + iot.Interface2JsonString(message))
return true
})
完整樣例
import (
"fmt"
iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go"
uuid "github.com/satori/go.uuid"
"time"
)
func main() {
// 創(chuàng)建一個設(shè)備并初始化
device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-mqtt", "123456789", "tcp://iot-mqtts.cn-north-4.myhuaweicloud.com:1883")
device.Init()
// 注冊平臺下發(fā)消息的callback,當(dāng)收到平臺下發(fā)的消息時,調(diào)用此callback.
// 支持注冊多個callback,并且按照注冊順序調(diào)用
device.AddMessageHandler(func(message iot.Message) bool {
fmt.Println("first handler called" + iot.Interface2JsonString(message))
return true
})
device.AddMessageHandler(func(message iot.Message) bool {
fmt.Println("second handler called" + iot.Interface2JsonString(message))
return true
})
//向平臺發(fā)送消息
message := iot.Message{
ObjectDeviceId: uuid.NewV4().String(),
Name: "Fist send message to platform",
Id: uuid.NewV4().String(),
Content: "Hello Huawei IoT Platform",
}
device.SendMessage(message)
time.Sleep(2 * time.Minute)
}
設(shè)備屬性
1、首先,在華為云IoT平臺創(chuàng)建一個設(shè)備,并在該設(shè)備下創(chuàng)建3個子設(shè)備,設(shè)備及子設(shè)備的信息如下:
設(shè)備ID:5fdb75cccbfe2f02ce81d4bf_go-mqtt
設(shè)備密鑰:123456789
子設(shè)備ID:5fdb75cccbfe2f02ce81d4bf_sub-device-1
子設(shè)備ID:5fdb75cccbfe2f02ce81d4bf_sub-device-2
子設(shè)備ID:5fdb75cccbfe2f02ce81d4bf_sub-device-3
2、使用SDK創(chuàng)建一個Device對象,并初始化Device。
// 創(chuàng)建設(shè)備并初始化
device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-mqtt", "123456789", "tcp://iot-mqtts.cn-north-4.myhuaweicloud.com:1883")
device.Init()
fmt.Printf("device connected: %v\n", device.IsConnected())
設(shè)備屬性上報
使用ReportProperties(properties ServiceProperty) bool 上報設(shè)備屬性
// 設(shè)備上報屬性
props := iot.ServicePropertyEntry{
ServiceId: "value",
EventTime: iot.DataCollectionTime(),
Properties: DemoProperties{
Value: "chen tong",
MsgType: "23",
},
}
var content []iot.ServicePropertyEntry
content = append(content, props)
services := iot.ServiceProperty{
Services: content,
}
device.ReportProperties(services)
網(wǎng)關(guān)批量設(shè)備屬性上報
使用BatchReportSubDevicesProperties(service DevicesService) 實現(xiàn)網(wǎng)關(guān)批量設(shè)備屬性上報
// 批量上報子設(shè)備屬性
subDevice1 := iot.DeviceService{
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-1",
Services: content,
}
subDevice2 := iot.DeviceService{
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-2",
Services: content,
}
subDevice3 := iot.DeviceService{
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-3",
Services: content,
}
var devices []iot.DeviceService
devices = append(devices, subDevice1, subDevice2, subDevice3)
device.BatchReportSubDevicesProperties(iot.DevicesService{
Devices: devices,
})
平臺設(shè)置設(shè)備屬性
使用AddPropertiesSetHandler(handler DevicePropertiesSetHandler) 注冊平臺設(shè)置設(shè)備屬性handler,當(dāng)接收到平臺的命令時SDK回調(diào)。
// 注冊平臺設(shè)置屬性callback,當(dāng)應(yīng)用通過API設(shè)置設(shè)備屬性時,會調(diào)用此callback,支持注冊多個callback
device.AddPropertiesSetHandler(func(propertiesSetRequest iot.DevicePropertyDownRequest) bool {
fmt.Println("I get property set command")
fmt.Printf("request is %s", iot.Interface2JsonString(propertiesSetRequest))
return true
})
平臺查詢設(shè)備屬性
使用SetPropertyQueryHandler(handler DevicePropertyQueryHandler)注冊平臺查詢設(shè)備屬性handler,當(dāng)接收到平臺的查詢請求時SDK回調(diào)。
// 注冊平臺查詢設(shè)備屬性callback,當(dāng)平臺查詢設(shè)備屬性時此callback被調(diào)用,僅支持設(shè)置一個callback
device.SetPropertyQueryHandler(func(query iot.DevicePropertyQueryRequest) iot.ServicePropertyEntry {
return iot.ServicePropertyEntry{
ServiceId: "value",
Properties: DemoProperties{
Value: "QUERY RESPONSE",
MsgType: "query property",
},
EventTime: "2020-12-19 02:23:24",
}
})
設(shè)備側(cè)獲取平臺的設(shè)備影子數(shù)據(jù)
使用QueryDeviceShadow(query DevicePropertyQueryRequest, handler DevicePropertyQueryResponseHandler) 可以查詢平臺的設(shè)備影子數(shù)據(jù),當(dāng)接收到平臺的響應(yīng)后SDK自動回調(diào)DevicePropertyQueryResponseHandler。
// 設(shè)備查詢設(shè)備影子數(shù)據(jù)
device.QueryDeviceShadow(iot.DevicePropertyQueryRequest{
ServiceId: "value",
}, func(response iot.DevicePropertyQueryResponse) {
fmt.Printf("query device shadow success.\n,device shadow data is %s\n", iot.Interface2JsonString(response))
})
完整樣例
import (
"fmt"
iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go"
"time"
)
func main() {
// 創(chuàng)建設(shè)備并初始化
device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-mqtt", "123456789", "tcp://iot-mqtts.cn-north-4.myhuaweicloud.com:1883")
device.Init()
fmt.Printf("device connected: %v\n", device.IsConnected())
// 注冊平臺設(shè)置屬性callback,當(dāng)應(yīng)用通過API設(shè)置設(shè)備屬性時,會調(diào)用此callback,支持注冊多個callback
device.AddPropertiesSetHandler(func(propertiesSetRequest iot.DevicePropertyDownRequest) bool {
fmt.Println("I get property set command")
fmt.Printf("request is %s", iot.Interface2JsonString(propertiesSetRequest))
return true
})
// 注冊平臺查詢設(shè)備屬性callback,當(dāng)平臺查詢設(shè)備屬性時此callback被調(diào)用,僅支持設(shè)置一個callback
device.SetPropertyQueryHandler(func(query iot.DevicePropertyQueryRequest) iot.ServicePropertyEntry {
return iot.ServicePropertyEntry{
ServiceId: "value",
Properties: DemoProperties{
Value: "QUERY RESPONSE",
MsgType: "query property",
},
EventTime: "2020-12-19 02:23:24",
}
})
// 設(shè)備上報屬性
props := iot.ServicePropertyEntry{
ServiceId: "value",
EventTime: iot.DataCollectionTime(),
Properties: DemoProperties{
Value: "chen tong",
MsgType: "23",
},
}
var content []iot.ServicePropertyEntry
content = append(content, props)
services := iot.ServiceProperty{
Services: content,
}
device.ReportProperties(services)
// 設(shè)備查詢設(shè)備影子數(shù)據(jù)
device.QueryDeviceShadow(iot.DevicePropertyQueryRequest{
ServiceId: "value",
}, func(response iot.DevicePropertyQueryResponse) {
fmt.Printf("query device shadow success.\n,device shadow data is %s\n", iot.Interface2JsonString(response))
})
// 批量上報子設(shè)備屬性
subDevice1 := iot.DeviceService{
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-1",
Services: content,
}
subDevice2 := iot.DeviceService{
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-2",
Services: content,
}
subDevice3 := iot.DeviceService{
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-3",
Services: content,
}
var devices []iot.DeviceService
devices = append(devices, subDevice1, subDevice2, subDevice3)
device.BatchReportSubDevicesProperties(iot.DevicesService{
Devices: devices,
})
time.Sleep(1 * time.Minute)
}
type DemoProperties struct {
Value string `json:"value"`
MsgType string `json:"msgType"`
}