Ported location recorder

This commit is contained in:
2024-09-18 16:41:26 +02:00
parent 197b9a3d63
commit d8d6c8bb35
4 changed files with 249 additions and 17 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"net/http"
"strings"
"time"
"github.com/spf13/viper"
@@ -30,6 +31,8 @@ func HandleHaMessage(w http.ResponseWriter, r *http.Request) {
switch message.Target {
case "poo_recorder":
handlePooRecorderMsg(message)
case "location_recorder":
handleLocationRecorderMsg(message)
}
}
@@ -42,6 +45,27 @@ func handlePooRecorderMsg(message haMessage) {
}
func handleLocationRecorderMsg(message haMessage) {
if message.Action == "record" {
port := viper.GetString("port")
req, err := http.NewRequest("POST", "http://localhost:"+port+"/location/record", strings.NewReader(strings.ReplaceAll(message.Content, "'", "\"")))
if err != nil {
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg Error creating request to location recorder", err))
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: time.Second * 1,
}
_, err = client.Do(req)
if err != nil {
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg Error sending request to location recorder", err))
}
} else {
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg Unknown action", message.Action))
}
}
func handleGetLatestPoo() {
client := &http.Client{
Timeout: time.Second * 1,