Bootstrap Python rewrite skeleton
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
package homeassistant
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"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"`
|
||||
}
|
||||
|
||||
type HomeAssistant struct {
|
||||
ticktickUtil ticktickutil.TicktickUtil
|
||||
}
|
||||
|
||||
type actionTask struct {
|
||||
Action string `json:"action"`
|
||||
DueHour int `json:"due_hour"`
|
||||
}
|
||||
|
||||
func NewHomeAssistant(ticktick ticktickutil.TicktickUtil) *HomeAssistant {
|
||||
return &HomeAssistant{
|
||||
ticktickUtil: ticktick,
|
||||
}
|
||||
}
|
||||
|
||||
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("homeassistant.HandleHaMessage: Error decoding request body", err))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
switch message.Target {
|
||||
case "poo_recorder":
|
||||
res := ha.handlePooRecorderMsg(message)
|
||||
if !res {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error handling poo recorder message"))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
}
|
||||
case "location_recorder":
|
||||
res := ha.handleLocationRecorderMsg(message)
|
||||
if !res {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error handling location recorder message"))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
}
|
||||
case "ticktick":
|
||||
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("homeassistant.HandleHaMessage: Unknown target", message.Target))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (ha *HomeAssistant) handlePooRecorderMsg(message haMessage) bool {
|
||||
switch message.Action {
|
||||
case "get_latest":
|
||||
return ha.handleGetLatestPoo()
|
||||
default:
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handlePooRecorderMsg: Unknown action", message.Action))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (ha *HomeAssistant) handleLocationRecorderMsg(message haMessage) bool {
|
||||
if message.Action == "record" {
|
||||
port := viper.GetString("port")
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * 1,
|
||||
}
|
||||
_, err := client.Post("http://localhost:"+port+"/location/record", "application/json", strings.NewReader(strings.ReplaceAll(message.Content, "'", "\"")))
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handleLocationRecorderMsg: Error sending request to location recorder", err))
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handleLocationRecorderMsg: Unknown action", message.Action))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (ha *HomeAssistant) handleTicktickMsg(message haMessage) bool {
|
||||
switch message.Action {
|
||||
case "create_action_task":
|
||||
return ha.createActionTask(message)
|
||||
default:
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handleTicktickMsg: Unknown action", message.Action))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
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("homeassistant.handleGetLatestPoo: Error sending request to poo recorder", err))
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (ha *HomeAssistant) createActionTask(message haMessage) bool {
|
||||
if !viper.IsSet("homeassistant.actionTaskProjectId") {
|
||||
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 false
|
||||
}
|
||||
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{
|
||||
ProjectId: projectId,
|
||||
Title: task.Action,
|
||||
DueDate: dueTicktick,
|
||||
}
|
||||
err = ha.ticktickUtil.CreateTask(ticktickTask)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintf("homeassistant.createActionTask: Error creating task %s", err))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
package homeassistant
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/t-liu93/home-automation-backend/util/ticktickutil"
|
||||
)
|
||||
|
||||
var (
|
||||
loggerText = new(bytes.Buffer)
|
||||
)
|
||||
|
||||
type MockTicktickUtil struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockTicktickUtil) HandleAuthCode(w http.ResponseWriter, r *http.Request) {
|
||||
m.Called(w, r)
|
||||
}
|
||||
|
||||
func (m *MockTicktickUtil) GetTasks(projectId string) []ticktickutil.Task {
|
||||
args := m.Called(projectId)
|
||||
return args.Get(0).([]ticktickutil.Task)
|
||||
}
|
||||
|
||||
func (m *MockTicktickUtil) HasDuplicateTask(projectId string, taskTitile string) bool {
|
||||
args := m.Called(projectId, taskTitile)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (m *MockTicktickUtil) CreateTask(task ticktickutil.Task) error {
|
||||
args := m.Called(task)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func SetupTearDown(t *testing.T) (func(), *HomeAssistant) {
|
||||
loggertearDown := loggerSetupTeardown()
|
||||
mockTicktick := &MockTicktickUtil{}
|
||||
ha := NewHomeAssistant(mockTicktick)
|
||||
|
||||
return func() {
|
||||
loggertearDown()
|
||||
viper.Reset()
|
||||
}, ha
|
||||
}
|
||||
|
||||
func loggerSetupTeardown() func() {
|
||||
logger := slog.New(slog.NewTextHandler(loggerText, nil))
|
||||
defaultLogger := slog.Default()
|
||||
slog.SetDefault(logger)
|
||||
|
||||
return func() {
|
||||
slog.SetDefault(defaultLogger)
|
||||
loggerText.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleHaMessageJsonDecodeError(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
invalidRequestBody := ` { "target": "poo_recorder", "action": "get_latest", "content": " }`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(invalidRequestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.HandleHaMessage: Error decoding request body")
|
||||
}
|
||||
|
||||
func TestHandlePooRecorderMsgGetLatest(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
defer teardown()
|
||||
requestBody := `{"target": "poo_recorder", "action": "get_latest", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodGet, r.Method)
|
||||
assert.Equal(t, "/poo/latest", r.URL.Path)
|
||||
}))
|
||||
defer server.Close()
|
||||
port := strings.Split(server.URL, ":")[2]
|
||||
viper.Set("port", port)
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Empty(t, loggerText.String())
|
||||
}
|
||||
|
||||
func TestHandlePooRecorderMsgUnknownAction(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "poo_recorder", "action": "unknown_action", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.handlePooRecorderMsg: Unknown action")
|
||||
}
|
||||
|
||||
func TestHandlePooRecorderMsgGetLatestError(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "poo_recorder", "action": "get_latest", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
port := "invalid port"
|
||||
viper.Set("port", port)
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.handleGetLatestPoo: Error sending request to poo recorder")
|
||||
}
|
||||
|
||||
func TestHandleLocationRecorderMsg(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "location_recorder", "action": "record", "content": "{'person': 'test', 'latitude': '1.0', 'longitude': '2.0', 'altitude': '3.0'}"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodPost, r.Method)
|
||||
assert.Equal(t, "/location/record", r.URL.Path)
|
||||
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
port := strings.Split(server.URL, ":")[2]
|
||||
viper.Set("port", port)
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Empty(t, loggerText.String())
|
||||
}
|
||||
|
||||
func TestHandleLocationRecorderMsgUnknownAction(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "location_recorder", "action": "unknown_action", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.handleLocationRecorderMsg: Unknown action")
|
||||
}
|
||||
|
||||
func TestHandleLocationRecorderMsgRequestErr(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "location_recorder", "action": "record", "content": "{'person': 'test', 'latitude': '1.0', 'longitude': '2.0', 'altitude': '3.0'}"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
port := "invalid port"
|
||||
viper.Set("port", port)
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.handleLocationRecorderMsg: Error sending request to location recorder")
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgCreateActionTask(t *testing.T) {
|
||||
teardown, _ := SetupTearDown(t)
|
||||
defer teardown()
|
||||
const expectedProjectId = "test_project_id"
|
||||
const dueHour = 12
|
||||
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)
|
||||
|
||||
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
mockTicktick := &MockTicktickUtil{}
|
||||
mockTicktick.On("CreateTask", mock.Anything).Return(nil)
|
||||
ha := NewHomeAssistant(mockTicktick)
|
||||
viper.Set("homeassistant.actionTaskProjectId", expectedProjectId)
|
||||
ha.HandleHaMessage(w, req)
|
||||
expectedTask := ticktickutil.Task{
|
||||
Title: "test_action",
|
||||
DueDate: dueTicktick,
|
||||
ProjectId: expectedProjectId,
|
||||
}
|
||||
mockTicktick.AssertCalled(t, "CreateTask", expectedTask)
|
||||
mockTicktick.AssertNumberOfCalls(t, "CreateTask", 1)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Empty(t, loggerText.String())
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgUnknownAction(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "ticktick", "action": "unknown_action", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.handleTicktickMsg: Unknown action")
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgProjectIdUnset(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.createActionTask: actionTaskProjectId not found in config file")
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgJsonError(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
invalidRequestBody := ` { "target": "ticktick", "action": "create_action_task", "content": "{'title': 'tes, 'action': 'test_action', 'due_hour': 12}"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(invalidRequestBody))
|
||||
w := httptest.NewRecorder()
|
||||
viper.Set("homeassistant.actionTaskProjectId", "some project id")
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.createActionTask: Error unmarshalling")
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgTicktickUtilErr(t *testing.T) {
|
||||
teardown, _ := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
mockedTicktickUtil := &MockTicktickUtil{}
|
||||
viper.Set("homeassistant.actionTaskProjectId", "some project id")
|
||||
|
||||
mockedTicktickUtil.On("CreateTask", mock.Anything).Return(errors.New("some error"))
|
||||
|
||||
ha := NewHomeAssistant(mockedTicktickUtil)
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
|
||||
mockedTicktickUtil.AssertCalled(t, "CreateTask", mock.Anything)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.createActionTask: Error creating task")
|
||||
}
|
||||
|
||||
func TestHandleHaMessageUnknownTarget(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "unknown_target", "action": "record", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.HandleHaMessage: Unknown target")
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package locationRecorder
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
db *sql.DB
|
||||
)
|
||||
|
||||
const (
|
||||
currentDBVersion = 2
|
||||
)
|
||||
|
||||
type Location struct {
|
||||
Person string `json:"person"`
|
||||
DateTime string `json:"datetime"`
|
||||
Latitude float64 `json:"latitude"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
Altitude sql.NullFloat64 `json:"altitude,omitempty"`
|
||||
}
|
||||
|
||||
type LocationContent struct {
|
||||
Person string `json:"person"`
|
||||
Latitude string `json:"latitude"`
|
||||
Longitude string `json:"longitude"`
|
||||
Altitude string `json:"altitude,omitempty"`
|
||||
}
|
||||
|
||||
func Init() {
|
||||
initDb()
|
||||
}
|
||||
|
||||
func HandleRecordLocation(w http.ResponseWriter, r *http.Request) {
|
||||
var location LocationContent
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
err := decoder.Decode(&location)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("HandleRecordLocation Error decoding request body", err))
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
latiF64, _ := strconv.ParseFloat(location.Latitude, 64)
|
||||
longiF64, _ := strconv.ParseFloat(location.Longitude, 64)
|
||||
altiF64, _ := strconv.ParseFloat(location.Altitude, 64)
|
||||
InsertLocationNow(location.Person, latiF64, longiF64, altiF64)
|
||||
}
|
||||
|
||||
func InsertLocation(person string, datetime time.Time, latitude float64, longitude float64, altitude float64) {
|
||||
_, err := db.Exec(`INSERT OR IGNORE INTO location (person, datetime, latitude, longitude, altitude) VALUES (?, ?, ?, ?, ?)`,
|
||||
person, datetime.UTC().Format(time.RFC3339), latitude, longitude, altitude)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("LocationRecorder.InsertLocation Error inserting location", err))
|
||||
}
|
||||
}
|
||||
|
||||
func InsertLocationNow(person string, latitude float64, longitude float64, altitude float64) {
|
||||
InsertLocation(person, time.Now(), latitude, longitude, altitude)
|
||||
}
|
||||
|
||||
func initDb() {
|
||||
if !viper.InConfig("locationRecorder.dbPath") {
|
||||
slog.Info("LocationRecorderInit dbPath not found in config file, using default: location_recorder.db")
|
||||
viper.SetDefault("locationRecorder.dbPath", "location_recorder.db")
|
||||
}
|
||||
|
||||
dbPath := viper.GetString("locationRecorder.dbPath")
|
||||
err := error(nil)
|
||||
db, err = sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("LocationRecorderInit Error opening database", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("LocationRecorderInit Error pinging database", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
migrateDb()
|
||||
}
|
||||
|
||||
func migrateDb() {
|
||||
var userVersion int
|
||||
err := db.QueryRow("PRAGMA user_version").Scan(&userVersion)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("LocationRecorderInit Error getting db user version", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
if userVersion == 0 {
|
||||
migrateDb0To1(&userVersion)
|
||||
}
|
||||
if userVersion == 1 {
|
||||
migrateDb1To2(&userVersion)
|
||||
}
|
||||
if userVersion != currentDBVersion {
|
||||
slog.Error(fmt.Sprintln("LocationRecorderInit Error unsupported database version", userVersion))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func migrateDb0To1(userVersion *int) {
|
||||
// this is actually create new db
|
||||
slog.Info("Creating location recorder database version 1..")
|
||||
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS location (
|
||||
person TEXT NOT NULL,
|
||||
datetime TEXT NOT NULL,
|
||||
latitude REAL NOT NULL,
|
||||
longitude REAL NOT NULL,
|
||||
altitude REAL,
|
||||
PRIMARY KEY (person, datetime))`)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("LocationRecorderInit DB0To1 Error creating table", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = db.Exec(`PRAGMA user_version = 1`)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("LocationRecorderInit DB0To1 Error setting user version to 1", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
*userVersion = 1
|
||||
}
|
||||
|
||||
func migrateDb1To2(userVersion *int) {
|
||||
// this will change the datetime format into Real RFC3339
|
||||
slog.Info("Migrating location recorder database version 1 to 2..")
|
||||
dbTx, err := db.Begin()
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("LocationRecorderInit DB1To2 Error beginning transaction", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
fail := func(err error, step string) {
|
||||
slog.Error(fmt.Sprintf("LocationRecorderInit DB1To2 Error %s: %s", step, err))
|
||||
dbTx.Rollback()
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = dbTx.Exec(`ALTER TABLE location RENAME TO location_old`)
|
||||
if err != nil {
|
||||
fail(err, "renaming table")
|
||||
}
|
||||
_, err = dbTx.Exec(`CREATE TABLE IF NOT EXISTS location (
|
||||
person TEXT NOT NULL,
|
||||
datetime TEXT NOT NULL,
|
||||
latitude REAL NOT NULL,
|
||||
longitude REAL NOT NULL,
|
||||
altitude REAL,
|
||||
PRIMARY KEY (person, datetime))`)
|
||||
if err != nil {
|
||||
fail(err, "creating new table")
|
||||
}
|
||||
row, err := dbTx.Query(`SELECT person, datetime, latitude, longitude, altitude FROM location_old`)
|
||||
if err != nil {
|
||||
fail(err, "selecting from old table")
|
||||
}
|
||||
defer row.Close()
|
||||
for row.Next() {
|
||||
var location Location
|
||||
err = row.Scan(&location.Person, &location.DateTime, &location.Latitude, &location.Longitude, &location.Altitude)
|
||||
if err != nil {
|
||||
fail(err, "scanning row")
|
||||
}
|
||||
dateTime, err := time.Parse("2006-01-02T15:04:05-0700", location.DateTime)
|
||||
if err != nil {
|
||||
fail(err, "parsing datetime")
|
||||
}
|
||||
_, err = dbTx.Exec(`INSERT INTO location (person, datetime, latitude, longitude, altitude) VALUES (?, ?, ?, ?, ?)`, location.Person, dateTime.UTC().Format(time.RFC3339), location.Latitude, location.Longitude, location.Altitude)
|
||||
if err != nil {
|
||||
fail(err, "inserting new row")
|
||||
}
|
||||
}
|
||||
|
||||
_, err = dbTx.Exec(`DROP TABLE location_old`)
|
||||
if err != nil {
|
||||
fail(err, "dropping old table")
|
||||
}
|
||||
|
||||
_, err = dbTx.Exec(`PRAGMA user_version = 2`)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("LocationRecorderInit Error setting user version to 2", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
dbTx.Commit()
|
||||
*userVersion = 2
|
||||
}
|
||||
@@ -0,0 +1,366 @@
|
||||
package pooRecorder
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"log/slog"
|
||||
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
"github.com/jomei/notionapi"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/t-liu93/home-automation-backend/util/homeassistantutil"
|
||||
"github.com/t-liu93/home-automation-backend/util/notion"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
var (
|
||||
db *sql.DB
|
||||
scheduler *gocron.Scheduler
|
||||
)
|
||||
|
||||
type recordDetail struct {
|
||||
Status string `json:"status"`
|
||||
Latitude string `json:"latitude"`
|
||||
Longitude string `json:"longitude"`
|
||||
}
|
||||
|
||||
type pooStatusSensorAttributes struct {
|
||||
LastPoo string `json:"last_poo"`
|
||||
FriendlyName string `json:"friendly_name,"`
|
||||
}
|
||||
|
||||
type pooStatusWebhookBody struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type pooStatusDbEntry struct {
|
||||
Timestamp string
|
||||
Status string
|
||||
Latitude float64
|
||||
Longitude float64
|
||||
}
|
||||
|
||||
func Init(mainScheduler *gocron.Scheduler) {
|
||||
initDb()
|
||||
initScheduler(mainScheduler)
|
||||
notionDbSync()
|
||||
publishLatestPooSensor()
|
||||
}
|
||||
|
||||
func HandleRecordPoo(w http.ResponseWriter, r *http.Request) {
|
||||
var record recordDetail
|
||||
if !viper.InConfig("pooRecorder.tableId") {
|
||||
slog.Warn("HandleRecordPoo Table ID not found in config file")
|
||||
http.Error(w, "Table ID not found in config file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
err := decoder.Decode(&record)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("HandleRecordPoo Error decoding request body", err))
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
err = storeStatus(record, now)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("HandleRecordPoo Error storing status", err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
publishLatestPooSensor()
|
||||
if viper.InConfig("pooRecorder.webhookId") {
|
||||
homeassistantutil.TriggerWebhook(viper.GetString("pooRecorder.webhookId"), pooStatusWebhookBody{Status: record.Status})
|
||||
} else {
|
||||
slog.Warn("HandleRecordPoo Webhook ID not found in config file")
|
||||
}
|
||||
}
|
||||
|
||||
func HandleNotifyLatestPoo(w http.ResponseWriter, r *http.Request) {
|
||||
err := publishLatestPooSensor()
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("HandleNotifyLatestPoo Error publishing latest poo", err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
slog.Debug(fmt.Sprintln("HandleGetLatestPoo Latest poo"))
|
||||
}
|
||||
|
||||
func publishLatestPooSensor() error {
|
||||
var latest pooStatusDbEntry
|
||||
err := db.QueryRow(`SELECT timestamp, status, latitude, longitude FROM poo_records ORDER BY timestamp DESC LIMIT 1`).Scan(&latest.Timestamp, &latest.Status, &latest.Latitude, &latest.Longitude)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("HandleGetLatestPoo Error getting latest poo", err))
|
||||
return err
|
||||
}
|
||||
recordTime, err := time.Parse("2006-01-02T15:04Z07:00", latest.Timestamp)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("HandleGetLatestPoo Error parsing timestamp", err))
|
||||
return err
|
||||
}
|
||||
viper.SetDefault("pooRecorder.sensorEntityName", "sensor.test_poo_status")
|
||||
viper.SetDefault("pooRecorder.sensorFriendlyName", "Poo Status")
|
||||
sensorEntityName := viper.GetString("pooRecorder.sensorEntityName")
|
||||
sensorFriendlyName := viper.GetString("pooRecorder.sensorFriendlyName")
|
||||
recordTime = recordTime.Local()
|
||||
pooStatus := homeassistantutil.HttpSensor{
|
||||
EntityId: sensorEntityName,
|
||||
State: latest.Status,
|
||||
Attributes: pooStatusSensorAttributes{
|
||||
LastPoo: recordTime.Format("Mon | 2006-01-02 | 15:04"),
|
||||
FriendlyName: sensorFriendlyName,
|
||||
},
|
||||
}
|
||||
homeassistantutil.PublishSensor(pooStatus)
|
||||
return nil
|
||||
}
|
||||
|
||||
func initDb() {
|
||||
if !viper.InConfig("pooRecorder.dbPath") {
|
||||
slog.Info("PooRecorderInit dbPath not found in config file, using default: pooRecorder.db")
|
||||
viper.SetDefault("pooRecorder.dbPath", "pooRecorder.db")
|
||||
}
|
||||
|
||||
dbPath := viper.GetString("pooRecorder.dbPath")
|
||||
err := error(nil)
|
||||
db, err = sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderInit Error opening database", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderInit Error pinging database", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
migrateDb()
|
||||
}
|
||||
|
||||
func migrateDb() {
|
||||
var userVersion int
|
||||
err := db.QueryRow("PRAGMA user_version").Scan(&userVersion)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderInit Error getting db user version", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
if userVersion == 0 {
|
||||
migrateDb0To1(&userVersion)
|
||||
}
|
||||
}
|
||||
|
||||
func migrateDb0To1(userVersion *int) {
|
||||
// this is actually create new db
|
||||
slog.Info("Creating database version 1..")
|
||||
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS poo_records (
|
||||
timestamp TEXT NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
latitude REAL NOT NULL,
|
||||
longitude REAL NOT NULL,
|
||||
PRIMARY KEY (timestamp))`)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderInit Error creating table", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = db.Exec(`PRAGMA user_version = 1`)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderInit Error setting user version to 1", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
*userVersion = 1
|
||||
}
|
||||
|
||||
func initScheduler(mainScheduler *gocron.Scheduler) {
|
||||
scheduler = mainScheduler
|
||||
_, err := (*scheduler).NewJob(gocron.CronJob("0 5 * * *", false), gocron.NewTask(
|
||||
notionDbSync,
|
||||
))
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderInit Error creating scheduled task", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func notionDbSync() {
|
||||
slog.Info("PooRecorder Running DB sync with Notion..")
|
||||
if !viper.InConfig("pooRecorder.tableId") {
|
||||
slog.Warn("PooRecorder Table ID not found in config file, sync aborted")
|
||||
return
|
||||
}
|
||||
tableId := viper.GetString("pooRecorder.tableId")
|
||||
rowsNotion, err := notion.GetAllTableRows(tableId)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderSyncDb Failed to get table header", err))
|
||||
return
|
||||
}
|
||||
header := rowsNotion[0]
|
||||
rowsNotion = rowsNotion[1:] // remove header
|
||||
rowsDb, err := db.Query(`SELECT * FROM poo_records`)
|
||||
rowsDbMap := make(map[string]pooStatusDbEntry)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderSyncDb Failed to get db rows", err))
|
||||
return
|
||||
}
|
||||
defer rowsDb.Close()
|
||||
for rowsDb.Next() {
|
||||
var row pooStatusDbEntry
|
||||
err = rowsDb.Scan(&row.Timestamp, &row.Status, &row.Latitude, &row.Longitude)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderSyncDb Failed to scan db row", err))
|
||||
return
|
||||
}
|
||||
rowsDbMap[row.Timestamp] = row
|
||||
}
|
||||
// notion to db
|
||||
syncNotionToDb(rowsNotion, rowsDbMap)
|
||||
|
||||
// db to notion
|
||||
syncDbToNotion(header.GetID().String(), tableId, rowsNotion)
|
||||
|
||||
}
|
||||
|
||||
func syncNotionToDb(rowsNotion []notionapi.TableRowBlock, rowsDbMap map[string]pooStatusDbEntry) {
|
||||
counter := 0
|
||||
for _, rowNotion := range rowsNotion {
|
||||
rowNotionTimestamp := rowNotion.TableRow.Cells[0][0].PlainText + "T" + rowNotion.TableRow.Cells[1][0].PlainText
|
||||
rowNotionTime, err := time.ParseInLocation("2006-01-02T15:04", rowNotionTimestamp, time.Now().Location())
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("PooRecorderSyncDb Failed to parse timestamp", err))
|
||||
return
|
||||
}
|
||||
rowNotionTimeInDbFormat := rowNotionTime.UTC().Format("2006-01-02T15:04Z07:00")
|
||||
_, exists := rowsDbMap[rowNotionTimeInDbFormat]
|
||||
if !exists {
|
||||
locationNotion := rowNotion.TableRow.Cells[3][0].PlainText
|
||||
latitude, err := strconv.ParseFloat(strings.Split(locationNotion, ",")[0], 64)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderSyncDb Failed to parse latitude to float", err))
|
||||
return
|
||||
}
|
||||
longitude, err := strconv.ParseFloat(strings.Split(locationNotion, ",")[1], 64)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderSyncDb Failed to parse longitude to float", err))
|
||||
return
|
||||
}
|
||||
_, err = db.Exec(`INSERT INTO poo_records (timestamp, status, latitude, longitude) VALUES (?, ?, ?, ?)`,
|
||||
rowNotionTimeInDbFormat, rowNotion.TableRow.Cells[2][0].PlainText, latitude, longitude)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("PooRecorderSyncDb Failed to insert new row", err))
|
||||
return
|
||||
}
|
||||
counter++
|
||||
}
|
||||
}
|
||||
slog.Info(fmt.Sprintln("PooRecorderSyncDb Inserted", counter, "new rows from Notion to DB"))
|
||||
}
|
||||
|
||||
func syncDbToNotion(headerId string, tableId string, rowsNotion []notionapi.TableRowBlock) {
|
||||
counter := 0
|
||||
var rowsDbSlice []pooStatusDbEntry
|
||||
rowsDb, err := db.Query(`SELECT * FROM poo_records ORDER BY timestamp DESC`)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderSyncDb Failed to get db rows", err))
|
||||
return
|
||||
}
|
||||
defer rowsDb.Close()
|
||||
for rowsDb.Next() {
|
||||
var row pooStatusDbEntry
|
||||
err = rowsDb.Scan(&row.Timestamp, &row.Status, &row.Latitude, &row.Longitude)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintln("PooRecorderSyncDb Failed to scan db row", err))
|
||||
return
|
||||
}
|
||||
rowsDbSlice = append(rowsDbSlice, row)
|
||||
}
|
||||
startFromId := headerId
|
||||
for iNotion, iDb := 0, 0; iNotion < len(rowsNotion) && iDb < len(rowsDbSlice); {
|
||||
notionTimeStamp := rowsNotion[iNotion].TableRow.Cells[0][0].PlainText + "T" + rowsNotion[iNotion].TableRow.Cells[1][0].PlainText
|
||||
notionTime, err := time.ParseInLocation("2006-01-02T15:04", notionTimeStamp, time.Now().Location())
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("PooRecorderSyncDb Failed to parse notion timestamp", err))
|
||||
return
|
||||
}
|
||||
notionTimeStampInDbFormat := notionTime.UTC().Format("2006-01-02T15:04Z07:00")
|
||||
dbTimeStamp := rowsDbSlice[iDb].Timestamp
|
||||
dbTime, err := time.Parse("2006-01-02T15:04Z07:00", dbTimeStamp)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("PooRecorderSyncDb Failed to parse db timestamp", err))
|
||||
return
|
||||
}
|
||||
dbTimeLocal := dbTime.Local()
|
||||
dbTimeDate := dbTimeLocal.Format("2006-01-02")
|
||||
dbTimeTime := dbTimeLocal.Format("15:04")
|
||||
if notionTimeStampInDbFormat == dbTimeStamp {
|
||||
startFromId = rowsNotion[iNotion].GetID().String()
|
||||
iNotion++
|
||||
iDb++
|
||||
continue
|
||||
}
|
||||
if iNotion != len(rowsNotion)-1 {
|
||||
notionNextTimeStamp := rowsNotion[iNotion+1].TableRow.Cells[0][0].PlainText + "T" + rowsNotion[iNotion+1].TableRow.Cells[1][0].PlainText
|
||||
notionNextTime, err := time.ParseInLocation("2006-01-02T15:04", notionNextTimeStamp, time.Now().Location())
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("PooRecorderSyncDb Failed to parse next notion timestamp", err))
|
||||
return
|
||||
}
|
||||
if notionNextTime.After(notionTime) {
|
||||
slog.Error(fmt.Sprintf("PooRecorderSyncDb Notion timestamp %s is after next timestamp %s, checking, aborting", notionTimeStamp, notionNextTimeStamp))
|
||||
return
|
||||
}
|
||||
}
|
||||
id, err := notion.WriteTableRow([]string{
|
||||
dbTimeDate,
|
||||
dbTimeTime,
|
||||
rowsDbSlice[iDb].Status,
|
||||
fmt.Sprintf("%s,%s",
|
||||
strconv.FormatFloat(rowsDbSlice[iDb].Latitude, 'f', -1, 64),
|
||||
strconv.FormatFloat(rowsDbSlice[iDb].Longitude, 'f', -1, 64))},
|
||||
tableId,
|
||||
startFromId)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("PooRecorderSyncDb Failed to write row to Notion", err))
|
||||
return
|
||||
}
|
||||
startFromId = id
|
||||
iDb++
|
||||
counter++
|
||||
time.Sleep(400 * time.Millisecond)
|
||||
}
|
||||
slog.Info(fmt.Sprintln("PooRecorderSyncDb Inserted", counter, "new rows from DB to Notion"))
|
||||
}
|
||||
|
||||
func storeStatus(record recordDetail, timestamp time.Time) error {
|
||||
tableId := viper.GetString("pooRecorder.tableId")
|
||||
recordDate := timestamp.Format("2006-01-02")
|
||||
recordTime := timestamp.Format("15:04")
|
||||
slog.Debug(fmt.Sprintln("Recording poo", record.Status, "at", record.Latitude, record.Longitude))
|
||||
_, err := db.Exec(`INSERT OR IGNORE INTO poo_records (timestamp, status, latitude, longitude) VALUES (?, ?, ?, ?)`,
|
||||
timestamp.UTC().Format("2006-01-02T15:04Z07:00"), record.Status, record.Latitude, record.Longitude)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
header, err := notion.GetTableRows(tableId, 1, "")
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("HandleRecordPoo Failed to get table header", err))
|
||||
return
|
||||
}
|
||||
if len(header) == 0 {
|
||||
slog.Warn("HandleRecordPoo Table header not found")
|
||||
return
|
||||
}
|
||||
headerId := header[0].GetID()
|
||||
_, err = notion.WriteTableRow([]string{recordDate, recordTime, record.Status, record.Latitude + "," + record.Longitude}, tableId, headerId.String())
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("HandleRecordPoo Failed to write table row", err))
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user