✨ Adds fetching and toggling alerts
This commit is contained in:
119
server/main.go
119
server/main.go
@@ -24,12 +24,13 @@ var token string
|
||||
|
||||
// the condition is: distance >= threshold (is door open)
|
||||
var opened bool
|
||||
var openedChange = make(chan bool)
|
||||
|
||||
// is door locked
|
||||
var locked bool = false
|
||||
|
||||
// alert the user when door locked and but open?
|
||||
var alert bool = false
|
||||
// alerts the user when door locked and but open?
|
||||
var alerts bool = false
|
||||
var gotifyToken string
|
||||
var gotifyURL string
|
||||
|
||||
@@ -44,23 +45,23 @@ type ChangeLockReq struct {
|
||||
}
|
||||
|
||||
type ChangeAlertReq struct {
|
||||
Alert bool `json:"alert"`
|
||||
For int `json:"for"`
|
||||
Alerts bool `json:"alerts"`
|
||||
For int `json:"for"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
_ = godotenv.Load()
|
||||
token = os.Getenv("TOKEN")
|
||||
|
||||
alertRaw := strings.ToLower(os.Getenv("USE_ALERTS"))
|
||||
alert = alertRaw == "true" || alertRaw == "t" || alertRaw == "1" || alertRaw == "y" || alertRaw == "yes"
|
||||
alertsRaw := strings.ToLower(os.Getenv("USE_ALERTS"))
|
||||
alerts = alertsRaw == "true" || alertsRaw == "t" || alertsRaw == "1" || alertsRaw == "y" || alertsRaw == "yes"
|
||||
|
||||
gotifyToken = os.Getenv("GOTIFY_TOKEN")
|
||||
gotifyURL = os.Getenv("GOTIFY_URL")
|
||||
if alert && gotifyToken == "" {
|
||||
if alerts && gotifyToken == "" {
|
||||
log.Fatal("GOTIFY_TOKEN can't be empty when alerts are enabled")
|
||||
}
|
||||
if alert && gotifyURL == "" {
|
||||
if alerts && gotifyURL == "" {
|
||||
log.Fatal("GOTIFY_URL can't be empty when alerts are enabled")
|
||||
}
|
||||
|
||||
@@ -114,103 +115,17 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
app.GET("/", func(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, "Hello world!")
|
||||
})
|
||||
app.GET("/", helloWorld)
|
||||
|
||||
app.GET("/open", authed(func(c echo.Context) error {
|
||||
mut.Lock()
|
||||
o := opened
|
||||
mut.Unlock()
|
||||
app.GET("/opened", authed(getOpened))
|
||||
app.GET("/opened/ws", authed(getOpenedWs))
|
||||
app.POST("/opened", authed(setOpened))
|
||||
|
||||
return c.JSON(http.StatusOK, o)
|
||||
}))
|
||||
app.GET("/locked", authed(getLocked))
|
||||
app.POST("/locked", authed(setLocked))
|
||||
|
||||
app.POST("/open", authed(func(c echo.Context) error {
|
||||
var data ChangeOpenReq
|
||||
err := c.Bind(&data)
|
||||
if err != nil {
|
||||
return c.NoContent(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if data.Opened == opened {
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
mut.Lock()
|
||||
opened = data.Opened
|
||||
if locked && alert {
|
||||
var action string
|
||||
if opened {
|
||||
action = "opened"
|
||||
} else {
|
||||
action = "closed"
|
||||
}
|
||||
|
||||
go sendAlert(action)
|
||||
}
|
||||
|
||||
mut.Unlock()
|
||||
return c.NoContent(http.StatusOK)
|
||||
}))
|
||||
|
||||
app.GET("/lock", authed(func(c echo.Context) error {
|
||||
mut.Lock()
|
||||
l := locked
|
||||
mut.Unlock()
|
||||
|
||||
return c.JSON(http.StatusOK, l)
|
||||
}))
|
||||
|
||||
app.POST("/lock", authed(func(c echo.Context) error {
|
||||
var data ChangeLockReq
|
||||
err := c.Bind(&data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if data.Locked == locked {
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
mut.Lock()
|
||||
locked = data.Locked
|
||||
mut.Unlock()
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}))
|
||||
|
||||
app.GET("/alert", authed(func(c echo.Context) error {
|
||||
mut.Lock()
|
||||
a := alert
|
||||
mut.Unlock()
|
||||
|
||||
return c.JSON(http.StatusOK, a)
|
||||
}))
|
||||
|
||||
app.POST("/alert", authed(func(c echo.Context) error {
|
||||
var data ChangeAlertReq
|
||||
err := c.Bind(&data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if data.For > 0 {
|
||||
go func() {
|
||||
time.Sleep(time.Duration(data.For) * time.Second)
|
||||
|
||||
mut.Lock()
|
||||
alert = !data.Alert
|
||||
mut.Unlock()
|
||||
}()
|
||||
}
|
||||
|
||||
mut.Lock()
|
||||
alert = data.Alert
|
||||
mut.Unlock()
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}))
|
||||
app.GET("/alerts", authed(getAlerts))
|
||||
app.POST("/alerts", authed(setAlerts))
|
||||
|
||||
log.Fatal(app.Start(":1323"))
|
||||
}
|
||||
|
Reference in New Issue
Block a user