2020-04-17 19:39:01 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-04-17 19:39:06 +02:00
|
|
|
"go.bug.st/serial"
|
2020-04-17 23:01:00 +02:00
|
|
|
"gopkg.in/yaml.v2"
|
2020-04-17 19:39:06 +02:00
|
|
|
"io/ioutil"
|
2020-04-17 19:39:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ClientConfig struct {
|
2020-04-17 19:39:06 +02:00
|
|
|
SerialAddress string `yaml:"serial_address"`
|
|
|
|
BaudRate int `yaml:"serial_baudrate"`
|
2020-04-20 22:01:06 +02:00
|
|
|
BufferSize uint16 `yaml:"serial_buffer_size"`
|
2020-04-17 19:39:06 +02:00
|
|
|
Paranoid bool `yaml:"paranoid"`
|
|
|
|
Debug bool `yaml:"debug"`
|
|
|
|
GNUPGBinary string `yaml:"gnupg_bin"`
|
|
|
|
OpenSSLBinary string `yaml:"openssl_bin"`
|
|
|
|
MySQLDSN string `yaml:"mysql_dsn"`
|
2020-04-17 19:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var defaultConfig = ClientConfig{
|
2020-04-17 19:39:06 +02:00
|
|
|
SerialAddress: "/dev/ttyUSB0",
|
|
|
|
BaudRate: 115200,
|
2020-04-20 22:01:06 +02:00
|
|
|
BufferSize: 2048,
|
2020-04-17 19:39:01 +02:00
|
|
|
Paranoid: false,
|
|
|
|
Debug: false,
|
|
|
|
OpenSSLBinary: "/usr/bin/openssl",
|
|
|
|
GNUPGBinary: "/usr/bin/gpg",
|
|
|
|
MySQLDSN: "<username>:<password>@/database?parseTime=true",
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateExampleConfig(configFile string) (config *ClientConfig, err error) {
|
|
|
|
config = &defaultConfig
|
|
|
|
configBytes, err := yaml.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("could not generate configuration data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("example data for %s:\n\n---\n%s\n", configFile, configBytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func readConfig(configFile string) (config *ClientConfig, err error) {
|
|
|
|
source, err := ioutil.ReadFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("opening configuration file failed: %v", err)
|
|
|
|
if exampleConfig, err := generateExampleConfig(configFile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
logrus.Info("starting with default config")
|
|
|
|
return exampleConfig, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := yaml.Unmarshal(source, &config); err != nil {
|
|
|
|
return nil, fmt.Errorf("loading configuration file failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:39:06 +02:00
|
|
|
func fillSerialMode(clientConfig *ClientConfig) *serial.Mode {
|
|
|
|
return &serial.Mode{
|
|
|
|
BaudRate: clientConfig.BaudRate,
|
|
|
|
DataBits: 8,
|
|
|
|
StopBits: serial.OneStopBit,
|
|
|
|
Parity: serial.NoParity,
|
2020-04-17 19:39:01 +02:00
|
|
|
}
|
|
|
|
}
|