Start Ambient local exporter

This commit is contained in:
2025-01-04 13:15:19 -05:00
parent 774bbafad9
commit 9094cb3d3e
6 changed files with 323 additions and 0 deletions

61
pkg/ambient/http.go Normal file
View File

@ -0,0 +1,61 @@
package ambient
import (
"context"
"io"
"net/http"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
"github.com/rs/zerolog"
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/awn"
)
func GetAWNHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) {
l := zerolog.Ctx(appCtx)
tracer := otel.GetTracer(appCtx, "awn.http.handler")
return func(w http.ResponseWriter, r *http.Request) {
_, span := tracer.Start(r.Context(), "update")
defer span.End()
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
l.Trace().Bytes("body", bodyBytes)
l.Trace().Any("request", r.URL.Query()).Send()
update, err := awn.UnmarshalQueryParams(r.URL.Query())
if err != nil {
l.Err(err).Send()
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
l.Trace().Any("update", update).Send()
w.Write([]byte("ok"))
}
}
func GetWundergroundHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) {
l := zerolog.Ctx(appCtx)
tracer := otel.GetTracer(appCtx, "wunderground.http.handler")
return func(w http.ResponseWriter, r *http.Request) {
_, span := tracer.Start(r.Context(), "update")
defer span.End()
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
l.Trace().Bytes("body", bodyBytes)
l.Trace().Any("request", r.URL.Query()).Send()
w.Write([]byte("ok"))
}
}

47
pkg/awn/types.go Normal file
View File

@ -0,0 +1,47 @@
package awn
import (
"net/url"
"github.com/gorilla/schema"
)
type AmbientWeatherUpdate struct {
PassKey string `json:"PASSKEY,omitempty"`
StationType string `json:"stationtype,omitempty"`
DateUTC string `json:"dateutc,omitempty"`
TempF float32 `json:"tempf,omitempty"`
Humidity int `json:"humidity,omitempty"`
WindSpeedMPH float32 `json:"windspeedmph,omitempty"`
WindGustMPH float32 `json:"windgustmph,omitempty"`
MaxDailyGust float32 `json:"maxdailygust,omitempty"`
WindDir int `json:"winddir,omitempty"`
WindDirAVG10m int `json:"winddir_avg10m,omitempty"`
UV int `json:"uv,omitempty"`
SolarRadiation float32 `json:"solarradiation,omitempty"`
HourlyRainIn float32 `json:"hourlyrainin,omitempty"`
EventRainIn float32 `json:"eventrainin,omitempty"`
DailyRainIn float32 `json:"dailyrainin,omitempty"`
WeeklyRainIn float32 `json:"weeklyrainin,omitempty"`
MonthlyRainIn float32 `json:"monthlyrainin,omitempty"`
YearlyRainIn float32 `json:"yearlyrainin,omitempty"`
TotalRainIn float32 `json:"totalrainin,omitempty"`
BattOut int `json:"battout,omitempty"`
BattRain int `json:"battrain,omitempty"`
TempInF float32 `json:"tempinf,omitempty"`
HumidityIn int `json:"humidityin,omitempty"`
BaromRelIn float32 `json:"baromrelin,omitempty"`
BaromAbsIn float32 `json:"baromabsin,omitempty"`
BattIn int `json:"battin,omitempty"`
}
func UnmarshalQueryParams(query url.Values) (*AmbientWeatherUpdate, error) {
update := new(AmbientWeatherUpdate)
decoder := schema.NewDecoder()
if err := decoder.Decode(update, query); err != nil {
return nil, err
}
return update, nil
}