edgex_sink.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. default:
  215. err = event.AddSimpleReading(k1, vt, vv)
  216. }
  217. if err != nil {
  218. ctx.GetLogger().Errorf("%v", err)
  219. continue
  220. }
  221. r := event.Readings[len(event.Readings)-1]
  222. if mm1 != nil {
  223. event.Readings[len(event.Readings)-1] = mm1.decorate(&r)
  224. }
  225. }
  226. }
  227. }
  228. return event, nil
  229. }
  230. func getValueType(v interface{}) (string, interface{}, error) {
  231. vt := reflect.TypeOf(v)
  232. if vt == nil {
  233. return "", nil, fmt.Errorf("unsupported value nil")
  234. }
  235. k := vt.Kind()
  236. switch k {
  237. case reflect.Bool:
  238. return v2.ValueTypeBool, v, nil
  239. case reflect.String:
  240. return v2.ValueTypeString, v, nil
  241. case reflect.Int64:
  242. return v2.ValueTypeInt64, v, nil
  243. case reflect.Int:
  244. return v2.ValueTypeInt64, int64(v.(int)), nil
  245. case reflect.Float64:
  246. return v2.ValueTypeFloat64, v, nil
  247. case reflect.Slice:
  248. switch arrayValue := v.(type) {
  249. case []interface{}:
  250. if len(arrayValue) > 0 {
  251. kt := reflect.TypeOf(arrayValue[0])
  252. if kt == nil {
  253. return "", nil, fmt.Errorf("unsupported value %v(%s), the first element is nil", v, k)
  254. }
  255. switch kt.Kind() {
  256. case reflect.Bool:
  257. result := make([]bool, len(arrayValue))
  258. for i, av := range arrayValue {
  259. temp, ok := av.(bool)
  260. if !ok {
  261. return "", nil, fmt.Errorf("unable to cast value to []bool for %v", v)
  262. }
  263. result[i] = temp
  264. }
  265. return v2.ValueTypeBoolArray, result, nil
  266. case reflect.String:
  267. result := make([]string, len(arrayValue))
  268. for i, av := range arrayValue {
  269. temp, ok := av.(string)
  270. if !ok {
  271. return "", nil, fmt.Errorf("unable to cast value to []string for %v", v)
  272. }
  273. result[i] = temp
  274. }
  275. return v2.ValueTypeStringArray, result, nil
  276. case reflect.Int64, reflect.Int:
  277. result := make([]int64, len(arrayValue))
  278. for i, av := range arrayValue {
  279. temp, ok := av.(int64)
  280. if !ok {
  281. return "", nil, fmt.Errorf("unable to cast value to []int64 for %v", v)
  282. }
  283. result[i] = temp
  284. }
  285. return v2.ValueTypeInt64Array, result, nil
  286. case reflect.Float64:
  287. result := make([]float64, len(arrayValue))
  288. for i, av := range arrayValue {
  289. temp, ok := av.(float64)
  290. if !ok {
  291. return "", nil, fmt.Errorf("unable to cast value to []float64 for %v", v)
  292. }
  293. result[i] = temp
  294. }
  295. return v2.ValueTypeFloat64Array, result, nil
  296. }
  297. } else { // default to string array
  298. return v2.ValueTypeStringArray, []string{}, nil
  299. }
  300. case []byte:
  301. return v2.ValueTypeBinary, v, nil
  302. default:
  303. return "", nil, fmt.Errorf("unable to cast value to []interface{} for %v", v)
  304. }
  305. }
  306. return "", nil, fmt.Errorf("unsupported value %v(%s)", v, k)
  307. }
  308. func getValueByType(v interface{}, vt string) (interface{}, error) {
  309. switch vt {
  310. case v2.ValueTypeBool:
  311. return cast.ToBool(v, cast.CONVERT_SAMEKIND)
  312. case v2.ValueTypeInt8:
  313. return cast.ToInt8(v, cast.CONVERT_SAMEKIND)
  314. case v2.ValueTypeInt16:
  315. return cast.ToInt16(v, cast.CONVERT_SAMEKIND)
  316. case v2.ValueTypeInt32:
  317. return cast.ToInt32(v, cast.CONVERT_SAMEKIND)
  318. case v2.ValueTypeInt64:
  319. return cast.ToInt64(v, cast.CONVERT_SAMEKIND)
  320. case v2.ValueTypeUint8:
  321. return cast.ToUint8(v, cast.CONVERT_SAMEKIND)
  322. case v2.ValueTypeUint16:
  323. return cast.ToUint16(v, cast.CONVERT_SAMEKIND)
  324. case v2.ValueTypeUint32:
  325. return cast.ToUint32(v, cast.CONVERT_SAMEKIND)
  326. case v2.ValueTypeUint64:
  327. return cast.ToUint64(v, cast.CONVERT_SAMEKIND)
  328. case v2.ValueTypeFloat32:
  329. return cast.ToFloat32(v, cast.CONVERT_SAMEKIND)
  330. case v2.ValueTypeFloat64:
  331. return cast.ToFloat64(v, cast.CONVERT_SAMEKIND)
  332. case v2.ValueTypeString:
  333. return cast.ToString(v, cast.CONVERT_SAMEKIND)
  334. case v2.ValueTypeBoolArray:
  335. return cast.ToBoolSlice(v, cast.CONVERT_SAMEKIND)
  336. case v2.ValueTypeInt8Array:
  337. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  338. return cast.ToInt8(input, sn)
  339. }, "int8", cast.CONVERT_SAMEKIND)
  340. case v2.ValueTypeInt16Array:
  341. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  342. return cast.ToInt16(input, sn)
  343. }, "int16", cast.CONVERT_SAMEKIND)
  344. case v2.ValueTypeInt32Array:
  345. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  346. return cast.ToInt32(input, sn)
  347. }, "int32", cast.CONVERT_SAMEKIND)
  348. case v2.ValueTypeInt64Array:
  349. return cast.ToInt64Slice(v, cast.CONVERT_SAMEKIND)
  350. case v2.ValueTypeUint8Array:
  351. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  352. return cast.ToUint8(input, sn)
  353. }, "uint8", cast.CONVERT_SAMEKIND)
  354. case v2.ValueTypeUint16Array:
  355. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  356. return cast.ToUint16(input, sn)
  357. }, "uint16", cast.CONVERT_SAMEKIND)
  358. case v2.ValueTypeUint32Array:
  359. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  360. return cast.ToUint32(input, sn)
  361. }, "uint32", cast.CONVERT_SAMEKIND)
  362. case v2.ValueTypeUint64Array:
  363. return cast.ToUint64Slice(v, cast.CONVERT_SAMEKIND)
  364. case v2.ValueTypeFloat32Array:
  365. return cast.ToTypedSlice(v, func(input interface{}, sn cast.Strictness) (interface{}, error) {
  366. return cast.ToFloat32(input, sn)
  367. }, "float32", cast.CONVERT_SAMEKIND)
  368. case v2.ValueTypeFloat64Array:
  369. return cast.ToFloat64Slice(v, cast.CONVERT_SAMEKIND)
  370. case v2.ValueTypeStringArray:
  371. return cast.ToStringSlice(v, cast.CONVERT_SAMEKIND)
  372. case v2.ValueTypeBinary:
  373. var (
  374. bv []byte
  375. err error
  376. )
  377. switch vv := v.(type) {
  378. case string:
  379. if bv, err = base64.StdEncoding.DecodeString(vv); err != nil {
  380. return nil, fmt.Errorf("fail to decode binary value from %s: %v", vv, err)
  381. }
  382. case []byte:
  383. bv = vv
  384. default:
  385. return nil, fmt.Errorf("fail to decode binary value from %v: not binary type", vv)
  386. }
  387. return bv, nil
  388. default:
  389. return nil, fmt.Errorf("unsupported type %v", vt)
  390. }
  391. }
  392. func (ems *EdgexMsgBusSink) getMeta(result []map[string]interface{}) *meta {
  393. if ems.c.Metadata == "" {
  394. return newMetaFromMap(nil)
  395. }
  396. //Try to get the meta field
  397. for _, v := range result {
  398. if m, ok := v[ems.c.Metadata]; ok {
  399. if m1, ok1 := m.(map[string]interface{}); ok1 {
  400. return newMetaFromMap(m1)
  401. } else {
  402. conf.Log.Infof("Specified a meta field, but the field does not contains any EdgeX metadata.")
  403. }
  404. }
  405. }
  406. return newMetaFromMap(nil)
  407. }
  408. func (ems *EdgexMsgBusSink) Collect(ctx api.StreamContext, item interface{}) error {
  409. logger := ctx.GetLogger()
  410. var (
  411. evt *dtos.Event
  412. err error
  413. )
  414. switch payload := item.(type) {
  415. case map[string]interface{}:
  416. evt, err = ems.produceEvents(ctx, []map[string]interface{}{payload})
  417. case []map[string]interface{}:
  418. evt, err = ems.produceEvents(ctx, payload)
  419. default:
  420. // impossible
  421. return fmt.Errorf("receive invalid data %v", item)
  422. }
  423. if err != nil {
  424. return fmt.Errorf("Failed to convert to EdgeX event: %s.", err.Error())
  425. }
  426. var (
  427. data []byte
  428. topic string
  429. )
  430. if ems.c.MessageType == MessageTypeRequest {
  431. req := requests.NewAddEventRequest(*evt)
  432. data, _, err = req.Encode()
  433. if err != nil {
  434. return fmt.Errorf("unexpected error encode event %v", err)
  435. }
  436. } else {
  437. data, err = json.Marshal(evt)
  438. if err != nil {
  439. return fmt.Errorf("unexpected error MarshalEvent %v", err)
  440. }
  441. }
  442. env := types.NewMessageEnvelope(data, ctx)
  443. env.ContentType = ems.c.ContentType
  444. if ems.topic == "" { // dynamic topic
  445. topic = fmt.Sprintf("%s/%s/%s/%s", ems.c.TopicPrefix, evt.ProfileName, evt.DeviceName, evt.SourceName)
  446. } else {
  447. topic = ems.topic
  448. }
  449. if e := ems.client.Publish(env, topic); e != nil {
  450. logger.Errorf("%s: found error %s when publish to EdgeX message bus.\n", e)
  451. return fmt.Errorf("%s:%s", errorx.IOErr, e.Error())
  452. }
  453. logger.Debugf("Published %+v to EdgeX message bus topic %s", evt, topic)
  454. return nil
  455. }
  456. func (ems *EdgexMsgBusSink) Close(ctx api.StreamContext) error {
  457. logger := ctx.GetLogger()
  458. logger.Infof("Closing edgex sink")
  459. if ems.client != nil && ems.c.ConnectionSelector == "" {
  460. if e := ems.client.Disconnect(); e != nil {
  461. return e
  462. }
  463. }
  464. if ems.c.ConnectionSelector != "" {
  465. ctx.ReleaseConnection(ems.c.ConnectionSelector)
  466. }
  467. return nil
  468. }
  469. type eventMeta struct {
  470. id *string
  471. deviceName string
  472. profileName string
  473. sourceName string
  474. origin *int64
  475. tags map[string]string
  476. }
  477. type readingMeta struct {
  478. id *string
  479. deviceName *string
  480. profileName *string
  481. resourceName *string
  482. origin *int64
  483. valueType *string
  484. mediaType *string
  485. }
  486. func (m *readingMeta) decorate(r *dtos.BaseReading) dtos.BaseReading {
  487. if m.id != nil {
  488. r.Id = *m.id
  489. }
  490. if m.deviceName != nil {
  491. r.DeviceName = *m.deviceName
  492. }
  493. if m.profileName != nil {
  494. r.ProfileName = *m.profileName
  495. }
  496. if m.origin != nil {
  497. r.Origin = *m.origin
  498. }
  499. if m.valueType != nil {
  500. r.ValueType = *m.valueType
  501. }
  502. if m.mediaType != nil {
  503. r.MediaType = *m.mediaType
  504. }
  505. return *r
  506. }
  507. type meta struct {
  508. eventMeta
  509. readingMetas map[string]interface{}
  510. }
  511. func newMetaFromMap(m1 map[string]interface{}) *meta {
  512. result := &meta{
  513. eventMeta: eventMeta{},
  514. }
  515. for k, v := range m1 {
  516. switch k {
  517. case "id":
  518. if v1, ok := v.(string); ok {
  519. result.id = &v1
  520. }
  521. case "deviceName":
  522. if v1, ok := v.(string); ok {
  523. result.deviceName = v1
  524. }
  525. case "profileName":
  526. if v1, ok := v.(string); ok {
  527. result.profileName = v1
  528. }
  529. case "sourceName":
  530. if v1, ok := v.(string); ok {
  531. result.sourceName = v1
  532. }
  533. case "origin":
  534. if v1, ok := v.(float64); ok {
  535. temp := int64(v1)
  536. result.origin = &temp
  537. }
  538. case "tags":
  539. if v1, ok1 := v.(map[string]interface{}); ok1 {
  540. r := make(map[string]string)
  541. for k, vi := range v1 {
  542. s, ok := vi.(string)
  543. if ok {
  544. r[k] = s
  545. }
  546. }
  547. result.tags = r
  548. }
  549. default:
  550. if result.readingMetas == nil {
  551. result.readingMetas = make(map[string]interface{})
  552. }
  553. result.readingMetas[k] = v
  554. }
  555. }
  556. return result
  557. }
  558. func (m *meta) readingMeta(ctx api.StreamContext, readingName string) *readingMeta {
  559. vi, ok := m.readingMetas[readingName]
  560. if !ok {
  561. return nil
  562. }
  563. m1, ok := vi.(map[string]interface{})
  564. if !ok {
  565. ctx.GetLogger().Errorf("reading %s meta is not a map, but %v", readingName, vi)
  566. return nil
  567. }
  568. result := &readingMeta{}
  569. for k, v := range m1 {
  570. switch k {
  571. case "id":
  572. if v1, ok := v.(string); ok {
  573. result.id = &v1
  574. }
  575. case "deviceName":
  576. if v1, ok := v.(string); ok {
  577. result.deviceName = &v1
  578. }
  579. case "profileName":
  580. if v1, ok := v.(string); ok {
  581. result.profileName = &v1
  582. }
  583. case "resourceName":
  584. if v1, ok := v.(string); ok {
  585. result.resourceName = &v1
  586. }
  587. case "origin":
  588. if v1, ok := v.(float64); ok {
  589. temp := int64(v1)
  590. result.origin = &temp
  591. }
  592. case "valueType":
  593. if v1, ok := v.(string); ok {
  594. result.valueType = &v1
  595. }
  596. case "mediaType":
  597. if v1, ok := v.(string); ok {
  598. result.mediaType = &v1
  599. }
  600. default:
  601. ctx.GetLogger().Warnf("reading %s meta got unknown field %s of value %v", readingName, k, v)
  602. }
  603. }
  604. return result
  605. }
  606. func (m *meta) createEvent() *dtos.Event {
  607. event := dtos.NewEvent(m.profileName, m.deviceName, m.sourceName)
  608. if m.id != nil {
  609. event.Id = *m.id
  610. }
  611. if m.origin != nil {
  612. event.Origin = *m.origin
  613. }
  614. if m.tags != nil {
  615. event.Tags = m.tags
  616. }
  617. return &event
  618. }