sink.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2022 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 neuron
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/cast"
  21. "sort"
  22. )
  23. type sink struct {
  24. url string
  25. c *c
  26. connected bool
  27. }
  28. type c struct {
  29. NodeName string `json:"nodeName"`
  30. GroupName string `json:"groupName"`
  31. Tags []string `json:"tags"`
  32. // If sent with the raw converted string or let us range over the result map
  33. Raw bool `json:"raw"`
  34. }
  35. type neuronTemplate struct {
  36. GroupName string `json:"group_name"`
  37. NodeName string `json:"node_name"`
  38. TagName string `json:"tag_name"`
  39. Value interface{} `json:"value"`
  40. }
  41. func (s *sink) Configure(props map[string]interface{}) error {
  42. s.url = NeuronUrl
  43. cc := &c{
  44. NodeName: "unknown",
  45. GroupName: "unknown",
  46. Raw: false,
  47. }
  48. err := cast.MapToStruct(props, cc)
  49. if err != nil {
  50. return err
  51. }
  52. s.c = cc
  53. return nil
  54. }
  55. func (s *sink) Open(ctx api.StreamContext) error {
  56. ctx.GetLogger().Debugf("Opening neuron sink")
  57. err := createOrGetConnection(ctx, s.url)
  58. if err != nil {
  59. return err
  60. }
  61. s.connected = true
  62. return nil
  63. }
  64. func (s *sink) Collect(ctx api.StreamContext, data interface{}) error {
  65. ctx.GetLogger().Debugf("receive %+v", data)
  66. if s.c.Raw {
  67. r, _, err := ctx.TransformOutput(data)
  68. if err != nil {
  69. return err
  70. }
  71. return publish(ctx, r)
  72. } else {
  73. switch d := data.(type) {
  74. case []map[string]interface{}:
  75. for _, el := range d {
  76. err := s.SendMapToNeuron(ctx, el)
  77. if err != nil {
  78. ctx.GetLogger().Errorf("Error sending map to neuron: %v", err)
  79. }
  80. }
  81. return nil
  82. case map[string]interface{}:
  83. return s.SendMapToNeuron(ctx, d)
  84. default:
  85. ctx.GetLogger().Errorf("unrecognized format of %s", data)
  86. return nil
  87. }
  88. }
  89. }
  90. func (s *sink) Close(ctx api.StreamContext) error {
  91. ctx.GetLogger().Debugf("closing neuron sink")
  92. if s.connected {
  93. return closeConnection(ctx, s.url)
  94. }
  95. return nil
  96. }
  97. func (s *sink) SendMapToNeuron(ctx api.StreamContext, el map[string]interface{}) error {
  98. n, err := ctx.ParseTemplate(s.c.NodeName, el)
  99. if err != nil {
  100. return err
  101. }
  102. g, err := ctx.ParseTemplate(s.c.GroupName, el)
  103. if err != nil {
  104. return err
  105. }
  106. t := &neuronTemplate{
  107. NodeName: n,
  108. GroupName: g,
  109. }
  110. var (
  111. ok bool
  112. )
  113. if len(s.c.Tags) == 0 {
  114. if conf.IsTesting {
  115. var keys []string
  116. for k := range el {
  117. keys = append(keys, k)
  118. }
  119. sort.Strings(keys)
  120. for _, k := range keys {
  121. t.TagName = k
  122. t.Value = el[k]
  123. err := doPublish(ctx, t)
  124. if err != nil {
  125. ctx.GetLogger().Error(err)
  126. continue
  127. }
  128. }
  129. } else {
  130. for k, v := range el {
  131. t.TagName = k
  132. t.Value = v
  133. err := doPublish(ctx, t)
  134. if err != nil {
  135. ctx.GetLogger().Error(err)
  136. continue
  137. }
  138. }
  139. }
  140. } else {
  141. for _, tag := range s.c.Tags {
  142. t.TagName, err = ctx.ParseTemplate(tag, el)
  143. if err != nil {
  144. ctx.GetLogger().Errorf("Error parsing tag %s: %v", tag, err)
  145. continue
  146. }
  147. t.Value, ok = el[t.TagName]
  148. if !ok {
  149. ctx.GetLogger().Errorf("Error get the value of tag %s: %v", t.TagName, err)
  150. continue
  151. }
  152. err := doPublish(ctx, t)
  153. if err != nil {
  154. ctx.GetLogger().Error(err)
  155. }
  156. }
  157. }
  158. return nil
  159. }
  160. func doPublish(ctx api.StreamContext, t *neuronTemplate) error {
  161. r, err := json.Marshal(t)
  162. if err != nil {
  163. return fmt.Errorf("Error marshall the tag payload %v: %v", t, err)
  164. }
  165. err = publish(ctx, r)
  166. if err != nil {
  167. return fmt.Errorf("Error publish the tag payload %s: %v", t.TagName, err)
  168. }
  169. ctx.GetLogger().Debugf("Publish %s", r)
  170. return nil
  171. }
  172. func GetSink() *sink {
  173. return &sink{}
  174. }