connect_selector.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package conf
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. const CONNECTION_CONF = "connections/connection.yaml"
  7. type ConSelector struct {
  8. ConnSelectorStr string
  9. Type string // mqtt edgex
  10. CfgKey string // config key
  11. }
  12. func (c *ConSelector) Init() error {
  13. conTypeSel := strings.SplitN(c.ConnSelectorStr, ".", 2)
  14. if len(conTypeSel) != 2 {
  15. return fmt.Errorf("not a valid connection selector : %s", c.ConnSelectorStr)
  16. }
  17. c.Type = strings.ToLower(conTypeSel[0])
  18. c.CfgKey = strings.ToLower(conTypeSel[1])
  19. return nil
  20. }
  21. func (c *ConSelector) ReadCfgFromYaml() (props map[string]interface{}, err error) {
  22. var (
  23. found = false
  24. )
  25. cfg := make(map[string]interface{})
  26. err = LoadConfigByName(CONNECTION_CONF, &cfg)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if cons, ok := cfg[c.Type]; ok {
  31. if connItems, ok1 := cons.(map[string]interface{}); ok1 {
  32. if conItem, ok := connItems[c.CfgKey]; ok {
  33. if item, ok1 := conItem.(map[string]interface{}); ok1 {
  34. props = item
  35. found = true
  36. }
  37. }
  38. }
  39. }
  40. if !found {
  41. return nil, fmt.Errorf("not found connection Type and Selector: %s.%s", c.Type, c.CfgKey)
  42. }
  43. jsonPath := "sources/" + c.Type + ".json"
  44. if c.Type == "mqtt" {
  45. jsonPath = "mqtt_source.json"
  46. }
  47. err = CorrectsConfigKeysByJson(props, jsonPath)
  48. return props, err
  49. }