mqtt_sink.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. ms.adconf = adconf
  66. return nil
  67. }
  68. func (ms *MQTTSink) Open(ctx api.StreamContext) error {
  69. log := ctx.GetLogger()
  70. cli, err := clients.GetClient("mqtt", ms.config)
  71. if err != nil {
  72. log.Errorf("found error when get mqtt client config %v, error %s", ms.config, err.Error())
  73. return err
  74. }
  75. ms.cli = cli
  76. return nil
  77. }
  78. func (ms *MQTTSink) Collect(ctx api.StreamContext, item interface{}) error {
  79. return ms.collectWithTopic(ctx, item, ms.adconf.Tpc)
  80. }
  81. func (ms *MQTTSink) CollectResend(ctx api.StreamContext, item interface{}) error {
  82. return ms.collectWithTopic(ctx, item, ms.adconf.ResendTopic)
  83. }
  84. func (ms *MQTTSink) collectWithTopic(ctx api.StreamContext, item interface{}, topic string) error {
  85. logger := ctx.GetLogger()
  86. jsonBytes, _, err := ctx.TransformOutput(item)
  87. if err != nil {
  88. return err
  89. }
  90. logger.Debugf("%s publish %s", ctx.GetOpId(), jsonBytes)
  91. if ms.compressor != nil {
  92. jsonBytes, err = ms.compressor.Compress(jsonBytes)
  93. if err != nil {
  94. return err
  95. }
  96. }
  97. tpc, err := ctx.ParseTemplate(topic, item)
  98. if err != nil {
  99. return err
  100. }
  101. para := map[string]interface{}{
  102. "qos": ms.adconf.Qos,
  103. "retained": ms.adconf.Retained,
  104. }
  105. if err := ms.cli.Publish(ctx, tpc, jsonBytes, para); err != nil {
  106. return fmt.Errorf("%s: %s", errorx.IOErr, err.Error())
  107. }
  108. return nil
  109. }
  110. func (ms *MQTTSink) Close(ctx api.StreamContext) error {
  111. logger := ctx.GetLogger()
  112. logger.Infof("Closing mqtt sink")
  113. if ms.cli != nil {
  114. clients.ReleaseClient(ctx, ms.cli)
  115. }
  116. return nil
  117. }