Tuya pulsar client SDK for Golang
- AccessID: Provided by Tuya platform.
- AccessKey: provided by Tuya platform.
- Pulsar address: Select the pulsar address according to different business areas. You can find out the address from documents.
package main
import (
"context"
"encoding/base64"
"encoding/json"
pulsar "github.com/tuya/tuya-pulsar-sdk-go"
"github.com/tuya/tuya-pulsar-sdk-go/pkg/tylog"
"github.com/tuya/tuya-pulsar-sdk-go/pkg/tyutils"
)
func main() {
// SetInternalLogLevel(logrus.DebugLevel)
tylog.SetGlobalLog("sdk", false)
accessID := "accessID"
accessKey := "accessKey"
topic := pulsar.TopicForAccessID(accessID)
// create client
cfg := pulsar.ClientConfig{
PulsarAddr: pulsar.PulsarAddrCN,
}
c := pulsar.NewClient(cfg)
// create consumer
csmCfg := pulsar.ConsumerConfig{
Topic: topic,
Auth: pulsar.NewAuthProvider(accessID, accessKey),
}
csm, _ := c.NewConsumer(csmCfg)
// handle message
csm.ReceiveAndHandle(context.Background(), &helloHandler{AesSecret: accessKey[8:24]})
}
type helloHandler struct {
AesSecret string
}
func (h *helloHandler) HandlePayload(ctx context.Context, msg pulsar.Message, payload []byte) error {
tylog.Info("payload preview", tylog.String("payload", string(payload)))
// let's decode the payload with AES
m := map[string]interface{}{}
err := json.Unmarshal(payload, &m)
if err != nil {
tylog.Error("json unmarshal failed", tylog.ErrorField(err))
return nil
}
bs := m["data"].(string)
de, err := base64.StdEncoding.DecodeString(string(bs))
if err != nil {
tylog.Error("base64 decode failed", tylog.ErrorField(err))
return nil
}
decode := tyutils.EcbDecrypt(de, []byte(h.AesSecret))
tylog.Info("aes decode", tylog.ByteString("decode payload", decode))
return nil
}
- Make sure that the accessID and accessKey are correct.
- Make sure that the Pulsar address is correct, you should use
pulsar://mqe.tuyaus.com:7285
instead ofpulsar+ssl://mqe.tuyaus.com:7285
. - Make sure that the SDK code version you use is the latest.
Through the following code, you can see all communications with the pulsar service in the terminal.
func main(){
pulsar.SetInternalLogLevel(logrus.DebugLevel)
// other code
}
Through the following code, you can see the log information of tuya_pulsar_go_sdk
.
At the same time, the log will be saved in the logs/sdk.log
file.
func main(){
tylog.SetGlobalLog("sdk", false)
}
In a formal environment, you may not want the SDK logs to be output to the terminal. It is recommended that you use the following code to output the log to a file.
func main(){
tylog.SetGlobalLog("sdk", true)
}
You can get support from Tuya with the following methods:
- Tuya Smart Help Center: https://support.tuya.com/en/help
- Technical Support Council: https://iot.tuya.com/council