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.
76 lines
1.7 KiB
76 lines
1.7 KiB
package services
|
|
|
|
import (
|
|
"context"
|
|
"git.aiterp.net/lucifer/new-server/app/config"
|
|
"git.aiterp.net/lucifer/new-server/models"
|
|
"log"
|
|
"strconv"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var cancelMap = make(map[int]context.CancelFunc, 8)
|
|
var cancelMutex sync.Mutex
|
|
|
|
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 {
|
|
cancelMutex.Lock()
|
|
isRunning := cancelMap[bridge.ID] != nil
|
|
cancelMutex.Unlock()
|
|
if isRunning {
|
|
continue
|
|
}
|
|
|
|
driver, err := config.DriverProvider().Provide(bridge.Driver)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancelMutex.Lock()
|
|
cancelMap[bridge.ID] = cancel
|
|
cancelMutex.Unlock()
|
|
|
|
log.Printf("Running bridge \"%s\" (%d)", bridge.Name, bridge.ID)
|
|
|
|
go func(bridge models.Bridge, cancel func()) {
|
|
savedDevices, err := config.DeviceRepository().FetchByReference(ctx, models.RKBridgeID, strconv.Itoa(bridge.ID))
|
|
if err != nil {
|
|
log.Println("Failed to fetch devices from db for refresh:", err)
|
|
}
|
|
err = driver.Publish(ctx, bridge, savedDevices)
|
|
if err != nil {
|
|
log.Println("Failed to publish devices from db before run:", err)
|
|
}
|
|
|
|
err = driver.Run(ctx, bridge, config.EventChannel)
|
|
log.Printf("Bridge \"%s\" (%d) stopped: %s", bridge.Name, bridge.ID, err)
|
|
|
|
cancelMutex.Lock()
|
|
cancel()
|
|
cancelMap[bridge.ID] = nil
|
|
cancelMutex.Unlock()
|
|
}(bridge, cancel)
|
|
}
|
|
|
|
return nil
|
|
}
|