diff --git a/admin/main.go b/admin/main.go index 904bb01..5408c25 100644 --- a/admin/main.go +++ b/admin/main.go @@ -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 + } +} diff --git a/circuit.cddx b/circuit.cddx index 7c19f43..4dc76ef 100644 Binary files a/circuit.cddx and b/circuit.cddx differ diff --git a/circuit.png b/circuit.png index b389fb9..8db5e56 100644 Binary files a/circuit.png and b/circuit.png differ diff --git a/server/main.go b/server/main.go index 9ac3330..b44a9ab 100644 --- a/server/main.go +++ b/server/main.go @@ -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 != "" {