You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
928 B

4 years ago
  1. package services
  2. import (
  3. "context"
  4. "git.aiterp.net/lucifer/new-server/app/config"
  5. "log"
  6. "time"
  7. )
  8. var cancelMap = make(map[int]context.CancelFunc, 8)
  9. func ConnectToBridges() {
  10. go func() {
  11. for {
  12. err := runConnectToBridges()
  13. if err != nil {
  14. log.Println("Bridge connection error: ", err.Error())
  15. }
  16. time.Sleep(15 * time.Second)
  17. }
  18. }()
  19. }
  20. func runConnectToBridges() error {
  21. bridges, err := config.BridgeRepository().FetchAll(context.Background())
  22. if err != nil {
  23. return err
  24. }
  25. for _, bridge := range bridges {
  26. if cancelMap[bridge.ID] != nil {
  27. continue
  28. }
  29. driver, err := config.DriverProvider().Provide(bridge.Driver)
  30. if err != nil {
  31. return err
  32. }
  33. ctx, cancel := context.WithCancel(context.Background())
  34. err = driver.Run(ctx, bridge, config.EventChannel)
  35. cancelMap[bridge.ID] = cancel
  36. log.Printf("Connected to bridge \"%s\" (%d)", bridge.Name, bridge.ID)
  37. }
  38. return nil
  39. }