wip
This commit is contained in:
@@ -18,60 +18,68 @@ type haMessage struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type HomeAssistant struct {
|
||||
ticktickUtil ticktickutil.TicktickUtil
|
||||
}
|
||||
|
||||
type actionTask struct {
|
||||
Action string `json:"action"`
|
||||
DueHour int `json:"due_hour"`
|
||||
}
|
||||
|
||||
var (
|
||||
ticktickUtil ticktickutil.TicktickUtil
|
||||
)
|
||||
|
||||
func Init(ticktick ticktickutil.TicktickUtil) {
|
||||
ticktickUtil = ticktick
|
||||
func NewHomeAssistant(ticktick ticktickutil.TicktickUtil) *HomeAssistant {
|
||||
return &HomeAssistant{
|
||||
ticktickUtil: ticktick,
|
||||
}
|
||||
}
|
||||
|
||||
func HandleHaMessage(w http.ResponseWriter, r *http.Request) {
|
||||
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 {
|
||||
slog.Warn(fmt.Sprintln("HandleHaMessage: Error decoding request body", err))
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error decoding request body", err))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
switch message.Target {
|
||||
case "poo_recorder":
|
||||
res := handlePooRecorderMsg(message)
|
||||
res := ha.handlePooRecorderMsg(message)
|
||||
if !res {
|
||||
http.Error(w, "Error handling poo recorder message", http.StatusInternalServerError)
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error handling poo recorder message"))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
}
|
||||
case "location_recorder":
|
||||
res := handleLocationRecorderMsg(message)
|
||||
res := ha.handleLocationRecorderMsg(message)
|
||||
if !res {
|
||||
http.Error(w, "Error handling location recorder message", http.StatusInternalServerError)
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error handling location recorder message"))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
}
|
||||
case "ticktick":
|
||||
handleTicktickMsg(message)
|
||||
res := ha.handleTicktickMsg(message)
|
||||
if !res {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error handling ticktick message"))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
}
|
||||
default:
|
||||
slog.Warn(fmt.Sprintln("HandleHaMessage Unknown target", message.Target))
|
||||
http.Error(w, "Unknown target", http.StatusBadRequest)
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Unknown target", message.Target))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func handlePooRecorderMsg(message haMessage) bool {
|
||||
func (ha *HomeAssistant) handlePooRecorderMsg(message haMessage) bool {
|
||||
switch message.Action {
|
||||
case "get_latest":
|
||||
return handleGetLatestPoo()
|
||||
return ha.handleGetLatestPoo()
|
||||
default:
|
||||
slog.Warn(fmt.Sprintln("handlePooRecorderMsg: Unknown action", message.Action))
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handlePooRecorderMsg: Unknown action", message.Action))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func handleLocationRecorderMsg(message haMessage) bool {
|
||||
func (ha *HomeAssistant) handleLocationRecorderMsg(message haMessage) bool {
|
||||
if message.Action == "record" {
|
||||
port := viper.GetString("port")
|
||||
client := &http.Client{
|
||||
@@ -79,49 +87,52 @@ func handleLocationRecorderMsg(message haMessage) bool {
|
||||
}
|
||||
_, err := client.Post("http://localhost:"+port+"/location/record", "application/json", strings.NewReader(strings.ReplaceAll(message.Content, "'", "\"")))
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg: Error sending request to location recorder", err))
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handleLocationRecorderMsg: Error sending request to location recorder", err))
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg: Unknown action", message.Action))
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handleLocationRecorderMsg: Unknown action", message.Action))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func handleTicktickMsg(message haMessage) {
|
||||
func (ha *HomeAssistant) handleTicktickMsg(message haMessage) bool {
|
||||
switch message.Action {
|
||||
case "create_action_task":
|
||||
createActionTask(message)
|
||||
return ha.createActionTask(message)
|
||||
default:
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handleTicktickMsg: Unknown action", message.Action))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetLatestPoo() bool {
|
||||
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 {
|
||||
slog.Warn(fmt.Sprintln("handleGetLatestPoo: Error sending request to poo recorder", err))
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handleGetLatestPoo: Error sending request to poo recorder", err))
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func createActionTask(message haMessage) {
|
||||
func (ha *HomeAssistant) createActionTask(message haMessage) bool {
|
||||
if !viper.InConfig("homeassistant.actionTaskProjectId") {
|
||||
slog.Warn("Homeassistant.createActionTask actionTaskProjectId not found in config file")
|
||||
return
|
||||
slog.Warn("homeassistant.createActionTask: actionTaskProjectId not found in config file")
|
||||
return false
|
||||
}
|
||||
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
|
||||
slog.Warn(fmt.Sprintln("homeassistant.createActionTask: Error unmarshalling", err))
|
||||
return false
|
||||
}
|
||||
dueHour := task.DueHour
|
||||
due := time.Now().Add(time.Hour * time.Duration(dueHour))
|
||||
@@ -132,8 +143,10 @@ func createActionTask(message haMessage) {
|
||||
Title: task.Action,
|
||||
DueDate: dueTicktick,
|
||||
}
|
||||
err = ticktickUtil.CreateTask(ticktickTask)
|
||||
err = ha.ticktickUtil.CreateTask(ticktickTask)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintf("Homeassistant.createActionTask Error creating task %s", err))
|
||||
slog.Warn(fmt.Sprintf("homeassistant.createActionTask: Error creating task %s", err))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user