基于 Pulsar 获取消息推送(GO SDK)

更新时间:2024-09-10 01:49:13

云项目中提供消息订阅功能,当项目中的设备相关状态发生变化时(如设备注册、设备数据上报、设备离线等),开放消息平台通过 Pulsar 主动推送各种事件数据给外部合作伙伴,以满足合作伙伴对消息实时性和消息持久化的要求。

前提条件

  • 已完成项目创建。项目创建方法参见 快速入门

操作步骤

目前涂鸦基于开源的 Pulsar 系统进行了定制改进,提供 Golang 版本 Tuya-Pulsar-SDK-Go

第一步:配置开发环境

  1. Tuya-Pulsar-Go 获取 SDK 地址。

    基于 Pulsar 获取消息推送(GO SDK)

  2. Projects > Get from VCS > URL,输入 SDK 地址,单击 Clone
    基于 Pulsar 获取消息推送(GO SDK)

第二步:配置云项目参数

example > main.go 文件,编辑对应的 PulsarAddraccessIDaccessKey 参数。

基于 Pulsar 获取消息推送(GO SDK)

  • 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’ ,执行文件。

基于 Pulsar 获取消息推送(GO SDK)

执行结果:

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"}

解析 Data 数据

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"}

解析 Data 数据

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
			}
		]
	}"
}

相关文档