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