Compare commits

...

6 Commits

Author SHA1 Message Date
87f0cbac01 Fix typos, update TODO
All checks were successful
Build and Publish / release (push) Successful in 3m44s
2025-01-07 11:28:34 -05:00
4c93303f27 Consolidate battery metrics 2025-01-07 10:27:02 -05:00
e8654e76bc Update TODO 2025-01-07 08:53:06 -05:00
54f725c822 Update go-app 2025-01-05 20:33:28 -05:00
c002331fd4 Fix NaN with zero values
All checks were successful
Build and Publish / release (push) Successful in 3m7s
2025-01-05 19:05:24 -05:00
2ed4eca56c Add support for co2 battery
All checks were successful
Build and Publish / release (push) Successful in 2m57s
2025-01-05 18:39:58 -05:00
10 changed files with 124 additions and 94 deletions

View File

@ -1,11 +1,15 @@
# TODO # TODO
- [ ] Fix shutdown
- [ ] Configuration for app - [ ] Configuration for app
- [ ] Helm Chart - [ ] Helm Chart
- [ ] Update README - [ ] Update README
- [ ] Add new fields from WS-2192 - [ ] Add Grafana dashboard
- [ ] Add device name field with ID/Key mappings
- [ ] Add device type field with ID/Key mappings
## Done ## Done
- [x] Consolidate battery status into one metric with device label
- [x] Fix shutdown
- [x] Add new fields from WS-2192
- [x] Gitea CI - [x] Gitea CI
- [x] Version flag - [x] Version flag
- [x] Makefile - [x] Makefile

2
go.mod
View File

@ -3,7 +3,7 @@ module gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter
go 1.23.4 go 1.23.4
require ( require (
gitea.libretechconsulting.com/rmcguire/go-app v0.2.0 gitea.libretechconsulting.com/rmcguire/go-app v0.3.0
github.com/gorilla/schema v1.4.1 github.com/gorilla/schema v1.4.1
github.com/rs/zerolog v1.33.0 github.com/rs/zerolog v1.33.0
go.opentelemetry.io/otel v1.33.0 go.opentelemetry.io/otel v1.33.0

2
go.sum
View File

@ -2,6 +2,8 @@ gitea.libretechconsulting.com/rmcguire/go-app v0.1.3 h1:EwmEJLpN+rQjJ5stGEkZsqED
gitea.libretechconsulting.com/rmcguire/go-app v0.1.3/go.mod h1:wHOWh4O4AMDATQ3WEUYjq5a5bnICPBpu5G6BsNxqN38= 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 h1:pOm/PysC0IWPuEbmEjNSHHa8Qc5OhuoksYExcuJMFE4=
gitea.libretechconsulting.com/rmcguire/go-app v0.2.0/go.mod h1:wHOWh4O4AMDATQ3WEUYjq5a5bnICPBpu5G6BsNxqN38= 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/go.mod h1:wHOWh4O4AMDATQ3WEUYjq5a5bnICPBpu5G6BsNxqN38=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA= github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=

View File

@ -16,10 +16,10 @@ 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()
// Read config and environment, set up logging, load up
// an appCtx, and prepare ambient weather local exporter
ctx = app.MustSetupConfigAndLogging(ctx) ctx = app.MustSetupConfigAndLogging(ctx)
aw := ambient.New(ctx).Init()
aw := ambient.New(ctx)
aw.MustInit()
awApp := app.App{ awApp := app.App{
AppContext: ctx, AppContext: ctx,

View File

@ -37,10 +37,12 @@ func New(appCtx context.Context) *AmbientWeather {
} }
} }
func (aw *AmbientWeather) MustInit() { // Initialize with defaults, set logger from context
func (aw *AmbientWeather) Init() *AmbientWeather {
aw.awnProvider = &awn.AWNProvider{} aw.awnProvider = &awn.AWNProvider{}
aw.wuProvider = &wunderground.WUProvider{} aw.wuProvider = &wunderground.WUProvider{}
aw.l = zerolog.Ctx(aw.appCtx) aw.l = zerolog.Ctx(aw.appCtx)
return aw
} }
func (aw *AmbientWeather) GetAWNHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) { func (aw *AmbientWeather) GetAWNHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) {
@ -55,7 +57,7 @@ func (aw *AmbientWeather) GetWundergroundHandlerFunc(appCtx context.Context) fun
} }
} }
// Takes an HTTP requests and convers it to a // Takes an HTTP requests and converts it to a
// stable type. Enrich is called on the type to complete // stable type. Enrich is called on the type to complete
// any missing fields as the two providers supported by Ambient // any missing fields as the two providers supported by Ambient
// devices (awn/wunderground) produce different fields // devices (awn/wunderground) produce different fields

View File

