95 lines
1.9 KiB
Go
95 lines
1.9 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"th7/data/config"
|
|
"th7/data/thermocouple"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var (
|
|
ErrConfigBadThermocoupleType = errors.New("unrecognised thermocouple type")
|
|
)
|
|
|
|
var (
|
|
thermocoupleTypes = map[string]thermocouple.Type{
|
|
"B": thermocouple.B,
|
|
"E": thermocouple.E,
|
|
"J": thermocouple.J,
|
|
"K": thermocouple.K,
|
|
"N": thermocouple.N,
|
|
"R": thermocouple.R,
|
|
"S": thermocouple.S,
|
|
"T": thermocouple.T,
|
|
"U": thermocouple.None, // for uV measurements
|
|
}
|
|
)
|
|
|
|
func getThermocoupleType(t string) (thermocouple.Type, error) {
|
|
t = strings.ToUpper(t)
|
|
if val, ok := thermocoupleTypes[t]; ok {
|
|
return val, nil
|
|
} else {
|
|
return thermocouple.None, ErrConfigBadThermocoupleType
|
|
}
|
|
}
|
|
|
|
func Load() (config.Config, error) {
|
|
|
|
var cfg config.Config
|
|
var err error
|
|
|
|
v := viper.New()
|
|
v.AddConfigPath(".")
|
|
v.SetConfigFile("config.toml")
|
|
|
|
err = v.ReadInConfig()
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
|
|
SetDefaultConfig(v)
|
|
|
|
cfg.Board.Port = v.GetInt("TH7.port")
|
|
cfg.Board.Cache = v.GetBool("TH7.cache")
|
|
cfg.Board.Led = v.GetBool("TH7.LED")
|
|
cfg.Board.Logfreq = v.GetInt("DB.freq")
|
|
cfg.Board.Debug = v.GetBool("TH7.debug")
|
|
cfg.Board.NoLogging = v.GetBool("TH7.nolog")
|
|
|
|
cfg.Channels = make([]config.Channel, 0)
|
|
|
|
for i := 1; i < 8; i++ {
|
|
var c config.Channel
|
|
var tc thermocouple.Type
|
|
var head = fmt.Sprintf("Channel_%d", i)
|
|
|
|
if v.GetString(head+".type") == "NOTSET" {
|
|
continue
|
|
}
|
|
|
|
c.Id = i
|
|
c.Gain = v.GetFloat64(head + ".gain")
|
|
c.Offset = v.GetFloat64(head + ".offset")
|
|
|
|
tc, err := getThermocoupleType(v.GetString(head + ".type"))
|
|
if err != nil {
|
|
fmt.Printf("%s.type=%s\n", head, v.GetString(head+".type"))
|
|
return cfg, err
|
|
}
|
|
|
|
c.Thermo = tc
|
|
c.Filter.SampleSize = v.GetInt(head + ".filter.samples")
|
|
c.Filter.Type = v.GetInt(head + ".filter.type")
|
|
|
|
cfg.Channels = append(cfg.Channels, c)
|
|
}
|
|
|
|
cfg.DB = v.GetStringMap("DB")
|
|
|
|
return cfg, nil
|
|
}
|