✨ Adds lock and alert commands to admin
This commit is contained in:
parent
e09d716eba
commit
9e312c3f40
153
admin/main.go
153
admin/main.go
@ -2,12 +2,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
@ -85,6 +87,8 @@ func makeReqRet(method string, url string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
|
|
||||||
cmd := &cli.Command{
|
cmd := &cli.Command{
|
||||||
Name: "doorctl",
|
Name: "doorctl",
|
||||||
Usage: "door-alarm server administration tool",
|
Usage: "door-alarm server administration tool",
|
||||||
@ -113,6 +117,46 @@ func main() {
|
|||||||
},
|
},
|
||||||
Action: get,
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
opened, err := makeReqRet(http.MethodGet, "/read")
|
opened, err := makeReqRet(http.MethodGet, fmt.Sprintf("%s/read", server))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -146,7 +190,7 @@ func get(ctx context.Context, cmd *cli.Command) error {
|
|||||||
if cmd.Bool("raw") {
|
if cmd.Bool("raw") {
|
||||||
fmt.Println(opened)
|
fmt.Println(opened)
|
||||||
} else {
|
} else {
|
||||||
if string(opened) == "true" {
|
if string(opened) == "true\n" {
|
||||||
fmt.Println("door is open")
|
fmt.Println("door is open")
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("door is closed")
|
fmt.Println("door is closed")
|
||||||
@ -155,3 +199,108 @@ func get(ctx context.Context, cmd *cli.Command) error {
|
|||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BIN
circuit.cddx
BIN
circuit.cddx
Binary file not shown.
BIN
circuit.png
BIN
circuit.png
Binary file not shown.
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
@ -103,7 +103,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.GET("/", func(c echo.Context) error {
|
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 {
|
app.GET("/read", authed(func(c echo.Context) error {
|
||||||
@ -143,7 +143,7 @@ func main() {
|
|||||||
return c.NoContent(http.StatusOK)
|
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()
|
mut.Lock()
|
||||||
l := locked
|
l := locked
|
||||||
mut.Unlock()
|
mut.Unlock()
|
||||||
@ -151,7 +151,7 @@ func main() {
|
|||||||
return c.JSON(http.StatusOK, l)
|
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()
|
mut.Lock()
|
||||||
locked = true
|
locked = true
|
||||||
mut.Unlock()
|
mut.Unlock()
|
||||||
@ -159,7 +159,7 @@ func main() {
|
|||||||
return c.NoContent(http.StatusOK)
|
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()
|
mut.Lock()
|
||||||
locked = false
|
locked = false
|
||||||
mut.Unlock()
|
mut.Unlock()
|
||||||
@ -167,6 +167,14 @@ func main() {
|
|||||||
return c.NoContent(http.StatusOK)
|
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 {
|
app.POST("/alerts/pause", authed(func(c echo.Context) error {
|
||||||
pauseForRaw := c.QueryParam("for")
|
pauseForRaw := c.QueryParam("for")
|
||||||
if pauseForRaw != "" {
|
if pauseForRaw != "" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user