Weather service proxy support
All checks were successful
Build and Publish / release (push) Successful in 3m51s
All checks were successful
Build and Publish / release (push) Successful in 3m51s
This commit is contained in:
@ -4,11 +4,13 @@ import (
|
||||
"math"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/ambient/config"
|
||||
)
|
||||
|
||||
// Attempts to complete missing fields that may not
|
||||
// be set by a specific provider, such as DewPoint and WindChill
|
||||
func (u *WeatherUpdate) Enrich() {
|
||||
func (u *WeatherUpdate) Enrich(weatherStations ...*config.WeatherStation) {
|
||||
if u == nil {
|
||||
return
|
||||
}
|
||||
|
@ -119,46 +119,46 @@ func (wm *WeatherMetrics) Update(u *WeatherUpdate) {
|
||||
attributes = append(attributes,
|
||||
attribute.String("station_type", *u.StationType))
|
||||
}
|
||||
if u.StationInfo != nil {
|
||||
if u.StationInfo.Name != nil {
|
||||
if u.StationConfig != nil {
|
||||
if u.StationConfig.Name != "" {
|
||||
attributes = append(attributes,
|
||||
attribute.String("station_name", *u.StationInfo.Name))
|
||||
attribute.String("station_name", u.StationConfig.Name))
|
||||
}
|
||||
if u.StationInfo.Equipment != nil {
|
||||
if u.StationConfig.Equipment != "" {
|
||||
attributes = append(attributes,
|
||||
attribute.String("station_equipment", *u.StationInfo.Equipment))
|
||||
attribute.String("station_equipment", u.StationConfig.Equipment))
|
||||
}
|
||||
}
|
||||
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TempOutdoorF, FloatVal: u.TempOutdoorF, Field: FieldTempOutdoorF, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TempIndoorF, FloatVal: u.TempIndoorF, Field: FieldTempIndoorF, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.HumidityOudoor, IntVal: u.HumidityOudoor, Field: FieldHumidityOudoor, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.HumidityIndoor, IntVal: u.HumidityIndoor, Field: FieldHumidityIndoor, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WindSpeedMPH, FloatVal: u.WindSpeedMPH, Field: FieldWindSpeedMPH, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WindGustMPH, FloatVal: u.WindGustMPH, Field: FieldWindGustMPH, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.MaxDailyGust, FloatVal: u.MaxDailyGust, Field: FieldMaxDailyGust, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.WindDir, IntVal: u.WindDir, Field: FieldWindDir, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.WindDirAvg10m, IntVal: u.WindDirAvg10m, Field: FieldWindDirAvg10m, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.UV, IntVal: u.UV, Field: FieldUV, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.SolarRadiation, FloatVal: u.SolarRadiation, Field: FieldSolarRadiation, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.HourlyRainIn, FloatVal: u.HourlyRainIn, Field: FieldHourlyRainIn, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.EventRainIn, FloatVal: u.EventRainIn, Field: FieldEventRainIn, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.DailyRainIn, FloatVal: u.DailyRainIn, Field: FieldDailyRainIn, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WeeklyRainIn, FloatVal: u.WeeklyRainIn, Field: FieldWeeklyRainIn, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.MonthlyRainIn, FloatVal: u.MonthlyRainIn, Field: FieldMonthlyRainIn, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.YearlyRainIn, FloatVal: u.YearlyRainIn, Field: FieldYearlyRainIn, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TotalRainIn, FloatVal: u.TotalRainIn, Field: FieldTotalRainIn, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.BaromRelativeIn, FloatVal: u.BaromRelativeIn, Field: FieldBaromRelativeIn, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.BaromAbsoluteIn, FloatVal: u.BaromAbsoluteIn, Field: FieldBaromAbsoluteIn, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.DewPointF, FloatVal: u.DewPointF, Field: FieldDewPointF, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WindChillF, FloatVal: u.WindChillF, Field: FieldWindChillF, Attributes: attributes, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TempOutdoorF, FloatVal: u.TempOutdoorF, Field: FieldTempOutdoorF, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TempIndoorF, FloatVal: u.TempIndoorF, Field: FieldTempIndoorF, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.HumidityOudoor, IntVal: u.HumidityOudoor, Field: FieldHumidityOudoor, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.HumidityIndoor, IntVal: u.HumidityIndoor, Field: FieldHumidityIndoor, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WindSpeedMPH, FloatVal: u.WindSpeedMPH, Field: FieldWindSpeedMPH, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WindGustMPH, FloatVal: u.WindGustMPH, Field: FieldWindGustMPH, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.MaxDailyGust, FloatVal: u.MaxDailyGust, Field: FieldMaxDailyGust, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.WindDir, IntVal: u.WindDir, Field: FieldWindDir, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.WindDirAvg10m, IntVal: u.WindDirAvg10m, Field: FieldWindDirAvg10m, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.UV, IntVal: u.UV, Field: FieldUV, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.SolarRadiation, FloatVal: u.SolarRadiation, Field: FieldSolarRadiation, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.HourlyRainIn, FloatVal: u.HourlyRainIn, Field: FieldHourlyRainIn, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.EventRainIn, FloatVal: u.EventRainIn, Field: FieldEventRainIn, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.DailyRainIn, FloatVal: u.DailyRainIn, Field: FieldDailyRainIn, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WeeklyRainIn, FloatVal: u.WeeklyRainIn, Field: FieldWeeklyRainIn, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.MonthlyRainIn, FloatVal: u.MonthlyRainIn, Field: FieldMonthlyRainIn, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.YearlyRainIn, FloatVal: u.YearlyRainIn, Field: FieldYearlyRainIn, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TotalRainIn, FloatVal: u.TotalRainIn, Field: FieldTotalRainIn, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.BaromRelativeIn, FloatVal: u.BaromRelativeIn, Field: FieldBaromRelativeIn, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.BaromAbsoluteIn, FloatVal: u.BaromAbsoluteIn, Field: FieldBaromAbsoluteIn, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.DewPointF, FloatVal: u.DewPointF, Field: FieldDewPointF, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WindChillF, FloatVal: u.WindChillF, Field: FieldWindChillF, Attributes: attributes, Station: u.StationConfig})
|
||||
|
||||
// Batteries
|
||||
for _, battery := range u.Batteries {
|
||||
batAttr := attributes
|
||||
batAttr = append(batAttr, attribute.String("component", battery.Component))
|
||||
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.BatteryStatus, IntVal: battery.Status, Field: FieldBatteries, Attributes: batAttr, StationInfo: u.StationInfo})
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.BatteryStatus, IntVal: battery.Status, Field: FieldBatteries, Attributes: batAttr, Station: u.StationConfig})
|
||||
}
|
||||
|
||||
wm.UpdatesReceived.Add(wm.appCtx, 1)
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/ambient/config"
|
||||
)
|
||||
|
||||
type MetricRecorder struct {
|
||||
@ -20,15 +22,15 @@ type RecordOpts struct {
|
||||
IntVal *int
|
||||
FloatVal *float64
|
||||
Attributes []attribute.KeyValue
|
||||
Field WeatherUpdateField
|
||||
StationInfo *StationInfo
|
||||
Field string
|
||||
Station *config.WeatherStation
|
||||
}
|
||||
|
||||
func (r *MetricRecorder) Record(opts *RecordOpts) {
|
||||
if opts.StationInfo != nil && !opts.keep() {
|
||||
if opts.Station != nil && !opts.keep() {
|
||||
r.l.Trace().
|
||||
Str("field", string(opts.Field)).
|
||||
Str("station", *opts.StationInfo.Name).
|
||||
Str("station", opts.Station.Name).
|
||||
Msg("Metric dropped by station config")
|
||||
return
|
||||
} else if opts.Int64Gauge == nil && opts.Float64Gauge == nil {
|
||||
@ -39,8 +41,8 @@ func (r *MetricRecorder) Record(opts *RecordOpts) {
|
||||
if opts.Int64Gauge != nil {
|
||||
if opts.IntVal == nil {
|
||||
log := r.l.Trace().Str("field", string(opts.Field))
|
||||
if opts.StationInfo != nil {
|
||||
log = log.Str("station", *opts.StationInfo.Name)
|
||||
if opts.Station != nil {
|
||||
log = log.Str("station", opts.Station.Name)
|
||||
}
|
||||
log.Msg("Dropping nil int metric")
|
||||
return
|
||||
@ -49,8 +51,8 @@ func (r *MetricRecorder) Record(opts *RecordOpts) {
|
||||
} else if opts.Float64Gauge != nil {
|
||||
if opts.FloatVal == nil {
|
||||
log := r.l.Trace().Str("field", string(opts.Field))
|
||||
if opts.StationInfo != nil {
|
||||
log = log.Str("station", *opts.StationInfo.Name)
|
||||
if opts.Station != nil {
|
||||
log = log.Str("station", opts.Station.Name)
|
||||
}
|
||||
log.Msg("Dropping nil float metric")
|
||||
return
|
||||
@ -61,8 +63,8 @@ func (r *MetricRecorder) Record(opts *RecordOpts) {
|
||||
|
||||
func (o *RecordOpts) keep() bool {
|
||||
// If keep fields are given, only check keep fields
|
||||
if len(o.StationInfo.Keep) > 0 {
|
||||
for _, f := range o.StationInfo.Keep {
|
||||
if len(o.Station.KeepMetrics) > 0 {
|
||||
for _, f := range o.Station.KeepMetrics {
|
||||
if f == o.Field {
|
||||
return true
|
||||
}
|
||||
@ -70,7 +72,7 @@ func (o *RecordOpts) keep() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, f := range o.StationInfo.Drop {
|
||||
for _, f := range o.Station.DropMetrics {
|
||||
if f == o.Field {
|
||||
return false
|
||||
}
|
||||
|
@ -2,13 +2,15 @@ package weather
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/ambient/config"
|
||||
)
|
||||
|
||||
// Stable intermediate struct containing superset of fields
|
||||
// between AWN and Wunderground style updates from Ambient devices
|
||||
type WeatherUpdate struct {
|
||||
DateUTC *time.Time
|
||||
StationInfo *StationInfo
|
||||
StationConfig *config.WeatherStation
|
||||
StationID *string
|
||||
StationType *string
|
||||
TempOutdoorF *float64
|
||||
@ -38,15 +40,6 @@ type WeatherUpdate struct {
|
||||
WindChillF *float64
|
||||
// First URL parameters given to AWN/Wunderground
|
||||
// if proxying is enabled
|
||||
WeatherServiceCredentials map[string]string
|
||||
}
|
||||
|
||||
type StationInfo struct {
|
||||
Type *string
|
||||
Equipment *string
|
||||
Name *string
|
||||
Keep []WeatherUpdateField
|
||||
Drop []WeatherUpdateField
|
||||
}
|
||||
|
||||
type BatteryStatus struct {
|
||||
@ -54,48 +47,40 @@ type BatteryStatus struct {
|
||||
Status *int
|
||||
}
|
||||
|
||||
type WeatherUpdateField string
|
||||
|
||||
// NOTE: Annoyance to avoid string constant comparisons
|
||||
// CHORE: Maintain this
|
||||
// CHORE: Maintain this, used to check against
|
||||
// keep and drop lists
|
||||
// TODO: Use refelct/ast to generate code
|
||||
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"
|
||||
FieldDateUTC = "DateUTC"
|
||||
FieldStationType = "StationType"
|
||||
FieldTempOutdoorF = "TempOutdoorF"
|
||||
FieldTempIndoorF = "TempIndoorF"
|
||||
FieldHumidityOudoor = "HumidityOudoor"
|
||||
FieldHumidityIndoor = "HumidityIndoor"
|
||||
FieldWindSpeedMPH = "WindSpeedMPH"
|
||||
FieldWindGustMPH = "WindGustMPH"
|
||||
FieldMaxDailyGust = "MaxDailyGust"
|
||||
FieldWindDir = "WindDir"
|
||||
FieldWindDirAvg10m = "WindDirAvg10m"
|
||||
FieldUV = "UV"
|
||||
FieldSolarRadiation = "SolarRadiation"
|
||||
FieldHourlyRainIn = "HourlyRainIn"
|
||||
FieldEventRainIn = "EventRainIn"
|
||||
FieldDailyRainIn = "DailyRainIn"
|
||||
FieldWeeklyRainIn = "WeeklyRainIn"
|
||||
FieldMonthlyRainIn = "MonthlyRainIn"
|
||||
FieldYearlyRainIn = "YearlyRainIn"
|
||||
FieldTotalRainIn = "TotalRainIn"
|
||||
FieldBatteries = "Batteries"
|
||||
FieldBaromRelativeIn = "BaromRelativeIn"
|
||||
FieldBaromAbsoluteIn = "BaromAbsoluteIn"
|
||||
FieldDewPointF = "DewPointF"
|
||||
FieldWindChillF = "WindChillF"
|
||||
)
|
||||
|
||||
func (u *WeatherUpdate) GetStationName() string {
|
||||
if u.StationInfo != nil {
|
||||
return u.StationInfo.GetName()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (si *StationInfo) GetName() string {
|
||||
if si.Name != nil {
|
||||
return *si.Name
|
||||
if u.StationConfig != nil {
|
||||
return u.StationConfig.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
Reference in New Issue
Block a user