2024-09-17 16:28:12 +02:00
|
|
|
package homeassistant
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"net/http"
|
2024-09-18 16:41:26 +02:00
|
|
|
"strings"
|
2024-09-17 16:28:12 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2024-10-22 16:40:19 +02:00
|
|
|
"github.com/t-liu93/home-automation-backend/util/ticktickutil"
|
2024-09-17 16:28:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type haMessage struct {
|
|
|
|
|
Target string `json:"target"`
|
|
|
|
|
Action string `json:"action"`
|
|
|
|
|
Content string `json:"content"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 15:10:33 +02:00
|
|
|
type actionTask struct {
|
|
|
|
|
Action string `json:"action"`
|
|
|
|
|
DueHour int `json:"due_hour"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 16:40:19 +02:00
|
|
|
var (
|
|
|
|
|
ticktickUtil ticktickutil.TicktickUtil
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Init(ticktick ticktickutil.TicktickUtil) {
|
|
|
|
|
ticktickUtil = ticktick
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 14:09:51 +02:00
|
|
|
func HandleHaMessage(w http.ResponseWriter, r *http.Request) {
|
2024-09-17 16:28:12 +02:00
|
|
|
var message haMessage
|
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
|
decoder.DisallowUnknownFields()
|
|
|
|
|
err := decoder.Decode(&message)
|
|
|
|
|
if err != nil {
|
2024-10-23 13:53:29 +02:00
|
|
|
slog.Warn(fmt.Sprintln("HandleHaMessage: Error decoding request body", err))
|
2024-09-17 16:28:12 +02:00
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch message.Target {
|
|
|
|
|
case "poo_recorder":
|
2024-10-23 13:53:29 +02:00
|
|
|
res := handlePooRecorderMsg(message)
|
|
|
|
|
if !res {
|
|
|
|
|
http.Error(w, "Error handling poo recorder message", http.StatusInternalServerError)
|
|
|
|
|
}
|
2024-09-18 16:41:26 +02:00
|
|
|
case "location_recorder":
|
2024-10-23 13:53:29 +02:00
|
|
|
res := handleLocationRecorderMsg(message)
|
|
|
|
|
if !res {
|
|
|
|
|
http.Error(w, "Error handling location recorder message", http.StatusInternalServerError)
|
|
|
|
|
}
|
2024-09-19 15:10:33 +02:00
|
|
|
case "ticktick":
|
2024-10-22 14:09:51 +02:00
|
|
|
handleTicktickMsg(message)
|
|
|
|
|
default:
|
|
|
|
|
slog.Warn(fmt.Sprintln("HandleHaMessage Unknown target", message.Target))
|
|
|
|
|
http.Error(w, "Unknown target", http.StatusBadRequest)
|
2024-09-17 16:28:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 13:53:29 +02:00
|
|
|
func handlePooRecorderMsg(message haMessage) bool {
|
2024-09-17 16:28:12 +02:00
|
|
|
switch message.Action {
|
|
|
|
|
case "get_latest":
|
2024-10-23 13:53:29 +02:00
|
|
|
return handleGetLatestPoo()
|
|
|
|
|
default:
|
|
|
|
|
slog.Warn(fmt.Sprintln("handlePooRecorderMsg: Unknown action", message.Action))
|
|
|
|
|
return false
|
2024-09-17 16:28:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 13:53:29 +02:00
|
|
|
func handleLocationRecorderMsg(message haMessage) bool {
|
2024-09-18 16:41:26 +02:00
|
|
|
if message.Action == "record" {
|
|
|
|
|
port := viper.GetString("port")
|
|
|
|
|
client := &http.Client{
|
|
|
|
|
Timeout: time.Second * 1,
|
|
|
|
|
}
|
2024-10-23 13:53:29 +02:00
|
|
|
_, err := client.Post("http://localhost:"+port+"/location/record", "application/json", strings.NewReader(strings.ReplaceAll(message.Content, "'", "\"")))
|
2024-09-18 16:41:26 +02:00
|
|
|
if err != nil {
|
2024-10-23 13:53:29 +02:00
|
|
|
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg: Error sending request to location recorder", err))
|
|
|
|
|
return false
|
2024-09-18 16:41:26 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-10-23 13:53:29 +02:00
|
|
|
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg: Unknown action", message.Action))
|
|
|
|
|
return false
|
2024-09-18 16:41:26 +02:00
|
|
|
}
|
2024-10-23 13:53:29 +02:00
|
|
|
return true
|
2024-09-18 16:41:26 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-19 15:10:33 +02:00
|
|
|
func handleTicktickMsg(message haMessage) {
|
|
|
|
|
switch message.Action {
|
|
|
|
|
case "create_action_task":
|
|
|
|
|
createActionTask(message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 14:09:51 +02:00
|
|
|
func handleGetLatestPoo() bool {
|
2024-09-17 16:28:12 +02:00
|
|
|
client := &http.Client{
|
|
|
|
|
Timeout: time.Second * 1,
|
|
|
|
|
}
|
|
|
|
|
port := viper.GetString("port")
|
|
|
|
|
_, err := client.Get("http://localhost:" + port + "/poo/latest")
|
|
|
|
|
if err != nil {
|
2024-10-23 13:53:29 +02:00
|
|
|
slog.Warn(fmt.Sprintln("handleGetLatestPoo: Error sending request to poo recorder", err))
|
2024-10-22 14:09:51 +02:00
|
|
|
return false
|
2024-09-17 16:28:12 +02:00
|
|
|
}
|
2024-10-22 14:09:51 +02:00
|
|
|
|
|
|
|
|
return true
|
2024-09-17 16:28:12 +02:00
|
|
|
}
|
2024-09-19 15:10:33 +02:00
|
|
|
|
|
|
|
|
func createActionTask(message haMessage) {
|
|
|
|
|
if !viper.InConfig("homeassistant.actionTaskProjectId") {
|
|
|
|
|
slog.Warn("Homeassistant.createActionTask actionTaskProjectId not found in config file")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
projectId := viper.GetString("homeassistant.actionTaskProjectId")
|
|
|
|
|
detail := strings.ReplaceAll(message.Content, "'", "\"")
|
|
|
|
|
var task actionTask
|
|
|
|
|
err := json.Unmarshal([]byte(detail), &task)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Warn(fmt.Sprintln("Homeassistant.createActionTask Error unmarshalling", err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
dueHour := task.DueHour
|
|
|
|
|
due := time.Now().Add(time.Hour * time.Duration(dueHour))
|
|
|
|
|
dueNextMidnight := time.Date(due.Year(), due.Month(), due.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, 1)
|
2024-10-22 16:40:19 +02:00
|
|
|
dueTicktick := dueNextMidnight.UTC().Format(ticktickutil.DateTimeLayout)
|
|
|
|
|
ticktickTask := ticktickutil.Task{
|
2024-09-19 15:10:33 +02:00
|
|
|
ProjectId: projectId,
|
|
|
|
|
Title: task.Action,
|
|
|
|
|
DueDate: dueTicktick,
|
|
|
|
|
}
|
2024-10-22 16:40:19 +02:00
|
|
|
err = ticktickUtil.CreateTask(ticktickTask)
|
2024-09-19 15:10:33 +02:00
|
|
|
if err != nil {
|
|
|
|
|
slog.Warn(fmt.Sprintf("Homeassistant.createActionTask Error creating task %s", err))
|
|
|
|
|
}
|
|
|
|
|
}
|