mqtt_sink.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. }
  31. type MQTTSink struct {
  32. adconf *AdConf
  33. config map[string]interface{}
  34. cli api.MessageClient
  35. compressor message.Compressor
  36. }
  37. func (ms *MQTTSink) hasKeys(str []string, ps map[string]interface{}) bool {
  38. for _, v := range str {
  39. if _, ok := ps[v]; ok {
  40. return true
  41. }
  42. }
  43. return false
  44. }
  45. func (ms *MQTTSink) Configure(ps map[string]interface{}) error {
  46. adconf := &AdConf{}
  47. cast.MapToStruct(ps, adconf)
  48. if adconf.Tpc == "" {
  49. return fmt.Errorf("mqtt sink is missing property topic")
  50. }
  51. if adconf.Qos != 0 && adconf.Qos != 1 && adconf.Qos != 2 {
  52. return fmt.Errorf("invalid qos value %v, the value could be only int 0 or 1 or 2", adconf.Qos)
  53. }
  54. var err error
  55. if adconf.Compression != "" {
  56. ms.compressor, err = compressor.GetCompressor(adconf.Compression)
  57. if err != nil {
  58. return fmt.Errorf("invalid compression method %s", adconf.Compression)
  59. }
  60. }
  61. ms.config = ps
  62. ms.adconf = adconf
  63. return nil
  64. }
  65. func (ms *MQTTSink) Open(ctx api.StreamContext) error {
  66. log := ctx.GetLogger()
  67. cli, err := clients.GetClient("mqtt", ms.config)
  68. if err != nil {
  69. log.Errorf("found error when get mqtt client config %v, error %s", ms.config, err.Error())
  70. return err
  71. }
  72. ms.cli = cli
  73. return nil
  74. }
  75. func (ms *MQTTSink) Collect(ctx api.StreamContext, item interface{}) error {
  76. logger := ctx.GetLogger()
  77. jsonBytes, _, err := ctx.TransformOutput(item)
  78. if err != nil {
  79. return err
  80. }
  81. logger.Debugf("%s publish %s", ctx.GetOpId(), jsonBytes)
  82. if ms.compressor != nil {
  83. jsonBytes, err = ms.compressor.Compress(jsonBytes)
  84. if err != nil {
  85. return err
  86. }
  87. }
  88. tpc, err := ctx.ParseTemplate(ms.adconf.Tpc, item)
  89. if err != nil {
  90. return err
  91. }
  92. para := map[string]interface{}{
  93. "qos": ms.adconf.Qos,
  94. "retained": ms.adconf.Retained,
  95. }
  96. if err := ms.cli.Publish(ctx, tpc, jsonBytes, para); err != nil {
  97. return fmt.Errorf("%s: %s", errorx.IOErr, err.Error())
  98. }
  99. return nil
  100. }
  101. func (ms *MQTTSink) Close(ctx api.StreamContext) error {
  102. logger := ctx.GetLogger()
  103. logger.Infof("Closing mqtt sink")
  104. if ms.cli != nil {
  105. clients.ReleaseClient(ctx, ms.cli)
  106. }
  107. return nil
  108. }