mqtt_sink.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2021-2023 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. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/compressor"
  18. "github.com/lf-edge/ekuiper/internal/topo/connection/clients"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/cast"
  21. "github.com/lf-edge/ekuiper/pkg/errorx"
  22. "github.com/lf-edge/ekuiper/pkg/message"
  23. )
  24. // AdConf is the advanced configuration for the mqtt sink
  25. type AdConf struct {
  26. Tpc string `json:"topic"`
  27. Qos byte `json:"qos"`
  28. Retained bool `json:"retained"`
  29. Compression string `json:"compression"`
  30. ResendTopic string `json:"resendDestination"`
  31. }
  32. type MQTTSink struct {
  33. adconf *AdConf
  34. config map[string]interface{}
  35. cli api.MessageClient
  36. compressor message.Compressor
  37. }
  38. func (ms *MQTTSink) hasKeys(str []string, ps map[string]interface{}) bool {
  39. for _, v := range str {
  40. if _, ok := ps[v]; ok {
  41. return true
  42. }
  43. }
  44. return false
  45. }
  46. func (ms *MQTTSink) Configure(ps map[string]interface{}) error {
  47. adconf := &AdConf{}
  48. err := cast.MapToStruct(ps, adconf)
  49. if err != nil {
  50. return err
  51. }
  52. if adconf.Tpc == "" {
  53. return fmt.Errorf("mqtt sink is missing property topic")
  54. }
  55. if adconf.Qos != 0 && adconf.Qos != 1 && adconf.Qos != 2 {
  56. return fmt.Errorf("invalid qos value %v, the value could be only int 0 or 1 or 2", adconf.Qos)
  57. }
  58. if adconf.Compression != "" {
  59. ms.compressor, err = compressor.GetCompressor(adconf.Compression)
  60. if err != nil {
  61. return fmt.Errorf("invalid compression method %s", adconf.Compression)
  62. }
  63. }
  64. ms.config = ps
  65. if adconf.ResendTopic == "" {
  66. adconf.ResendTopic = adconf.Tpc
  67. }
  68. ms.adconf = adconf
  69. return nil
  70. }
  71. func (ms *MQTTSink) Open(ctx api.StreamContext) error {
  72. log := ctx.GetLogger()
  73. cli, err := clients.GetClient("mqtt", ms.config)
  74. if err != nil {
  75. log.Errorf("found error when get mqtt client config %v, error %s", ms.config, err.Error())
  76. return err
  77. }
  78. ms.cli = cli
  79. return nil
  80. }
  81. func (ms *MQTTSink) Collect(ctx api.StreamContext, item interface{}) error {
  82. return ms.collectWithTopic(ctx, item, ms.adconf.Tpc)
  83. }
  84. func (ms *MQTTSink) CollectResend(ctx api.StreamContext, item interface{}) error {
  85. return ms.collectWithTopic(ctx, item, ms.adconf.ResendTopic)
  86. }
  87. func (ms *MQTTSink) collectWithTopic(ctx api.StreamContext, item interface{}, topic string) error {
  88. logger := ctx.GetLogger()
  89. jsonBytes, _, err := ctx.TransformOutput(item)
  90. if err != nil {
  91. return err
  92. }
  93. logger.Debugf("%s publish %s", ctx.GetOpId(), jsonBytes)
  94. if ms.compressor != nil {
  95. jsonBytes, err = ms.compressor.Compress(jsonBytes)
  96. if err != nil {
  97. return err
  98. }
  99. }
  100. tpc, err := ctx.ParseTemplate(topic, item)
  101. if err != nil {
  102. return err
  103. }
  104. para := map[string]interface{}{
  105. "qos": ms.adconf.Qos,
  106. "retained": ms.adconf.Retained,
  107. }
  108. if err := ms.cli.Publish(ctx, tpc, jsonBytes, para); err != nil {
  109. return fmt.Errorf("%s: %s", errorx.IOErr, err.Error())
  110. }
  111. return nil
  112. }
  113. func (ms *MQTTSink) Close(ctx api.StreamContext) error {
  114. logger := ctx.GetLogger()
  115. logger.Infof("Closing mqtt sink")
  116. if ms.cli != nil {
  117. clients.ReleaseClient(ctx, ms.cli)
  118. }
  119. return nil
  120. }