Compare commits

...

26 Commits

Author SHA1 Message Date
849dbfb6ff Update go-app
All checks were successful
Build and Publish / release (push) Successful in 4m5s
2025-01-08 21:31:50 -05:00
dc2470da71 Improve metrics recording
Some checks failed
Build and Publish / release (push) Failing after 40s
2025-01-08 21:28:38 -05:00
c23177b62d Support metric prefix change
Some checks failed
Build and Publish / release (push) Failing after 40s
2025-01-08 16:59:27 -05:00
86653cf589 Use custom config type 2025-01-08 16:49:31 -05:00
fa0a9f4ddc Fix wind speed mapping for awn
All checks were successful
Build and Publish / release (push) Successful in 2m46s
2025-01-08 14:24:56 -05:00
689790fe86 Update TODO 2025-01-08 14:24:46 -05:00
3af1cc40a5 Fix metric prefix
All checks were successful
Build and Publish / release (push) Successful in 3m29s
2025-01-07 15:08:14 -05:00
4ff684abe3 Update TODO 2025-01-07 11:35:14 -05:00
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
2e3cfb44f4 Don't enrich nil updates
All checks were successful
Build and Publish / release (push) Successful in 2m48s
2025-01-05 17:37:14 -05:00
be8a4f3bca Don't enrich nil updates 2025-01-05 17:36:39 -05:00
96dfcf7e53 Update sample
All checks were successful
Build and Publish / release (push) Successful in 3m58s
2025-01-05 16:24:52 -05:00
5fc16b39af Upgrade go-app 2025-01-05 16:23:15 -05:00
1ae58f64ca Move docker compose sample 2025-01-05 15:16:16 -05:00
bb4e4aa2af Update TODO 2025-01-05 13:19:30 -05:00
f102ef50bf Upgrade to fix env overrides
All checks were successful
Build and Publish / release (push) Successful in 4m7s
2025-01-05 13:13:19 -05:00
d3d251ef30 Update TODO 2025-01-05 11:47:06 -05:00
eecaf2a82e Add latest tag
All checks were successful
Build and Publish / release (push) Successful in 2m44s
2025-01-05 11:44:48 -05:00
f0f6c58f0b Fix versioning
All checks were successful
Build and Publish / release (push) Successful in 3m24s
2025-01-05 11:36:30 -05:00
681c7f703b Add Linux ARM64
Some checks failed
Build and Publish / release (push) Failing after 3m27s
2025-01-05 11:32:21 -05:00
a11936ae65 Fix action versions
Some checks failed
Build and Publish / release (push) Failing after 2m51s
2025-01-05 11:28:17 -05:00
19 changed files with 555 additions and 235 deletions

View File

@ -65,7 +65,7 @@ jobs:
fi
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
uses: docker/setup-buildx-action@v3
- name: Log in to Custom Registry
uses: docker/login-action@v3
@ -75,11 +75,13 @@ jobs:
password: ${{ secrets.API_TOKEN }}
- name: Build and Push Docker Image
uses: docker/build-push-action@v4
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.DOCKER_IMG }}:${{ github.ref_name }}
tags: |
${{ env.DOCKER_IMG }}:${{ github.ref_name }}
${{ env.DOCKER_IMG }}:latest
build-args: |
VER_PKG={{ env.VER_PKG }}
VER_PKG=${{ env.VER_PKG }}
VERSION=${{ github.ref_name }}

4
.gitignore vendored
View File

