ambient-local-exporter/pkg/provider/awn/provider.go

110 lines
2.6 KiB
Go
Raw Normal View History

2025-01-04 22:51:55 +00:00
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{}
2025-01-12 20:30:37 +00:00
const (
providerName = "awn"
awnURL = "http://ambientweather.net/data/report"
)
2025-01-04 22:51:55 +00:00
2025-01-12 22:49:54 +00:00
// Battery Sensors
const (
BattOutdoorSensor = "OutdoorSensor"
BattIndoorSensor = "IndoorSensor"
BattRainSensor = "RainSensor"
BattCO2Sensor = "CO2Sensor"
)
2025-01-04 22:51:55 +00:00
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 {
2025-01-09 02:28:38 +00:00
updateTime := time.Now()
if awnUpdate.DateUTC != nil {
ut, err := time.Parse(time.DateTime, *awnUpdate.DateUTC)
if err == nil {
updateTime = ut
}
2025-01-04 22:51:55 +00:00
}
return &weather.WeatherUpdate{
2025-01-07 15:27:02 +00:00
DateUTC: &updateTime,
2025-01-09 02:28:38 +00:00
StationID: awnUpdate.PassKey,
StationType: awnUpdate.StationType,
2025-01-07 15:27:02 +00:00
TempOutdoorF: awnUpdate.TempF,
HumidityOudoor: awnUpdate.Humidity,
2025-01-08 19:24:56 +00:00
WindSpeedMPH: awnUpdate.WindSpeedMPH,
2025-01-07 15:27:02 +00:00
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,
Batteries: []weather.BatteryStatus{
{
2025-01-12 22:49:54 +00:00
Component: BattOutdoorSensor,
2025-01-07 15:27:02 +00:00
Status: awnUpdate.BattOut,
},
{
2025-01-12 22:49:54 +00:00
Component: BattIndoorSensor,
2025-01-07 15:27:02 +00:00
Status: awnUpdate.BattIn,
},
{
2025-01-12 22:49:54 +00:00
Component: BattRainSensor,
2025-01-07 15:27:02 +00:00
Status: awnUpdate.BattRain,
},
{
2025-01-12 22:49:54 +00:00
Component: BattCO2Sensor,
2025-01-07 15:27:02 +00:00
Status: awnUpdate.BattCO2,
},
},
2025-01-12 22:23:32 +00:00
TempIndoorF: awnUpdate.TempInF,
HumidityIndoor: awnUpdate.HumidityIn,
BaromRelativeIn: awnUpdate.BaromRelIn,
BaromAbsoluteIn: awnUpdate.BaromAbsIn,
2025-01-04 22:51:55 +00:00
}
}
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
}