diff --git a/server/main.go b/server/main.go index a83db01..f7be446 100644 --- a/server/main.go +++ b/server/main.go @@ -24,7 +24,6 @@ var token string // the condition is: distance >= threshold (is door open) var opened = false -var openedChange = make(chan any) // is door locked var locked = false @@ -118,7 +117,6 @@ func main() { app.GET("/", helloWorld) app.GET("/opened", authed(getOpened)) - app.GET("/opened/ws", authed(getOpenedWs)) app.POST("/opened", authed(setOpened)) app.GET("/locked", authed(getLocked)) diff --git a/server/routes.go b/server/routes.go index 7beeb8f..30f62ba 100644 --- a/server/routes.go +++ b/server/routes.go @@ -4,13 +4,10 @@ import ( "net/http" "time" - "github.com/gorilla/websocket" "github.com/labstack/echo/v4" "github.com/labstack/gommon/log" ) -var upgrader = websocket.Upgrader{} - func helloWorld(c echo.Context) error { return c.JSON(http.StatusOK, "Hello world!") } @@ -23,39 +20,6 @@ func getOpened(c echo.Context) error { return c.JSON(http.StatusOK, o) } -func getOpenedWs(c echo.Context) error { - ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil) - if err != nil { - return err - } - defer func() { _ = ws.Close() }() - - // empty the channel - emptied := false - for !emptied { - select { - case <-openedChange: - default: - emptied = true - } - } - - for { - <-openedChange - mut.Lock() - - err = ws.WriteJSON(opened) - mut.Unlock() - - if err != nil { - log.Error(err) - break - } - } - - return err -} - func setOpened(c echo.Context) error { var data ChangeOpenReq err := c.Bind(&data) @@ -80,9 +44,6 @@ func setOpened(c echo.Context) error { go sendAlert(action) } - go func() { - openedChange <- 0 - }() mut.Unlock() return c.NoContent(http.StatusOK) } diff --git a/web/app.vue b/web/app.vue index 5339dc3..352cec4 100644 --- a/web/app.vue +++ b/web/app.vue @@ -7,3 +7,9 @@ + + diff --git a/web/composables/useAPI.ts b/web/composables/useAPI.ts index a832c22..c114c4e 100644 --- a/web/composables/useAPI.ts +++ b/web/composables/useAPI.ts @@ -1,5 +1,4 @@ -import type { FetchResponse } from "ofetch" -import { AxiosError, type AxiosResponse } from "axios" +import { type AxiosResponse } from "axios" export function useAPI(route: string = "/") { return `https://api.door.svitan.dev${route}` diff --git a/web/pages/index.vue b/web/pages/index.vue index 31cdaff..d713db1 100644 --- a/web/pages/index.vue +++ b/web/pages/index.vue @@ -4,11 +4,11 @@ >

- {{ open ? "Opened" : "Closed" }} + {{ opened ? "Opened" : "Closed" }}

@@ -55,11 +55,11 @@
- {{ alert ? "Turn off" : "Turn on" }} + {{ alerts ? "Turn off" : "Turn on" }}
@@ -90,9 +90,9 @@ import axios from "axios" const token = useToken() -const open = ref(true) +const opened = ref(true) const locked = ref(true) -const alert = ref(false) +const alerts = ref(false) const toast = useToast() @@ -120,7 +120,7 @@ async function toggleLock() { } async function toggleAlert() { - const desired = !alert.value + const desired = !alerts.value axios .post(useAPI("/alerts"), { alert: desired }, { headers: useHeaders() }) .then((res) => { @@ -128,7 +128,7 @@ async function toggleAlert() { toast.add({ title: `Alerts were turned ${desired ? "on" : "off"}`, }) - alert.value = desired + alerts.value = desired }) }) .catch(handleRequestError) @@ -139,26 +139,43 @@ onMounted(() => { return navigateTo("/token") } - axios - .get(useAPI("/locked"), { - headers: useHeaders(), - }) - .then((res) => { - handleResponse(res, (res) => { - locked.value = res.data + setInterval(() => { + axios + .get(useAPI("/opened"), { + headers: useHeaders(), }) - }) - .catch(handleRequestError) + .then((res) => { + handleResponse(res, (res) => { + opened.value = res.data + }) + }) + .catch(handleRequestError) + }, 1000) - axios - .get(useAPI("/alerts"), { - headers: useHeaders(), - }) - .then((res) => { - handleResponse(res, (res) => { - alert.value = res.data + setInterval(() => { + axios + .get(useAPI("/locked"), { + headers: useHeaders(), }) - }) - .catch(handleRequestError) + .then((res) => { + handleResponse(res, (res) => { + locked.value = res.data + }) + }) + .catch(handleRequestError) + }, 1000) + + setInterval(() => { + axios + .get(useAPI("/alerts"), { + headers: useHeaders(), + }) + .then((res) => { + handleResponse(res, (res) => { + alerts.value = res.data + }) + }) + .catch(handleRequestError) + }, 1000) })