package services import ( "context" "git.aiterp.net/lucifer/new-server/app/config" "log" "time" ) var cancelMap = make(map[int]context.CancelFunc, 8) func ConnectToBridges() { go func() { for { err := runConnectToBridges() if err != nil { log.Println("Bridge connection error: ", err.Error()) } time.Sleep(15 * time.Second) } }() } func runConnectToBridges() error { bridges, err := config.BridgeRepository().FetchAll(context.Background()) if err != nil { return err } for _, bridge := range bridges { if cancelMap[bridge.ID] != nil { continue } driver, err := config.DriverProvider().Provide(bridge.Driver) if err != nil { return err } ctx, cancel := context.WithCancel(context.Background()) err = driver.Run(ctx, bridge, config.EventChannel) cancelMap[bridge.ID] = cancel log.Printf("Connected to bridge \"%s\" (%d)", bridge.Name, bridge.ID) } return nil }