Add auto reconnect function.
All checks were successful
Build Docker image / build (push) Successful in 49s

This commit is contained in:
Pieter Hollander 2023-12-06 13:51:14 +01:00
parent ff52e2d65b
commit 71e579b14c
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg

111
main.go
View file

@ -40,7 +40,7 @@ type Payload struct {
var db *sql.DB
func main() {
// Load environment variables from .env file if it exists
// // Load environment variables from .env file if it exists
// err := godotenv.Load()
// if err != nil {
// log.Println("Error loading .env file:", err)
@ -48,52 +48,17 @@ func main() {
// Connect to PostgreSQL
pgConnStr := os.Getenv("PG_DB")
var err error
db, err = sql.Open("postgres", pgConnStr)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Enable TimescaleDB
// _, err = db.Exec(`
// CREATE EXTENSION IF NOT EXISTS timescaledb;
// `)
if err != nil {
log.Fatal("Error creating TimescaleDB extension:", err)
if err := connectToPostgreSQL(pgConnStr); err != nil {
log.Fatal("Error connecting to PostgreSQL:", err)
}
// Create table if not exists
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS p1 (
timestamp TIMESTAMPTZ,
delivered_tariff1 INT,
delivered_tariff2 INT,
returned_tariff1 INT,
returned_tariff2 INT,
delivery_all INT,
returning_all INT,
failures INT,
long_failures INT,
gas INT,
voltage_l1 INT,
voltage_l2 INT,
voltage_l3 INT,
current_l1 INT,
current_l2 INT,
current_l3 INT,
delivery_l1 INT,
delivery_l2 INT,
delivery_l3 INT,
returning_l1 INT,
returning_l2 INT,
returning_l3 INT
);
-- SELECT create_hypertable('p1', 'timestamp', if_not_exists => TRUE);
`)
if err != nil {
log.Fatal("Error creating table:", err)
}
// pgConnStr := os.Getenv("PG_DB")
// var err error
// db, err = sql.Open("postgres", pgConnStr)
// if err != nil {
// log.Fatal(err)
// }
// defer db.Close()
// Initialize MQTT options
opts := mqtt.NewClientOptions()
@ -184,3 +149,59 @@ func insertData(timestamp time.Time, payload Payload) error {
)
return err
}
func connectToPostgreSQL(pgConnStr string) error {
// Connect to PostgreSQL
var err error
for {
db, err = sql.Open("postgres", pgConnStr)
if err == nil {
break // Successfully connected
}
log.Println("Error connecting to PostgreSQL:", err)
time.Sleep(5 * time.Second) // Retry after 5 seconds
}
// Enable TimescaleDB
_, err = db.Exec(`
CREATE EXTENSION IF NOT EXISTS timescaledb;
`)
if err != nil {
log.Fatal("Error creating TimescaleDB extension:", err)
}
// Create table if not exists
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS p1 (
timestamp TIMESTAMPTZ,
delivered_tariff1 INT,
delivered_tariff2 INT,
returned_tariff1 INT,
returned_tariff2 INT,
delivery_all INT,
returning_all INT,
failures INT,
long_failures INT,
gas INT,
voltage_l1 INT,
voltage_l2 INT,
voltage_l3 INT,
current_l1 INT,
current_l2 INT,
current_l3 INT,
delivery_l1 INT,
delivery_l2 INT,
delivery_l3 INT,
returning_l1 INT,
returning_l2 INT,
returning_l3 INT
);
SELECT create_hypertable('p1', 'timestamp', if_not_exists => TRUE);
`)
if err != nil {
log.Fatal("Error creating table:", err)
}
return nil
}