edgex_sink.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. //go:build edgex
  15. // +build edgex
  16. package sink
  17. import (
  18. "encoding/base64"
  19. "encoding/json"
  20. "fmt"
  21. v2 "github.com/edgexfoundry/go-mod-core-contracts/v2/common"
  22. "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
  23. "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/requests"
  24. "github.com/edgexfoundry/go-mod-messaging/v2/messaging"
  25. "github.com/edgexfoundry/go-mod-messaging/v2/pkg/types"
  26. "github.com/lf-edge/ekuiper/internal/conf"
  27. "github.com/lf-edge/ekuiper/pkg/api"
  28. "github.com/lf-edge/ekuiper/pkg/cast"
  29. "github.com/lf-edge/ekuiper/pkg/errorx"
  30. "reflect"
  31. )
  32. type messageType string
  33. const (
  34. MessageTypeEvent messageType = "event"
  35. MessageTypeRequest messageType = "request"
  36. )
  37. type EdgexConf struct {
  38. Protocol string `json:"protocol"`
  39. Host string `json:"host"`
  40. Port int `json:"port"`
  41. Topic string `json:"topic"`
  42. TopicPrefix string `json:"topicPrefix"`
  43. Type string `json:"type"`
  44. MessageType messageType `json:"messageType"`
  45. ContentType string `json:"contentType"`
  46. DeviceName string `json:"deviceName"`
  47. ProfileName string `json:"profileName"`
  48. SourceName string `json:"sourceName"`
  49. Metadata string `json:"metadata"`
  50. Optional map[string]string `json:"optional"`
  51. ConnectionSelector string `json:"connectionSelector"`
  52. DataTemplate string `json:"dataTemplate"`
  53. }
  54. type EdgexMsgBusSink struct {
  55. c *EdgexConf
  56. topic string
  57. conf *types.MessageBusConfig
  58. client messaging.MessageClient
  59. }
  60. func (ems *EdgexMsgBusSink) Configure(ps map[string]interface{}) error {
  61. var (
  62. defaultProtocol = "redis"
  63. defaultServer = "localhost"
  64. defaultType = messaging.Redis
  65. defaultPort = 6379
  66. )
  67. c := &EdgexConf{
  68. MessageType: MessageTypeEvent,
  69. ContentType: "application/json",
  70. DeviceName: "ekuiper",
  71. ProfileName: "ekuiperProfile",
  72. }
  73. err := cast.MapToStruct(ps, c)
  74. if err != nil {
  75. return fmt.Errorf("read properties %v fail with error: %v", ps, err)
  76. }
  77. if c.ConnectionSelector != "" {
  78. conf.Log.Infof("use connection selector %s for edgex sink", c.ConnectionSelector)
  79. if c.Protocol != "" || c.Host != "" || c.Type != "" || c.Port != 0 {
  80. return fmt.Errorf("connectionSelector can not coexist with other connection configs, properties %v", ps)
  81. }
  82. if c.MessageType != MessageTypeEvent && c.MessageType != MessageTypeRequest {
  83. return fmt.Errorf("specified wrong messageType value %s", c.MessageType)
  84. }
  85. if c.MessageType == MessageTypeEvent && c.ContentType != "application/json" {
  86. return fmt.Errorf("specified wrong contentType value %s: only 'application/json' is supported if messageType is event", c.ContentType)
  87. }
  88. } else {
  89. if c.Host == "" {
  90. c.Host = defaultServer
  91. }
  92. if c.Protocol == "" {
  93. c.Protocol = defaultProtocol
  94. }
  95. if c.Type == "" {
  96. c.Type = defaultType
  97. }
  98. if c.Port == 0 {
  99. c.Port = defaultPort
  100. }
  101. if c.Port < 0 {
  102. return fmt.Errorf("specified wrong port value, expect positive integer but got %d", c.Port)
  103. }
  104. if c.Type != messaging.ZeroMQ && c.Type != messaging.MQTT && c.Type != messaging.Redis {
  105. return fmt.Errorf("specified wrong type value %s", c.Type)
  106. }
  107. if c.MessageType != MessageTypeEvent && c.MessageType != MessageTypeRequest {
  108. return fmt.Errorf("specified wrong messageType value %s", c.MessageType)
  109. }
  110. if c.MessageType == MessageTypeEvent && c.ContentType != "application/json" {
  111. return fmt.Errorf("specified wrong contentType value %s: only 'application/json' is supported if messageType is event", c.ContentType)
  112. }
  113. }
  114. if c.Topic != "" && c.TopicPrefix != "" {
  115. return fmt.Errorf("not allow to specify both topic and topicPrefix, please set one only")
  116. }
  117. if c.DataTemplate != "" {
  118. conf.Log.Warn("edgex sink does not support dataTemplate, just ignore")
  119. }
  120. ems.c = c
  121. ems.conf = &types.MessageBusConfig{
  122. PublishHost: types.HostInfo{
  123. Host: c.Host,
  124. Port: c.Port,
  125. Protocol: c.Protocol,
  126. },
  127. Type: c.Type,
  128. Optional: c.Optional,
  129. }
  130. return nil
  131. }
  132. func (ems *EdgexMsgBusSink) getClient(ctx api.StreamContext) error {
  133. log := ctx.GetLogger()
  134. if ems.c.ConnectionSelector != "" {
  135. con, err := ctx.GetConnection(ems.c.ConnectionSelector)
  136. if err != nil {
  137. log.Errorf("The edgex client for connection selector %s get fail with error: %s", ems.c.ConnectionSelector, err)
  138. return err
  139. }
  140. ems.client = con.(messaging.MessageClient)
  141. log.Infof("The edge client for connection selector %s get successfully", ems.c.ConnectionSelector)
  142. } else {
  143. c, err := messaging.NewMessageClient(*ems.conf)
  144. if err != nil {
  145. return err
  146. }
  147. ems.client = c
  148. if err := ems.client.Connect(); err != nil {
  149. return fmt.Errorf("Failed to connect to edgex message bus: " + err.Error())
  150. }
  151. log.Infof("The connection to edgex messagebus is established successfully.")
  152. }
  153. return nil
  154. }
  155. func (ems *EdgexMsgBusSink) Open(ctx api.StreamContext) error {
  156. log := ctx.GetLogger()
  157. log.Infof("Using configuration for EdgeX message bus sink: %+v", ems.c)
  158. if err := ems.getClient(ctx); err != nil {
  159. return fmt.Errorf("Failed to get edgex message client: " + err.Error())
  160. }
  161. if ems.c.SourceName == "" {
  162. ems.c.SourceName = ctx.GetRuleId()
  163. }
  164. if ems.c.Topic == "" && ems.c.TopicPrefix == "" {
  165. ems.topic = "application"
  166. } else if ems.c.Topic != "" {
  167. ems.topic = ems.c.Topic
  168. } else if ems.c.Metadata == "" { // If meta data are static, the "dynamic" topic is static
  169. ems.topic = fmt.Sprintf("%s/%s/%s/%s", ems.c.TopicPrefix, ems.c.ProfileName, ems.c.DeviceName, ems.c.SourceName)
  170. } else {
  171. ems.topic = "" // calculate dynamically
  172. }
  173. return nil
  174. }
  175. func (ems *EdgexMsgBusSink) produceEvents(ctx api.StreamContext, m []map[string]interface{}) (*dtos.Event, error) {
  176. m1 := ems.getMeta(m)
  177. event := m1.createEvent()
  178. //Override the devicename if user specified the value
  179. if event.DeviceName == "" {
  180. event.DeviceName = ems.c.DeviceName
  181. }
  182. if event.ProfileName == "" {
  183. event.ProfileName = ems.c.ProfileName
  184. }
  185. if event.SourceName == "" {
  186. event.SourceName = ems.c.SourceName
  187. }
  188. for _, v := range m {
  189. for k1, v1 := range v {
  190. // Ignore nil values
  191. if k1 == ems.c.Metadata || v1 == nil {
  192. continue
  193. } else {
  194. var (
  195. vt string
  196. vv interface{}
  197. err error
  198. )
  199. mm1 := m1.readingMeta(ctx, k1)
  200. if mm1 != nil && mm1.valueType != nil {
  201. vt = *mm1.valueType
  202. vv, err = getValueByType(v1, vt)
  203. } else {
  204. vt, vv, err = getValueType(v1)
  205. }
  206. if err != nil {
  207. ctx.GetLogger().Errorf("%v", err)
  208. continue
  209. }
  210. switch vt {
  211. case v2.ValueTypeBinary:
  212. // default media type
  213. event.AddBinaryReading(k1, vv.([]byte), "application/text")
  214. case v2.ValueTypeObject:
  215. event.AddObjectReading(k1, vv)
  216. default:
  217. err = event.AddSimpleReading(k1, vt, vv)
  218. }
  219. if err != nil {
  220. ctx.GetLogger().Errorf("%v", err)
  221. continue
  222. }
  223. r := event.Readings[len(event.Readings)-1]
  224. if mm1 != nil {
  225. event.Readings[len(event.Readings)-1] = mm1.decorate(&r)
  226. }
  227. }
  228. }
  229. }
  230. return event, nil
  231. }
  232. func getValueType(v interface{}) (string, interface{}, error) {
  233. vt := reflect.TypeOf(v)
  234. if vt == nil {
  235. return "", nil, fmt.Errorf("unsupported value nil")
  236. }
  237. k := vt.Kind()
  238. switch k {
  239. case reflect.Bool:
  240. return v2.ValueTypeBool, v, nil
  241. case reflect.String:
  242. return v2.ValueTypeString, v, nil
  243. case reflect.Uint8:
  244. return v2.ValueTypeUint8, v, nil
  245. case reflect.Uint16:
  246. return v2.ValueTypeUint16, v, nil
  247. case reflect.Uint32:
  248. return v2.ValueTypeUint32, v, nil
  249. case reflect.Uint64:
  250. return v2.ValueTypeUint64, v, nil
  251. case reflect.Uint:
  252. return v2.ValueTypeUint64, uint64(v.(uint)), nil
  253. case reflect.Int8:
  254. return v2.ValueTypeInt8, v, nil
  255. case reflect.Int16:
  256. return v2.ValueTypeInt16, v, nil
  257. case reflect.Int32:
  258. return v2.ValueTypeInt32, v, nil
  259. case reflect.Int64:
  260. return v2.ValueTypeInt64, v, nil
  261. case reflect.Int:
  262. return v2.ValueTypeInt64, int64(v.(int)), nil
  263. case reflect.Float32:
  264. return v2.ValueTypeFloat32, v, nil
  265. case reflect.Float64:
  266. return v2.ValueTypeFloat64, v, nil
  267. case reflect.Slice:
  268. switch arrayValue := v.(type) {
  269. case []interface{}:
  270. if len(arrayValue) > 0 {
  271. kt := reflect.TypeOf(arrayValue[0])
  272. if kt == nil {
  273. return "", nil, fmt.Errorf("unsupported value %v(%s), the first element is nil", v, k)
  274. }
  275. switch kt.Kind() {
  276. case reflect.Bool:
  277. result := make([]bool, len(arrayValue))
  278. for i, av := range arrayValue {
  279. temp, ok := av.(bool)
  280. if !ok {
  281. return "", nil, fmt.Errorf("unable to cast value to []bool for %v", v)
  282. }
  283. result[i] = temp
  284. }
  285. return v2.ValueTypeBoolArray, result, nil
  286. case reflect.String:
  287. result := make([]string, len(arrayValue))
  288. for i, av := range arrayValue {
  289. temp, ok := av.(string)
  290. if !ok {
  291. return "", nil, fmt.Errorf("unable to cast value to []string for %v", v)
  292. }
  293. result[i] = temp
  294. }
  295. return v2.ValueTypeStringArray, result, nil
  296. case reflect.Int8:
  297. result := make([]int8, len(arrayValue))
  298. for i, av := range arrayValue {
  299. temp, ok := av.(int8)
  300. if !ok {
  301. return "", nil, fmt.Errorf("unable to cast value to []int8 for %v", v)
  302. }
  303. result[i] = temp
  304. }
  305. return v2.ValueTypeInt8Array, result, nil
  306. case reflect.Int16:
  307. result := make([]int16, len(arrayValue))
  308. for i, av := range arrayValue {
  309. temp, ok := av.(int16)
  310. if !ok {
  311. return "", nil, fmt.Errorf("unable to cast value to []int16 for %v", v)
  312. }
  313. result[i] = temp
  314. }
  315. return v2.ValueTypeInt16Array, result, nil
  316. case reflect.Int32:
  317. result := make([]int32, len(arrayValue))
  318. for i, av := range arrayValue {
  319. temp, ok := av.(int32)
  320. if !ok {
  321. return "", nil, fmt.Errorf("unable to cast value to []int32 for %v", v)
  322. }
  323. result[i] = temp
  324. }
  325. return v2.ValueTypeInt32Array, result, nil
  326. case reflect.Int64, reflect.Int:
  327. result := make([]int64, len(arrayValue))
  328. for i, av := range arrayValue {
  329. temp, ok := av.(int64)
  330. if !ok {
  331. return "", nil, fmt.Errorf("unable to cast value to []int64 for %v", v)
  332. }
  333. result[i] = temp
  334. }
  335. return v2.ValueTypeInt64Array, result, nil
  336. case reflect.Uint8:
  337. result := make([]uint8, len(arrayValue))
  338. for i, av := range arrayValue {
  339. temp, ok := av.(uint8)
  340. if !ok {
  341. return "", nil, fmt.Errorf("unable to cast value to []uint8 for %v", v)
  342. }
  343. result[i] = temp
  344. }
  345. return v2.ValueTypeUint8Array, result, nil
  346. case reflect.Uint16:
  347. result := make([]uint16, len(arrayValue))
  348. for i, av := range arrayValue {
  349. temp, ok := av.(uint16)
  350. if !ok {
  351. return "", nil, fmt.Errorf("unable to cast value to []uint16 for %v", v)
  352. }
  353. result[i] = temp
  354. }
  355. return v2.ValueTypeUint16Array, result, nil
  356. case reflect.Uint32:
  357. result := make([]uint32, len(arrayValue))
  358. for i, av := range arrayValue {
  359. temp, ok := av.(uint32)
  360. if !ok {
  361. return "", nil, fmt.Errorf("unable to cast value to []uint32 for %v", v)
  362. }
  363. result[i] = temp
  364. }
  365. return v2.ValueTypeUint32Array, result, nil
  366. case reflect.Uint64, reflect.Uint:
  367. result := make([]uint64, len(arrayValue))
  368. for i, av := range arrayValue {
  369. temp, ok := av.(uint64)
  370. if !ok {
  371. return "", nil, fmt.Errorf("unable to cast value to []uint64 for %v", v)
  372. }
  373. result[i] = temp
  374. }
  375. return v2.ValueTypeUint64Array, result, nil
  376. case reflect.Float32:
  377. result := make([]float32, len(arrayValue))
  378. for i, av := range arrayValue {
  379. temp, ok := av.(float32)
  380. if !ok {
  381. return "", nil, fmt.Errorf("unable to cast value to []float32 for %v", v)
  382. }
  383. result[i] = temp
  384. }
  385. return v2.ValueTypeFloat64Array, result, nil
  386. case reflect.Float64:
  387. result := make([]float64, len(arrayValue))
  388. for i, av := range arrayValue {
  389. temp, ok := av.(float64)
  390. if !ok {
  391. return "", nil, fmt.Errorf("unable to cast value to []float64 for %v", v)
  392. }
  393. result[i] = temp
  394. }
  395. return v2.ValueTypeFloat64Array, result, nil
  396. }
  397. } else { // default to string array
  398. return v2.ValueTypeStringArray, []string{}, nil
  399. }
  400. case []byte:
  401. return v2.ValueTypeBinary, v, nil
  402. }
  403. }
  404. return v2.ValueTypeObject, v, nil
  405. }
  406. func getValueByType(v interface{}, vt string) (interface{}, error) {
  407. switch vt {
  408. case v2.ValueTypeBool:
  409. return cast.ToBool(v, cast.CONVERT_SAMEKIND)
  410. case v2.ValueTypeInt8:
  411. return cast.ToInt8(v, cast.CONVERT_SAMEKIND)
  412. case v2.ValueTypeInt16:
  413. return cast.ToInt16(v, cast.CONVERT_SAMEKIND)
  414. case v2.ValueTypeInt32:
  415. return cast.ToInt32(v, cast.CONVERT_SAMEKIND)
  416. case v2.ValueTypeInt64:
  417. return cast.ToInt64(v, cast.CONVERT_SAMEKIND)
  418. case v2.ValueTypeUint8:
  419. return cast.ToUint8(v, cast.CONVERT_SAMEKIND)
  420. case v2.ValueTypeUint16:
  421. return cast.ToUint16(v, cast.CONVERT_SAMEKIND)
  422. case v2.ValueTypeUint32:
  423. return cast.ToUint32(v, cast.CONVERT_SAMEKIND)
  424. case v2.ValueTypeUint64:
  425. return cast.ToUint64(v, cast.CONVERT_SAMEKIND)
  426. case v2.ValueTypeFloat32:
  427. return cast.ToFloat32(v, cast.CONVERT_SAMEKIND)
  428. case v2.ValueTypeFloat64:
  429. return cast.ToFloat64(v, cast.CONVERT_SAMEKIND)
  430. case v2.ValueTypeString:
  431. return cast.ToString(v, cast.CONVERT_SAMEKIND)
  432. case v2.ValueTypeBoolArray:
  433. return cast.ToBoolSlice(v, cast.CONVERT_SAMEKIND)
  434. case v2.ValueTypeInt8Array:
  435. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  436. return cast.ToInt8(input, sn)
  437. }, "int8", cast.CONVERT_SAMEKIND)
  438. case v2.ValueTypeInt16Array:
  439. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  440. return cast.ToInt16(input, sn)
  441. }, "int16", cast.CONVERT_SAMEKIND)
  442. case v2.ValueTypeInt32Array:
  443. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  444. return cast.ToInt32(input, sn)
  445. }, "int32", cast.CONVERT_SAMEKIND)
  446. case v2.ValueTypeInt64Array:
  447. return cast.ToInt64Slice(v, cast.CONVERT_SAMEKIND)
  448. case v2.ValueTypeUint8Array:
  449. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  450. return cast.ToUint8(input, sn)
  451. }, "uint8", cast.CONVERT_SAMEKIND)
  452. case v2.ValueTypeUint16Array:
  453. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  454. return cast.ToUint16(input, sn)
  455. }, "uint16", cast.CONVERT_SAMEKIND)
  456. case v2.ValueTypeUint32Array:
  457. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  458. return cast.ToUint32(input, sn)
  459. }, "uint32", cast.CONVERT_SAMEKIND)
  460. case v2.ValueTypeUint64Array:
  461. return cast.ToUint64Slice(v, cast.CONVERT_SAMEKIND)
  462. case v2.ValueTypeFloat32Array:
  463. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  464. return cast.ToFloat32(input, sn)
  465. }, "float32", cast.CONVERT_SAMEKIND)
  466. case v2.ValueTypeFloat64Array:
  467. return cast.ToFloat64Slice(v, cast.CONVERT_SAMEKIND)
  468. case v2.ValueTypeStringArray:
  469. return cast.ToStringSlice(v, cast.CONVERT_SAMEKIND)
  470. case v2.ValueTypeBinary:
  471. var (
  472. bv []byte
  473. err error
  474. )
  475. switch vv := v.(type) {
  476. case string:
  477. if bv, err = base64.StdEncoding.DecodeString(vv); err != nil {
  478. return nil, fmt.Errorf("fail to decode binary value from %s: %v", vv, err)
  479. }
  480. case []byte:
  481. bv = vv
  482. default:
  483. return nil, fmt.Errorf("fail to decode binary value from %v: not binary type", vv)
  484. }
  485. return bv, nil
  486. case v2.ValueTypeObject:
  487. return v, nil
  488. default:
  489. return nil, fmt.Errorf("unsupported type %v", vt)
  490. }
  491. }
  492. func (ems *EdgexMsgBusSink) getMeta(result []map[string]interface{}) *meta {
  493. if ems.c.Metadata == "" {
  494. return newMetaFromMap(nil)
  495. }
  496. //Try to get the meta field
  497. for _, v := range result {
  498. if m, ok := v[ems.c.Metadata]; ok {
  499. if m1, ok1 := m.(map[string]interface{}); ok1 {
  500. return newMetaFromMap(m1)
  501. } else {
  502. conf.Log.Infof("Specified a meta field, but the field does not contains any EdgeX metadata.")
  503. }
  504. }
  505. }
  506. return newMetaFromMap(nil)
  507. }
  508. func (ems *EdgexMsgBusSink) Collect(ctx api.StreamContext, item interface{}) error {
  509. logger := ctx.GetLogger()
  510. var (
  511. evt *dtos.Event
  512. err error
  513. )
  514. switch payload := item.(type) {
  515. case map[string]interface{}:
  516. evt, err = ems.produceEvents(ctx, []map[string]interface{}{payload})
  517. case []map[string]interface{}:
  518. evt, err = ems.produceEvents(ctx, payload)
  519. default:
  520. // impossible
  521. return fmt.Errorf("receive invalid data %v", item)
  522. }
  523. if err != nil {
  524. return fmt.Errorf("Failed to convert to EdgeX event: %s.", err.Error())
  525. }
  526. var (
  527. data []byte
  528. topic string
  529. )
  530. if ems.c.MessageType == MessageTypeRequest {
  531. req := requests.NewAddEventRequest(*evt)
  532. data, _, err = req.Encode()
  533. if err != nil {
  534. return fmt.Errorf("unexpected error encode event %v", err)
  535. }
  536. } else {
  537. data, err = json.Marshal(evt)
  538. if err != nil {
  539. return fmt.Errorf("unexpected error MarshalEvent %v", err)
  540. }
  541. }
  542. env := types.NewMessageEnvelope(data, ctx)
  543. env.ContentType = ems.c.ContentType
  544. if ems.topic == "" { // dynamic topic
  545. topic = fmt.Sprintf("%s/%s/%s/%s", ems.c.TopicPrefix, evt.ProfileName, evt.DeviceName, evt.SourceName)
  546. } else {
  547. topic = ems.topic
  548. }
  549. if e := ems.client.Publish(env, topic); e != nil {
  550. logger.Errorf("%s: found error %s when publish to EdgeX message bus.\n", e)
  551. return fmt.Errorf("%s:%s", errorx.IOErr, e.Error())
  552. }
  553. logger.Debugf("Published %+v to EdgeX message bus topic %s", evt, topic)
  554. return nil
  555. }
  556. func (ems *EdgexMsgBusSink) Close(ctx api.StreamContext) error {
  557. logger := ctx.GetLogger()
  558. logger.Infof("Closing edgex sink")
  559. if ems.client != nil && ems.c.ConnectionSelector == "" {
  560. if e := ems.client.Disconnect(); e != nil {
  561. return e
  562. }
  563. }
  564. if ems.c.ConnectionSelector != "" {
  565. ctx.ReleaseConnection(ems.c.ConnectionSelector)
  566. }
  567. return nil
  568. }
  569. type eventMeta struct {
  570. id *string
  571. deviceName string
  572. profileName string
  573. sourceName string
  574. origin *int64
  575. tags map[string]interface{}
  576. }
  577. type readingMeta struct {
  578. id *string
  579. deviceName *string
  580. profileName *string
  581. resourceName *string
  582. origin *int64
  583. valueType *string
  584. mediaType *string
  585. }
  586. func (m *readingMeta) decorate(r *dtos.BaseReading) dtos.BaseReading {
  587. if m.id != nil {
  588. r.Id = *m.id
  589. }
  590. if m.deviceName != nil {
  591. r.DeviceName = *m.deviceName
  592. }
  593. if m.profileName != nil {
  594. r.ProfileName = *m.profileName
  595. }
  596. if m.origin != nil {
  597. r.Origin = *m.origin
  598. }
  599. if m.valueType != nil {
  600. r.ValueType = *m.valueType
  601. }
  602. if m.mediaType != nil {
  603. r.MediaType = *m.mediaType
  604. }
  605. return *r
  606. }
  607. type meta struct {
  608. eventMeta
  609. readingMetas map[string]interface{}
  610. }
  611. func newMetaFromMap(m1 map[string]interface{}) *meta {
  612. result := &meta{
  613. eventMeta: eventMeta{},
  614. }
  615. for k, v := range m1 {
  616. switch k {
  617. case "id":
  618. if v1, ok := v.(string); ok {
  619. result.id = &v1
  620. }
  621. case "deviceName":
  622. if v1, ok := v.(string); ok {
  623. result.deviceName = v1
  624. }
  625. case "profileName":
  626. if v1, ok := v.(string); ok {
  627. result.profileName = v1
  628. }
  629. case "sourceName":
  630. if v1, ok := v.(string); ok {
  631. result.sourceName = v1
  632. }
  633. case "origin":
  634. if v1, ok := v.(float64); ok {
  635. temp := int64(v1)
  636. result.origin = &temp
  637. }
  638. case "tags":
  639. if v1, ok1 := v.(map[string]interface{}); ok1 {
  640. result.tags = v1
  641. }
  642. default:
  643. if result.readingMetas == nil {
  644. result.readingMetas = make(map[string]interface{})
  645. }
  646. result.readingMetas[k] = v
  647. }
  648. }
  649. return result
  650. }
  651. func (m *meta) readingMeta(ctx api.StreamContext, readingName string) *readingMeta {
  652. vi, ok := m.readingMetas[readingName]
  653. if !ok {
  654. return nil
  655. }
  656. m1, ok := vi.(map[string]interface{})
  657. if !ok {
  658. ctx.GetLogger().Errorf("reading %s meta is not a map, but %v", readingName, vi)
  659. return nil
  660. }
  661. result := &readingMeta{}
  662. for k, v := range m1 {
  663. switch k {
  664. case "id":
  665. if v1, ok := v.(string); ok {
  666. result.id = &v1
  667. }
  668. case "deviceName":
  669. if v1, ok := v.(string); ok {
  670. result.deviceName = &v1
  671. }
  672. case "profileName":
  673. if v1, ok := v.(string); ok {
  674. result.profileName = &v1
  675. }
  676. case "resourceName":
  677. if v1, ok := v.(string); ok {
  678. result.resourceName = &v1
  679. }
  680. case "origin":
  681. if v1, ok := v.(float64); ok {
  682. temp := int64(v1)
  683. result.origin = &temp
  684. }
  685. case "valueType":
  686. if v1, ok := v.(string); ok {
  687. result.valueType = &v1
  688. }
  689. case "mediaType":
  690. if v1, ok := v.(string); ok {
  691. result.mediaType = &v1
  692. }
  693. default:
  694. ctx.GetLogger().Warnf("reading %s meta got unknown field %s of value %v", readingName, k, v)
  695. }
  696. }
  697. return result
  698. }
  699. func (m *meta) createEvent() *dtos.Event {
  700. event := dtos.NewEvent(m.profileName, m.deviceName, m.sourceName)
  701. if m.id != nil {
  702. event.Id = *m.id
  703. }
  704. if m.origin != nil {
  705. event.Origin = *m.origin
  706. }
  707. if m.tags != nil {
  708. event.Tags = m.tags
  709. }
  710. return &event
  711. }