Compare commits

..

No commits in common. "fdd1bd94c197f1e9b7bc31217f9984ab2fcac1fd" and "7575193024627e6b012ad3505fb7e4fc29646979" have entirely different histories.

2 changed files with 6 additions and 20 deletions

11
main.go
View File

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

View File

@ -2,12 +2,10 @@
import { onMount } from "../../../node_modules/svelte/internal"; import { onMount } from "../../../node_modules/svelte/internal";
import icon from "$lib/images/sauna.svg" import icon from "$lib/images/sauna.svg"
let backend_url = "https://home.blackfinn.de/api/sauna"; let backend_url = "https://home.blackfinn.de/sauna/sample";
let temperature_value = 0.0 let temperature_value = 0.0
let temperature_unit = "°C" let temperature_unit = "°C"
let time = "00:00:00";
let updated = false;
function get_temperature() { function get_temperature() {
fetch(backend_url) fetch(backend_url)
@ -15,8 +13,6 @@
.then(data => { .then(data => {
temperature_unit = data.unit temperature_unit = data.unit
temperature_value = data.value temperature_value = data.value
time = data.time.slice(11, 19)
updated = JSON.parse(data.valid);
}).catch(error => { }).catch(error => {
console.log(error); console.log(error);
return []; return [];
@ -42,8 +38,5 @@
<figure> <figure>
<img src={icon} alt="Sauna" width=150/> <img src={icon} alt="Sauna" width=150/>
</figure> </figure>
{#if updated}
<div>{time} Uhr:</div>
<h1>{temperature_value} {temperature_unit}</h1> <h1>{temperature_value} {temperature_unit}</h1>
{/if}
</section> </section>