@ -57,9 +57,24 @@ func MapAwnUpdate(awnUpdate *AmbientWeatherUpdate) *weather.WeatherUpdate {
MonthlyRainIn: awnUpdate.MonthlyRainIn, MonthlyRainIn: awnUpdate.MonthlyRainIn,
YearlyRainIn: awnUpdate.YearlyRainIn, YearlyRainIn: awnUpdate.YearlyRainIn,
TotalRainIn: awnUpdate.TotalRainIn, TotalRainIn: awnUpdate.TotalRainIn,
BattOutdoorSensor: awnUpdate.BattOut, Batteries: []weather.BatteryStatus{
BattIndoorSensor: awnUpdate.BattIn, {
BattRainSensor: awnUpdate.BattRain, Component: "OutdoorSensor",
Status: awnUpdate.BattOut,
},
{
Component: "IndoorSensor",
Status: awnUpdate.BattIn,
},
{
Component: "RainSensor",
Status: awnUpdate.BattRain,
},
{
Component: "CO2Sensor",
Status: awnUpdate.BattCO2,
},
},
TempIndoorF: awnUpdate.TempInF, TempIndoorF: awnUpdate.TempInF,
HumidityIndoor: awnUpdate.HumidityIn, HumidityIndoor: awnUpdate.HumidityIn,
BaromRelativeIn: awnUpdate.BaromRelIn, BaromRelativeIn: awnUpdate.BaromRelIn,

View File

@ -27,4 +27,5 @@ type AmbientWeatherUpdate struct {
BaromRelIn float64 `json:"baromrelin,omitempty" schema:"baromrelin"` BaromRelIn float64 `json:"baromrelin,omitempty" schema:"baromrelin"`
BaromAbsIn float64 `json:"baromabsin,omitempty" schema:"baromabsin"` BaromAbsIn float64 `json:"baromabsin,omitempty" schema:"baromabsin"`
BattIn int `json:"battin,omitempty" schema:"battin"` BattIn int `json:"battin,omitempty" schema:"battin"`
BattCO2 int `json:"batt_co2,omitempty" schema:"batt_co2"`
} }

View File

@ -13,7 +13,7 @@ func (u *WeatherUpdate) Enrich() {
u.WindChillF = CalculateWindChill(u.TempOutdoorF, u.WindSpeedMPH) u.WindChillF = CalculateWindChill(u.TempOutdoorF, u.WindSpeedMPH)
} }
if u.DewPointF == 0 { if u.DewPointF == 0 && (u.TempOutdoorF != 0 && u.HumidityOudoor != 0) {
u.DewPointF = CalculateDewPoint(u.TempOutdoorF, float64(u.HumidityOudoor)) u.DewPointF = CalculateDewPoint(u.TempOutdoorF, float64(u.HumidityOudoor))
} }

View File

