Adds lock and alert commands to admin

This commit is contained in:
Daniel Svitan 2025-05-31 19:10:45 +02:00
parent e09d716eba
commit 9e312c3f40
4 changed files with 163 additions and 6 deletions

View File

@ -2,12 +2,14 @@ package main
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"github.com/spf13/viper"
"github.com/urfave/cli/v3"
@ -85,6 +87,8 @@ func makeReqRet(method string, url string) ([]byte, error) {
}
func main() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
cmd := &cli.Command{
Name: "doorctl",
Usage: "door-alarm server administration tool",
@ -113,6 +117,46 @@ func main() {
},
Action: get,
},
{
Name: "lock",
Usage: "change lock status",
Commands: []*cli.Command{
{
Name: "do",
Usage: "lock the door",
Action: createManageLock(true),
},
{
Name: "undo",
Usage: "unlock the door",
Action: createManageLock(false),
},
},
Action: getLock,
},
{
Name: "alerts",
Usage: "change alerts status",
Commands: []*cli.Command{
{
Name: "pause",
Usage: "pause alerts",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "for",
Usage: "pause for x seconds",
},
},
Action: createManageAlerts(true),
},
{
Name: "resume",
Usage: "resume alerts",
Action: createManageAlerts(false),
},
},
Action: getAlerts,
},
},
}
@ -138,7 +182,7 @@ func get(ctx context.Context, cmd *cli.Command) error {
return err
}
opened, err := makeReqRet(http.MethodGet, "/read")
opened, err := makeReqRet(http.MethodGet, fmt.Sprintf("%s/read", server))
if err != nil {
return err
}
@ -146,7 +190,7 @@ func get(ctx context.Context, cmd *cli.Command) error {
if cmd.Bool("raw") {
fmt.Println(opened)
} else {
if string(opened) == "true" {
if string(opened) == "true\n" {
fmt.Println("door is open")
} else {
fmt.Println("door is closed")
@ -155,3 +199,108 @@ func get(ctx context.Context, cmd *cli.Command) error {
return nil
}
func getLock(context.Context, *cli.Command) error {
err := load()
if err != nil {
return err
}
locked, err := makeReqRet(http.MethodGet, fmt.Sprintf("%s/lock", server))
if err != nil {
return err
}
if string(locked) == "true\n" {
fmt.Println("door is locked")
} else {
fmt.Println("door is unlocked")
}
return nil
}
func createManageLock(do bool) func(context.Context, *cli.Command) error {
return func(ctx context.Context, cmd *cli.Command) error {
err := load()
if err != nil {
return err
}
var actionShort string
var action string
if do {
actionShort = "do"
action = "lock"
} else {
actionShort = "undo"
action = "unlock"
}
_, err = makeReqRet(http.MethodPost, fmt.Sprintf("%s/lock/%s", server, actionShort))
if err != nil {
return err
}
fmt.Printf("door was %sed\n", action)
return nil
}
}
func getAlerts(context.Context, *cli.Command) error {
err := load()
if err != nil {
return err
}
alert, err := makeReqRet(http.MethodGet, fmt.Sprintf("%s/alerts", server))
if err != nil {
return err
}
if string(alert) == "true\n" {
fmt.Println("alerts are active")
} else {
fmt.Println("alerts are paused")
}
return nil
}
func createManageAlerts(pause bool) func(context.Context, *cli.Command) error {
return func(ctx context.Context, cmd *cli.Command) error {
err := load()
if err != nil {
return err
}
var action string
if pause {
action = "pause"
} else {
action = "resume"
}
var rest string
var secs int
secsRaw := cmd.String("for")
if secsRaw != "" {
secs, err = strconv.Atoi(secsRaw)
if err != nil {
return err
}
rest = fmt.Sprintf("?for=%d", secs)
}
_, err = makeReqRet(http.MethodPost, fmt.Sprintf("%s/alerts/%s%s", server, action, rest))
if err != nil {
return err
}
if secsRaw != "" {
rest = fmt.Sprintf(" for %d seconds", secs)
}
fmt.Printf("alerts were %sd%s\n", action, rest)
return nil
}
}

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -103,7 +103,7 @@ func main() {
}
app.GET("/", func(c echo.Context) error {
return c.JSON(http.StatusOK, "Hello, World!")
return c.JSON(http.StatusOK, "Hello world!")
})
app.GET("/read", authed(func(c echo.Context) error {
@ -143,7 +143,7 @@ func main() {
return c.NoContent(http.StatusOK)
}))
app.GET("/locked", authed(func(c echo.Context) error {
app.GET("/lock", authed(func(c echo.Context) error {
mut.Lock()
l := locked
mut.Unlock()
@ -151,7 +151,7 @@ func main() {
return c.JSON(http.StatusOK, l)
}))
app.POST("/lock", authed(func(c echo.Context) error {
app.POST("/lock/do", authed(func(c echo.Context) error {
mut.Lock()
locked = true
mut.Unlock()
@ -159,7 +159,7 @@ func main() {
return c.NoContent(http.StatusOK)
}))
app.POST("/unlock", authed(func(c echo.Context) error {
app.POST("/lock/undo", authed(func(c echo.Context) error {
mut.Lock()
locked = false
mut.Unlock()
@ -167,6 +167,14 @@ func main() {
return c.NoContent(http.StatusOK)
}))
app.GET("/alerts", authed(func(c echo.Context) error {
mut.Lock()
a := alert
mut.Unlock()
return c.JSON(http.StatusOK, a)
}))
app.POST("/alerts/pause", authed(func(c echo.Context) error {
pauseForRaw := c.QueryParam("for")
if pauseForRaw != "" {