🔨 Replaces websockets with interval fetches
This commit is contained in:
@@ -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))
|
||||||
|
@@ -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)
|
||||||
}
|
}
|
||||||
|
@@ -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>
|
||||||
|
@@ -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}`
|
||||||
|
@@ -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,6 +139,20 @@ onMounted(() => {
|
|||||||
return navigateTo("/token")
|
return navigateTo("/token")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
|
axios
|
||||||
|
.get<boolean>(useAPI("/opened"), {
|
||||||
|
headers: useHeaders(),
|
||||||
|
})
|
||||||
|
.then((res) => {
|
||||||
|
handleResponse(res, (res) => {
|
||||||
|
opened.value = res.data
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch(handleRequestError)
|
||||||
|
}, 1000)
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
axios
|
axios
|
||||||
.get<boolean>(useAPI("/locked"), {
|
.get<boolean>(useAPI("/locked"), {
|
||||||
headers: useHeaders(),
|
headers: useHeaders(),
|
||||||
@@ -149,16 +163,19 @@ onMounted(() => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
.catch(handleRequestError)
|
.catch(handleRequestError)
|
||||||
|
}, 1000)
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
axios
|
axios
|
||||||
.get<boolean>(useAPI("/alerts"), {
|
.get<boolean>(useAPI("/alerts"), {
|
||||||
headers: useHeaders(),
|
headers: useHeaders(),
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
handleResponse(res, (res) => {
|
handleResponse(res, (res) => {
|
||||||
alert.value = res.data
|
alerts.value = res.data
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.catch(handleRequestError)
|
.catch(handleRequestError)
|
||||||
|
}, 1000)
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
Reference in New Issue
Block a user