-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconfig.go
More file actions
103 lines (89 loc) · 3.08 KB
/
config.go
File metadata and controls
103 lines (89 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"os"
"strconv"
"strings"
"time"
)
// Version is set at build time via -ldflags.
var Version = "dev"
// Config holds all configuration for the application.
type Config struct {
MulticastAddress string
Interface string // bind to specific NIC (e.g. "eth0")
DBusName string
SMASusyID uint32
IsEnergyMeter bool
LogLevel string
DiagnoseMode bool
StaleTimeout time.Duration
ShowVersion bool
}
// ParseConfig parses CLI flags with env-var fallbacks for backward compatibility.
func ParseConfig() Config {
var cfg Config
var susyID uint64
var staleSeconds int
flag.StringVar(&cfg.MulticastAddress, "multicast-address", envOrDefault("MULTICAST_ADDRESS", "239.12.255.254:9522"), "SMA multicast address:port")
flag.StringVar(&cfg.Interface, "interface", envOrDefault("INTERFACE", ""), "network interface to bind (e.g. eth0)")
flag.StringVar(&cfg.DBusName, "dbus-name", envOrDefault("DBUS_NAME", "com.victronenergy.grid.cgwacs_ttyUSB0_di30_mb1"), "D-Bus service name")
flag.StringVar(&cfg.LogLevel, "log-level", envOrDefault("LOG_LEVEL", "info"), "log level (debug, info, warn, error)")
flag.BoolVar(&cfg.IsEnergyMeter, "energy-meter", envBoolOrDefault("SMA_ENERGY_METER", false), "SMA Energy Meter 1.0 mode (vs SHM 2.0)")
flag.BoolVar(&cfg.DiagnoseMode, "diagnose", false, "run diagnostic checks and exit")
flag.BoolVar(&cfg.ShowVersion, "version", false, "print version and exit")
flag.IntVar(&staleSeconds, "stale-timeout", envIntOrDefault("STALE_TIMEOUT", 30), "seconds without data before marking stale")
susyDefault := envOrDefault("SMASUSYID", "0")
flag.Uint64Var(&susyID, "susy-id", parseUint64(susyDefault), "SMA SUSy ID filter (0 = accept all)")
flag.Parse()
cfg.SMASusyID = uint32(susyID)
cfg.StaleTimeout = time.Duration(staleSeconds) * time.Second
return cfg
}
func envOrDefault(key, fallback string) string {
if v, ok := os.LookupEnv(key); ok {
return v
}
return fallback
}
func envBoolOrDefault(key string, fallback bool) bool {
v, ok := os.LookupEnv(key)
if !ok {
return fallback
}
switch strings.ToLower(v) {
case "true", "1", "yes":
return true
default:
return false
}
}
func envIntOrDefault(key string, fallback int) int {
v, ok := os.LookupEnv(key)
if !ok {
return fallback
}
n, err := strconv.Atoi(v)
if err != nil {
return fallback
}
return n
}
func parseUint64(s string) uint64 {
n, _ := strconv.ParseUint(s, 10, 64)
return n
}