@ -20,7 +20,11 @@
# Go workspace file
go.work
go.work.sum
.env
bin/*
config.yaml
docker-compose.yml

View File

@ -3,7 +3,7 @@ CMD_NAME := ambient-local-exporter
.PHONY: all test build docker install clean
VERSION ?= development # Default to "development" if VERSION is not set
PLATFORMS := linux/amd64 darwin/amd64 darwin/arm64
PLATFORMS := linux/amd64 linux/arm64 darwin/amd64 darwin/arm64
OUTPUT_DIR := bin
VER_PKG := gitea.libretechconsulting.com/rmcguire/go-app/pkg/config.Version
DOCKER_IMG := gitea.libretechconsulting.com/rmcguire/ambient-local-exporter

24
TODO.md
View File

@ -1,10 +1,20 @@
# TODO
- [ ] Fix shutdown
- [ ] Configuration for app
- [ ] Makefile
- [ ] Dockerfile
- [ ] Helm Chart
- [ ] Gitea CI
- [ ] Update README
- [ ] Version flag
- [ ] Add Grafana dashboard
- [ ] Add proxy to upstream support
- [ ] Add new spans
## Done
- [x] Configuration for app
- [x] Configurable metric prefix
- [x] Add device name field with ID/Key mappings
- [x] Add device type field with ID/Key mappings
- [x] Move EVERYTHING to pointers to support nil
- [x] Consolidate battery status into one metric with device label
- [x] Fix shutdown
- [x] Add new fields from WS-2192
- [x] Gitea CI
- [x] Version flag
- [x] Makefile
- [x] Dockerfile

View File

@ -10,6 +10,10 @@ services:
APP_LOG_FORMAT: json ## console, json
APP_LOG_TIME_FORMAT: rfc3339 ## long, short, unix, rfc3339, off
APP_HTTP_LISTEN: 0.0.0.0:8080
APP_HTTP_READ_TIMEOUT: 10s
APP_HTTP_WRITE_TIMEOUT: 10s
APP_HTTP_IDLE_TIMEOUT: 30s
APP_HTTP_LOG_REQUESTS: true
APP_OTEL_STDOUT_ENABLED: false
APP_OTEL_METRIC_INTERVAL_SECS: 30
OTEL_EXPORTER_OTLP_ENDPOINT: http://otel.libretechconsulting.com:4317 # Set to your otel collector

14
go.mod
View File

@ -3,7 +3,7 @@ module gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter
go 1.23.4
require (
gitea.libretechconsulting.com/rmcguire/go-app v0.1.1
gitea.libretechconsulting.com/rmcguire/go-app v0.3.1
github.com/gorilla/schema v1.4.1
github.com/rs/zerolog v1.33.0
go.opentelemetry.io/otel v1.33.0
@ -13,7 +13,7 @@ require (
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/caarlos0/env/v9 v9.0.0 // indirect
github.com/caarlos0/env/v11 v11.3.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
@ -40,12 +40,12 @@ require (
go.opentelemetry.io/otel/sdk v1.33.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.33.0 // indirect
go.opentelemetry.io/otel/trace v1.33.0 // indirect
go.opentelemetry.io/proto/otlp v1.4.0 // indirect
golang.org/x/net v0.33.0 // indirect
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250102185135-69823020774d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250102185135-69823020774d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250106144421-5f5ef82da422 // indirect
google.golang.org/grpc v1.69.2 // indirect
google.golang.org/protobuf v1.36.1 // indirect
google.golang.org/protobuf v1.36.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

22
go.sum
View File

@ -1,11 +1,11 @@
gitea.libretechconsulting.com/rmcguire/go-app v0.1.0 h1:H4TMgQ463oRNOyoi0FAvfGtOoDn651zNZStxM+sdNuU=
gitea.libretechconsulting.com/rmcguire/go-app v0.1.0/go.mod h1:p0ajkpFvzzD6VZ4xSjuowtwGRb1DjMfo/iG6LyFqFCs=
gitea.libretechconsulting.com/rmcguire/go-app v0.1.1 h1:Hrxqi1tqz8mf0baBsWgFe/S4jyMtIuPqH2FlanJUMNc=
gitea.libretechconsulting.com/rmcguire/go-app v0.1.1/go.mod h1:p0ajkpFvzzD6VZ4xSjuowtwGRb1DjMfo/iG6LyFqFCs=
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.1 h1:jydcJ+Vv8sk+Le7nTI2+b6E7FfV+ShBJo7YdxmdaCYc=
gitea.libretechconsulting.com/rmcguire/go-app v0.3.1/go.mod h1:wHOWh4O4AMDATQ3WEUYjq5a5bnICPBpu5G6BsNxqN38=
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/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc=
github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020=
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
github.com/caarlos0/env/v11 v11.3.1/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
@ -93,10 +93,14 @@ go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qq
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
go.opentelemetry.io/proto/otlp v1.4.0 h1:TA9WRvW6zMwP+Ssb6fLoUIuirti1gGbP28GcKG1jgeg=
go.opentelemetry.io/proto/otlp v1.4.0/go.mod h1:PPBWZIP98o2ElSqI35IHfu7hIhSwvc5N38Jw8pXuGFY=
go.opentelemetry.io/proto/otlp v1.5.0 h1:xJvq7gMzB31/d406fB8U5CBdyQGw4P399D1aQWU/3i4=
go.opentelemetry.io/proto/otlp v1.5.0/go.mod h1:keN8WnHxOy8PG0rQZjJJ5A2ebUoafqWp0eVQ4yIXvJ4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@ -106,12 +110,18 @@ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
google.golang.org/genproto/googleapis/api v0.0.0-20250102185135-69823020774d h1:H8tOf8XM88HvKqLTxe755haY6r1fqqzLbEnfrmLXlSA=
google.golang.org/genproto/googleapis/api v0.0.0-20250102185135-69823020774d/go.mod h1:2v7Z7gP2ZUOGsaFyxATQSRoBnKygqVq2Cwnvom7QiqY=
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422 h1:GVIKPyP/kLIyVOgOnTwFOrvQaQUzOzGMCxgFUOEmm24=
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422/go.mod h1:b6h1vNKhxaSoEI+5jc3PJUCustfli/mRab7295pY7rw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250102185135-69823020774d h1:xJJRGY7TJcvIlpSrN3K6LAWgNFUILlO+OMAqtg9aqnw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250102185135-69823020774d/go.mod h1:3ENsm/5D1mzDyhpzeRi1NR784I0BcofWBoSc5QqqMK4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250106144421-5f5ef82da422 h1:3UsHvIr4Wc2aW4brOaSCmcxh9ksica6fHEr8P1XhkYw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250106144421-5f5ef82da422/go.mod h1:3ENsm/5D1mzDyhpzeRi1NR784I0BcofWBoSc5QqqMK4=
google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.36.2 h1:R8FeyR1/eLmkutZOM5CWghmo5itiG9z0ktFlTVLuTmU=
google.golang.org/protobuf v1.36.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

19
main.go
View File

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

View File

@ -24,6 +24,7 @@ type AmbientWeather struct {
// when either "AmbientWeather" or "Wunderground" are selected
// in the "Custom" section of the AWNet app, or the web UI
// of an Ambient WeatherHub
config *AmbientLocalExporterConfig
awnProvider provider.AmbientProvider
wuProvider provider.AmbientProvider
appCtx context.Context
@ -31,16 +32,21 @@ type AmbientWeather struct {
l *zerolog.Logger
}
func New(appCtx context.Context) *AmbientWeather {
func New(appCtx context.Context, awConfig *AmbientLocalExporterConfig) *AmbientWeather {
return &AmbientWeather{
config: awConfig,
appCtx: appCtx,
}
}
func (aw *AmbientWeather) MustInit() {
// Initialize with defaults, set logger from context
func (aw *AmbientWeather) Init() *AmbientWeather {
aw.awnProvider = &awn.AWNProvider{}
aw.wuProvider = &wunderground.WUProvider{}
aw.l = zerolog.Ctx(aw.appCtx)
aw.l.Trace().Any("awConfig", aw.config).Send()
return aw
}
func (aw *AmbientWeather) GetAWNHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) {
@ -55,7 +61,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
// any missing fields as the two providers supported by Ambient
// devices (awn/wunderground) produce different fields
@ -85,16 +91,22 @@ func (aw *AmbientWeather) handleProviderRequest(
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
// Calculate any fields that may be missing
// such as dew point and wind chill
update.Enrich()
// Update metrics
// Prepare metrics if this is the first update
if aw.metrics == nil {
aw.metrics = weather.MustInitMetrics(aw.appCtx)
aw.InitMetrics()
}
// Enrich station if configured
aw.enrichStation(update)
// Update metrics
aw.metrics.Update(update)
l.Debug().
@ -103,3 +115,26 @@ func (aw *AmbientWeather) handleProviderRequest(
Msg("successfully handled update")
w.Write([]byte("ok"))
}
func (aw *AmbientWeather) InitMetrics() {
if aw.config.MetricPrefix != "" {
weather.MetricPrefix = aw.config.MetricPrefix
}
aw.metrics = weather.MustInitMetrics(aw.appCtx)
}
func (aw *AmbientWeather) enrichStation(update *weather.WeatherUpdate) {
if update != nil && update.StationID != nil && *update.StationID != "" {
for _, station := range aw.config.WeatherStations {
if *update.StationID == station.AWNPassKey || *update.StationID == station.WundergroundID {
update.StationInfo = &weather.StationInfo{
Type: update.StationType,
Equipment: &station.Equipment,
Name: &station.Name,
Keep: station.KeepMetrics,
Drop: station.DropMetrics,
}
}
}
}
}

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

@ -0,0 +1,37 @@
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
// will be excluded if present in discardMetrics
//
// If anything is present in keepMetrics, it is solely applied,
// ignoring discardMetrics
KeepMetrics []weather.WeatherUpdateField `yaml:"keepMetrics"`
DropMetrics []weather.WeatherUpdateField `yaml:"dropMetrics"`
}

View File

@ -33,37 +33,56 @@ func (awn *AWNProvider) ReqToWeather(_ context.Context, r *http.Request) (
}
func MapAwnUpdate(awnUpdate *AmbientWeatherUpdate) *weather.WeatherUpdate {
updateTime, err := time.Parse(time.DateTime, awnUpdate.DateUTC)
if err != nil {
updateTime = time.Now()
updateTime := time.Now()
if awnUpdate.DateUTC != nil {
ut, err := time.Parse(time.DateTime, *awnUpdate.DateUTC)
if err == nil {
updateTime = ut
}
}
return &weather.WeatherUpdate{
StationType: awnUpdate.StationType,
DateUTC: &updateTime,
TempOutdoorF: awnUpdate.TempF,
HumidityOudoor: awnUpdate.Humidity,
WindSpeedMPH: awnUpdate.WindGustMPH,
WindGustMPH: awnUpdate.WindGustMPH,
MaxDailyGust: awnUpdate.MaxDailyGust,
WindDir: awnUpdate.WindDir,
WindDirAvg10m: awnUpdate.WindDirAVG10m,
UV: awnUpdate.UV,
SolarRadiation: awnUpdate.SolarRadiation,
HourlyRainIn: awnUpdate.HourlyRainIn,
EventRainIn: awnUpdate.EventRainIn,
DailyRainIn: awnUpdate.DailyRainIn,
WeeklyRainIn: awnUpdate.WeeklyRainIn,
MonthlyRainIn: awnUpdate.MonthlyRainIn,
YearlyRainIn: awnUpdate.YearlyRainIn,
TotalRainIn: awnUpdate.TotalRainIn,
BattOutdoorSensor: awnUpdate.BattOut,
BattIndoorSensor: awnUpdate.BattIn,
BattRainSensor: awnUpdate.BattRain,
TempIndoorF: awnUpdate.TempInF,
HumidityIndoor: awnUpdate.HumidityIn,
BaromRelativeIn: awnUpdate.BaromRelIn,
BaromAbsoluteIn: awnUpdate.BaromAbsIn,
DateUTC: &updateTime,
StationID: awnUpdate.PassKey,
StationType: awnUpdate.StationType,
TempOutdoorF: awnUpdate.TempF,
HumidityOudoor: awnUpdate.Humidity,
WindSpeedMPH: awnUpdate.WindSpeedMPH,
WindGustMPH: awnUpdate.WindGustMPH,
MaxDailyGust: awnUpdate.MaxDailyGust,
WindDir: awnUpdate.WindDir,
WindDirAvg10m: awnUpdate.WindDirAVG10m,
UV: awnUpdate.UV,
SolarRadiation: awnUpdate.SolarRadiation,
HourlyRainIn: awnUpdate.HourlyRainIn,
EventRainIn: awnUpdate.EventRainIn,
DailyRainIn: awnUpdate.DailyRainIn,
WeeklyRainIn: awnUpdate.WeeklyRainIn,
MonthlyRainIn: awnUpdate.MonthlyRainIn,
YearlyRainIn: awnUpdate.YearlyRainIn,
TotalRainIn: awnUpdate.TotalRainIn,
Batteries: []weather.BatteryStatus{
{
Component: "OutdoorSensor",
Status: awnUpdate.BattOut,
},
{
Component: "IndoorSensor",
Status: awnUpdate.BattIn,
},
{
Component: "RainSensor",
Status: awnUpdate.BattRain,
},
{
Component: "CO2Sensor",
Status: awnUpdate.BattCO2,
},
},
TempIndoorF: awnUpdate.TempInF,
HumidityIndoor: awnUpdate.HumidityIn,
BaromRelativeIn: awnUpdate.BaromRelIn,
BaromAbsoluteIn: awnUpdate.BaromAbsIn,
}
}

View File

@ -1,30 +1,31 @@
package awn
type AmbientWeatherUpdate struct {
PassKey string `json:"PASSKEY,omitempty" schema:"PASSKEY"`
StationType string `json:"stationtype,omitempty" schema:"stationtype"`
DateUTC string `json:"dateutc,omitempty" schema:"dateutc"`
TempF float64 `json:"tempf,omitempty" schema:"tempf"`
Humidity int `json:"humidity,omitempty" schema:"humidity"`
WindSpeedMPH float64 `json:"windspeedmph,omitempty" schema:"windspeedmph"`
WindGustMPH float64 `json:"windgustmph,omitempty" schema:"windgustmph"`
MaxDailyGust float64 `json:"maxdailygust,omitempty" schema:"maxdailygust"`
WindDir int `json:"winddir,omitempty" schema:"winddir"`
WindDirAVG10m int `json:"winddir_avg10m,omitempty" schema:"winddir_avg10m"`
UV int `json:"uv,omitempty" schema:"uv"`
SolarRadiation float64 `json:"solarradiation,omitempty" schema:"solarradiation"`
HourlyRainIn float64 `json:"hourlyrainin,omitempty" schema:"hourlyrainin"`
EventRainIn float64 `json:"eventrainin,omitempty" schema:"eventrainin"`
DailyRainIn float64 `json:"dailyrainin,omitempty" schema:"dailyrainin"`
WeeklyRainIn float64 `json:"weeklyrainin,omitempty" schema:"weeklyrainin"`
MonthlyRainIn float64 `json:"monthlyrainin,omitempty" schema:"monthlyrainin"`
YearlyRainIn float64 `json:"yearlyrainin,omitempty" schema:"yearlyrainin"`
TotalRainIn float64 `json:"totalrainin,omitempty" schema:"totalrainin"`
BattOut int `json:"battout,omitempty" schema:"battout"`
BattRain int `json:"battrain,omitempty" schema:"battrain"`
TempInF float64 `json:"tempinf,omitempty" schema:"tempinf"`
HumidityIn int `json:"humidityin,omitempty" schema:"humidityin"`
BaromRelIn float64 `json:"baromrelin,omitempty" schema:"baromrelin"`
BaromAbsIn float64 `json:"baromabsin,omitempty" schema:"baromabsin"`
BattIn int `json:"battin,omitempty" schema:"battin"`
PassKey *string `json:"PASSKEY,omitempty" schema:"PASSKEY"`
StationType *string `json:"stationtype,omitempty" schema:"stationtype"`
DateUTC *string `json:"dateutc,omitempty" schema:"dateutc"`
TempF *float64 `json:"tempf,omitempty" schema:"tempf"`
Humidity *int `json:"humidity,omitempty" schema:"humidity"`
WindSpeedMPH *float64 `json:"windspeedmph,omitempty" schema:"windspeedmph"`
WindGustMPH *float64 `json:"windgustmph,omitempty" schema:"windgustmph"`
MaxDailyGust *float64 `json:"maxdailygust,omitempty" schema:"maxdailygust"`
WindDir *int `json:"winddir,omitempty" schema:"winddir"`
WindDirAVG10m *int `json:"winddir_avg10m,omitempty" schema:"winddir_avg10m"`
UV *int `json:"uv,omitempty" schema:"uv"`
SolarRadiation *float64 `json:"solarradiation,omitempty" schema:"solarradiation"`
HourlyRainIn *float64 `json:"hourlyrainin,omitempty" schema:"hourlyrainin"`
EventRainIn *float64 `json:"eventrainin,omitempty" schema:"eventrainin"`
DailyRainIn *float64 `json:"dailyrainin,omitempty" schema:"dailyrainin"`
WeeklyRainIn *float64 `json:"weeklyrainin,omitempty" schema:"weeklyrainin"`
MonthlyRainIn *float64 `json:"monthlyrainin,omitempty" schema:"monthlyrainin"`
YearlyRainIn *float64 `json:"yearlyrainin,omitempty" schema:"yearlyrainin"`
TotalRainIn *float64 `json:"totalrainin,omitempty" schema:"totalrainin"`
BattOut *int `json:"battout,omitempty" schema:"battout"`
BattRain *int `json:"battrain,omitempty" schema:"battrain"`
TempInF *float64 `json:"tempinf,omitempty" schema:"tempinf"`
HumidityIn *int `json:"humidityin,omitempty" schema:"humidityin"`
BaromRelIn *float64 `json:"baromrelin,omitempty" schema:"baromrelin"`
BaromAbsIn *float64 `json:"baromabsin,omitempty" schema:"baromabsin"`
BattIn *int `json:"battin,omitempty" schema:"battin"`
BattCO2 *int `json:"batt_co2,omitempty" schema:"batt_co2"`
}

View File

@ -33,16 +33,21 @@ func (wu *WUProvider) ReqToWeather(_ context.Context, r *http.Request) (
}
func MapWUUpdate(wuUpdate *WundergroundUpdate) *weather.WeatherUpdate {
updateTime, err := time.Parse(time.DateTime, wuUpdate.DateUTC)
if err != nil {
updateTime = time.Now()
updateTime := time.Now()
if wuUpdate.DateUTC != nil {
ut, err := time.Parse(time.DateTime, *wuUpdate.DateUTC)
if err == nil {
updateTime = ut
}
}
return &weather.WeatherUpdate{
StationType: wuUpdate.SoftwareType,
DateUTC: &updateTime,
TempOutdoorF: wuUpdate.Tempf,
HumidityOudoor: wuUpdate.Humidity,
StationID: wuUpdate.ID,
StationType: wuUpdate.SoftwareType,
TempOutdoorF: wuUpdate.Tempf,
HumidityOudoor: wuUpdate.Humidity,
WindSpeedMPH: wuUpdate.WindGustMPH,
WindGustMPH: wuUpdate.WindGustMPH,
WindDir: wuUpdate.WindDir,

View File

@ -1,29 +1,29 @@
package wunderground
type WundergroundUpdate struct {
ID string `json:"ID,omitempty" schema:"ID"`
Password string `json:"PASSWORD,omitempty" schema:"PASSWORD"`
UV int `json:"UV,omitempty" schema:"UV"`
Action string `json:"action,omitempty" schema:"action"`
BaromIn float64 `json:"baromin,omitempty" schema:"baromin"`
DailyRainIn float64 `json:"dailyrainin,omitempty" schema:"dailyrainin"`
DateUTC string `json:"dateutc,omitempty" schema:"dateutc"`
DewPtF float64 `json:"dewptf,omitempty" schema:"dewptf"`
Humidity int `json:"humidity,omitempty" schema:"humidity"`
IndoorHumidity int `json:"indoorhumidity,omitempty" schema:"indoorhumidity"`
IndoorTempF float64 `json:"indoortempf,omitempty" schema:"indoortempf"`
LowBatt bool `json:"lowbatt,omitempty" schema:"lowbatt"`
MonthlyRainIn float64 `json:"monthlyrainin,omitempty" schema:"monthlyrainin"`
RainIn float64 `json:"rainin,omitempty" schema:"rainin"`
Realtime bool `json:"realtime,omitempty" schema:"realtime"`
Rtfreq int `json:"rtfreq,omitempty" schema:"rtfreq"`
SoftwareType string `json:"softwaretype,omitempty" schema:"softwaretype"`
SolarRadiation float64 `json:"solarradiation,omitempty" schema:"solarradiation"`
Tempf float64 `json:"tempf,omitempty" schema:"tempf"`
WeeklyRainIn float64 `json:"weeklyrainin,omitempty" schema:"weeklyrainin"`
WindChillF float64 `json:"windchillf,omitempty" schema:"windchillf"`
WindDir int `json:"winddir,omitempty" schema:"winddir"`
WindGustMPH float64 `json:"windgustmph,omitempty" schema:"windgustmph"`
WindSpeedMPH float64 `json:"windspeedmph,omitempty" schema:"windspeedmph"`
YearlyRainIn float64 `json:"yearlyrainin,omitempty" schema:"yearlyrainin"`
ID *string `json:"ID,omitempty" schema:"ID"`
Password *string `json:"PASSWORD,omitempty" schema:"PASSWORD"`
UV *int `json:"UV,omitempty" schema:"UV"`
Action *string `json:"action,omitempty" schema:"action"`
BaromIn *float64 `json:"baromin,omitempty" schema:"baromin"`
DailyRainIn *float64 `json:"dailyrainin,omitempty" schema:"dailyrainin"`
DateUTC *string `json:"dateutc,omitempty" schema:"dateutc"`
DewPtF *float64 `json:"dewptf,omitempty" schema:"dewptf"`
Humidity *int `json:"humidity,omitempty" schema:"humidity"`
IndoorHumidity *int `json:"indoorhumidity,omitempty" schema:"indoorhumidity"`
IndoorTempF *float64 `json:"indoortempf,omitempty" schema:"indoortempf"`
LowBatt *bool `json:"lowbatt,omitempty" schema:"lowbatt"`
MonthlyRainIn *float64 `json:"monthlyrainin,omitempty" schema:"monthlyrainin"`
RainIn *float64 `json:"rainin,omitempty" schema:"rainin"`
Realtime *bool `json:"realtime,omitempty" schema:"realtime"`
Rtfreq *int `json:"rtfreq,omitempty" schema:"rtfreq"`
SoftwareType *string `json:"softwaretype,omitempty" schema:"softwaretype"`
SolarRadiation *float64 `json:"solarradiation,omitempty" schema:"solarradiation"`
Tempf *float64 `json:"tempf,omitempty" schema:"tempf"`
WeeklyRainIn *float64 `json:"weeklyrainin,omitempty" schema:"weeklyrainin"`
WindChillF *float64 `json:"windchillf,omitempty" schema:"windchillf"`
WindDir *int `json:"winddir,omitempty" schema:"winddir"`
WindGustMPH *float64 `json:"windgustmph,omitempty" schema:"windgustmph"`
WindSpeedMPH *float64 `json:"windspeedmph,omitempty" schema:"windspeedmph"`
YearlyRainIn *float64 `json:"yearlyrainin,omitempty" schema:"yearlyrainin"`
}

View File

@ -5,15 +5,23 @@ import "math"
// Attempts to complete missing fields that may not
// be set by a specific provider, such as DewPoint and WindChill
func (u *WeatherUpdate) Enrich() {
if u.WindChillF == 0 {
u.WindChillF = CalculateWindChill(u.TempOutdoorF, u.WindSpeedMPH)
if u == nil {
return
}
if u.DewPointF == 0 {
u.DewPointF = CalculateDewPoint(u.TempOutdoorF, float64(u.HumidityOudoor))
if u.WindChillF == nil && u.TempOutdoorF != nil && u.WindSpeedMPH != nil {
wc := CalculateWindChill(*u.TempOutdoorF, *u.WindSpeedMPH)
u.WindChillF = &wc
}
if u.BaromAbsoluteIn == 0 {
if u.DewPointF == nil && (u.TempOutdoorF != nil && u.HumidityOudoor != nil) {
if *u.TempOutdoorF != 0 || *u.HumidityOudoor != 0 {
dp := CalculateDewPoint(*u.TempOutdoorF, float64(*u.HumidityOudoor))
u.DewPointF = &dp
}
}
if u.BaromAbsoluteIn == nil && u.BaromRelativeIn != nil {
u.BaromAbsoluteIn = u.BaromRelativeIn
}
}

View File

@ -5,6 +5,7 @@ import (
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
@ -12,137 +13,153 @@ import (
type WeatherMetrics struct {
// Weather Metrics
TempOutdoorF metric.Float64Gauge
TempIndoorF metric.Float64Gauge
HumidityOudoor metric.Int64Gauge
HumidityIndoor metric.Int64Gauge
WindSpeedMPH metric.Float64Gauge
WindGustMPH metric.Float64Gauge
MaxDailyGust metric.Float64Gauge
WindDir metric.Int64Gauge
WindDirAvg10m metric.Int64Gauge
UV metric.Int64Gauge
SolarRadiation metric.Float64Gauge
HourlyRainIn metric.Float64Gauge
EventRainIn metric.Float64Gauge
DailyRainIn metric.Float64Gauge
WeeklyRainIn metric.Float64Gauge
MonthlyRainIn metric.Float64Gauge
YearlyRainIn metric.Float64Gauge
TotalRainIn metric.Float64Gauge
BattOutdoorSensor metric.Int64Gauge
BattIndoorSensor metric.Int64Gauge
BattRainSensor metric.Int64Gauge
BaromRelativeIn metric.Float64Gauge
BaromAbsoluteIn metric.Float64Gauge
DewPointF metric.Float64Gauge
WindChillF metric.Float64Gauge
TempOutdoorF metric.Float64Gauge
TempIndoorF metric.Float64Gauge
HumidityOudoor metric.Int64Gauge
HumidityIndoor metric.Int64Gauge
WindSpeedMPH metric.Float64Gauge
WindGustMPH metric.Float64Gauge
MaxDailyGust metric.Float64Gauge
WindDir metric.Int64Gauge
WindDirAvg10m metric.Int64Gauge
UV metric.Int64Gauge
SolarRadiation metric.Float64Gauge
HourlyRainIn metric.Float64Gauge
EventRainIn metric.Float64Gauge
DailyRainIn metric.Float64Gauge
WeeklyRainIn metric.Float64Gauge
MonthlyRainIn metric.Float64Gauge
YearlyRainIn metric.Float64Gauge
TotalRainIn metric.Float64Gauge
BatteryStatus metric.Int64Gauge
BaromRelativeIn metric.Float64Gauge
BaromAbsoluteIn metric.Float64Gauge
DewPointF metric.Float64Gauge
WindChillF metric.Float64Gauge
// Internal Telemetry
UpdatesReceived metric.Int64Counter
appCtx context.Context
cfg *config.AppConfig
meter metric.Meter
recorder *MetricRecorder
}
var MetricPrefix = "weather"
func MustInitMetrics(appCtx context.Context) *WeatherMetrics {
wm := &WeatherMetrics{
appCtx: appCtx,
cfg: config.MustFromCtx(appCtx),
appCtx: appCtx,
cfg: config.MustFromCtx(appCtx),
recorder: &MetricRecorder{ctx: appCtx, l: zerolog.Ctx(appCtx)},
}
wm.meter = otel.GetMeter(appCtx, "weather", "metrics")
// Weather Metrics
wm.TempOutdoorF, _ = wm.meter.Float64Gauge("weather_temp_outdoor_f",
wm.TempOutdoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_outdoor_f",
metric.WithDescription("Outdoor Temperature in Faherenheit"))
wm.TempIndoorF, _ = wm.meter.Float64Gauge("weather_temp_indoor_f",
wm.TempIndoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_indoor_f",
metric.WithDescription("Indoor Temperature in Faherenheit"))
wm.HumidityOudoor, _ = wm.meter.Int64Gauge("weather_humidity_oudoor",
wm.HumidityOudoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_outdoor",
metric.WithDescription("Outdoor Humidity %"))
wm.HumidityIndoor, _ = wm.meter.Int64Gauge("weather_humidity_indoor",
wm.HumidityIndoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_indoor",
metric.WithDescription("Indoor Humidity %"))
wm.WindSpeedMPH, _ = wm.meter.Float64Gauge("weather_wind_speed_mph",
wm.WindSpeedMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_speed_mph",
metric.WithDescription("Wind Speed in MPH"))
wm.WindGustMPH, _ = wm.meter.Float64Gauge("weather_wind_gust_mph",
wm.WindGustMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_gust_mph",
metric.WithDescription("Wind Gust in MPH"))
wm.MaxDailyGust, _ = wm.meter.Float64Gauge("weather_max_daily_gust",
wm.MaxDailyGust, _ = wm.meter.Float64Gauge(MetricPrefix+"_max_daily_gust",
metric.WithDescription("Max Daily Wind Gust"))
wm.WindDir, _ = wm.meter.Int64Gauge("weather_wind_dir",
wm.WindDir, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir",
metric.WithDescription("Wind Direction in Degrees"))
wm.WindDirAvg10m, _ = wm.meter.Int64Gauge("weather_wind_dir_avg_10m",
wm.WindDirAvg10m, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir_avg_10m",
metric.WithDescription("Wind Direction 10m Average"))
wm.UV, _ = wm.meter.Int64Gauge("weather_uv",
wm.UV, _ = wm.meter.Int64Gauge(MetricPrefix+"_uv",
metric.WithDescription("UV Index"))
wm.SolarRadiation, _ = wm.meter.Float64Gauge("weather_solar_radiation",
wm.SolarRadiation, _ = wm.meter.Float64Gauge(MetricPrefix+"_solar_radiation",
metric.WithDescription("Solar Radiation in W/㎡"))
wm.HourlyRainIn, _ = wm.meter.Float64Gauge("weather_hourly_rain_in",
wm.HourlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_hourly_rain_in",
metric.WithDescription("Hourly Rain in Inches"))
wm.EventRainIn, _ = wm.meter.Float64Gauge("weather_event_rain_in",
wm.EventRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_event_rain_in",
metric.WithDescription("Event Rain in Inches"))
wm.DailyRainIn, _ = wm.meter.Float64Gauge("weather_daily_rain_in",
wm.DailyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_daily_rain_in",
metric.WithDescription("Daily Rain in Inches"))
wm.WeeklyRainIn, _ = wm.meter.Float64Gauge("weather_weekly_rain_in",
wm.WeeklyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_weekly_rain_in",
metric.WithDescription("Weekly Rain in Inches"))
wm.MonthlyRainIn, _ = wm.meter.Float64Gauge("weather_monthly_rain_in",
wm.MonthlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_monthly_rain_in",
metric.WithDescription("Monthly Rain in Inches"))
wm.YearlyRainIn, _ = wm.meter.Float64Gauge("weather_yearly_rain_in",
wm.YearlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_yearly_rain_in",
metric.WithDescription("Yearly Rain in Inches"))
wm.TotalRainIn, _ = wm.meter.Float64Gauge("weather_total_rain_in",
wm.TotalRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_total_rain_in",
metric.WithDescription("Total Rain in Inches"))
wm.BattOutdoorSensor, _ = wm.meter.Int64Gauge("weather_batt_outdoor_sensor",
metric.WithDescription("Outdoor Equipment Battery"))
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.BatteryStatus, _ = wm.meter.Int64Gauge(MetricPrefix+"_battery_status",
metric.WithDescription("Per-component battery status"))
wm.BaromRelativeIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_relative_in",
metric.WithDescription("Relative Pressure in Inches of Mercury"))
wm.BaromAbsoluteIn, _ = wm.meter.Float64Gauge("weather_barometric_pressure_absolute_in",
wm.BaromAbsoluteIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_absolute_in",
metric.WithDescription("Absolute Pressure in Inches of Mercury"))
wm.DewPointF, _ = wm.meter.Float64Gauge("weather_dew_point_f",
wm.DewPointF, _ = wm.meter.Float64Gauge(MetricPrefix+"_dew_point_f",
metric.WithDescription("Dew Point in Faherenheit"))
wm.WindChillF, _ = wm.meter.Float64Gauge("weather_wind_chill_f",
wm.WindChillF, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_chill_f",
metric.WithDescription("Wind Chill in Faherenheit"))
// Internal Telemetry
wm.UpdatesReceived, _ = wm.meter.Int64Counter("weather_updates_received",
wm.UpdatesReceived, _ = wm.meter.Int64Counter(MetricPrefix+"_updates_received",
metric.WithDescription("Metric Updates Processed by Exporter"))
return wm
}
func (wm *WeatherMetrics) Update(u *WeatherUpdate) {
attributes := attribute.NewSet(
attributes := []attribute.KeyValue{
semconv.ServiceVersion(wm.cfg.Version),
attribute.String("station_type", u.StationType),
)
}
if u.StationType != nil {
attributes = append(attributes,
attribute.String("station_type", *u.StationType))
}
if u.StationInfo != nil {
if u.StationInfo.Name != nil {
attributes = append(attributes,
attribute.String("station_name", *u.StationInfo.Name))
}
if u.StationInfo.Equipment != nil {
attributes = append(attributes,
attribute.String("station_equipment", *u.StationInfo.Equipment))
}
}
wm.TempOutdoorF.Record(wm.appCtx, u.TempOutdoorF, metric.WithAttributeSet(attributes))
wm.TempIndoorF.Record(wm.appCtx, u.TempIndoorF, metric.WithAttributeSet(attributes))
wm.HumidityOudoor.Record(wm.appCtx, int64(u.HumidityOudoor), metric.WithAttributeSet(attributes))
wm.HumidityIndoor.Record(wm.appCtx, int64(u.HumidityIndoor), metric.WithAttributeSet(attributes))
wm.WindSpeedMPH.Record(wm.appCtx, u.WindSpeedMPH, metric.WithAttributeSet(attributes))
wm.WindGustMPH.Record(wm.appCtx, u.WindGustMPH, metric.WithAttributeSet(attributes))
wm.MaxDailyGust.Record(wm.appCtx, u.MaxDailyGust, metric.WithAttributeSet(attributes))
wm.WindDir.Record(wm.appCtx, int64(u.WindDir), metric.WithAttributeSet(attributes))
wm.WindDirAvg10m.Record(wm.appCtx, int64(u.WindDirAvg10m), metric.WithAttributeSet(attributes))
wm.UV.Record(wm.appCtx, int64(u.UV), metric.WithAttributeSet(attributes))
wm.SolarRadiation.Record(wm.appCtx, u.SolarRadiation, metric.WithAttributeSet(attributes))
wm.HourlyRainIn.Record(wm.appCtx, u.HourlyRainIn, metric.WithAttributeSet(attributes))
wm.EventRainIn.Record(wm.appCtx, u.EventRainIn, metric.WithAttributeSet(attributes))
wm.DailyRainIn.Record(wm.appCtx, u.DailyRainIn, metric.WithAttributeSet(attributes))
wm.WeeklyRainIn.Record(wm.appCtx, u.WeeklyRainIn, metric.WithAttributeSet(attributes))
wm.MonthlyRainIn.Record(wm.appCtx, u.MonthlyRainIn, metric.WithAttributeSet(attributes))
wm.YearlyRainIn.Record(wm.appCtx, u.YearlyRainIn, 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.BaromAbsoluteIn.Record(wm.appCtx, u.BaromAbsoluteIn, metric.WithAttributeSet(attributes))
wm.DewPointF.Record(wm.appCtx, u.DewPointF, metric.WithAttributeSet(attributes))
wm.WindChillF.Record(wm.appCtx, u.WindChillF, metric.WithAttributeSet(attributes))
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})
// 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.UpdatesReceived.Add(wm.appCtx, 1)
}

View File

@ -0,0 +1,104 @@
package weather
import (
"context"
"errors"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
type MetricRecorder struct {
ctx context.Context
l *zerolog.Logger
}
type RecordOpts struct {
Float64Gauge metric.Float64Gauge
Int64Gauge metric.Int64Gauge
IntVal *int
FloatVal *float64
Attributes []attribute.KeyValue
Field WeatherUpdateField
StationInfo *StationInfo
}
func (r *MetricRecorder) Record(opts *RecordOpts) {
if opts.StationInfo != nil && !opts.keep() {
r.l.Trace().
Str("field", string(opts.Field)).
Str("station", *opts.StationInfo.Name).
Msg("Metric dropped by station config")
return
} else if opts.Int64Gauge == nil && opts.Float64Gauge == nil {
r.l.Err(errors.New("neither int nor float gauge provided")).Send()
return
}
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)
}
log.Msg("Dropping nil int metric")
return
}
r.recordInt(opts.Int64Gauge, *opts.IntVal, opts.Attributes...)
} 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)
}
log.Msg("Dropping nil float metric")
return
}
r.recordFloat(opts.Float64Gauge, *opts.FloatVal, opts.Attributes...)
}
}
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 f == o.Field {
return true
}
}
return false
}
for _, f := range o.StationInfo.Drop {
if f == o.Field {
return false
}
}
return true
}
func (r *MetricRecorder) recordInt(
m metric.Int64Gauge, value int, attributes ...attribute.KeyValue,
) {
// Prepare metric attributes
options := make([]metric.RecordOption, 0, len(attributes))
if len(attributes) > 0 {
options = append(options, metric.WithAttributes(attributes...))
}
val := int64(value)
m.Record(r.ctx, val, options...)
}
func (r *MetricRecorder) recordFloat(
m metric.Float64Gauge, value float64, attributes ...attribute.KeyValue,
) {
// Prepare metric attributes
options := make([]metric.RecordOption, 0, len(attributes))
if len(attributes) > 0 {
options = append(options, metric.WithAttributes(attributes...))
}
m.Record(r.ctx, value, options...)
}

View File

@ -7,33 +7,78 @@ import (
// Stable intermediate struct containing superset of fields
// between AWN and Wunderground style updates from Ambient devices
type WeatherUpdate struct {
DateUTC *time.Time
StationType string
TempOutdoorF float64
TempIndoorF float64
HumidityOudoor int
HumidityIndoor int
WindSpeedMPH float64
WindGustMPH float64
MaxDailyGust float64
WindDir int
WindDirAvg10m int
UV int
SolarRadiation float64
HourlyRainIn float64
EventRainIn float64
DailyRainIn float64
WeeklyRainIn float64
MonthlyRainIn float64
YearlyRainIn float64
TotalRainIn float64
BattOutdoorSensor int
BattIndoorSensor int
BattRainSensor int
BaromRelativeIn float64
BaromAbsoluteIn float64
DateUTC *time.Time
StationInfo *StationInfo
StationID *string
StationType *string
TempOutdoorF *float64
TempIndoorF *float64
HumidityOudoor *int
HumidityIndoor *int
WindSpeedMPH *float64
WindGustMPH *float64
MaxDailyGust *float64
WindDir *int
WindDirAvg10m *int
UV *int
SolarRadiation *float64
HourlyRainIn *float64
EventRainIn *float64
DailyRainIn *float64
WeeklyRainIn *float64
MonthlyRainIn *float64
YearlyRainIn *float64
TotalRainIn *float64
Batteries []BatteryStatus
BaromRelativeIn *float64
BaromAbsoluteIn *float64
// These fields may be calculated
// if not otherwise set
DewPointF float64
WindChillF float64
DewPointF *float64
WindChillF *float64
}
type StationInfo struct {
Type *string
Equipment *string
Name *string
Keep []WeatherUpdateField
Drop []WeatherUpdateField
}
type BatteryStatus struct {
Component string
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"
)

8
unset_env.sh Executable file
View File

@ -0,0 +1,8 @@
#!env sh
unset APP_NAME
unset APP_LOG_LEVEL
unset APP_LOG_FORMAT
unset APP_LOG_TIME_FORMAT
unset APP_HTTP_LISTEN
unset APP_OTEL_STDOUT_ENABLED
unset APP_OTEL_METRIC_INTERVAL_SECS