BREAKING: Migrate REMOTE_URL to OPENDTU_ADDRESS.
All checks were successful
Build Docker image / build (push) Successful in 1m12s
Build Golang packages / release (push) Successful in 2m4s

BREAKING: Migrate opendtu to opendtu_address
Add authentication capability for locked-down opendtu's.
Updated setup examples with new variables.
This commit is contained in:
Pieter Hollander 2024-07-26 23:46:19 +02:00
parent bf885fb84c
commit 48d0382b0e
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg
6 changed files with 109 additions and 21 deletions

View file

@ -26,7 +26,10 @@ services:
restart: always restart: always
environment: environment:
DB_URL: ${DB_URL} DB_URL: ${DB_URL}
REMOTE_URL: ${REMOTE_URL} OPENDTU_ADDRESS: ${OPENDTU_ADDRESS}
OPENDTU_AUTH: ${OPENDTU_AUTH}
OPENDTU_USERNAME: ${OPENDTU_USERNAME}
OPENDTU_PASSWORD: ${OPENDTU_PASSWORD}
TIMESCALEDB_ENABLED: ${TIMESCALEDB_ENABLED} TIMESCALEDB_ENABLED: ${TIMESCALEDB_ENABLED}
TZ: ${TZ} TZ: ${TZ}
depends_on: depends_on:

View file

@ -26,7 +26,10 @@ services:
image: git.hollander.online/energy/opendtu-logger:latest image: git.hollander.online/energy/opendtu-logger:latest
environment: environment:
DB_URL: ${DB_URL} DB_URL: ${DB_URL}
REMOTE_URL: ${REMOTE_URL} OPENDTU_ADDRESS: ${OPENDTU_ADDRESS}
OPENDTU_AUTH: ${OPENDTU_AUTH}
OPENDTU_USERNAME: ${OPENDTU_USERNAME}
OPENDTU_PASSWORD: ${OPENDTU_PASSWORD}
TIMESCALEDB_ENABLED: ${TIMESCALEDB_ENABLED} TIMESCALEDB_ENABLED: ${TIMESCALEDB_ENABLED}
TZ: ${TZ} TZ: ${TZ}
depends_on: depends_on:

View file

@ -5,7 +5,10 @@ services:
image: git.hollander.online/energy/opendtu-logger:latest image: git.hollander.online/energy/opendtu-logger:latest
environment: environment:
DB_URL: ${DB_URL} DB_URL: ${DB_URL}
REMOTE_URL: ${REMOTE_URL} OPENDTU_ADDRESS: ${OPENDTU_ADDRESS}
OPENDTU_AUTH: ${OPENDTU_AUTH}
OPENDTU_USERNAME: ${OPENDTU_USERNAME}
OPENDTU_PASSWORD: ${OPENDTU_PASSWORD}
TIMESCALEDB_ENABLED: ${TIMESCALEDB_ENABLED} TIMESCALEDB_ENABLED: ${TIMESCALEDB_ENABLED}
TZ: ${TZ} TZ: ${TZ}
depends_on: depends_on:

View file

@ -1,5 +1,9 @@
# OpenDTU
OPENDTU_ADDRESS="192.168.1.89:80"
OPENDTU_AUTH=false
OPENDTU_USERNAME=
OPENDTU_PASSWORD=
# OpenDTU Logger # OpenDTU Logger
REMOTE_URL="192.168.1.89:80"
DB_URL="host=timescaledb port=5432 user=postgres password=secret dbname=opendtu_logger sslmode=disable" DB_URL="host=timescaledb port=5432 user=postgres password=secret dbname=opendtu_logger sslmode=disable"
TIMESCALEDB_ENABLED=true TIMESCALEDB_ENABLED=true
TZ="Europe/Amsterdam" TZ="Europe/Amsterdam"

96
main.go
View file

