mqtt_sink.go 2.8 KB

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