155 lines
4.0 KiB
Go
Raw Normal View History

2025-01-04 17:51:55 -05: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 15:30:37 -05:00
const (
providerName = "awn"
awnURL = "http://ambientweather.net/data/report"
)
2025-01-04 17:51:55 -05:00
2025-01-12 17:49:54 -05:00
// Battery Sensors
const (
BattOutdoorSensor = "OutdoorSensor"
BattIndoorSensor = "IndoorSensor"
BattRainSensor = "RainSensor"
BattCO2Sensor = "CO2Sensor"
2025-03-04 20:11:08 -05:00
THSensor = "TempHumiditySensor"
2025-01-12 17:49:54 -05:00
)
2025-01-04 17:51:55 -05: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-08 21:28:38 -05:00
updateTime := time.Now()
if awnUpdate.DateUTC != nil {
ut, err := time.Parse(time.DateTime, *awnUpdate.DateUTC)
if err == nil {
updateTime = ut
}
2025-01-04 17:51:55 -05:00
}
return &weather.WeatherUpdate{
2025-01-07 10:27:02 -05:00
DateUTC: &updateTime,
2025-01-08 21:28:38 -05:00
StationID: awnUpdate.PassKey,
StationType: awnUpdate.StationType,
2025-01-07 10:27:02 -05:00
TempOutdoorF: awnUpdate.TempF,
HumidityOudoor: awnUpdate.Humidity,
2025-01-08 14:24:56 -05:00
WindSpeedMPH: awnUpdate.WindSpeedMPH,
2025-01-07 10:27:02 -05: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 17:49:54 -05:00
Component: BattOutdoorSensor,
2025-01-07 10:27:02 -05:00
Status: awnUpdate.BattOut,
},
{
2025-01-12 17:49:54 -05:00
Component: BattIndoorSensor,
2025-01-07 10:27:02 -05:00
Status: awnUpdate.BattIn,
},
{
2025-01-12 17:49:54 -05:00
Component: BattRainSensor,
2025-01-07 10:27:02 -05:00
Status: awnUpdate.BattRain,
},
{
2025-01-12 17:49:54 -05:00
Component: BattCO2Sensor,
2025-01-07 10:27:02 -05:00
Status: awnUpdate.BattCO2,
},
2025-03-04 20:11:08 -05:00
// Temp and Humidity Sensors
{
Component: THSensor + "1",
Status: awnUpdate.Batt1,
},
{
Component: THSensor + "2",
Status: awnUpdate.Batt2,
},
{
Component: THSensor + "3",
Status: awnUpdate.Batt3,
},
{
Component: THSensor + "4",
Status: awnUpdate.Batt4,
},
{
Component: THSensor + "5",
Status: awnUpdate.Batt5,
},
{
Component: THSensor + "6",
Status: awnUpdate.Batt6,
},
{
Component: THSensor + "7",
Status: awnUpdate.Batt7,
},
{
Component: THSensor + "8",
Status: awnUpdate.Batt8,
},
2025-01-07 10:27:02 -05:00
},
2025-01-12 17:23:32 -05:00
TempIndoorF: awnUpdate.TempInF,
HumidityIndoor: awnUpdate.HumidityIn,
BaromRelativeIn: awnUpdate.BaromRelIn,
BaromAbsoluteIn: awnUpdate.BaromAbsIn,
2025-03-04 20:11:08 -05:00
// TODO: Permit mapping to config name
TempHumiditySensors: []*weather.TempHumiditySensor{
{Name: "Sensor1", TempF: awnUpdate.Temp1F, Humidity: awnUpdate.Humidity1},
{Name: "Sensor2", TempF: awnUpdate.Temp2F, Humidity: awnUpdate.Humidity2},
{Name: "Sensor3", TempF: awnUpdate.Temp3F, Humidity: awnUpdate.Humidity3},
{Name: "Sensor4", TempF: awnUpdate.Temp4F, Humidity: awnUpdate.Humidity4},
{Name: "Sensor5", TempF: awnUpdate.Temp5F, Humidity: awnUpdate.Humidity5},
{Name: "Sensor6", TempF: awnUpdate.Temp6F, Humidity: awnUpdate.Humidity6},
{Name: "Sensor7", TempF: awnUpdate.Temp7F, Humidity: awnUpdate.Humidity7},
{Name: "Sensor8", TempF: awnUpdate.Temp8F, Humidity: awnUpdate.Humidity8},
},
2025-01-04 17:51:55 -05: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
}