ambient-local-exporter/pkg/provider/wunderground/proxy.go

82 lines
2.7 KiB
Go
Raw Normal View History

2025-01-12 22:23:32 +00:00
package wunderground
import (
"context"
"errors"
"net/url"
"time"
"github.com/go-resty/resty/v2"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"k8s.io/utils/ptr"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/weather"
)
func (wu *WUProvider) ProxyReq(ctx context.Context, update *weather.WeatherUpdate) error {
tracer := otel.GetTracer(ctx, "wuProvider", "proxyReq")
ctx, span := tracer.Start(ctx, "proxyToWunderground")
defer span.End()
if update.StationConfig.WundergroundID == "" {
err := errors.New("no wunderground id set in update")
span.RecordError(err)
return err
} else if update.StationConfig.WundergroundPassword == "" {
err := errors.New("no wunderground id set in update")
span.RecordError(err)
return err
}
params := updateToWuParams(update)
resp, err := resty.New().R().
SetContext(ctx).
SetQueryParamsFromValues(*params).
Get(wuURL)
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
}
span.SetAttributes(
attribute.Int("statusCode", resp.StatusCode()),
attribute.String("body", string(resp.Body())),
)
return err
}
func updateToWuParams(u *weather.WeatherUpdate) *url.Values {
params := &url.Values{}
params.Set("dateutc", time.Now().Format(time.DateTime))
weather.SetURLVal(params, "ID", &u.StationConfig.WundergroundID)
weather.SetURLVal(params, "PASSWORD", &u.StationConfig.WundergroundPassword)
weather.SetURLVal(params, "UV", u.UV)
weather.SetURLVal(params, "action", ptr.To("updateraw"))
weather.SetURLVal(params, "baromin", u.BaromRelativeIn)
weather.SetURLVal(params, "dailyrainin", u.DailyRainIn)
weather.SetURLVal(params, "dewptf", u.DewPointF)
weather.SetURLVal(params, "humidity", u.HumidityOudoor)
weather.SetURLVal(params, "indoorhumidity", u.HumidityIndoor)
weather.SetURLVal(params, "indoortempf", u.TempIndoorF)
weather.SetURLVal(params, "lowbatt", ptr.To(0))
weather.SetURLVal(params, "monthlyrainin", u.MonthlyRainIn)
weather.SetURLVal(params, "rainin", u.HourlyRainIn)
weather.SetURLVal(params, "realtime", ptr.To(1))
weather.SetURLVal(params, "rtfreq", ptr.To(60))
weather.SetURLVal(params, "softwaretype", u.StationType)
weather.SetURLVal(params, "solarradiation", u.SolarRadiation)
weather.SetURLVal(params, "tempf", u.TempOutdoorF)
weather.SetURLVal(params, "weeklyrainin", u.WeeklyRainIn)
weather.SetURLVal(params, "windchillf", u.WindChillF)
weather.SetURLVal(params, "winddir", u.WindDir)
weather.SetURLVal(params, "windgustmph", u.WindGustMPH)
weather.SetURLVal(params, "windspeedmph", u.WindSpeedMPH)
weather.SetURLVal(params, "yearlyrainin", u.YearlyRainIn)
return params
}