mqtt.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mqtt
  15. import (
  16. "crypto/tls"
  17. "fmt"
  18. MQTT "github.com/eclipse/paho.mqtt.golang"
  19. "github.com/google/uuid"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/internal/pkg/cert"
  22. "github.com/lf-edge/ekuiper/pkg/cast"
  23. "github.com/lf-edge/ekuiper/pkg/errorx"
  24. "strings"
  25. )
  26. type MQTTConnectionConfig struct {
  27. Servers []string `json:"servers"`
  28. Server string `json:"server"`
  29. PVersion string `json:"protocolVersion"`
  30. ClientId string `json:"clientid"`
  31. Uname string `json:"username"`
  32. Password string `json:"password"`
  33. Certification string `json:"certificationPath"`
  34. PrivateKPath string `json:"privateKeyPath"`
  35. RootCaPath string `json:"rootCaPath"`
  36. InsecureSkipVerify bool `json:"insecureSkipVerify"`
  37. }
  38. type MQTTClient struct {
  39. srv string
  40. clientid string
  41. pVersion uint
  42. uName string
  43. password string
  44. tls *tls.Config
  45. conn MQTT.Client
  46. }
  47. func (ms *MQTTClient) CfgValidate(props map[string]interface{}) error {
  48. cfg := MQTTConnectionConfig{}
  49. err := cast.MapToStruct(props, &cfg)
  50. if err != nil {
  51. return fmt.Errorf("failed to get config, the error is %s", err)
  52. }
  53. if srv := cfg.Servers; len(srv) != 0 {
  54. ms.srv = srv[0]
  55. } else if cfg.Server != "" {
  56. ms.srv = cfg.Server
  57. } else {
  58. return fmt.Errorf("missing server property")
  59. }
  60. if cfg.ClientId == "" {
  61. if newUUID, err := uuid.NewUUID(); err != nil {
  62. return fmt.Errorf("failed to get uuid, the error is %s", err)
  63. } else {
  64. ms.clientid = newUUID.String()
  65. }
  66. } else {
  67. ms.clientid = cfg.ClientId
  68. }
  69. ms.pVersion = 3
  70. if cfg.PVersion == "3.1.1" {
  71. ms.pVersion = 4
  72. }
  73. tlsOpts := cert.TlsConfigurationOptions{
  74. SkipCertVerify: cfg.InsecureSkipVerify,
  75. CertFile: cfg.Certification,
  76. KeyFile: cfg.PrivateKPath,
  77. CaFile: cfg.RootCaPath,
  78. }
  79. conf.Log.Infof("Connect MQTT broker %s with TLS configs: %v.", ms.srv, tlsOpts)
  80. tlscfg, err := cert.GenerateTLSForClient(tlsOpts)
  81. if err != nil {
  82. return err
  83. }
  84. ms.tls = tlscfg
  85. ms.uName = cfg.Uname
  86. ms.password = strings.Trim(cfg.Password, " ")
  87. return nil
  88. }
  89. func (ms *MQTTClient) onConnectLost(_ MQTT.Client, err error) {
  90. conf.Log.Warnf("The connection to mqtt broker %s client id %s disconnected with error: %s ", ms.srv, ms.clientid, err.Error())
  91. }
  92. func (ms *MQTTClient) Connect(handler MQTT.OnConnectHandler) error {
  93. opts := MQTT.NewClientOptions().AddBroker(ms.srv).SetProtocolVersion(ms.pVersion).SetCleanSession(false)
  94. opts = opts.SetTLSConfig(ms.tls)
  95. if ms.uName != "" {
  96. opts = opts.SetUsername(ms.uName)
  97. }
  98. if ms.password != "" {
  99. opts = opts.SetPassword(ms.password)
  100. }
  101. opts = opts.SetClientID(ms.clientid)
  102. opts = opts.SetAutoReconnect(true)
  103. opts.OnConnect = handler
  104. opts.OnConnectionLost = ms.onConnectLost
  105. c := MQTT.NewClient(opts)
  106. if token := c.Connect(); token.Wait() && token.Error() != nil {
  107. conf.Log.Errorf("The connection to mqtt broker %s failed : %s ", ms.srv, token.Error())
  108. return fmt.Errorf("found error when connecting for %s: %s", ms.srv, token.Error())
  109. }
  110. conf.Log.Infof("The connection to mqtt broker is established successfully for %s.", ms.srv)
  111. ms.conn = c
  112. return nil
  113. }
  114. func (ms *MQTTClient) Subscribe(topic string, qos byte, handler MQTT.MessageHandler) error {
  115. if token := ms.conn.Subscribe(topic, qos, handler); token.Wait() && token.Error() != nil {
  116. return fmt.Errorf("%s: %s", errorx.IOErr, token.Error())
  117. }
  118. return nil
  119. }
  120. func (ms *MQTTClient) Publish(topic string, qos byte, retained bool, message []byte) error {
  121. if token := ms.conn.Publish(topic, qos, retained, message); token.Wait() && token.Error() != nil {
  122. return fmt.Errorf("%s: %s", errorx.IOErr, token.Error())
  123. }
  124. return nil
  125. }
  126. func (ms *MQTTClient) Disconnect() error {
  127. conf.Log.Infof("Closing the connection to mqtt broker for %s", ms.srv)
  128. if ms.conn != nil && ms.conn.IsConnected() {
  129. ms.conn.Disconnect(5000)
  130. }
  131. return nil
  132. }