generated from rmcguire/go-server-with-otel
61 lines
2.5 KiB
Go
61 lines
2.5 KiB
Go
// Package config contains ServiceConfig, which is intended
|
|
// to extend go-app/pkg/config.AppConfig. Add any custom fields
|
|
// here, and it will automatically be handled by go-app, including
|
|
// overrides by environment, and json schema generation
|
|
package config
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/caarlos0/env/v11"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
|
)
|
|
|
|
// DefaultPollInterval is used when PollInterval is unset. Each tick re-fetches
|
|
// device state and daily energy/water usage over REST.
|
|
const DefaultPollInterval = 1 * time.Minute
|
|
|
|
type ServiceConfig struct {
|
|
// Rheem EcoNet cloud credentials. Prefer supplying the password via
|
|
// the ECONET_PASSWORD environment variable rather than config.yaml.
|
|
EconetEmail string `yaml:"econetEmail" json:"econetEmail,omitempty" env:"ECONET_EMAIL"`
|
|
EconetPassword string `yaml:"econetPassword" json:"econetPassword,omitempty" env:"ECONET_PASSWORD"`
|
|
|
|
// Necessary due to ancient TLS certificate run by cloudblade that isn't trusted in modern ca-certificate bundles
|
|
EconetTLSInsecure bool `yaml:"econetTLSInsecure" json:"econetTLSInsecure,omitempty" env:"ECONET_TLS_INSECURE" default:"false"`
|
|
|
|
// CostPerKWH is the electricity price in US dollars per kWh (e.g. 0.18
|
|
// means 18 cents/kWh, NOT 18). It derives the econet_energy_cost_dollars
|
|
// metric from kWh energy usage.
|
|
CostPerKWH float64 `yaml:"costPerKWH" json:"costPerKWH,omitempty" env:"ECONET_COST_PER_KWH" default:"0.19"`
|
|
|
|
// PollInterval controls how often device state and daily energy/water
|
|
// usage are re-fetched over REST.
|
|
PollInterval time.Duration `yaml:"pollInterval" json:"pollInterval,omitempty" env:"ECONET_POLL_INTERVAL"`
|
|
|
|
// Embeds go-app config, used by go-app to
|
|
// merge custom config into go-app config, and to produce
|
|
// a complete configuration json schema
|
|
*config.AppConfig
|
|
}
|
|
|
|
// LoadEnv applies environment-variable overrides to the custom ServiceConfig
|
|
// fields. go-app's MustLoadConfigInto only handles env for the embedded
|
|
// AppConfig, so the caller must apply env for custom fields. The embedded
|
|
// AppConfig is skipped here to preserve go-app's own env/yaml precedence.
|
|
func (c *ServiceConfig) LoadEnv() error {
|
|
appConfig := c.AppConfig
|
|
c.AppConfig = nil
|
|
defer func() { c.AppConfig = appConfig }()
|
|
return env.Parse(c)
|
|
}
|
|
|
|
// GetPollInterval returns the configured poll interval or the default.
|
|
func (c *ServiceConfig) GetPollInterval() time.Duration {
|
|
if c.PollInterval <= 0 {
|
|
return DefaultPollInterval
|
|
}
|
|
return c.PollInterval
|
|
}
|