Preparations for adding event timestamps.
This commit is contained in:
parent
6c810b4e2b
commit
1dbea69f52
1 changed files with 32 additions and 6 deletions
30
main.go
30
main.go
|
@ -107,6 +107,8 @@ type Event struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
StartTime int `json:"start_time"`
|
StartTime int `json:"start_time"`
|
||||||
EndTime int `json:"end_time"`
|
EndTime int `json:"end_time"`
|
||||||
|
StartTimestamp time.Time
|
||||||
|
EndTimestamp time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventsResponse struct {
|
type EventsResponse struct {
|
||||||
|
@ -305,6 +307,27 @@ func createTables(db *sql.DB) {
|
||||||
end_time INT
|
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 (
|
CREATE TABLE IF NOT EXISTS opendtu_hints (
|
||||||
-- id SERIAL PRIMARY KEY,
|
-- id SERIAL PRIMARY KEY,
|
||||||
timestamp TIMESTAMPTZ,
|
timestamp TIMESTAMPTZ,
|
||||||
|
@ -445,6 +468,9 @@ func queryEventsEndpoint(inverterSerial string) (*EventsResponse, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// The events counter reported by OpenDTU resets every day.
|
// 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.
|
// 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
|
// Longest day NL: sun up 4:16, sun down 22:50
|
||||||
// Shortest day NL: sun up 8:44, sun down 16:25
|
// 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 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() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
time.Sleep(30 * time.Minute)
|
time.Sleep(10 * time.Minute)
|
||||||
updatedEvents, err := queryEventsEndpoint(inverterSerial)
|
updatedEvents, err := queryEventsEndpoint(inverterSerial)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Error querying events endpoint for updates", "error", err)
|
logger.Error("Error querying events endpoint for updates", "error", err)
|
||||||
|
|
Loading…
Reference in a new issue