🔨 Replaces websockets with interval fetches
All checks were successful
Gitea Build Action / build-go (push) Successful in 36s
Gitea Build Action / build-nuxt (push) Successful in 10m7s

This commit is contained in:
2025-06-16 11:56:20 +02:00
parent 4b2248a6d7
commit d858b9e7c8
5 changed files with 55 additions and 74 deletions

View File

@@ -24,7 +24,6 @@ var token string
// the condition is: distance >= threshold (is door open) // the condition is: distance >= threshold (is door open)
var opened = false var opened = false
var openedChange = make(chan any)
// is door locked // is door locked
var locked = false var locked = false
@@ -118,7 +117,6 @@ func main() {
app.GET("/", helloWorld) app.GET("/", helloWorld)
app.GET("/opened", authed(getOpened)) app.GET("/opened", authed(getOpened))
app.GET("/opened/ws", authed(getOpenedWs))
app.POST("/opened", authed(setOpened)) app.POST("/opened", authed(setOpened))
app.GET("/locked", authed(getLocked)) app.GET("/locked", authed(getLocked))

View File

@@ -4,13 +4,10 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/gommon/log" "github.com/labstack/gommon/log"
) )
var upgrader = websocket.Upgrader{}
func helloWorld(c echo.Context) error { func helloWorld(c echo.Context) error {
return c.JSON(http.StatusOK, "Hello world!") return c.JSON(http.StatusOK, "Hello world!")
} }
@@ -23,39 +20,6 @@ func getOpened(c echo.Context) error {
return c.JSON(http.StatusOK, o) 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 { func setOpened(c echo.Context) error {
var data ChangeOpenReq var data ChangeOpenReq
err := c.Bind(&data) err := c.Bind(&data)
@@ -80,9 +44,6 @@ func setOpened(c echo.Context) error {
go sendAlert(action) go sendAlert(action)
} }
go func() {
openedChange <- 0
}()
mut.Unlock() mut.Unlock()
return c.NoContent(http.StatusOK) return c.NoContent(http.StatusOK)
} }

View File

@@ -7,3 +7,9 @@
<style> <style>
@import "assets/css/main.css"; @import "assets/css/main.css";
</style> </style>
<script lang="ts" setup>
useHead({
title: "Door alarm",
})
</script>

View File

@@ -1,5 +1,4 @@
import type { FetchResponse } from "ofetch" import { type AxiosResponse } from "axios"
import { AxiosError, type AxiosResponse } from "axios"
export function useAPI(route: string = "/") { export function useAPI(route: string = "/") {
return `https://api.door.svitan.dev${route}` return `https://api.door.svitan.dev${route}`

View File

@@ -4,11 +4,11 @@
> >
<div class="flex items-center justify-center flex-col gap-y-2 w-32"> <div class="flex items-center justify-center flex-col gap-y-2 w-32">
<div <div
:class="`flex items-center justify-center border-solid border-2 border-${open ? (locked ? 'error' : 'secondary') : 'primary'} rounded-lg w-full h-32`" :class="`flex items-center justify-center border-solid border-2 border-${opened ? (locked ? 'error' : 'secondary') : 'primary'} rounded-lg w-full h-32`"
> >
<UIcon <UIcon
:name=" :name="
open opened
? 'material-symbols:door-open' ? 'material-symbols:door-open'
: 'material-symbols:door-front' : 'material-symbols:door-front'
" "
@@ -17,9 +17,9 @@
</div> </div>
<p <p
:class="`flex items-center justify-center text-${open ? (locked ? 'error' : 'secondary') : 'primary'} text-xl w-full h-10`" :class="`flex items-center justify-center text-${opened ? (locked ? 'error' : 'secondary') : 'primary'} text-xl w-full h-10`"
> >
{{ open ? "Opened" : "Closed" }} {{ opened ? "Opened" : "Closed" }}
</p> </p>
</div> </div>
@@ -55,11 +55,11 @@
<div class="flex items-center justify-center flex-col gap-y-2 w-32"> <div class="flex items-center justify-center flex-col gap-y-2 w-32">
<div <div
:class="`flex items-center justify-center border-solid border-2 border-${alert ? 'primary' : 'secondary'} rounded-lg w-full h-32`" :class="`flex items-center justify-center border-solid border-2 border-${alerts ? 'primary' : 'secondary'} rounded-lg w-full h-32`"
> >
<UIcon <UIcon
:name=" :name="
alert alerts
? 'material-symbols:volume-up' ? 'material-symbols:volume-up'
: 'material-symbols:volume-off' : 'material-symbols:volume-off'
" "
@@ -69,17 +69,17 @@
<UButton <UButton
:icon=" :icon="
alert alerts
? 'material-symbols:volume-off' ? 'material-symbols:volume-off'
: 'material-symbols:volume-up' : 'material-symbols:volume-up'
" "
size="xl" size="xl"
:color="alert ? 'primary' : 'secondary'" :color="alerts ? 'primary' : 'secondary'"
variant="solid" variant="solid"
class="flex items-center justify-center w-full h-10" class="flex items-center justify-center w-full h-10"
@click="toggleAlert" @click="toggleAlert"
> >
{{ alert ? "Turn off" : "Turn on" }} {{ alerts ? "Turn off" : "Turn on" }}
</UButton> </UButton>
</div> </div>
</main> </main>
@@ -90,9 +90,9 @@ import axios from "axios"
const token = useToken() const token = useToken()
const open = ref(true) const opened = ref(true)
const locked = ref(true) const locked = ref(true)
const alert = ref(false) const alerts = ref(false)
const toast = useToast() const toast = useToast()
@@ -120,7 +120,7 @@ async function toggleLock() {
} }
async function toggleAlert() { async function toggleAlert() {
const desired = !alert.value const desired = !alerts.value
axios axios
.post(useAPI("/alerts"), { alert: desired }, { headers: useHeaders() }) .post(useAPI("/alerts"), { alert: desired }, { headers: useHeaders() })
.then((res) => { .then((res) => {
@@ -128,7 +128,7 @@ async function toggleAlert() {
toast.add({ toast.add({
title: `Alerts were turned ${desired ? "on" : "off"}`, title: `Alerts were turned ${desired ? "on" : "off"}`,
}) })
alert.value = desired alerts.value = desired
}) })
}) })
.catch(handleRequestError) .catch(handleRequestError)
@@ -139,26 +139,43 @@ onMounted(() => {
return navigateTo("/token") return navigateTo("/token")
} }
axios setInterval(() => {
.get<boolean>(useAPI("/locked"), { axios
headers: useHeaders(), .get<boolean>(useAPI("/opened"), {
}) headers: useHeaders(),
.then((res) => {
handleResponse(res, (res) => {
locked.value = res.data
}) })
}) .then((res) => {
.catch(handleRequestError) handleResponse(res, (res) => {
opened.value = res.data
})
})
.catch(handleRequestError)
}, 1000)
axios setInterval(() => {
.get<boolean>(useAPI("/alerts"), { axios
headers: useHeaders(), .get<boolean>(useAPI("/locked"), {
}) headers: useHeaders(),
.then((res) => {
handleResponse(res, (res) => {
alert.value = res.data
}) })
}) .then((res) => {
.catch(handleRequestError) handleResponse(res, (res) => {
locked.value = res.data
})
})
.catch(handleRequestError)
}, 1000)
setInterval(() => {
axios
.get<boolean>(useAPI("/alerts"), {
headers: useHeaders(),
})
.then((res) => {
handleResponse(res, (res) => {
alerts.value = res.data
})
})
.catch(handleRequestError)
}, 1000)
}) })
</script> </script>