Ported location recorder

This commit is contained in:
2024-09-18 16:41:26 +02:00
parent 197b9a3d63
commit d8d6c8bb35
4 changed files with 249 additions and 17 deletions

View File

@@ -32,7 +32,8 @@ type recordDetail struct {
}
type pooStatusSensorAttributes struct {
LastPoo string `json:"last_poo"`
LastPoo string `json:"last_poo"`
FriendlyName string `json:"friendly_name,"`
}
type pooStatusWebhookBody struct {
@@ -49,6 +50,8 @@ type pooStatusDbEntry struct {
func Init(mainScheduler *gocron.Scheduler) {
initDb()
initScheduler(mainScheduler)
notionDbSync()
publishLatestPooSensor()
}
func HandleRecordPoo(w http.ResponseWriter, r *http.Request) {
@@ -73,14 +76,7 @@ func HandleRecordPoo(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
pooStatus := homeassistantutil.HttpSensor{
EntityId: "sensor.test_poo_sensor",
State: record.Status,
Attributes: pooStatusSensorAttributes{
LastPoo: now.Format("Mon | 2006-01-02 | 15:04"),
},
}
homeassistantutil.PublishSensor(pooStatus)
publishLatestPooSensor()
if viper.InConfig("pooRecorder.webhookId") {
homeassistantutil.TriggerWebhook(viper.GetString("pooRecorder.webhookId"), pooStatusWebhookBody{Status: record.Status})
} else {
@@ -89,34 +85,47 @@ func HandleRecordPoo(w http.ResponseWriter, r *http.Request) {
}
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))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
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))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
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: "sensor.test_poo_sensor",
EntityId: sensorEntityName,
State: latest.Status,
Attributes: pooStatusSensorAttributes{
LastPoo: recordTime.Format("Mon | 2006-01-02 | 15:04"),
LastPoo: recordTime.Format("Mon | 2006-01-02 | 15:04"),
FriendlyName: sensorFriendlyName,
},
}
homeassistantutil.PublishSensor(pooStatus)
slog.Debug(fmt.Sprintln("HandleGetLatestPoo Latest poo", pooStatus.State, "at", pooStatus.Attributes.(pooStatusSensorAttributes).LastPoo))
return nil
}
func initDb() {
if !viper.InConfig("pooRecorder.dbPath") {
slog.Info("HandleRecordPoo dbPath not found in config file, using default: pooRecorder.db")
slog.Info("PooRecorderInit dbPath not found in config file, using default: pooRecorder.db")
viper.SetDefault("pooRecorder.dbPath", "pooRecorder.db")
}