Refactor and complete providers
This commit is contained in:
79
pkg/provider/awn/provider.go
Normal file
79
pkg/provider/awn/provider.go
Normal file
@ -0,0 +1,79 @@
|
||||
package awn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/schema"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
type AWNProvider struct{}
|
||||
|
||||
const providerName = "awn"
|
||||
|
||||
func (awn *AWNProvider) Name() string {
|
||||
return providerName
|
||||
}
|
||||
|
||||
// Takes an inbound request from the ambient device and maps
|
||||
// to a stable struct for weather updates
|
||||
func (awn *AWNProvider) ReqToWeather(_ context.Context, r *http.Request) (
|
||||
*weather.WeatherUpdate, error,
|
||||
) {
|
||||
awnUpdate, err := UnmarshalQueryParams(r.URL.Query())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return MapAwnUpdate(awnUpdate), nil
|
||||
}
|
||||
|
||||
func MapAwnUpdate(awnUpdate *AmbientWeatherUpdate) *weather.WeatherUpdate {
|
||||
updateTime, err := time.Parse(time.DateTime, awnUpdate.DateUTC)
|
||||
if err != nil {
|
||||
updateTime = time.Now()
|
||||
}
|
||||
|
||||
return &weather.WeatherUpdate{
|
||||
StationType: awnUpdate.StationType,
|
||||
DateUTC: &updateTime,
|
||||
TempF: awnUpdate.TempF,
|
||||
Humidity: 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,
|
||||
TempInsideF: awnUpdate.TempInF,
|
||||
HumidityInside: awnUpdate.HumidityIn,
|
||||
BaromRelativeIn: awnUpdate.BaromRelIn,
|
||||
BaromAbsoluteIn: awnUpdate.BaromAbsIn,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user