2022-02-21 14:16:06 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2022-02-23 13:02:46 +00:00
|
|
|
"strconv"
|
2022-02-21 14:16:06 +00:00
|
|
|
"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 {
|
2022-02-23 13:02:46 +00:00
|
|
|
index, err := strconv.Atoi(c.FormValue("Index"))
|
2022-02-21 14:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-23 13:02:46 +00:00
|
|
|
for i, device := range config.Config.Devices {
|
|
|
|
if i == index {
|
2022-02-21 14:16:06 +00:00
|
|
|
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 {
|
2022-02-23 13:02:46 +00:00
|
|
|
index, err := strconv.Atoi(c.FormValue("Index"))
|
2022-02-21 14:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-23 13:02:46 +00:00
|
|
|
for i := range config.Config.Devices {
|
|
|
|
if i == index {
|
2022-02-21 14:16:06 +00:00
|
|
|
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")
|
|
|
|
}
|