@ -30,9 +30,7 @@ type WeatherMetrics struct {
MonthlyRainIn metric.Float64Gauge MonthlyRainIn metric.Float64Gauge
YearlyRainIn metric.Float64Gauge YearlyRainIn metric.Float64Gauge
TotalRainIn metric.Float64Gauge TotalRainIn metric.Float64Gauge
BattOutdoorSensor metric.Int64Gauge BatteryStatus metric.Int64Gauge
BattIndoorSensor metric.Int64Gauge
BattRainSensor metric.Int64Gauge
BaromRelativeIn metric.Float64Gauge BaromRelativeIn metric.Float64Gauge
BaromAbsoluteIn metric.Float64Gauge BaromAbsoluteIn metric.Float64Gauge
DewPointF metric.Float64Gauge DewPointF metric.Float64Gauge
@ -58,7 +56,7 @@ func MustInitMetrics(appCtx context.Context) *WeatherMetrics {
metric.WithDescription("Outdoor Temperature in Faherenheit")) metric.WithDescription("Outdoor Temperature in Faherenheit"))
wm.TempIndoorF, _ = wm.meter.Float64Gauge("weather_temp_indoor_f", wm.TempIndoorF, _ = wm.meter.Float64Gauge("weather_temp_indoor_f",
metric.WithDescription("Indoor Temperature in Faherenheit")) metric.WithDescription("Indoor Temperature in Faherenheit"))
wm.HumidityOudoor, _ = wm.meter.Int64Gauge("weather_humidity_oudoor", wm.HumidityOudoor, _ = wm.meter.Int64Gauge("weather_humidity_outdoor",
metric.WithDescription("Outdoor Humidity %")) metric.WithDescription("Outdoor Humidity %"))
wm.HumidityIndoor, _ = wm.meter.Int64Gauge("weather_humidity_indoor", wm.HumidityIndoor, _ = wm.meter.Int64Gauge("weather_humidity_indoor",
metric.WithDescription("Indoor Humidity %")) metric.WithDescription("Indoor Humidity %"))
@ -90,12 +88,8 @@ func MustInitMetrics(appCtx context.Context) *WeatherMetrics {
metric.WithDescription("Yearly Rain in Inches")) metric.WithDescription("Yearly Rain in Inches"))
wm.TotalRainIn, _ = wm.meter.Float64Gauge("weather_total_rain_in", wm.TotalRainIn, _ = wm.meter.Float64Gauge("weather_total_rain_in",
metric.WithDescription("Total Rain in Inches")) metric.WithDescription("Total Rain in Inches"))
wm.BattOutdoorSensor, _ = wm.meter.Int64Gauge("weather_batt_outdoor_sensor", wm.BatteryStatus, _ = wm.meter.Int64Gauge("battery_status",
metric.WithDescription("Outdoor Equipment Battery")) metric.WithDescription("Per-component battery status"))
wm.BattIndoorSensor, _ = wm.meter.Int64Gauge("weather_batt_indoor_sensor",
metric.WithDescription("Indoor Equipmenet Battery"))
wm.BattRainSensor, _ = wm.meter.Int64Gauge("weather_batt_rain_sensor",
metric.WithDescription("Rain Sensor Battery"))
wm.BaromRelativeIn, _ = wm.meter.Float64Gauge("weather_barometric_pressure_relative_in", wm.BaromRelativeIn, _ = wm.meter.Float64Gauge("weather_barometric_pressure_relative_in",
metric.WithDescription("Relative Pressure in Inches of Mercury")) metric.WithDescription("Relative Pressure in Inches of Mercury"))
wm.BaromAbsoluteIn, _ = wm.meter.Float64Gauge("weather_barometric_pressure_absolute_in", wm.BaromAbsoluteIn, _ = wm.meter.Float64Gauge("weather_barometric_pressure_absolute_in",
@ -136,13 +130,18 @@ func (wm *WeatherMetrics) Update(u *WeatherUpdate) {
wm.MonthlyRainIn.Record(wm.appCtx, u.MonthlyRainIn, metric.WithAttributeSet(attributes)) wm.MonthlyRainIn.Record(wm.appCtx, u.MonthlyRainIn, metric.WithAttributeSet(attributes))
wm.YearlyRainIn.Record(wm.appCtx, u.YearlyRainIn, metric.WithAttributeSet(attributes)) wm.YearlyRainIn.Record(wm.appCtx, u.YearlyRainIn, metric.WithAttributeSet(attributes))
wm.TotalRainIn.Record(wm.appCtx, u.TotalRainIn, metric.WithAttributeSet(attributes)) wm.TotalRainIn.Record(wm.appCtx, u.TotalRainIn, metric.WithAttributeSet(attributes))
wm.BattOutdoorSensor.Record(wm.appCtx, int64(u.BattOutdoorSensor), metric.WithAttributeSet(attributes))
wm.BattIndoorSensor.Record(wm.appCtx, int64(u.BattIndoorSensor), metric.WithAttributeSet(attributes))
wm.BattRainSensor.Record(wm.appCtx, int64(u.BattRainSensor), metric.WithAttributeSet(attributes))
wm.BaromRelativeIn.Record(wm.appCtx, u.BaromRelativeIn, metric.WithAttributeSet(attributes)) wm.BaromRelativeIn.Record(wm.appCtx, u.BaromRelativeIn, metric.WithAttributeSet(attributes))
wm.BaromAbsoluteIn.Record(wm.appCtx, u.BaromAbsoluteIn, metric.WithAttributeSet(attributes)) wm.BaromAbsoluteIn.Record(wm.appCtx, u.BaromAbsoluteIn, metric.WithAttributeSet(attributes))
wm.DewPointF.Record(wm.appCtx, u.DewPointF, metric.WithAttributeSet(attributes)) wm.DewPointF.Record(wm.appCtx, u.DewPointF, metric.WithAttributeSet(attributes))
wm.WindChillF.Record(wm.appCtx, u.WindChillF, metric.WithAttributeSet(attributes)) wm.WindChillF.Record(wm.appCtx, u.WindChillF, metric.WithAttributeSet(attributes))
// Batteries
for _, battery := range u.Batteries {
wm.BatteryStatus.Record(wm.appCtx, int64(battery.Status),
metric.WithAttributeSet(attributes),
metric.WithAttributes(attribute.String("component", battery.Component)),
)
}
wm.UpdatesReceived.Add(wm.appCtx, 1) wm.UpdatesReceived.Add(wm.appCtx, 1)
} }

View File

@ -27,9 +27,11 @@ type WeatherUpdate struct {
MonthlyRainIn float64 MonthlyRainIn float64
YearlyRainIn float64 YearlyRainIn float64
TotalRainIn float64 TotalRainIn float64
BattOutdoorSensor int Batteries []BatteryStatus
BattIndoorSensor int // BattOutdoorSensor int
BattRainSensor 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
@ -37,3 +39,8 @@ type WeatherUpdate struct {
DewPointF float64 DewPointF float64
WindChillF float64 WindChillF float64
} }
type BatteryStatus struct {
Component string
Status int
}