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.
 
 
 
 

144 lines
3.1 KiB

package api
import (
"fmt"
"git.aiterp.net/lucifer/new-server/app/config"
"git.aiterp.net/lucifer/new-server/models"
"github.com/gin-gonic/gin"
"log"
"time"
)
func Bridges(r gin.IRoutes) {
r.GET("", handler(func(c *gin.Context) (interface{}, error) {
return config.BridgeRepository().FetchAll(ctxOf(c))
}))
r.GET("/:id", handler(func(c *gin.Context) (interface{}, error) {
return config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
}))
r.POST("", handler(func(c *gin.Context) (interface{}, error) {
var body struct {
Driver models.DriverKind `json:"driver"`
Address string `json:"address"`
Token string `json:"token"`
ForceSave bool `json:"forceSave"`
DryRun bool `json:"dryRun"`
}
err := parseBody(c, &body)
if err != nil {
return nil, err
}
driver, err := config.DriverProvider().Provide(body.Driver)
if err != nil {
return nil, err
}
if body.ForceSave {
bridge := models.Bridge{
Name: fmt.Sprintf("Manually saved %s bridge (%s)", body.Driver, body.Address),
Driver: body.Driver,
Address: body.Address,
Token: body.Token,
}
err := config.BridgeRepository().Save(ctxOf(c), &bridge)
if err != nil {
return nil, err
}
return []models.Bridge{bridge}, nil
}
bridges, err := driver.SearchBridge(ctxOf(c), body.Address, body.Token, body.DryRun)
if err != nil {
return nil, err
}
if !body.DryRun {
for _, bridge := range bridges {
err := config.BridgeRepository().Save(ctxOf(c), &bridge)
if err != nil {
return nil, err
}
log.Printf("Saved new bridge: %s (%s)", bridge.Name, bridge.Address)
}
}
return bridges, nil
}))
r.PUT("/:id", handler(func(c *gin.Context) (interface{}, error) {
var body struct {
Name *string `json:"name"`
}
err := parseBody(c, &body)
if err != nil {
return nil, err
}
bridge, err := config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
if err != nil {
return nil, err
}
if body.Name != nil {
bridge.Name = *body.Name
}
err = config.BridgeRepository().Save(ctxOf(c), &bridge)
if err != nil {
return nil, err
}
return bridge, nil
}))
r.PUT("/:id/search", handler(func(c *gin.Context) (interface{}, error) {
var body struct {
TimeoutMS int64 `json:"timeoutMs"`
}
err := parseBody(c, &body)
if err != nil {
return nil, err
}
if body.TimeoutMS <= 0 {
body.TimeoutMS = 30000
}
bridge, err := config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
if err != nil {
return nil, err
}
driver, err := config.DriverProvider().Provide(bridge.Driver)
if err != nil {
return nil, err
}
devices, err := driver.SearchDevices(ctxOf(c), bridge, time.Millisecond * time.Duration(body.TimeoutMS))
if err != nil {
return nil, err
}
return devices, nil
}))
r.DELETE("/:id", handler(func(c *gin.Context) (interface{}, error) {
bridge, err := config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
if err != nil {
return nil, err
}
err = config.BridgeRepository().Delete(ctxOf(c), &bridge)
if err != nil {
return nil, err
}
return bridge, nil
}))
}