Use custom config type

This commit is contained in:
Ryan McGuire 2025-01-08 16:49:31 -05:00
parent fa0a9f4ddc
commit 86653cf589
5 changed files with 102 additions and 33 deletions

4
go.sum
View File

@ -1,7 +1,3 @@
gitea.libretechconsulting.com/rmcguire/go-app v0.1.3 h1:EwmEJLpN+rQjJ5stGEkZsqEDa5F/YnDAEeqJB9XlFn4=
gitea.libretechconsulting.com/rmcguire/go-app v0.1.3/go.mod h1:wHOWh4O4AMDATQ3WEUYjq5a5bnICPBpu5G6BsNxqN38=
gitea.libretechconsulting.com/rmcguire/go-app v0.2.0 h1:pOm/PysC0IWPuEbmEjNSHHa8Qc5OhuoksYExcuJMFE4=
gitea.libretechconsulting.com/rmcguire/go-app v0.2.0/go.mod h1:wHOWh4O4AMDATQ3WEUYjq5a5bnICPBpu5G6BsNxqN38=
gitea.libretechconsulting.com/rmcguire/go-app v0.3.0 h1:TSR6oEDBX+83975gmgGgU/cTFgfG999+9N/1h4RAXq0= gitea.libretechconsulting.com/rmcguire/go-app v0.3.0 h1:TSR6oEDBX+83975gmgGgU/cTFgfG999+9N/1h4RAXq0=
gitea.libretechconsulting.com/rmcguire/go-app v0.3.0/go.mod h1:wHOWh4O4AMDATQ3WEUYjq5a5bnICPBpu5G6BsNxqN38= gitea.libretechconsulting.com/rmcguire/go-app v0.3.0/go.mod h1:wHOWh4O4AMDATQ3WEUYjq5a5bnICPBpu5G6BsNxqN38=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=

17
main.go
View File

