更新时间:2024-09-10 01:49:13
云项目中提供消息订阅功能,当项目中的设备相关状态发生变化时(如设备注册、设备数据上报、设备离线等),开放消息平台通过 Pulsar 主动推送各种事件数据给外部合作伙伴,以满足合作伙伴对消息实时性和消息持久化的要求。
目前涂鸦基于开源的 Pulsar 系统进行了定制改进,提供 Golang 版本 Tuya-Pulsar-SDK-Go。
在 Tuya-Pulsar-Go 获取 SDK 地址。
在 Projects > Get from VCS > URL,输入 SDK 地址,单击 Clone。
在 example > main.go 文件,编辑对应的 PulsarAddr、accessID 和 accessKey 参数。
accessId
:填写云开发平台中 API 授权密钥的 Access ID。accessKey
:填写云开发平台中 API 授权密钥的 Access Secret 。PulsarAddr
:具体参数可查看 client.go,务必修改为私有云对应域名地址。代码示例如下
// SetInternalLogLevel(logrus.DebugLevel)
tylog.SetGlobalLog("sdk", false)
accessID := "xtu7maff*****8ufo"
accessKey := "479bcba6d******c4e080f7"
topic := pulsar.TopicForAccessID(accessID)
// create client
cfg := pulsar.ClientConfig{
PulsarAddr: pulsar.PulsarAddrCN,
}
c := pulsar.NewClient(cfg)
选中 example > main.go 文件,单击右键,选择 Run ‘go build main.go’ ,执行文件。
执行结果:
GOROOT=/usr/local/go #gosetup
GOPATH=/Users/even/Documengts/go #gosetup
/usr/local/go/bin/go build -o /private/var/folders/jf/_0x1y81n67x979xf09m_cv580000gn/T/GoLand/___go_build_main_go /Users/even/Documents/SDK/GoLang/tuya-pulsar-sdk-go/example/main.go #gosetup
/private/var/folders/jf/_0x1y81n67x979xf09m_cv580000gn/T/GoLand/___go_build_main_go
2021-08-11T11:08:08.652+0800 info tuya-pulsar-sdk-go/client.go:67 start creating consumer {"pulsar": "pulsar://mqe.tuyacn.com:7285", "topic": "persistent://xtu7maff*****8ufo/out/event"}
2021-08-11T11:43:45.088+0800 debug tuya-pulsar-sdk-go/consumer.go:53 consumerImpl receive message {"topic": "persistent://xtu7maff*****8ufo/out/event-partition-0"}
2021-08-11T11:43:45.088+0800 info example/main.go:42 payload preview {"payload": "{\"data\":\"nOFoTwoAb+60lp1Mq1Gi9pTUoNevgMb3n6t6RQeEf2/HceQwrrF0vuCyPhPmcgsLzQtf9wFhkSQ6IBE2P3yYPrIcrc/mOxsa2XLThUj1eW8JfG8gnY2R7wUL3Xn4MP48poM3HepgaRZOkpsSKmAqgdxobOPC7kNpk92hmXjB0PEOZ2Du4MbP4NSN49QcJ4MPgRVaIYpFeWwThl1uFaul6QeTXHlHe0KSqaqKUpDueFTjGH5tallpI10RZxofJ72f\",\"protocol\":4,\"pv\":\"2.0\",\"sign\":\"aa55a7512cae4c7a2170302c64a8152d\",\"t\":1628653425175}"}
2021-08-11T11:43:45.089+0800 info example/main.go:58 aes decode {"decode payload": "{\"dataId\":\"5422f320-fa56-11eb-8396-02425b0322e7\",\"devId\":\"vde*******13176\",\"productKey\":\"pp*****p\",\"status\":[{\"1\":\"true\",\"code\":\"switch_led\",\"t\":1628653425064,\"value\":true}]}"}
2021-08-11T11:43:45.089+0800 debug tuya-pulsar-sdk-go/consumer.go:67 Handler trace info {"msgID": "ledgerId:29026543 entryId:8 partition:0 ", "topic": "persistent://xtu7maff*****8ufo/out/event-partition-0", "decode spend": "14.417µs", "Unactive spend": "1.167µs", "ConsumerID spend": "875ns", "HandlePayload spend": "277.083µs", "Ack spend": "129.541µs", "total spend": "471.75µs"}
aes decode {
"decode payload": "{
\"dataId\":\"5422f320-fa56-11eb-8396-02425b0322e7\",
\"devId\":\"vde*******13176\",
\"productKey\":\"pp*****p\",
\"status\":[{
\"1\":\"true\",
\"code\":\"switch_led\",
\"t\":1628653425064,
\"value\":true
}]
}"
}
Data 数据解析方法参见 数据签名。SDK 中已封装 Data 解密方法,具体请查看 pkg > tyutils > aes.go 文件。
2021-08-11T11:39:02.086+0800 debug tuya-pulsar-sdk-go/consumer.go:53 consumerImpl receive message {"topic": "persistent://xtu7maff*****8ufo/out/event-partition-0"}
2021-08-11T11:39:02.089+0800 info example/main.go:42 payload preview {"payload": "{\"data\":\"YWLA8449ERiXEzbnOzEETWIHXXLCsmCgK01uJteO1xHHceQwrrF0vuCyPhPmcgsLzQtf9wFhkSQ6IBE2P3yYPrIcrc/mOxsa2XLThUj1eW8JfG8gnY2R7wUL3Xn4MP48poM3HepgaRZOkpsSKmAqgSlV4VEv7NtZ0T9wsM0xsQe+Gd3lcDRjtoWkeHTaIhvNoE/5/HN5VRTt8SFceoAp0HbL1+8oe+WxngU4aUzQQUDqlI27xZTlsgXlpK+rmyxy7z0o3iD5KROUiGtsAvdO0XJ+KAh08J5b48fs2hkT/LQxN25CG2nxnLbJVhQsTaeDxs3Ggo1C/+qAL04mfKryUw==\",\"protocol\":4,\"pv\":\"2.0\",\"sign\":\"67d95e7*******1814c4**7fa5b7c01\",\"t\":1628653142175}"}
2021-08-11T11:39:02.089+0800 info example/main.go:58 aes decode {"decode payload": "{\"dataId\":\"ab770414-fa55-11eb-8396-02425b0322e7\",\"devId\":\"vde*******13176\",\"productKey\":\"pp*****p\",\"status\":[{\"2\":\"white\",\"code\":\"work_mode\",\"t\":1628653142080,\"value\":\"white\"},{\"3\":\"507\",\"code\":\"bright_value_v2\",\"t\":1628653142080,\"value\":507}]}"}
2021-08-11T11:39:02.092+0800 debug tuya-pulsar-sdk-go/consumer.go:67 Handler trace info {"msgID": "ledgerId:29026543 entryId:6 partition:0 ", "topic": "persistent://xtu7maff*****8ufo/out/event-partition-0", "decode spend": "68.458µs", "Unactive spend": "1.041µs", "ConsumerID spend": "1.542µs", "HandlePayload spend": "3.312541ms", "Ack spend": "175.791µs", "total spend": "3.5965ms"}
aes decode {
"decode payload": "{
\"dataId\":\"ab770414-fa55-11eb-8396-02425b0322e7\",
\"devId\":\"vde*******13176\",
\"productKey\":\"pp*****p\",
\"status\":[
{
\"2\":\"white\",
\"code\":\"work_mode\",
\"t\":1628653142080,
\"value\":\"white\"
},
{
\"3\":\"507\",
\"code\":\"bright_value_v2\",
\"t\":1628653142080,
\"value\":507
}
]
}"
}
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