From 53722bdf182a4013fc86c0bc224e122f349830e1 Mon Sep 17 00:00:00 2001 From: Daniel Svitan Date: Sat, 7 Jun 2025 15:49:34 +0200 Subject: [PATCH] :bug: Fixes server requests --- admin/main.go | 30 +++++++++++++++++------------- peripheral/main.py | 2 +- server/main.go | 6 +++--- server/routes.go | 5 ++++- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/admin/main.go b/admin/main.go index 7c5acc6..7baf764 100644 --- a/admin/main.go +++ b/admin/main.go @@ -27,7 +27,7 @@ type ChangeLockReq struct { } type ChangeAlertReq struct { - Alert bool `json:"alert"` + Alert bool `json:"alerts"` For int `json:"for"` } @@ -135,14 +135,16 @@ func main() { Usage: "change lock status", Commands: []*cli.Command{ { - Name: "yes", - Usage: "lock the door", - Action: createManageLock(true), + Name: "yes", + Usage: "lock the door", + Aliases: []string{"y", "on", "1", "do"}, + Action: createManageLock(true), }, { - Name: "no", - Usage: "unlock the door", - Action: createManageLock(false), + Name: "no", + Usage: "unlock the door", + Aliases: []string{"n", "off", "0", "undo"}, + Action: createManageLock(false), }, }, Action: getLocked, @@ -152,13 +154,15 @@ func main() { Usage: "change alert status", Commands: []*cli.Command{ { - Name: "yes", - Usage: "resume alerts", - Action: createManageAlert(true), + Name: "yes", + Usage: "resume alerts", + Aliases: []string{"y", "on", "1", "do"}, + Action: createManageAlert(true), }, { - Name: "no", - Usage: "pause alerts", + Name: "no", + Usage: "pause alerts", + Aliases: []string{"n", "off", "0", "undo"}, Flags: []cli.Flag{ &cli.StringFlag{ Name: "for", @@ -322,7 +326,7 @@ func createManageAlert(alert bool) func(context.Context, *cli.Command) error { action = "paused" } - fmt.Printf("alerts were %sd%s\n", action, rest) + fmt.Printf("alerts were %s%s\n", action, rest) return nil } } diff --git a/peripheral/main.py b/peripheral/main.py index b8ddb1a..2e19158 100644 --- a/peripheral/main.py +++ b/peripheral/main.py @@ -98,7 +98,7 @@ class App: try: r = urequests.post( f"{self.server}/opened", - headers={"Authorization": self.token, "Content-Type": "application/json"}, + headers={"Authorization": f"Bearer {self.token}", "Content-Type": "application/json"}, data=raw ) print(f"State updated [{r.status_code}] {r.content.decode()}") diff --git a/server/main.go b/server/main.go index 27eecfd..a83db01 100644 --- a/server/main.go +++ b/server/main.go @@ -23,14 +23,14 @@ const TimeFormat = "2006-01-02 15:04:05" var token string // the condition is: distance >= threshold (is door open) -var opened bool +var opened = false var openedChange = make(chan any) // is door locked -var locked bool = false +var locked = false // alerts the user when door locked and but open? -var alerts bool = false +var alerts = false var gotifyToken string var gotifyURL string diff --git a/server/routes.go b/server/routes.go index 58b251b..a51cc6a 100644 --- a/server/routes.go +++ b/server/routes.go @@ -79,7 +79,9 @@ func setOpened(c echo.Context) error { go sendAlert(action) } - openedChange <- 0 + go func() { + openedChange <- 0 + }() mut.Unlock() return c.NoContent(http.StatusOK) } @@ -126,6 +128,7 @@ func setAlerts(c echo.Context) error { } if data.For > 0 { + log.Infof("pausing alerts for %d seconds", data.For) go func() { time.Sleep(time.Duration(data.For) * time.Second)