client_edgex.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. c := &EdgexConf{}
  42. err := cast.MapToStructStrict(props, c)
  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. if c.Server == "" {
  47. return fmt.Errorf("missing server property for connection selector %s", es.selector.ConnSelectorCfg)
  48. }
  49. if c.Port == 0 {
  50. return fmt.Errorf("missing port property for connection selector %s", es.selector.ConnSelectorCfg)
  51. }
  52. if c.Type != messaging.ZeroMQ && c.Type != messaging.MQTT && c.Type != messaging.Redis {
  53. return fmt.Errorf("specified wrong type value %s for connection selector %s", c.Type, es.selector.ConnSelectorCfg)
  54. }
  55. mbconf := types.MessageBusConfig{
  56. SubscribeHost: types.HostInfo{
  57. Protocol: c.Protocol,
  58. Host: c.Server,
  59. Port: c.Port,
  60. },
  61. PublishHost: types.HostInfo{
  62. Host: c.Server,
  63. Port: c.Port,
  64. Protocol: c.Protocol,
  65. },
  66. Type: c.Type}
  67. mbconf.Optional = c.Optional
  68. es.mbconf = mbconf
  69. return nil
  70. }
  71. func (es *EdgexClient) GetClient() (interface{}, error) {
  72. client, err := messaging.NewMessageClient(es.mbconf)
  73. if err != nil {
  74. return nil, err
  75. }
  76. if err := client.Connect(); err != nil {
  77. conf.Log.Errorf("The connection to edgex messagebus failed for connection selector : %s.", es.selector.ConnSelectorCfg)
  78. return nil, fmt.Errorf("Failed to connect to edgex message bus: " + err.Error())
  79. }
  80. conf.Log.Infof("The connection to edgex messagebus is established successfully for connection selector : %s.", es.selector.ConnSelectorCfg)
  81. es.client = client
  82. return client, nil
  83. }
  84. func (es *EdgexClient) CloseClient() error {
  85. conf.Log.Infof("Closing the connection to edgex messagebus for connection selector : %s.", es.selector.ConnSelectorCfg)
  86. if e := es.client.Disconnect(); e != nil {
  87. return e
  88. }
  89. return nil
  90. }