95 lines
2.1 KiB
Go
95 lines
2.1 KiB
Go
package driver
|
|
|
|
import (
|
|
"artNet/auth"
|
|
"artNet/models"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// sends a list of all buses in the driver
|
|
func (d *ArtNetDriver) GetAllBuses(c *gin.Context) {
|
|
var data any
|
|
if len(d.Buses) == 0 {
|
|
data = "no buses avaiable"
|
|
} else {
|
|
data = *d
|
|
}
|
|
|
|
c.JSON(200, gin.H{
|
|
"buses": data,
|
|
})
|
|
}
|
|
|
|
func (d *ArtNetDriver) CreateBus(c *gin.Context) {
|
|
_, err := auth.GetIDFromAuth(c)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "id: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
var payload models.Bus
|
|
|
|
if err := c.BindJSON(&payload); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "json: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
if payload.Name == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "bus name missing"})
|
|
return
|
|
} else if addr := net.ParseIP(payload.Ip); addr == nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "wrong ip '" + payload.Ip + "'"})
|
|
return
|
|
}
|
|
|
|
if _, ok := d.Buses[payload.Name]; ok {
|
|
c.JSON(http.StatusOK, gin.H{"message": "bus " + payload.Name + " exists already"})
|
|
return
|
|
}
|
|
|
|
bus := d.NewBus(payload.Name, payload.Ip, payload.GetPort())
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": fmt.Sprintf("bus '%s' successfully created with ip: %s and on port: %d", bus.Name, bus.Ip, bus.GetPort()),
|
|
})
|
|
d.cfgHandler.SaveCfg(*d)
|
|
}
|
|
|
|
func (d *ArtNetDriver) RemoveBus(c *gin.Context) {
|
|
_, err := auth.GetIDFromAuth(c)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "id: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
var payload models.Bus
|
|
|
|
if err := c.BindJSON(&payload); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "json: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
if payload.Name == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "bus name missing"})
|
|
return
|
|
}
|
|
|
|
if _, ok := d.Buses[payload.Name]; !ok {
|
|
c.JSON(http.StatusOK, gin.H{"message": "bus " + payload.Name + " not found"})
|
|
c.JSON(http.StatusOK, models.JsonResponse{
|
|
Message: "bus " + payload.Name + " not found"})
|
|
return
|
|
} else {
|
|
delete(d.Buses, payload.Name)
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.JsonResponse{
|
|
Message: fmt.Sprintf("bus '%s' successfully removed", payload.Name),
|
|
})
|
|
d.cfgHandler.SaveCfg(*d)
|
|
}
|