@ -12,15 +12,26 @@ import (
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/ambient" "gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/ambient"
) )
const defaultMetricPrefix = "weather"
func main() { func main() {
ctx, cncl := signal.NotifyContext(context.Background(), os.Kill, unix.SIGTERM) ctx, cncl := signal.NotifyContext(context.Background(), os.Kill, unix.SIGTERM)
defer cncl() defer cncl()
// Config type for app, which implements go-app/config.AppConfig
awConfig := &ambient.AmbientLocalExporterConfig{
MetricPrefix: defaultMetricPrefix,
WeatherStations: make([]ambient.WeatherStation, 0),
}
// Read config and environment, set up logging, load up // Read config and environment, set up logging, load up
// an appCtx, and prepare ambient weather local exporter // an appCtx, and prepare ambient weather local exporter
ctx = app.MustSetupConfigAndLogging(ctx) ctx, awConfig = app.MustLoadConfigInto(ctx, awConfig)
aw := ambient.New(ctx).Init()
// Prepare the exporter
aw := ambient.New(ctx, awConfig).Init()
// Define and prepare the app
awApp := app.App{ awApp := app.App{
AppContext: ctx, AppContext: ctx,
HTTP: &app.AppHTTP{ HTTP: &app.AppHTTP{
@ -43,7 +54,7 @@ func main() {
}, },
} }
// Run and wait
awApp.MustRun() awApp.MustRun()
<-awApp.Done() <-awApp.Done()
} }

View File

@ -24,6 +24,7 @@ type AmbientWeather struct {
// when either "AmbientWeather" or "Wunderground" are selected // when either "AmbientWeather" or "Wunderground" are selected
// in the "Custom" section of the AWNet app, or the web UI // in the "Custom" section of the AWNet app, or the web UI
// of an Ambient WeatherHub // of an Ambient WeatherHub
config *AmbientLocalExporterConfig
awnProvider provider.AmbientProvider awnProvider provider.AmbientProvider
wuProvider provider.AmbientProvider wuProvider provider.AmbientProvider
appCtx context.Context appCtx context.Context
@ -31,7 +32,7 @@ type AmbientWeather struct {
l *zerolog.Logger l *zerolog.Logger
} }
func New(appCtx context.Context) *AmbientWeather { func New(appCtx context.Context, awConfig *AmbientLocalExporterConfig) *AmbientWeather {
return &AmbientWeather{ return &AmbientWeather{
appCtx: appCtx, appCtx: appCtx,
} }

33
pkg/ambient/config.go Normal file
View File

@ -0,0 +1,33 @@
package ambient
import (
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/weather"
)
// This configuration includes all config from go-app/config.AppConfig
type AmbientLocalExporterConfig struct {
MetricPrefix string `yaml:"metricPrefix" default:"weather" env:"AMBIENT_METRIC_PREFIX"`
WeatherStations []WeatherStation `yaml:"weatherStations"` // No env, too complex, not worth the time
*config.AppConfig // Extends app config
}
type WeatherStation struct {
Name string `yaml:"name"` // Human Friendly Name (e.g. Back Yard Weather)
Equipment string `yaml:"equipment"` // Equipment Type (e.g. WS-5000)
// One of these is required, based on the type
// set in the "Custom" weather service on your
// console, weather station, or weather hub
WundergroundID string `yaml:"wundergroundID"`
AWNPassKey string `yaml:"awnPassKey"`
// Proxy updates to AWN or Wunderground
ProxyToAWN bool `yaml:"proxyToAWN"`
ProxyToWunderground bool `yaml:"proxyToWunderground"`
// Unreliable / unwanted metrics by name of WeatherUpdate Field
// These fields are mapped from the provider fields
DiscardMetrics []weather.WeatherUpdateField `yaml:"discardMetrics"`
}

View File

@ -7,31 +7,27 @@ import (
// Stable intermediate struct containing superset of fields // Stable intermediate struct containing superset of fields
// between AWN and Wunderground style updates from Ambient devices // between AWN and Wunderground style updates from Ambient devices
type WeatherUpdate struct { type WeatherUpdate struct {
DateUTC *time.Time DateUTC *time.Time
StationType string StationType string
TempOutdoorF float64 TempOutdoorF float64
TempIndoorF float64 TempIndoorF float64
HumidityOudoor int HumidityOudoor int
HumidityIndoor int HumidityIndoor int
WindSpeedMPH float64 WindSpeedMPH float64
WindGustMPH float64 WindGustMPH float64
MaxDailyGust float64 MaxDailyGust float64
WindDir int WindDir int
WindDirAvg10m int WindDirAvg10m int
UV int UV int
SolarRadiation float64 SolarRadiation float64
HourlyRainIn float64 HourlyRainIn float64
EventRainIn float64 EventRainIn float64
DailyRainIn float64 DailyRainIn float64
WeeklyRainIn float64 WeeklyRainIn float64
MonthlyRainIn float64 MonthlyRainIn float64
YearlyRainIn float64 YearlyRainIn float64
TotalRainIn float64 TotalRainIn float64
Batteries []BatteryStatus Batteries []BatteryStatus
// BattOutdoorSensor int
// BattIndoorSensor int
// BattRainSensor int
// BattCO2Sensor int
BaromRelativeIn float64 BaromRelativeIn float64
BaromAbsoluteIn float64 BaromAbsoluteIn float64
// These fields may be calculated // These fields may be calculated
@ -44,3 +40,35 @@ type BatteryStatus struct {
Component string Component string
Status int Status int
} }
type WeatherUpdateField string
// NOTE: Annoyance to avoid string constant comparisons
// CHORE: Maintain this
const (
FieldDateUTC WeatherUpdateField = "DateUTC"
FieldStationType WeatherUpdateField = "StationType"
FieldTempOutdoorF WeatherUpdateField = "TempOutdoorF"
FieldTempIndoorF WeatherUpdateField = "TempIndoorF"
FieldHumidityOudoor WeatherUpdateField = "HumidityOudoor"
FieldHumidityIndoor WeatherUpdateField = "HumidityIndoor"
FieldWindSpeedMPH WeatherUpdateField = "WindSpeedMPH"
FieldWindGustMPH WeatherUpdateField = "WindGustMPH"
FieldMaxDailyGust WeatherUpdateField = "MaxDailyGust"
FieldWindDir WeatherUpdateField = "WindDir"
FieldWindDirAvg10m WeatherUpdateField = "WindDirAvg10m"
FieldUV WeatherUpdateField = "UV"
FieldSolarRadiation WeatherUpdateField = "SolarRadiation"
FieldHourlyRainIn WeatherUpdateField = "HourlyRainIn"
FieldEventRainIn WeatherUpdateField = "EventRainIn"
FieldDailyRainIn WeatherUpdateField = "DailyRainIn"
FieldWeeklyRainIn WeatherUpdateField = "WeeklyRainIn"
FieldMonthlyRainIn WeatherUpdateField = "MonthlyRainIn"
FieldYearlyRainIn WeatherUpdateField = "YearlyRainIn"
FieldTotalRainIn WeatherUpdateField = "TotalRainIn"
FieldBatteries WeatherUpdateField = "Batteries"
FieldBaromRelativeIn WeatherUpdateField = "BaromRelativeIn"
FieldBaromAbsoluteIn WeatherUpdateField = "BaromAbsoluteIn"
FieldDewPointF WeatherUpdateField = "DewPointF"
FieldWindChillF WeatherUpdateField = "WindChillF"
)