diff --git a/main.go b/main.go index 743fff7..170a479 100644 --- a/main.go +++ b/main.go @@ -220,7 +220,7 @@ func handleMessage(message []byte, db *sql.DB) { continue } - // Insert events data into the events table + // Insert events data into the opendtu_events table insertEvents(db, inverter.Serial, events) } @@ -238,19 +238,16 @@ func createTables(db *sql.DB) { // An additional advantage is that it makes plotting in Grafana easier. // TODO: Foreign keys commented out as TimescaleDB hypertables don't support them. createTableSQL := ` - CREATE TABLE IF NOT EXISTS dtu_log ( - -- id SERIAL PRIMARY KEY, + CREATE TABLE IF NOT EXISTS opendtu_log ( timestamp TIMESTAMPTZ UNIQUE DEFAULT CURRENT_TIMESTAMP, - -- timestamp TIMESTAMPTZ, power NUMERIC, yieldday NUMERIC, yieldtotal NUMERIC ); - CREATE TABLE IF NOT EXISTS inverters ( - -- id SERIAL PRIMARY KEY, + CREATE TABLE IF NOT EXISTS opendtu_inverters ( timestamp TIMESTAMPTZ, - -- FOREIGN KEY (timestamp) REFERENCES dtu_log(timestamp), + -- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp), inverter_serial TEXT, name TEXT, producing BOOL, @@ -258,10 +255,9 @@ func createTables(db *sql.DB) { limit_absolute NUMERIC ); - CREATE TABLE IF NOT EXISTS inverters_ac ( - -- id SERIAL PRIMARY KEY, + CREATE TABLE IF NOT EXISTS opendtu_inverters_ac ( timestamp TIMESTAMPTZ, - -- FOREIGN KEY (timestamp) REFERENCES dtu_log(timestamp), + -- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp), inverter_serial TEXT, ac_number INT, power NUMERIC, @@ -272,10 +268,10 @@ func createTables(db *sql.DB) { reactivepower NUMERIC ); - CREATE TABLE IF NOT EXISTS inverters_dc ( + CREATE TABLE IF NOT EXISTS opendtu_inverters_dc ( -- id SERIAL PRIMARY KEY, timestamp TIMESTAMPTZ, - -- FOREIGN KEY (timestamp) REFERENCES dtu_log(timestamp), + -- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp), inverter_serial TEXT, dc_number INT, name TEXT, @@ -287,10 +283,10 @@ func createTables(db *sql.DB) { irradiation NUMERIC ); - CREATE TABLE IF NOT EXISTS inverters_inv ( + CREATE TABLE IF NOT EXISTS opendtu_inverters_inv ( -- id SERIAL PRIMARY KEY, timestamp TIMESTAMPTZ, - -- FOREIGN KEY (timestamp) REFERENCES dtu_log(timestamp), + -- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp), inverter_serial TEXT, temperature NUMERIC, power_dc NUMERIC, @@ -299,7 +295,7 @@ func createTables(db *sql.DB) { efficiency NUMERIC ); - CREATE TABLE IF NOT EXISTS events ( + CREATE TABLE IF NOT EXISTS opendtu_events ( -- id SERIAL PRIMARY KEY, timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, inverter_serial TEXT, @@ -309,23 +305,22 @@ func createTables(db *sql.DB) { end_time INT ); - CREATE TABLE IF NOT EXISTS dtu_hints ( + CREATE TABLE IF NOT EXISTS opendtu_hints ( -- id SERIAL PRIMARY KEY, timestamp TIMESTAMPTZ, - -- FOREIGN KEY (timestamp) REFERENCES dtu_log(timestamp), + -- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp), time_sync BOOL, radio_problem BOOL, default_password BOOL ); - -- UNIQUE here doesn't work, as e.g. one timestamp may be valid for multiple string data. - CREATE INDEX IF NOT EXISTS dtu_log_timestamp_idx ON dtu_log (timestamp); - CREATE INDEX IF NOT EXISTS inverters_timestamp_idx ON inverters (timestamp); - CREATE INDEX IF NOT EXISTS inverters_ac_timestamp_idx ON inverters_ac (timestamp); - CREATE INDEX IF NOT EXISTS inverters_dc_timestamp_idx ON inverters_dc (timestamp); - CREATE INDEX IF NOT EXISTS inverters_inv_timestamp_idx ON inverters_inv (timestamp); - CREATE INDEX IF NOT EXISTS events_timestamp_idx ON events (timestamp); - CREATE INDEX IF NOT EXISTS dtu_hints_timestamp_idx ON dtu_hints (timestamp); + CREATE INDEX IF NOT EXISTS opendtu_log_timestamp_idx ON opendtu_log (timestamp); + CREATE INDEX IF NOT EXISTS opendtu_inverters_timestamp_idx ON opendtu_inverters (timestamp); + CREATE INDEX IF NOT EXISTS opendtu_inverters_ac_timestamp_idx ON opendtu_inverters_ac (timestamp); + CREATE INDEX IF NOT EXISTS opendtu_inverters_dc_timestamp_idx ON opendtu_inverters_dc (timestamp); + CREATE INDEX IF NOT EXISTS opendtu_inverters_inv_timestamp_idx ON opendtu_inverters_inv (timestamp); + CREATE INDEX IF NOT EXISTS opendtu_events_timestamp_idx ON opendtu_events (timestamp); + CREATE INDEX IF NOT EXISTS opendtu_hints_timestamp_idx ON opendtu_hints (timestamp); ` @@ -337,13 +332,13 @@ func createTables(db *sql.DB) { enableTimescaleDB := ` -- CREATE EXTENSION IF NOT EXISTS timescaledb; - SELECT create_hypertable('dtu_log', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); - SELECT create_hypertable('inverters', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); - SELECT create_hypertable('inverters_ac', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); - SELECT create_hypertable('inverters_dc', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); - SELECT create_hypertable('inverters_inv', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); - SELECT create_hypertable('events', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); - SELECT create_hypertable('dtu_hints', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); + SELECT create_hypertable('opendtu_log', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); + SELECT create_hypertable('opendtu_inverters', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); + SELECT create_hypertable('opendtu_inverters_ac', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); + SELECT create_hypertable('opendtu_inverters_dc', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); + SELECT create_hypertable('opendtu_inverters_inv', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); + SELECT create_hypertable('opendtu_events', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); + SELECT create_hypertable('opendtu_hints', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); ` if timescaleEnabled == "true" { _, err := db.Exec(enableTimescaleDB) @@ -360,7 +355,7 @@ func insertLiveData(db *sql.DB, inverter Inverter, total Total, hints Hints) { // Insert data into log table _, err := db.Exec(` - INSERT INTO dtu_log (timestamp, power, yieldday, yieldtotal) + INSERT INTO opendtu_log (timestamp, power, yieldday, yieldtotal) VALUES ($1, $2, $3, $4); `, timestamp, total.Power.V, total.YieldDay.V, total.YieldTotal.V) if err != nil { @@ -371,57 +366,57 @@ func insertLiveData(db *sql.DB, inverter Inverter, total Total, hints Hints) { // Get the log ID of the inserted record // NOT IN USE: TimescaleDB doesn't support it. // var logID int - // err = db.QueryRow("SELECT id FROM dtu_log WHERE timestamp = $1", timestamp).Scan(&logID) + // err = db.QueryRow("SELECT id FROM opendtu_log WHERE timestamp = $1", timestamp).Scan(&logID) // if err != nil { - // logger.Error("Error getting dtu_log ID", "error", err) + // logger.Error("Error getting opendtu_log ID", "error", err) // return // } - // Insert data into inverters table + // Insert data into opendtu_inverters table _, err = db.Exec(` - INSERT INTO inverters (timestamp, inverter_serial, name, producing, limit_relative, limit_absolute) + INSERT INTO opendtu_inverters (timestamp, inverter_serial, name, producing, limit_relative, limit_absolute) VALUES ($1, $2, $3, $4, $5, $6); `, timestamp, inverter.Serial, inverter.Name, inverter.Producing, inverter.LimitRelative, inverter.LimitAbsolute) if err != nil { - logger.Error("Error inserting into inverters table", "error", err) + logger.Error("Error inserting into opendtu_inverters table", "error", err) return } - // Insert data into inverters_ac table + // Insert data into opendtu_inverters_ac table for acNumber, acData := range inverter.AC { _, err := db.Exec(` - INSERT INTO inverters_ac (timestamp, inverter_serial, ac_number, power, voltage, current, frequency, powerfactor, reactivepower) + INSERT INTO opendtu_inverters_ac (timestamp, inverter_serial, ac_number, power, voltage, current, frequency, powerfactor, reactivepower) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9); `, timestamp, inverter.Serial, acNumber, acData.Power.V, acData.Voltage.V, acData.Current.V, acData.Frequency.V, acData.PowerFactor.V, acData.ReactivePower.V) if err != nil { - logger.Error("Error inserting into inverters_ac table for AC", "error", "acNumber", acNumber, err) + logger.Error("Error inserting into opendtu_inverters_ac table for AC", "error", "acNumber", acNumber, err) } } - // Insert data into inverters_dc table + // Insert data into opendtu_inverters_dc table for dcNumber, dcData := range inverter.DC { _, err := db.Exec(` - INSERT INTO inverters_dc (timestamp, inverter_serial, dc_number, name, power, voltage, current, yieldday, yieldtotal, irradiation) + INSERT INTO opendtu_inverters_dc (timestamp, inverter_serial, dc_number, name, power, voltage, current, yieldday, yieldtotal, irradiation) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10); `, timestamp, inverter.Serial, dcNumber, dcData.Name.U, dcData.Power.V, dcData.Voltage.V, dcData.Current.V, dcData.YieldDay.V, dcData.YieldTotal.V, dcData.Irradiation.V) if err != nil { - log.Printf("Error inserting into inverters_dc table for DC %s: %v\n", dcNumber, err) + log.Printf("Error inserting into opendtu_inverters_dc table for DC %s: %v\n", dcNumber, err) } } - // Insert data into inverters_inv table + // Insert data into opendtu_inverters_inv table for invNumber, invData := range inverter.INV { _, err := db.Exec(` - INSERT INTO inverters_inv (timestamp, inverter_serial, temperature, efficiency, power_dc, yieldday, yieldtotal) + INSERT INTO opendtu_inverters_inv (timestamp, inverter_serial, temperature, efficiency, power_dc, yieldday, yieldtotal) VALUES ($1, $2, $3, $4, $5, $6, $7); `, timestamp, inverter.Serial, invData.Temperature.V, invData.Efficiency.V, invData.PowerDC.V, invData.YieldDay.V, invData.YieldTotal.V) if err != nil { - log.Printf("Error inserting into inverters_inv table for INV %s: %v\n", invNumber, err) + log.Printf("Error inserting into opendtu_inverters_inv table for INV %s: %v\n", invNumber, err) } } // Insert data into hints table _, err = db.Exec(` - INSERT INTO dtu_hints (timestamp, time_sync, radio_problem, default_password) + INSERT INTO opendtu_hints (timestamp, time_sync, radio_problem, default_password) VALUES ($1, $2, $3, $4); `, timestamp, hints.TimeSync, hints.RadioProblem, hints.DefaultPassword) if err != nil { @@ -449,9 +444,11 @@ func queryEventsEndpoint(inverterSerial string) (*EventsResponse, error) { return &eventsResponse, nil } +// TODO: This function should probably be altered to only select from current day. +// CHECK. func getPreviousEventsCount(db *sql.DB, inverterSerial string) int { var count int - err := db.QueryRow("SELECT COUNT(*) FROM events WHERE inverter_serial = $1", inverterSerial).Scan(&count) + err := db.QueryRow("SELECT COUNT(*) FROM opendtu_events WHERE inverter_serial = $1", inverterSerial).Scan(&count) if err != nil && err != sql.ErrNoRows { logger.Error("Error querying previous events count", "error", err) } @@ -464,11 +461,11 @@ func insertEvents(db *sql.DB, inverterSerial string, events *EventsResponse) { for _, event := range events.Events { // Insert events data into the events table _, err := db.Exec(` - INSERT INTO events (timestamp, inverter_serial, message_id, message, start_time, end_time) + INSERT INTO opendtu_events (timestamp, inverter_serial, message_id, message, start_time, end_time) VALUES ($1, $2, $3, $4, $5, $6); `, timestamp, inverterSerial, event.MessageID, event.Message, event.StartTime, event.EndTime) if err != nil { - logger.Error("Error inserting into events table", "error", err) + logger.Error("Error inserting into opendtu_events table", "error", err) } } @@ -492,12 +489,12 @@ func insertEvents(db *sql.DB, inverterSerial string, events *EventsResponse) { func updateEvents(db *sql.DB, inverterSerial string, events *EventsResponse) { for _, event := range events.Events { - // Update events data in the events table + // Update events data in the opendtu_events table _, err := db.Exec(` - UPDATE events SET end_time = $1 WHERE inverter_serial = $2 AND start_time = $3 AND end_time = 0; + UPDATE opendtu_events SET end_time = $1 WHERE inverter_serial = $2 AND start_time = $3 AND end_time = 0; `, event.EndTime, inverterSerial, event.StartTime) if err != nil { - logger.Error("Error updating events table", "error", err) + logger.Error("Error updating opendtu_events table", "error", err) } } }