83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
// Package config verwaltet das Laden und Parsen der TOML-Konfigurationsdateien
|
|
// für den Server-Daemon und alle angeschlossenen GUI-Clients.
|
|
package config
|
|
|
|
import (
|
|
"os"
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
// ADS1115Module definiert die Hardware-Parameter und Kanal-Labels einer ADC-Platine.
|
|
type ADS1115Module struct {
|
|
Address string `toml:"address"`
|
|
Labels []string `toml:"labels"` // Genau 4 Beschriftungen für die Kanäle A0 bis A3
|
|
}
|
|
|
|
// ServerSettings definiert die Netzwerkparameter des Server-Daemons.
|
|
type ServerSettings struct {
|
|
ListenAddress string `toml:"listen_address"`
|
|
Modules []ADS1115Module `toml:"modules"` // Unterstützt dynamisch 1 bis 4 Platinen
|
|
}
|
|
|
|
// ServerConfig bündelt alle Konfigurationseinstellungen des Servers.
|
|
type ServerConfig struct {
|
|
Server ServerSettings `toml:"server"`
|
|
}
|
|
|
|
// ClientSettings definiert die Verbindungsparameter für die Fyne-GUIs.
|
|
type ClientSettings struct {
|
|
ServerAddress string `toml:"server_address"`
|
|
StationName string `toml:"station_name"`
|
|
RefreshRateMs int `toml:"refresh_rate_ms"`
|
|
}
|
|
|
|
// ClientConfig bündelt alle Konfigurationseinstellungen des GUI-Clients.
|
|
type ClientConfig struct {
|
|
Client ClientSettings `toml:"client"`
|
|
}
|
|
|
|
// LoadServerConfig lädt die Konfiguration für den Server-Daemon aus einer TOML-Datei.
|
|
func LoadServerConfig(path string) (*ServerConfig, error) {
|
|
cfg := &ServerConfig{
|
|
Server: ServerSettings{
|
|
ListenAddress: "0.0.0.0:50051",
|
|
Modules: []ADS1115Module{
|
|
{
|
|
Address: "0x48",
|
|
Labels: []string{"Kanal 1", "Kanal 2", "Kanal 3", "Kanal 4"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
return cfg, nil
|
|
}
|
|
|
|
if _, err := toml.DecodeFile(path, cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
// LoadClientConfig lädt die Konfiguration für die GUI-Clients aus einer TOML-Datei.
|
|
func LoadClientConfig(path string) (*ClientConfig, error) {
|
|
cfg := &ClientConfig{
|
|
Client: ClientSettings{
|
|
ServerAddress: "127.0.0.1:50051",
|
|
StationName: "DL-UNKNOWN",
|
|
RefreshRateMs: 250,
|
|
},
|
|
}
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
return cfg, nil
|
|
}
|
|
|
|
if _, err := toml.DecodeFile(path, cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|