132 lines
4.6 KiB
Go
132 lines
4.6 KiB
Go
package model
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/pelletier/go-toml"
|
|
)
|
|
|
|
const (
|
|
defConfigFile = "/usr/local/gateway/src/config.toml"
|
|
// defConfigFile = "config.toml"
|
|
)
|
|
|
|
type MQTTConfig struct {
|
|
Broker string `toml:"broker"`
|
|
Port int `toml:"port"`
|
|
Client_id string `toml:"client_id"`
|
|
Username string `toml:"username"`
|
|
Password string `toml:"password"`
|
|
Keepalive int `toml:"keepalive"`
|
|
Source string `toml:"source"`
|
|
Device_id string `toml:"device_id"`
|
|
Sample_rate_strategy string `toml:"sample_rate_strategy"`
|
|
Long_rate_strategy int `toml:"long_rate_strategy"`
|
|
Windfarm string `toml:"windfarm"`
|
|
Fansnum string `toml:"fansnum"`
|
|
|
|
Ch0_measurpoint string `toml:"ch0_measurpoint"`
|
|
Ch0_measurpointdirection string `toml:"ch0_measurpointdirection"`
|
|
Ch0_sensorparameters float32 `toml:"ch0_sensorparameters"`
|
|
Ch0_samplingtime int `toml:"ch0_samplingtime"`
|
|
Ch0_samplingfrequency int `toml:"ch0_samplingfrequency"`
|
|
|
|
Ch1_measurpoint string `toml:"ch1_measurpoint"`
|
|
Ch1_measurpointdirection string `toml:"ch1_measurpointdirection"`
|
|
Ch1_sensorparameters float32 `toml:"ch1_sensorparameters"`
|
|
Ch1_samplingtime int `toml:"ch1_samplingtime"`
|
|
Ch1_samplingfrequency int `toml:"ch1_samplingfrequency"`
|
|
|
|
Ch2_measurpoint string `toml:"ch2_measurpoint"`
|
|
Ch2_measurpointdirection string `toml:"ch2_measurpointdirection"`
|
|
Ch2_sensorparameters float32 `toml:"ch2_sensorparameters"`
|
|
Ch2_samplingtime int `toml:"ch2_samplingtime"`
|
|
Ch2_samplingfrequency int `toml:"ch2_samplingfrequency"`
|
|
|
|
Ch3_measurpoint string `toml:"ch3_measurpoint"`
|
|
Ch3_measurpointdirection string `toml:"ch3_measurpointdirection"`
|
|
Ch3_sensorparameters float32 `toml:"ch3_sensorparameters"`
|
|
Ch3_samplingtime int `toml:"ch3_samplingtime"`
|
|
Ch3_samplingfrequency int `toml:"ch3_samplingfrequency"`
|
|
|
|
Ch4_measurpoint string `toml:"ch4_measurpoint"`
|
|
Ch4_measurpointdirection string `toml:"ch4_measurpointdirection"`
|
|
Ch4_sensorparameters float32 `toml:"ch4_sensorparameters"`
|
|
Ch4_samplingtime int `toml:"ch4_samplingtime"`
|
|
Ch4_samplingfrequency int `toml:"ch4_samplingfrequency"`
|
|
|
|
Ch5_measurpoint string `toml:"ch5_measurpoint"`
|
|
Ch5_measurpointdirection string `toml:"ch5_measurpointdirection"`
|
|
Ch5_sensorparameters float32 `toml:"ch5_sensorparameters"`
|
|
Ch5_samplingtime int `toml:"ch5_samplingtime"`
|
|
Ch5_samplingfrequency int `toml:"ch5_samplingfrequency"`
|
|
|
|
Ch6_measurpoint string `toml:"ch6_measurpoint"`
|
|
Ch6_measurpointdirection string `toml:"ch6_measurpointdirection"`
|
|
Ch6_sensorparameters float32 `toml:"ch6_sensorparameters"`
|
|
Ch6_samplingtime int `toml:"ch6_samplingtime"`
|
|
Ch6_samplingfrequency int `toml:"ch6_samplingfrequency"`
|
|
|
|
Ch7_measurpoint string `toml:"ch7_measurpoint"`
|
|
Ch7_measurpointdirection string `toml:"ch7_measurpointdirection"`
|
|
Ch7_sensorparameters float32 `toml:"ch7_sensorparameters"`
|
|
Ch7_samplingtime int `toml:"ch7_samplingtime"`
|
|
Ch7_samplingfrequency int `toml:"ch7_samplingfrequency"`
|
|
|
|
Ch8_measurpoint string `toml:"ch8_measurpoint"`
|
|
Ch8_measurpointdirection string `toml:"ch8_measurpointdirection"`
|
|
Ch8_sensorparameters float32 `toml:"ch8_sensorparameters"`
|
|
Ch8_samplingtime int `toml:"ch8_samplingtime"`
|
|
Ch8_samplingfrequency int `toml:"ch8_samplingfrequency"`
|
|
}
|
|
|
|
type USBConfig struct {
|
|
Vendor int `toml:"vendor"`
|
|
Product int `toml:"product"`
|
|
Sample_rate int `toml:"sample_rate"`
|
|
Sample_time int `toml:"sample_time"`
|
|
Dbpath string `toml:"dbpath"`
|
|
}
|
|
|
|
type SysConfig struct {
|
|
Logpath string `toml:"logpath"`
|
|
Pidpath string `toml:"pidpath"`
|
|
Ntp string `toml:"ntp"`
|
|
Secret string `toml:"secret"`
|
|
}
|
|
|
|
type Config struct {
|
|
MQTT MQTTConfig `toml:"mqtt"`
|
|
USB USBConfig `toml:"usb"`
|
|
Sys SysConfig `toml:"sys"`
|
|
}
|
|
|
|
func (c *Config) Read() (err error) {
|
|
data, err := os.ReadFile(defConfigFile)
|
|
if err != nil {
|
|
err = fmt.Errorf("error reading config file: %s", err)
|
|
return
|
|
}
|
|
|
|
if err = toml.Unmarshal(data, c); err != nil {
|
|
err = fmt.Errorf("failed to unmarshal config file: %s", err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c *Config) Write() (err error) {
|
|
data, err := toml.Marshal(c)
|
|
if err != nil {
|
|
err = fmt.Errorf("failed to marshal config file: %s", err)
|
|
return
|
|
}
|
|
|
|
if err = os.WriteFile(defConfigFile, data, os.FileMode(0644)); err != nil {
|
|
err = fmt.Errorf("error writing config file: %s", err)
|
|
return
|
|
}
|
|
return
|
|
}
|