generated from rmcguire/go-server-with-otel
implement econet exporter
This commit is contained in:
+36
-11
@@ -5,12 +5,30 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/caarlos0/env/v11"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
||||
)
|
||||
|
||||
// DefaultUsageInterval is used when UsageInterval is unset. Rheem's
|
||||
// energy/water history only updates hourly, so polling faster is wasteful.
|
||||
const DefaultUsageInterval = 5 * time.Minute
|
||||
|
||||
type ServiceConfig struct {
|
||||
Timezone string `yaml:"timezone" json:"timezone,omitempty" default:"UTC"`
|
||||
Opts *DemoOpts `yaml:"opts" json:"opts,omitempty"`
|
||||
// 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"`
|
||||
|
||||
// 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"`
|
||||
|
||||
// UsageInterval controls how often energy/water usage history is polled.
|
||||
UsageInterval time.Duration `yaml:"usageInterval" json:"usageInterval,omitempty" env:"ECONET_USAGE_INTERVAL"`
|
||||
|
||||
// Embeds go-app config, used by go-app to
|
||||
// merge custom config into go-app config, and to produce
|
||||
@@ -18,14 +36,21 @@ type ServiceConfig struct {
|
||||
*config.AppConfig
|
||||
}
|
||||
|
||||
type DemoOpts struct {
|
||||
FactLang string `yaml:"factLang" json:"factLang,omitempty" default:"en"`
|
||||
FactType FactType `yaml:"factType" json:"factType,omitempty" default:"random" enum:"today,random"`
|
||||
// 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)
|
||||
}
|
||||
|
||||
type FactType string
|
||||
|
||||
const (
|
||||
TypeToday FactType = "today"
|
||||
TypeRandom FactType = "random"
|
||||
)
|
||||
// GetUsageInterval returns the configured usage poll interval or the default.
|
||||
func (c *ServiceConfig) GetUsageInterval() time.Duration {
|
||||
if c.UsageInterval <= 0 {
|
||||
return DefaultUsageInterval
|
||||
}
|
||||
return c.UsageInterval
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user