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
}
}