Files
home-automation-backend/src/components/homeassistant/homeassistant.go

153 lines
4.5 KiB
Go
Raw Normal View History

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