Preparations for adding event timestamps.
All checks were successful
Build Docker image / build (push) Successful in 1m13s
Build Golang packages / release (push) Has been skipped

This commit is contained in:
Pieter Hollander 2024-07-01 14:39:19 +02:00
parent 6c810b4e2b
commit 1dbea69f52
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg

30
main.go
View file

@ -107,6 +107,8 @@ type Event struct {
Message string `json:"message"`
StartTime int `json:"start_time"`
EndTime int `json:"end_time"`
StartTimestamp time.Time
EndTimestamp time.Time
}
type EventsResponse struct {
@ -305,6 +307,27 @@ func createTables(db *sql.DB) {
end_time INT
);
DO $$
BEGIN
-- Check if start_timestamp column exists
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_name='opendtu_events'
AND column_name='start_timestamp') THEN
-- Add start_timestamp column
ALTER TABLE opendtu_events
ADD COLUMN start_timestamp TIMESTAMPTZ;
END IF;
-- Check if end_timestamp column exists
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_name='opendtu_events'
AND column_name='end_timestamp') THEN
-- Add end_timestamp column
ALTER TABLE opendtu_events
ADD COLUMN end_timestamp TIMESTAMPTZ;
END IF;
END $$;
CREATE TABLE IF NOT EXISTS opendtu_hints (
-- id SERIAL PRIMARY KEY,
timestamp TIMESTAMPTZ,
@ -445,6 +468,9 @@ func queryEventsEndpoint(inverterSerial string) (*EventsResponse, error) {
}
// The events counter reported by OpenDTU resets every day.
// However, this assumes that the inverters from which the events are pulled are reset every day, during the night.
// Additionally, this function requires OpenDTU to be set to "Clear Eventlog at midnight" for each inverter.
// "Clear Eventlog at midnight" should be set to ON in "Inverter settings" -> "pencil" -> "Advanced".
// To account for possible time drifts, the first and last 10 minutes of the day are excluded.
// Longest day NL: sun up 4:16, sun down 22:50
// Shortest day NL: sun up 8:44, sun down 16:25
@ -478,10 +504,10 @@ func insertEvents(db *sql.DB, inverterSerial string, events *EventsResponse) {
}
if len(events.Events) > 0 && events.Events[0].EndTime == 0 {
// If end_time is 0, schedule a job to update the corresponding message row every 30 minutes
// If end_time is 0, schedule a job to update the corresponding message row every 10 minutes
go func() {
for {
time.Sleep(30 * time.Minute)
time.Sleep(10 * time.Minute)
updatedEvents, err := queryEventsEndpoint(inverterSerial)
if err != nil {
logger.Error("Error querying events endpoint for updates", "error", err)