@ -1,5 +1,4 @@
// TODO: Storage optimisation: Map inverter serial to shorter serial. Use that for referring. // TODO: Storage optimisation: Map inverter serial to shorter serial. Use that for referring.
// TODO: Use username and password provided using Basic Authentication.
// TODO: Record Inverter struct data only on-change. // TODO: Record Inverter struct data only on-change.
// Idea: Make a full admin / config GUI and only configure through this utility. // Idea: Make a full admin / config GUI and only configure through this utility.
// Idea: Gather settings only on start-up. // Idea: Gather settings only on start-up.
@ -9,6 +8,7 @@ package main
import ( import (
"database/sql" "database/sql"
"encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/fs" "io/fs"
@ -150,7 +150,10 @@ type InverterSettingsData struct {
// Config settings struct // Config settings struct
type Config struct { type Config struct {
DB string `json:"db"` DB string `json:"db"`
OpenDTU string `json:"opendtu"` OpenDTUAddress string `json:"opendtu_address"`
OpenDTUAuth bool `json:"opendtu_auth"`
OpenDTUUser string `json:"opendtu_username"`
OpenDTUPassword string `json:"opendtu_password"`
TimescaleDB bool `json:"timescaledb"` TimescaleDB bool `json:"timescaledb"`
TZ string `json:"tz"` TZ string `json:"tz"`
} }
@ -173,15 +176,50 @@ func loadConfig() Config {
if err != nil { if err != nil {
log.Fatalf("Error parsing config file: %v", err) log.Fatalf("Error parsing config file: %v", err)
} }
if config.DB == "" {
log.Fatal("db connection settings are not set")
}
if config.OpenDTUAddress == "" {
log.Fatal("opendtu_address is not set")
}
if config.OpenDTUAuth {
if config.OpenDTUUser == "" {
log.Fatal("opendtu_username is not set, while opendtu_auth is set to enabled. Set opendtu_auth to false or set username")
}
if config.OpenDTUPassword == "" {
log.Fatal("opendtu_password is not set, while opendtu_auth is set to enabled. Set opendtu_auth to false or set password")
}
}
} else { } else {
logger.Info("JSON config file not found. Falling back to environment variables.")
// Fallback to environment variables // Fallback to environment variables
config.DB = os.Getenv("DB_URL") config.DB = os.Getenv("DB_URL")
if config.DB == "" { if config.DB == "" {
log.Fatal("DB_URL environment variable is not set.") log.Fatal("DB_URL environment variable is not set.")
} }
config.OpenDTU = os.Getenv("REMOTE_URL") config.OpenDTUAddress = os.Getenv("OPENDTU_ADDRESS")
if config.OpenDTU == "" { if config.OpenDTUAddress == "" {
log.Fatal("REMOTE_URL environment variable is not set.") log.Fatal("OPENDTU_ADDRESS environment variable is not set.")
}
openDTUAuthStr := os.Getenv("OPENDTU_AUTH")
if openDTUAuthStr != "" {
openDTUAuth, err := strconv.ParseBool(openDTUAuthStr)
if err != nil {
log.Fatalf("Error parsing OPENDTU_AUTH: %v", err)
}
config.OpenDTUAuth = openDTUAuth
}
if config.OpenDTUAuth {
config.OpenDTUUser = os.Getenv("OPENDTU_USERNAME")
if config.OpenDTUUser == "" {
log.Fatal("OPENDTU_USERNAME environment variable is not set.")
}
config.OpenDTUPassword = os.Getenv("OPENDTU_PASSWORD")
if config.OpenDTUPassword == "" {
log.Fatal("OPENDTU_PASSWORD environment variable is not set.")
}
} }
timescaleDBStr := os.Getenv("TIMESCALEDB_ENABLED") timescaleDBStr := os.Getenv("TIMESCALEDB_ENABLED")
@ -194,6 +232,10 @@ func loadConfig() Config {
} }
config.TZ = os.Getenv("TZ") config.TZ = os.Getenv("TZ")
} }
_, err = time.LoadLocation(config.TZ)
if err != nil {
logger.Warn("invalid timezone")
}
return config return config
} }
@ -218,10 +260,16 @@ func main() {
migrateDB(db) migrateDB(db)
// Create WebSocket URL from config variable // Create WebSocket URL from config variable
wsURL := "ws://" + config.OpenDTU + "/livedata" wsURL := "ws://" + config.OpenDTUAddress + "/livedata"
// Create headers with optional Basic Auth
headers := http.Header{}
if config.OpenDTUAuth {
headers.Set("Authorization", basicAuth(config.OpenDTUUser, config.OpenDTUPassword))
}
// Establish WebSocket connection // Establish WebSocket connection
c, _, err := websocket.DefaultDialer.Dial(wsURL, nil) c, _, err := websocket.DefaultDialer.Dial(wsURL, headers)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -424,15 +472,33 @@ func migrateFS(db *sql.DB, migrationFS fs.FS, dir string) error {
} }
func queryEventsEndpoint(inverterSerial string) (*EventsResponse, error) { func queryEventsEndpoint(inverterSerial string) (*EventsResponse, error) {
remoteURL := config.OpenDTU endpoint := fmt.Sprintf("http://"+config.OpenDTUAddress+"/api/eventlog/status?inv=%s", inverterSerial)
endpoint := fmt.Sprintf("http://"+remoteURL+"/api/eventlog/status?inv=%s", inverterSerial)
resp, err := http.Get(endpoint) // Create a new HTTP request
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
if config.OpenDTUAuth {
// Add Basic Auth header
req.Header.Add("Authorization", basicAuth(config.OpenDTUUser, config.OpenDTUPassword))
}
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
// Check for HTTP errors
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP request failed with status: %s", resp.Status)
}
// Decode the response
var eventsResponse EventsResponse var eventsResponse EventsResponse
if err := json.NewDecoder(resp.Body).Decode(&eventsResponse); err != nil { if err := json.NewDecoder(resp.Body).Decode(&eventsResponse); err != nil {
return nil, err return nil, err
@ -508,6 +574,12 @@ func updateEvents(db *sql.DB, inverterSerial string, events *EventsResponse) {
} }
} }
// basicAuth generates the Basic Auth header value
func basicAuth(username, password string) string {
credentials := username + ":" + password
return "Basic " + base64.StdEncoding.EncodeToString([]byte(credentials))
}
// TODO: finish this function. // TODO: finish this function.
// func updateInverterConfig(db *sql.DB) { // func updateInverterConfig(db *sql.DB) {
// // Periodically query the /api/inverter/list // // Periodically query the /api/inverter/list
@ -526,8 +598,8 @@ func updateEvents(db *sql.DB, inverterSerial string, events *EventsResponse) {
// } // }
// func queryConfigEndpoint() (*InverterSettingsData, error) { // func queryConfigEndpoint() (*InverterSettingsData, error) {
// remoteURL := os.Getenv("REMOTE_URL") // openDTUAddress := os.Getenv("OPENDTU_ADDRESS")
// endpoint := fmt.Sprintf("http://" + remoteURL + "/api/inverter/list") // endpoint := fmt.Sprintf("http://" + openDTUAddress + "/api/inverter/list")
// resp, err := http.Get(endpoint) // resp, err := http.Get(endpoint)
// if err != nil { // if err != nil {

View file

@ -16,7 +16,10 @@ Type=simple
User=opendtu-logger User=opendtu-logger
Group=opendtu-logger Group=opendtu-logger
Environment="REMOTE_URL=opendtu.local:80" Environment="OPENDTU_ADDRESS=opendtu.local:80"
Environment="OPENDTU_AUTH=false"
Environment="OPENDTU_USERNAME=admin"
Environment="OPENDTU_PASSWORD=your_super_secret_password"
Environment="DB_URL=host=localhost port=5432 user=postgres password=secret dbname=dtu sslmode=disable" Environment="DB_URL=host=localhost port=5432 user=postgres password=secret dbname=dtu sslmode=disable"
Environment="TIMESCALEDB_ENABLED=true" Environment="TIMESCALEDB_ENABLED=true"
Environment="TZ=Europe/Amsterdam" Environment="TZ=Europe/Amsterdam"