mqtt_sink.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/topo/connection/clients"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/cast"
  20. "github.com/lf-edge/ekuiper/pkg/errorx"
  21. )
  22. type MQTTSink struct {
  23. tpc string
  24. qos byte
  25. retained bool
  26. config map[string]interface{}
  27. cli api.MessageClient
  28. }
  29. func (ms *MQTTSink) hasKeys(str []string, ps map[string]interface{}) bool {
  30. for _, v := range str {
  31. if _, ok := ps[v]; ok {
  32. return true
  33. }
  34. }
  35. return false
  36. }
  37. func (ms *MQTTSink) Configure(ps map[string]interface{}) error {
  38. tpc, ok := ps["topic"]
  39. if !ok {
  40. return fmt.Errorf("mqtt sink is missing property topic")
  41. }
  42. var qos byte = 0
  43. if qosRec, ok := ps["qos"]; ok {
  44. if v, err := cast.ToInt(qosRec, cast.STRICT); err == nil {
  45. qos = byte(v)
  46. }
  47. if qos != 0 && qos != 1 && qos != 2 {
  48. return fmt.Errorf("not valid qos value %v, the value could be only int 0 or 1 or 2", qos)
  49. }
  50. }
  51. retained := false
  52. if pk, ok := ps["retained"]; ok {
  53. if v, ok := pk.(bool); ok {
  54. retained = v
  55. }
  56. }
  57. ms.config = ps
  58. ms.qos = qos
  59. ms.tpc = tpc.(string)
  60. ms.retained = retained
  61. return nil
  62. }
  63. func (ms *MQTTSink) Open(ctx api.StreamContext) error {
  64. log := ctx.GetLogger()
  65. cli, err := clients.GetClient("mqtt", ms.config)
  66. if err != nil {
  67. log.Errorf("found error when get mqtt client config %v, error %s", ms.config, err.Error())
  68. return err
  69. }
  70. ms.cli = cli
  71. return nil
  72. }
  73. func (ms *MQTTSink) Collect(ctx api.StreamContext, item interface{}) error {
  74. logger := ctx.GetLogger()
  75. jsonBytes, _, err := ctx.TransformOutput(item)
  76. if err != nil {
  77. return err
  78. }
  79. logger.Debugf("%s publish %s", ctx.GetOpId(), jsonBytes)
  80. tpc, err := ctx.ParseTemplate(ms.tpc, item)
  81. if err != nil {
  82. return err
  83. }
  84. para := map[string]interface{}{
  85. "qos": ms.qos,
  86. "retained": ms.retained,
  87. }
  88. if err := ms.cli.Publish(ctx, tpc, jsonBytes, para); err != nil {
  89. return fmt.Errorf("%s: %s", errorx.IOErr, err.Error())
  90. }
  91. return nil
  92. }
  93. func (ms *MQTTSink) Close(ctx api.StreamContext) error {
  94. logger := ctx.GetLogger()
  95. logger.Infof("Closing mqtt sink")
  96. if ms.cli != nil {
  97. clients.ReleaseClient(ctx, ms.cli)
  98. }
  99. return nil
  100. }