package monitor import ( "encoding/json" "os" "time" ) // Config holds the daemon configuration parameters. type Config struct { RiemannAddr string `json:"riemann_addr"` RiemannProto string `json:"riemann_proto"` RiemannTimeout time.Duration `json:"riemann_timeout_seconds"` BluetoothInterface string `json:"bluetooth_interface"` SwitchbotMac string `json:"switchbot_mac"` WatchdogTimeout time.Duration `json:"watchdog_timeout_minutes"` ResetCommand string `json:"reset_command"` PublishInterval time.Duration `json:"publish_interval_seconds"` LogLevel string `json:"log_level"` CO2WarnThreshold int `json:"co2_warn_threshold"` CO2CritThreshold int `json:"co2_crit_threshold"` BattWarnThreshold int `json:"batt_warn_threshold"` BattCritThreshold int `json:"batt_crit_threshold"` } // DefaultConfig returns the default configuration. func DefaultConfig() *Config { return &Config{ RiemannAddr: "localhost:5555", RiemannProto: "tcp", RiemannTimeout: 5 * time.Second, BluetoothInterface: "hci0", SwitchbotMac: "", // empty means match any SwitchBot Monitor Pro (CO2) WatchdogTimeout: 5 * time.Minute, ResetCommand: "sudo hciconfig hci0 down && sleep 2 && sudo hciconfig hci0 up", PublishInterval: 60 * time.Second, LogLevel: "info", CO2WarnThreshold: 1000, CO2CritThreshold: 1400, BattWarnThreshold: 20, BattCritThreshold: 10, } } // LoadConfig reads a JSON config file and overrides the defaults. func LoadConfig(path string) (*Config, error) { config := DefaultConfig() // If file doesn't exist, just return defaults if path == "" { return config, nil } if _, err := os.Stat(path); os.IsNotExist(err) { return config, nil } data, err := os.ReadFile(path) if err != nil { return nil, err } // We use a temporary structure to parse durations correctly type configTemp struct { RiemannAddr string `json:"riemann_addr"` RiemannProto string `json:"riemann_proto"` RiemannTimeoutSeconds int `json:"riemann_timeout_seconds"` BluetoothInterface string `json:"bluetooth_interface"` SwitchbotMac string `json:"switchbot_mac"` WatchdogTimeoutMinutes int `json:"watchdog_timeout_minutes"` ResetCommand string `json:"reset_command"` PublishIntervalSeconds int `json:"publish_interval_seconds"` LogLevel string `json:"log_level"` CO2WarnThreshold int `json:"co2_warn_threshold"` CO2CritThreshold int `json:"co2_crit_threshold"` BattWarnThreshold int `json:"batt_warn_threshold"` BattCritThreshold int `json:"batt_crit_threshold"` } var temp configTemp if err := json.Unmarshal(data, &temp); err != nil { return nil, err } if temp.RiemannAddr != "" { config.RiemannAddr = temp.RiemannAddr } if temp.RiemannProto != "" { config.RiemannProto = temp.RiemannProto } if temp.RiemannTimeoutSeconds > 0 { config.RiemannTimeout = time.Duration(temp.RiemannTimeoutSeconds) * time.Second } if temp.BluetoothInterface != "" { config.BluetoothInterface = temp.BluetoothInterface } if temp.SwitchbotMac != "" { config.SwitchbotMac = temp.SwitchbotMac } if temp.WatchdogTimeoutMinutes > 0 { config.WatchdogTimeout = time.Duration(temp.WatchdogTimeoutMinutes) * time.Minute } if temp.ResetCommand != "" { config.ResetCommand = temp.ResetCommand } if temp.PublishIntervalSeconds >= 0 { config.PublishInterval = time.Duration(temp.PublishIntervalSeconds) * time.Second } if temp.LogLevel != "" { config.LogLevel = temp.LogLevel } if temp.CO2WarnThreshold > 0 { config.CO2WarnThreshold = temp.CO2WarnThreshold } if temp.CO2CritThreshold > 0 { config.CO2CritThreshold = temp.CO2CritThreshold } if temp.BattWarnThreshold > 0 { config.BattWarnThreshold = temp.BattWarnThreshold } if temp.BattCritThreshold > 0 { config.BattCritThreshold = temp.BattCritThreshold } return config, nil }