client_edgex.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // +build edgex
  15. package connection
  16. import (
  17. "fmt"
  18. "github.com/edgexfoundry/go-mod-messaging/v2/messaging"
  19. "github.com/edgexfoundry/go-mod-messaging/v2/pkg/types"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/pkg/cast"
  22. )
  23. func init() {
  24. registerClientFactory("edgex", func(s *ConSelector) Client {
  25. return &EdgexClient{selector: s}
  26. })
  27. }
  28. type EdgexClient struct {
  29. selector *ConSelector
  30. mbconf types.MessageBusConfig
  31. client messaging.MessageClient
  32. }
  33. type EdgexConf struct {
  34. Protocol string `json:"protocol"`
  35. Server string `json:"server"`
  36. Port int `json:"port"`
  37. Type string `json:"type"`
  38. Optional map[string]string `json:"optional"`
  39. }
  40. func (es *EdgexClient) CfgValidate(props map[string]interface{}) error {
  41. edgexJsonPath := "sources/edgex.json"
  42. err := conf.CorrectsConfigKeysByJson(props, edgexJsonPath)
  43. if err != nil {
  44. return fmt.Errorf("read properties %v fail for connection selector %s with error: %v", props, es.selector.ConnSelectorCfg, err)
  45. }
  46. c := &EdgexConf{}
  47. err = cast.MapToStructStrict(props, c)
  48. if err != nil {
  49. return fmt.Errorf("map config map to struct %v fail for connection selector %s with error: %v", props, es.selector.ConnSelectorCfg, err)
  50. }
  51. if c.Server == "" {
  52. return fmt.Errorf("missing server property for connection selector %s", es.selector.ConnSelectorCfg)
  53. }
  54. if c.Port == 0 {
  55. return fmt.Errorf("missing port property for connection selector %s", es.selector.ConnSelectorCfg)
  56. }
  57. if c.Type != messaging.ZeroMQ && c.Type != messaging.MQTT && c.Type != messaging.Redis {
  58. return fmt.Errorf("specified wrong type value %s for connection selector %s", c.Type, es.selector.ConnSelectorCfg)
  59. }
  60. mbconf := types.MessageBusConfig{
  61. SubscribeHost: types.HostInfo{
  62. Protocol: c.Protocol,
  63. Host: c.Server,
  64. Port: c.Port,
  65. },
  66. PublishHost: types.HostInfo{
  67. Host: c.Server,
  68. Port: c.Port,
  69. Protocol: c.Protocol,
  70. },
  71. Type: c.Type}
  72. mbconf.Optional = c.Optional
  73. es.mbconf = mbconf
  74. return nil
  75. }
  76. func (es *EdgexClient) GetClient() (interface{}, error) {
  77. client, err := messaging.NewMessageClient(es.mbconf)
  78. if err != nil {
  79. return nil, err
  80. }
  81. if err := client.Connect(); err != nil {
  82. conf.Log.Errorf("The connection to edgex messagebus failed for connection selector : %s.", es.selector.ConnSelectorCfg)
  83. return nil, fmt.Errorf("Failed to connect to edgex message bus: " + err.Error())
  84. }
  85. conf.Log.Infof("The connection to edgex messagebus is established successfully for connection selector : %s.", es.selector.ConnSelectorCfg)
  86. es.client = client
  87. return client, nil
  88. }
  89. func (es *EdgexClient) CloseClient() error {
  90. conf.Log.Infof("Closing the connection to edgex messagebus for connection selector : %s.", es.selector.ConnSelectorCfg)
  91. if e := es.client.Disconnect(); e != nil {
  92. return e
  93. }
  94. return nil
  95. }