Refactor and complete providers
This commit is contained in:
70
pkg/provider/wunderground/provider.go
Normal file
70
pkg/provider/wunderground/provider.go
Normal file
@ -0,0 +1,70 @@
|
||||
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{}
|
||||
|
||||
const providerName = "weatherunderground"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func MapWUUpdate(wuUpdate *WundergroundUpdate) *weather.WeatherUpdate {
|
||||
updateTime, err := time.Parse(time.DateTime, wuUpdate.DateUTC)
|
||||
if err != nil {
|
||||
updateTime = time.Now()
|
||||
}
|
||||
|
||||
return &weather.WeatherUpdate{
|
||||
StationType: wuUpdate.SoftwareType,
|
||||
DateUTC: &updateTime,
|
||||
TempF: wuUpdate.Tempf,
|
||||
Humidity: 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,
|
||||
TempInsideF: wuUpdate.IndoorTempF,
|
||||
HumidityInside: wuUpdate.IndoorHumidity,
|
||||
BaromRelativeIn: wuUpdate.BaromIn,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user