2022-11-12 14:20:29 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-11-16 00:07:01 +00:00
|
|
|
"strings"
|
2022-11-12 14:20:29 +00:00
|
|
|
"th7/data/config"
|
|
|
|
"th7/data/thermocouple"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrConfigBadThermocoupleType = errors.New("unrecognised thermocouple type")
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-11-23 20:46:54 +00:00
|
|
|
thermocoupleTypes = map[string]thermocouple.Type{
|
2023-11-21 16:19:20 +00:00
|
|
|
"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
|
2022-11-12 14:20:29 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func getThermocoupleType(t string) (thermocouple.Type, error) {
|
2022-11-16 00:07:01 +00:00
|
|
|
t = strings.ToUpper(t)
|
2023-11-23 20:46:54 +00:00
|
|
|
if val, ok := thermocoupleTypes[t]; ok {
|
2022-11-12 14:20:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-23 11:32:57 +00:00
|
|
|
SetDefaultConfig(v)
|
2022-11-12 14:20:29 +00:00
|
|
|
|
|
|
|
cfg.Board.Port = v.GetInt("TH7.port")
|
|
|
|
cfg.Board.Cache = v.GetBool("TH7.cache")
|
|
|
|
cfg.Board.Led = v.GetBool("TH7.LED")
|
2023-11-21 14:45:14 +00:00
|
|
|
cfg.Board.Logfreq = v.GetInt("DB.freq")
|
2022-11-12 16:22:55 +00:00
|
|
|
cfg.Board.Debug = v.GetBool("TH7.debug")
|
2022-11-16 00:07:01 +00:00
|
|
|
cfg.Board.NoLogging = v.GetBool("TH7.nolog")
|
2023-11-25 10:37:02 +00:00
|
|
|
cfg.Board.NoWeb = v.GetBool("TH7.noweb")
|
2022-11-12 14:20:29 +00:00
|
|
|
|
|
|
|
cfg.Channels = make([]config.Channel, 0)
|
|
|
|
|
|
|
|
for i := 1; i < 8; i++ {
|
2022-11-16 00:07:01 +00:00
|
|
|
var c config.Channel
|
|
|
|
var tc thermocouple.Type
|
2022-11-12 14:20:29 +00:00
|
|
|
var head = fmt.Sprintf("Channel_%d", i)
|
|
|
|
|
2022-11-16 00:07:01 +00:00
|
|
|
if v.GetString(head+".type") == "NOTSET" {
|
2022-11-12 14:20:29 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Id = i
|
|
|
|
c.Gain = v.GetFloat64(head + ".gain")
|
|
|
|
c.Offset = v.GetFloat64(head + ".offset")
|
|
|
|
|
2022-11-16 00:07:01 +00:00
|
|
|
tc, err := getThermocoupleType(v.GetString(head + ".type"))
|
2022-11-12 14:20:29 +00:00
|
|
|
if err != nil {
|
2023-11-23 16:55:57 +00:00
|
|
|
fmt.Printf("%s.type=%s\n", head, v.GetString(head+".type"))
|
2022-11-12 14:20:29 +00:00
|
|
|
return cfg, err
|
|
|
|
}
|
2023-11-21 14:57:53 +00:00
|
|
|
|
2023-11-23 11:32:57 +00:00
|
|
|
c.Thermo = tc
|
2023-11-25 10:37:02 +00:00
|
|
|
c.Log = v.GetBool(head + ".log")
|
2023-11-21 14:57:53 +00:00
|
|
|
c.Filter.SampleSize = v.GetInt(head + ".filter.samples")
|
2023-11-21 16:19:20 +00:00
|
|
|
c.Filter.Type = v.GetInt(head + ".filter.type")
|
2023-11-21 14:57:53 +00:00
|
|
|
|
2022-11-12 14:20:29 +00:00
|
|
|
cfg.Channels = append(cfg.Channels, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.DB = v.GetStringMap("DB")
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|