68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package web
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.ulra.eu/adro/miniwol/config"
|
|
"git.ulra.eu/adro/miniwol/lib"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func add(c echo.Context) error {
|
|
device := config.Device{}
|
|
err := c.Bind(&device)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
config.Config.Devices = append(config.Config.Devices, device)
|
|
config.Save()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.Redirect(http.StatusSeeOther, "/")
|
|
}
|
|
|
|
func wake(c echo.Context) error {
|
|
index, err := strconv.Atoi(c.FormValue("Index"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for i, device := range config.Config.Devices {
|
|
if i == index {
|
|
if !strings.Contains(device.IP, ":") {
|
|
device.IP += ":9"
|
|
}
|
|
err := lib.SendPacket(":0", device.IP, device.MAC)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Redirect(http.StatusSeeOther, "/")
|
|
}
|
|
}
|
|
return errors.New("device not found")
|
|
}
|
|
|
|
func remove(c echo.Context) error {
|
|
index, err := strconv.Atoi(c.FormValue("Index"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for i := range config.Config.Devices {
|
|
if i == index {
|
|
config.Config.Devices = append(config.Config.Devices[:i], config.Config.Devices[i+1:]...)
|
|
err := config.Save()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.Redirect(http.StatusSeeOther, "/")
|
|
}
|
|
}
|
|
return errors.New("device not found")
|
|
}
|