2025-01-04 22:51:55 +00:00
|
|
|
package wunderground
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/schema"
|
|
|
|
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/weather"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WUProvider struct{}
|
|
|
|
|
2025-01-12 20:30:37 +00:00
|
|
|
const (
|
|
|
|
providerName = "weatherunderground"
|
|
|
|
wuURL = "http://rtupdate.wunderground.com/weatherstation/updateweatherstation.php"
|
|
|
|
)
|
2025-01-04 22:51:55 +00:00
|
|
|
|
|
|
|
func (wu *WUProvider) Name() string {
|
|
|
|
return providerName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes an inbound request from the ambient device and maps
|
|
|
|
// to a stable struct for weather updates
|
|
|
|
func (wu *WUProvider) ReqToWeather(_ context.Context, r *http.Request) (
|
|
|
|
*weather.WeatherUpdate, error,
|
|
|
|
) {
|
|
|
|
wuUpdate, err := UnmarshalQueryParams(r.URL.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return MapWUUpdate(wuUpdate), nil
|
|
|
|
}
|
|
|
|
|
2025-01-12 20:30:37 +00:00
|
|
|
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()
|
|
|
|
//
|
|
|
|
// resp, err := resty.New().R().
|
|
|
|
// SetContext(ctx).
|
|
|
|
// SetQueryParamsFromValues(r.URL.Query()).
|
|
|
|
// 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
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-01-04 22:51:55 +00:00
|
|
|
func MapWUUpdate(wuUpdate *WundergroundUpdate) *weather.WeatherUpdate {
|
2025-01-09 02:28:38 +00:00
|
|
|
updateTime := time.Now()
|
|
|
|
|
|
|
|
if wuUpdate.DateUTC != nil {
|
|
|
|
ut, err := time.Parse(time.DateTime, *wuUpdate.DateUTC)
|
|
|
|
if err == nil {
|
|
|
|
updateTime = ut
|
|
|
|
}
|
2025-01-04 22:51:55 +00:00
|
|
|
}
|
|
|
|
|
2025-01-12 20:30:37 +00:00
|
|
|
credentials := make(map[string]string)
|
|
|
|
if wuUpdate.ID != nil && wuUpdate.Password != nil {
|
|
|
|
credentials["ID"] = *wuUpdate.ID
|
|
|
|
credentials["Password"] = *wuUpdate.Password
|
|
|
|
}
|
|
|
|
|
2025-01-04 22:51:55 +00:00
|
|
|
return &weather.WeatherUpdate{
|
2025-01-12 20:30:37 +00:00
|
|
|
DateUTC: &updateTime,
|
|
|
|
StationID: wuUpdate.ID,
|
|
|
|
StationType: wuUpdate.SoftwareType,
|
|
|
|
TempOutdoorF: wuUpdate.Tempf,
|
|
|
|
HumidityOudoor: wuUpdate.Humidity,
|
|
|
|
WindSpeedMPH: wuUpdate.WindGustMPH,
|
|
|
|
WindGustMPH: wuUpdate.WindGustMPH,
|
|
|
|
WindDir: wuUpdate.WindDir,
|
|
|
|
UV: wuUpdate.UV,
|
|
|
|
SolarRadiation: wuUpdate.SolarRadiation,
|
|
|
|
HourlyRainIn: wuUpdate.RainIn,
|
|
|
|
DailyRainIn: wuUpdate.DailyRainIn,
|
|
|
|
WeeklyRainIn: wuUpdate.WeeklyRainIn,
|
|
|
|
MonthlyRainIn: wuUpdate.MonthlyRainIn,
|
|
|
|
YearlyRainIn: wuUpdate.YearlyRainIn,
|
|
|
|
TempIndoorF: wuUpdate.IndoorTempF,
|
|
|
|
HumidityIndoor: wuUpdate.IndoorHumidity,
|
|
|
|
BaromRelativeIn: wuUpdate.BaromIn,
|
|
|
|
WeatherServiceCredentials: credentials,
|
2025-01-04 22:51:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalQueryParams(query url.Values) (*WundergroundUpdate, error) {
|
|
|
|
update := new(WundergroundUpdate)
|
|
|
|
|
|
|
|
if err := schema.NewDecoder().Decode(update, query); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return update, nil
|
|
|
|
}
|