mqtt_init.go 631 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package mqtt_binding
  2. import (
  3. "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client"
  4. )
  5. var (
  6. topicPrefix string
  7. )
  8. var mqttClientInstance *mqtt_client.MqttClient
  9. func Init(prefix string, opts *mqtt_client.MqttClientOptions) error {
  10. topicPrefix = prefix
  11. mqttClient, err := mqtt_client.NewMqttClient(opts)
  12. if err != nil {
  13. return err
  14. }
  15. err = mqttClient.Connect()
  16. if err != nil {
  17. return err
  18. }
  19. mqttClientInstance = mqttClient
  20. return nil
  21. }
  22. func Destroy() {
  23. if mqttClientInstance != nil {
  24. mqttClientInstance.Disconnect()
  25. mqtt_client.DestroyMqttClient(mqttClientInstance)
  26. mqttClientInstance = nil
  27. }
  28. }