Sauna: Add update timestamp to sauna api

This commit is contained in:
Thomas Klaehn
2023-02-21 08:06:05 +01:00
parent 7575193024
commit 84d84b4313
2 changed files with 20 additions and 6 deletions

15
main.go
View File

@@ -6,13 +6,16 @@ import (
"log"
"net/http"
"sync"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
type temperature struct {
Value float64 `json:"value"`
Unit string `json:"unit"`
Value float64 `json:"value"`
Unit string `json:"unit"`
Time time.Time `json:"time"`
Valid bool `json:"valid"`
}
var (
@@ -33,11 +36,15 @@ var saunaHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message
log.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
sauna_mutex.Lock()
err := json.Unmarshal(msg.Payload(), &sauna_temperature)
sauna_mutex.Unlock()
sauna_temperature.Time = time.Now()
if err != nil {
sauna_temperature.Valid = false
sauna_mutex.Unlock()
logger.Print(err)
return
}
sauna_temperature.Valid = true
sauna_mutex.Unlock()
}
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
@@ -99,7 +106,7 @@ func main() {
// Serve files from static folder
http.Handle("/", http.FileServer(http.Dir(webui_path)))
http.HandleFunc("/sauna/sample", http_endpoint_sauna)
http.HandleFunc("/api/sauna", http_endpoint_sauna)
port := ":5000"
logger.Println("Server is running on port" + port)