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

132 lines
3.6 KiB
Go

package homeassistant
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
"strings"
"time"
"github.com/spf13/viper"
"github.com/t-liu93/home-automation-backend/util/ticktick"
)
type haMessage struct {
Target string `json:"target"`
Action string `json:"action"`
Content string `json:"content"`
}
type actionTask struct {
Action string `json:"action"`
DueHour int `json:"due_hour"`
}
type HaMessageHandler struct {
PooRecorderHandler func(message haMessage)
LocationRecorderHandler func(message haMessage)
TicktickHandler func(message haMessage)
}
var DefaultHandler = HaMessageHandler{
PooRecorderHandler: handlePooRecorderMsg,
LocationRecorderHandler: handleLocationRecorderMsg,
TicktickHandler: handleTicktickMsg,
}
func (handler HaMessageHandler) HandleHaMessage(w http.ResponseWriter, r *http.Request) {
var message haMessage
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
err := decoder.Decode(&message)
if err != nil {
slog.Warn(fmt.Sprintln("HandleHaMessage Error decoding request body", err))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
switch message.Target {
case "poo_recorder":
handler.PooRecorderHandler(message)
case "location_recorder":
handler.LocationRecorderHandler(message)
case "ticktick":
handler.TicktickHandler(message)
}
}
func handlePooRecorderMsg(message haMessage) {
switch message.Action {
case "get_latest":
handleGetLatestPoo()
}
}
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 handleTicktickMsg(message haMessage) {
switch message.Action {
case "create_action_task":
createActionTask(message)
}
}
func handleGetLatestPoo() {
client := &http.Client{
Timeout: time.Second * 1,
}
port := viper.GetString("port")
_, err := client.Get("http://localhost:" + port + "/poo/latest")
if err != nil {
slog.Warn(fmt.Sprintln("handleGetLatestPoo Error sending request to poo recorder", err))
}
}
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)
dueTicktick := dueNextMidnight.UTC().Format(ticktick.DateTimeLayout)
ticktickTask := ticktick.Task{
ProjectId: projectId,
Title: task.Action,
DueDate: dueTicktick,
}
err = ticktick.CreateTask(ticktickTask)
if err != nil {
slog.Warn(fmt.Sprintf("Homeassistant.createActionTask Error creating task %s", err))
}
}