weatherfeeder: split the former maximumdirect.net/weatherd project in two.
feedkit now contains a reusable core, while weatherfeeder is a concrete implementation that includes weather-specific functions.
This commit is contained in:
56
internal/sources/builtins.go
Normal file
56
internal/sources/builtins.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package sources
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/nws"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/openmeteo"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/openweather"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
fksource "gitea.maximumdirect.net/ejr/feedkit/sources"
|
||||
)
|
||||
|
||||
// RegisterBuiltins registers the source drivers that ship with this binary.
|
||||
// Keeping this in one place makes main.go very readable.
|
||||
func RegisterBuiltins(r *fksource.Registry) {
|
||||
// NWS drivers
|
||||
r.Register("nws_observation", func(cfg config.SourceConfig) (fksource.Source, error) {
|
||||
return nws.NewObservationSource(cfg)
|
||||
})
|
||||
r.Register("nws_alerts", func(cfg config.SourceConfig) (fksource.Source, error) {
|
||||
return nws.NewAlertsSource(cfg)
|
||||
})
|
||||
r.Register("nws_forecast", func(cfg config.SourceConfig) (fksource.Source, error) {
|
||||
return nws.NewForecastSource(cfg)
|
||||
})
|
||||
|
||||
// Open-Meteo drivers
|
||||
r.Register("openmeteo_observation", func(cfg config.SourceConfig) (fksource.Source, error) {
|
||||
return openmeteo.NewObservationSource(cfg)
|
||||
})
|
||||
|
||||
// OpenWeatherMap drivers
|
||||
r.Register("openweather_observation", func(cfg config.SourceConfig) (fksource.Source, error) {
|
||||
return openweather.NewObservationSource(cfg)
|
||||
})
|
||||
}
|
||||
|
||||
// Optional: centralize some common config checks used by multiple drivers.
|
||||
//
|
||||
// NOTE: feedkit/config.SourceConfig intentionally keeps driver-specific options
|
||||
// inside cfg.Params, so drivers can evolve independently without feedkit
|
||||
// importing domain config packages.
|
||||
func RequireURL(cfg config.SourceConfig) error {
|
||||
if cfg.Params == nil {
|
||||
return fmt.Errorf("source %q: params.url is required", cfg.Name)
|
||||
}
|
||||
|
||||
// Canonical key is "url". We also accept "URL" as a convenience.
|
||||
url, ok := cfg.ParamString("url", "URL")
|
||||
if !ok {
|
||||
return fmt.Errorf("source %q: params.url is required", cfg.Name)
|
||||
}
|
||||
_ = url // (optional) return it if you want this helper to provide the value
|
||||
return nil
|
||||
}
|
||||
54
internal/sources/nws/alerts.go
Normal file
54
internal/sources/nws/alerts.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package nws
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
)
|
||||
|
||||
type AlertsSource struct {
|
||||
name string
|
||||
url string
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func NewAlertsSource(cfg config.SourceConfig) (*AlertsSource, error) {
|
||||
if strings.TrimSpace(cfg.Name) == "" {
|
||||
return nil, fmt.Errorf("nws_alerts: name is required")
|
||||
}
|
||||
if cfg.Params == nil {
|
||||
return nil, fmt.Errorf("nws_alerts %q: params are required (need params.url and params.user_agent)", cfg.Name)
|
||||
}
|
||||
|
||||
// Driver-specific options live in cfg.Params to keep feedkit domain-agnostic.
|
||||
// Use the typed accessor so callers can’t accidentally pass non-strings to TrimSpace.
|
||||
url, ok := cfg.ParamString("url", "URL")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nws_alerts %q: params.url is required", cfg.Name)
|
||||
}
|
||||
|
||||
ua, ok := cfg.ParamString("user_agent", "userAgent")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nws_alerts %q: params.user_agent is required", cfg.Name)
|
||||
}
|
||||
|
||||
return &AlertsSource{
|
||||
name: cfg.Name,
|
||||
url: url,
|
||||
userAgent: ua,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *AlertsSource) Name() string { return s.name }
|
||||
|
||||
// Kind is used for routing/policy.
|
||||
// The envelope type is event.Event; payload will eventually be something like model.WeatherAlert.
|
||||
func (s *AlertsSource) Kind() event.Kind { return event.Kind("alert") }
|
||||
|
||||
func (s *AlertsSource) Poll(ctx context.Context) ([]event.Event, error) {
|
||||
_ = ctx
|
||||
return nil, fmt.Errorf("nws.AlertsSource.Poll: TODO implement (url=%s)", s.url)
|
||||
}
|
||||
51
internal/sources/nws/forecast.go
Normal file
51
internal/sources/nws/forecast.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package nws
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
)
|
||||
|
||||
type ForecastSource struct {
|
||||
name string
|
||||
url string
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func NewForecastSource(cfg config.SourceConfig) (*ForecastSource, error) {
|
||||
if strings.TrimSpace(cfg.Name) == "" {
|
||||
return nil, fmt.Errorf("nws_forecast: name is required")
|
||||
}
|
||||
if cfg.Params == nil {
|
||||
return nil, fmt.Errorf("nws_forecast %q: params are required (need params.url and params.user_agent)", cfg.Name)
|
||||
}
|
||||
|
||||
url, ok := cfg.ParamString("url", "URL")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nws_forecast %q: params.url is required", cfg.Name)
|
||||
}
|
||||
|
||||
ua, ok := cfg.ParamString("user_agent", "userAgent")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nws_forecast %q: params.user_agent is required", cfg.Name)
|
||||
}
|
||||
|
||||
return &ForecastSource{
|
||||
name: cfg.Name,
|
||||
url: url,
|
||||
userAgent: ua,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *ForecastSource) Name() string { return s.name }
|
||||
|
||||
// Kind is used for routing/policy.
|
||||
func (s *ForecastSource) Kind() event.Kind { return event.Kind("forecast") }
|
||||
|
||||
func (s *ForecastSource) Poll(ctx context.Context) ([]event.Event, error) {
|
||||
_ = ctx
|
||||
return nil, fmt.Errorf("nws.ForecastSource.Poll: TODO implement (url=%s)", s.url)
|
||||
}
|
||||
709
internal/sources/nws/observation.go
Normal file
709
internal/sources/nws/observation.go
Normal file
@@ -0,0 +1,709 @@
|
||||
package nws
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/model"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/standards"
|
||||
)
|
||||
|
||||
// ObservationSource polls an NWS station observation endpoint and emits a single Observation Event.
|
||||
//
|
||||
// This corresponds to URLs like:
|
||||
//
|
||||
// https://api.weather.gov/stations/KSTL/observations/latest
|
||||
type ObservationSource struct {
|
||||
name string
|
||||
url string
|
||||
userAgent string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewObservationSource(cfg config.SourceConfig) (*ObservationSource, error) {
|
||||
if strings.TrimSpace(cfg.Name) == "" {
|
||||
return nil, fmt.Errorf("nws_observation: name is required")
|
||||
}
|
||||
if cfg.Params == nil {
|
||||
return nil, fmt.Errorf("nws_observation %q: params are required (need params.url and params.user_agent)", cfg.Name)
|
||||
}
|
||||
|
||||
// feedkit keeps config domain-agnostic by storing driver-specific settings in Params.
|
||||
// Use ParamString so we don't have to type-assert cfg.Params["url"] everywhere.
|
||||
url, ok := cfg.ParamString("url", "URL")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nws_observation %q: params.url is required", cfg.Name)
|
||||
}
|
||||
|
||||
ua, ok := cfg.ParamString("user_agent", "userAgent")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nws_observation %q: params.user_agent is required", cfg.Name)
|
||||
}
|
||||
|
||||
// A small timeout is good hygiene for daemons: you want polls to fail fast,
|
||||
// not hang forever and block subsequent ticks.
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
return &ObservationSource{
|
||||
name: cfg.Name,
|
||||
url: url,
|
||||
userAgent: ua,
|
||||
client: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *ObservationSource) Name() string { return s.name }
|
||||
|
||||
// Kind is used for routing/policy.
|
||||
func (s *ObservationSource) Kind() event.Kind { return event.Kind("observation") }
|
||||
|
||||
// Poll fetches "current conditions" and emits exactly one Event (under normal conditions).
|
||||
func (s *ObservationSource) Poll(ctx context.Context) ([]event.Event, error) {
|
||||
obs, eventID, err := s.fetchAndParse(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// EffectiveAt is optional.
|
||||
// For observations, the natural effective time is the observation timestamp.
|
||||
var effectiveAt *time.Time
|
||||
if !obs.Timestamp.IsZero() {
|
||||
t := obs.Timestamp
|
||||
effectiveAt = &t
|
||||
}
|
||||
|
||||
e := event.Event{
|
||||
ID: eventID,
|
||||
Kind: s.Kind(),
|
||||
Source: s.name,
|
||||
EmittedAt: time.Now().UTC(),
|
||||
EffectiveAt: effectiveAt,
|
||||
|
||||
// Optional: makes downstream decoding/inspection easier.
|
||||
Schema: "weather.observation.v1",
|
||||
|
||||
// Payload remains domain-specific for now.
|
||||
Payload: obs,
|
||||
}
|
||||
|
||||
if err := e.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []event.Event{e}, nil
|
||||
}
|
||||
|
||||
// --- JSON parsing (minimal model of NWS observation payload) ---
|
||||
|
||||
type nwsObservationResponse struct {
|
||||
ID string `json:"id"` // a stable unique identifier URL in the payload you pasted
|
||||
Properties struct {
|
||||
StationID string `json:"stationId"`
|
||||
StationName string `json:"stationName"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
TextDescription string `json:"textDescription"`
|
||||
Icon string `json:"icon"`
|
||||
|
||||
RawMessage string `json:"rawMessage"`
|
||||
|
||||
Elevation struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"elevation"`
|
||||
|
||||
Temperature struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"temperature"`
|
||||
|
||||
Dewpoint struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"dewpoint"`
|
||||
|
||||
WindDirection struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"windDirection"`
|
||||
|
||||
WindSpeed struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"windSpeed"`
|
||||
|
||||
WindGust struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"windGust"`
|
||||
|
||||
BarometricPressure struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"barometricPressure"`
|
||||
|
||||
SeaLevelPressure struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"seaLevelPressure"`
|
||||
|
||||
Visibility struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"visibility"`
|
||||
|
||||
RelativeHumidity struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"relativeHumidity"`
|
||||
|
||||
WindChill struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"windChill"`
|
||||
|
||||
HeatIndex struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"heatIndex"`
|
||||
|
||||
// NWS returns "presentWeather" as decoded METAR phenomena objects.
|
||||
// We decode these initially as generic maps so we can:
|
||||
// 1) preserve the raw objects in model.PresentWeather{Raw: ...}
|
||||
// 2) also decode them into a typed struct for our WMO mapping logic.
|
||||
PresentWeather []map[string]any `json:"presentWeather"`
|
||||
|
||||
CloudLayers []struct {
|
||||
Base struct {
|
||||
UnitCode string `json:"unitCode"`
|
||||
Value *float64 `json:"value"`
|
||||
} `json:"base"`
|
||||
Amount string `json:"amount"`
|
||||
} `json:"cloudLayers"`
|
||||
} `json:"properties"`
|
||||
}
|
||||
|
||||
// metarPhenomenon is a typed view of NWS presentWeather objects.
|
||||
// You provided the schema for these values (intensity/modifier/weather/rawString).
|
||||
type metarPhenomenon struct {
|
||||
Intensity *string `json:"intensity"` // "light", "heavy", or null
|
||||
Modifier *string `json:"modifier"` // "freezing", "showers", etc., or null
|
||||
Weather string `json:"weather"` // e.g., "rain", "snow", "fog_mist", ...
|
||||
RawString string `json:"rawString"`
|
||||
// InVicinity exists in the schema; we ignore it for now because WMO codes
|
||||
// don't directly represent "in vicinity" semantics.
|
||||
InVicinity *bool `json:"inVicinity"`
|
||||
}
|
||||
|
||||
func (s *ObservationSource) fetchAndParse(ctx context.Context) (model.WeatherObservation, string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", s.url, nil)
|
||||
if err != nil {
|
||||
return model.WeatherObservation{}, "", err
|
||||
}
|
||||
|
||||
// NWS requests: a real User-Agent with contact info is strongly recommended.
|
||||
req.Header.Set("User-Agent", s.userAgent)
|
||||
req.Header.Set("Accept", "application/geo+json, application/json")
|
||||
|
||||
res, err := s.client.Do(req)
|
||||
if err != nil {
|
||||
return model.WeatherObservation{}, "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||
return model.WeatherObservation{}, "", fmt.Errorf("nws_observation %q: HTTP %s", s.name, res.Status)
|
||||
}
|
||||
|
||||
var parsed nwsObservationResponse
|
||||
if err := json.NewDecoder(res.Body).Decode(&parsed); err != nil {
|
||||
return model.WeatherObservation{}, "", err
|
||||
}
|
||||
|
||||
// Parse timestamp (RFC3339)
|
||||
var ts time.Time
|
||||
if strings.TrimSpace(parsed.Properties.Timestamp) != "" {
|
||||
t, err := time.Parse(time.RFC3339, parsed.Properties.Timestamp)
|
||||
if err != nil {
|
||||
return model.WeatherObservation{}, "", fmt.Errorf("nws_observation %q: invalid timestamp %q: %w",
|
||||
s.name, parsed.Properties.Timestamp, err)
|
||||
}
|
||||
ts = t
|
||||
}
|
||||
|
||||
cloudLayers := make([]model.CloudLayer, 0, len(parsed.Properties.CloudLayers))
|
||||
for _, cl := range parsed.Properties.CloudLayers {
|
||||
cloudLayers = append(cloudLayers, model.CloudLayer{
|
||||
BaseMeters: cl.Base.Value,
|
||||
Amount: cl.Amount,
|
||||
})
|
||||
}
|
||||
|
||||
// Preserve the raw presentWeather objects (as before) in the domain model.
|
||||
present := make([]model.PresentWeather, 0, len(parsed.Properties.PresentWeather))
|
||||
for _, pw := range parsed.Properties.PresentWeather {
|
||||
present = append(present, model.PresentWeather{Raw: pw})
|
||||
}
|
||||
|
||||
// Decode presentWeather into a typed slice for improved mapping.
|
||||
phenomena := decodeMetarPhenomena(parsed.Properties.PresentWeather)
|
||||
|
||||
// Provider description (NWS vocabulary). We store this for troubleshooting only.
|
||||
providerDesc := strings.TrimSpace(parsed.Properties.TextDescription)
|
||||
|
||||
// Map NWS -> canonical WMO code using best-effort heuristics:
|
||||
// 1) presentWeather (METAR phenomena) if present
|
||||
// 2) provider textDescription keywords
|
||||
// 3) cloud layers fallback
|
||||
wmo := mapNWSToWMO(providerDesc, cloudLayers, phenomena)
|
||||
|
||||
// Canonical text comes from our shared WMO table.
|
||||
// NWS does not give us an explicit day/night flag here, so we leave it nil.
|
||||
canonicalText := standards.WMOText(wmo, nil)
|
||||
|
||||
obs := model.WeatherObservation{
|
||||
StationID: parsed.Properties.StationID,
|
||||
StationName: parsed.Properties.StationName,
|
||||
Timestamp: ts,
|
||||
|
||||
// Canonical conditions
|
||||
ConditionCode: wmo,
|
||||
ConditionText: canonicalText,
|
||||
IsDay: nil,
|
||||
|
||||
// Provider evidence (for troubleshooting mapping)
|
||||
ProviderRawDescription: providerDesc,
|
||||
|
||||
// Human-facing fields:
|
||||
// Populate TextDescription with canonical text so downstream output stays consistent.
|
||||
TextDescription: canonicalText,
|
||||
IconURL: parsed.Properties.Icon,
|
||||
|
||||
TemperatureC: parsed.Properties.Temperature.Value,
|
||||
DewpointC: parsed.Properties.Dewpoint.Value,
|
||||
|
||||
WindDirectionDegrees: parsed.Properties.WindDirection.Value,
|
||||
WindSpeedKmh: parsed.Properties.WindSpeed.Value,
|
||||
WindGustKmh: parsed.Properties.WindGust.Value,
|
||||
|
||||
BarometricPressurePa: parsed.Properties.BarometricPressure.Value,
|
||||
SeaLevelPressurePa: parsed.Properties.SeaLevelPressure.Value,
|
||||
VisibilityMeters: parsed.Properties.Visibility.Value,
|
||||
|
||||
RelativeHumidityPercent: parsed.Properties.RelativeHumidity.Value,
|
||||
WindChillC: parsed.Properties.WindChill.Value,
|
||||
HeatIndexC: parsed.Properties.HeatIndex.Value,
|
||||
|
||||
ElevationMeters: parsed.Properties.Elevation.Value,
|
||||
RawMessage: parsed.Properties.RawMessage,
|
||||
|
||||
PresentWeather: present,
|
||||
CloudLayers: cloudLayers,
|
||||
}
|
||||
|
||||
// Event ID: prefer the NWS-provided "id" (stable unique URL), else fall back to computed.
|
||||
eventID := strings.TrimSpace(parsed.ID)
|
||||
if eventID == "" {
|
||||
eventID = fmt.Sprintf("observation:%s:%s:%s",
|
||||
s.name,
|
||||
obs.StationID,
|
||||
obs.Timestamp.UTC().Format(time.RFC3339Nano),
|
||||
)
|
||||
}
|
||||
|
||||
return obs, eventID, nil
|
||||
}
|
||||
|
||||
func decodeMetarPhenomena(raw []map[string]any) []metarPhenomenon {
|
||||
if len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
out := make([]metarPhenomenon, 0, len(raw))
|
||||
for _, m := range raw {
|
||||
// Encode/decode is slightly inefficient, but it's simple and very readable.
|
||||
// presentWeather payloads are small; this is fine for a polling daemon.
|
||||
b, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
var p metarPhenomenon
|
||||
if err := json.Unmarshal(b, &p); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
p.Weather = strings.ToLower(strings.TrimSpace(p.Weather))
|
||||
p.RawString = strings.TrimSpace(p.RawString)
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// mapNWSToWMO maps NWS signals into a canonical WMO code.
|
||||
//
|
||||
// Precedence:
|
||||
// 1. METAR phenomena (presentWeather) — most reliable for precip/hazards
|
||||
// 2. textDescription keywords — weaker, but still useful
|
||||
// 3. cloud layers fallback — only for sky-only conditions
|
||||
func mapNWSToWMO(providerDesc string, cloudLayers []model.CloudLayer, phenomena []metarPhenomenon) model.WMOCode {
|
||||
// 1) Prefer METAR phenomena if present.
|
||||
if code := wmoFromPhenomena(phenomena); code != model.WMOUnknown {
|
||||
return code
|
||||
}
|
||||
|
||||
// 2) Fall back to provider textDescription keywords.
|
||||
if code := wmoFromTextDescription(providerDesc); code != model.WMOUnknown {
|
||||
return code
|
||||
}
|
||||
|
||||
// 3) Fall back to cloud layers.
|
||||
if code := wmoFromCloudLayers(cloudLayers); code != model.WMOUnknown {
|
||||
return code
|
||||
}
|
||||
|
||||
return model.WMOUnknown
|
||||
}
|
||||
|
||||
func wmoFromPhenomena(phenomena []metarPhenomenon) model.WMOCode {
|
||||
if len(phenomena) == 0 {
|
||||
return model.WMOUnknown
|
||||
}
|
||||
|
||||
// Helper accessors (avoid repeating nil checks everywhere).
|
||||
intensityOf := func(p metarPhenomenon) string {
|
||||
if p.Intensity == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.ToLower(strings.TrimSpace(*p.Intensity))
|
||||
}
|
||||
modifierOf := func(p metarPhenomenon) string {
|
||||
if p.Modifier == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.ToLower(strings.TrimSpace(*p.Modifier))
|
||||
}
|
||||
|
||||
// Pass 1: thunder + hail overrides everything (hazard).
|
||||
//
|
||||
// WMO provides:
|
||||
// 95 = thunderstorm
|
||||
// 96 = light thunderstorms with hail
|
||||
// 99 = thunderstorms with hail
|
||||
hasThunder := false
|
||||
hailIntensity := ""
|
||||
for _, p := range phenomena {
|
||||
switch p.Weather {
|
||||
case "thunderstorms":
|
||||
hasThunder = true
|
||||
case "hail":
|
||||
if hailIntensity == "" {
|
||||
hailIntensity = intensityOf(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
if hasThunder {
|
||||
if hailIntensity != "" || containsWeather(phenomena, "hail") {
|
||||
if hailIntensity == "heavy" {
|
||||
return 99
|
||||
}
|
||||
// Default to "light" hail when unknown
|
||||
return 96
|
||||
}
|
||||
return 95
|
||||
}
|
||||
|
||||
// Pass 2: freezing hazards.
|
||||
//
|
||||
// Modifier includes "freezing".
|
||||
for _, p := range phenomena {
|
||||
if modifierOf(p) != "freezing" {
|
||||
continue
|
||||
}
|
||||
|
||||
switch p.Weather {
|
||||
case "rain":
|
||||
if intensityOf(p) == "light" {
|
||||
return 66
|
||||
}
|
||||
// Default to freezing rain when unknown/heavy.
|
||||
return 67
|
||||
|
||||
case "drizzle":
|
||||
if intensityOf(p) == "light" {
|
||||
return 56
|
||||
}
|
||||
return 57
|
||||
|
||||
case "fog", "fog_mist":
|
||||
// "Freezing fog" isn't a perfect match for "Rime Fog",
|
||||
// but within our current WMO subset, 48 is the closest.
|
||||
return 48
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 3: fog / obscuration.
|
||||
for _, p := range phenomena {
|
||||
switch p.Weather {
|
||||
case "fog", "fog_mist":
|
||||
return 45
|
||||
case "haze", "smoke", "dust", "sand", "spray", "volcanic_ash":
|
||||
// Our current WMO table subset doesn't include haze/smoke/dust codes.
|
||||
// "Foggy" (45) is a reasonable umbrella for "visibility obscured".
|
||||
return 45
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 4: precip families.
|
||||
for _, p := range phenomena {
|
||||
inten := intensityOf(p)
|
||||
mod := modifierOf(p)
|
||||
|
||||
// Handle "showers" modifier explicitly (rain vs snow showers).
|
||||
if mod == "showers" {
|
||||
switch p.Weather {
|
||||
case "rain":
|
||||
if inten == "light" {
|
||||
return 80
|
||||
}
|
||||
if inten == "heavy" {
|
||||
return 82
|
||||
}
|
||||
return 81
|
||||
|
||||
case "snow":
|
||||
if inten == "light" {
|
||||
return 85
|
||||
}
|
||||
return 86
|
||||
}
|
||||
}
|
||||
|
||||
switch p.Weather {
|
||||
// Drizzle
|
||||
case "drizzle":
|
||||
if inten == "heavy" {
|
||||
return 55
|
||||
}
|
||||
if inten == "light" {
|
||||
return 51
|
||||
}
|
||||
return 53
|
||||
|
||||
// Rain
|
||||
case "rain":
|
||||
if inten == "heavy" {
|
||||
return 65
|
||||
}
|
||||
if inten == "light" {
|
||||
return 61
|
||||
}
|
||||
return 63
|
||||
|
||||
// Snow
|
||||
case "snow":
|
||||
if inten == "heavy" {
|
||||
return 75
|
||||
}
|
||||
if inten == "light" {
|
||||
return 71
|
||||
}
|
||||
return 73
|
||||
|
||||
// Snow grains
|
||||
case "snow_grains":
|
||||
return 77
|
||||
|
||||
// We don’t currently have sleet/ice pellet codes in our shared WMO subset.
|
||||
// We make conservative choices within the available codes.
|
||||
case "ice_pellets", "snow_pellets":
|
||||
// Closest within our subset is "Snow" (73). If you later expand the WMO table
|
||||
// to include sleet/ice pellet codes, update this mapping.
|
||||
return 73
|
||||
}
|
||||
}
|
||||
|
||||
return model.WMOUnknown
|
||||
}
|
||||
|
||||
func containsWeather(phenomena []metarPhenomenon, weather string) bool {
|
||||
weather = strings.ToLower(strings.TrimSpace(weather))
|
||||
for _, p := range phenomena {
|
||||
if p.Weather == weather {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func wmoFromTextDescription(providerDesc string) model.WMOCode {
|
||||
s := strings.ToLower(strings.TrimSpace(providerDesc))
|
||||
if s == "" {
|
||||
return model.WMOUnknown
|
||||
}
|
||||
|
||||
// Thunder / hail
|
||||
if strings.Contains(s, "thunder") {
|
||||
if strings.Contains(s, "hail") {
|
||||
return 99
|
||||
}
|
||||
return 95
|
||||
}
|
||||
|
||||
// Freezing hazards
|
||||
if strings.Contains(s, "freezing rain") {
|
||||
if strings.Contains(s, "light") {
|
||||
return 66
|
||||
}
|
||||
return 67
|
||||
}
|
||||
if strings.Contains(s, "freezing drizzle") {
|
||||
if strings.Contains(s, "light") {
|
||||
return 56
|
||||
}
|
||||
return 57
|
||||
}
|
||||
|
||||
// Drizzle
|
||||
if strings.Contains(s, "drizzle") {
|
||||
if strings.Contains(s, "heavy") || strings.Contains(s, "dense") {
|
||||
return 55
|
||||
}
|
||||
if strings.Contains(s, "light") {
|
||||
return 51
|
||||
}
|
||||
return 53
|
||||
}
|
||||
|
||||
// Showers
|
||||
if strings.Contains(s, "showers") {
|
||||
if strings.Contains(s, "heavy") {
|
||||
return 82
|
||||
}
|
||||
if strings.Contains(s, "light") {
|
||||
return 80
|
||||
}
|
||||
return 81
|
||||
}
|
||||
|
||||
// Rain
|
||||
if strings.Contains(s, "rain") {
|
||||
if strings.Contains(s, "heavy") {
|
||||
return 65
|
||||
}
|
||||
if strings.Contains(s, "light") {
|
||||
return 61
|
||||
}
|
||||
return 63
|
||||
}
|
||||
|
||||
// Snow
|
||||
if strings.Contains(s, "snow showers") {
|
||||
if strings.Contains(s, "light") {
|
||||
return 85
|
||||
}
|
||||
return 86
|
||||
}
|
||||
if strings.Contains(s, "snow grains") {
|
||||
return 77
|
||||
}
|
||||
if strings.Contains(s, "snow") {
|
||||
if strings.Contains(s, "heavy") {
|
||||
return 75
|
||||
}
|
||||
if strings.Contains(s, "light") {
|
||||
return 71
|
||||
}
|
||||
return 73
|
||||
}
|
||||
|
||||
// Fog
|
||||
if strings.Contains(s, "rime fog") {
|
||||
return 48
|
||||
}
|
||||
if strings.Contains(s, "fog") || strings.Contains(s, "mist") {
|
||||
return 45
|
||||
}
|
||||
|
||||
// Sky-only
|
||||
if strings.Contains(s, "overcast") {
|
||||
return 3
|
||||
}
|
||||
if strings.Contains(s, "cloudy") {
|
||||
return 3
|
||||
}
|
||||
if strings.Contains(s, "partly cloudy") {
|
||||
return 2
|
||||
}
|
||||
if strings.Contains(s, "mostly sunny") || strings.Contains(s, "mostly clear") ||
|
||||
strings.Contains(s, "mainly sunny") || strings.Contains(s, "mainly clear") {
|
||||
return 1
|
||||
}
|
||||
if strings.Contains(s, "clear") || strings.Contains(s, "sunny") {
|
||||
return 0
|
||||
}
|
||||
|
||||
return model.WMOUnknown
|
||||
}
|
||||
|
||||
func wmoFromCloudLayers(cloudLayers []model.CloudLayer) model.WMOCode {
|
||||
// NWS cloud layer amount values commonly include:
|
||||
// OVC, BKN, SCT, FEW, SKC, CLR, VV (vertical visibility / obscured sky)
|
||||
//
|
||||
// We interpret these conservatively:
|
||||
// - OVC / BKN / VV => Cloudy (3)
|
||||
// - SCT => Partly Cloudy (2)
|
||||
// - FEW => Mainly Sunny/Clear (1)
|
||||
// - CLR / SKC => Sunny/Clear (0)
|
||||
//
|
||||
// If multiple layers exist, we bias toward the "most cloudy" layer.
|
||||
mostCloudy := ""
|
||||
|
||||
for _, cl := range cloudLayers {
|
||||
a := strings.ToUpper(strings.TrimSpace(cl.Amount))
|
||||
if a == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
switch a {
|
||||
case "OVC":
|
||||
return 3
|
||||
case "BKN", "VV":
|
||||
if mostCloudy != "OVC" {
|
||||
mostCloudy = a
|
||||
}
|
||||
case "SCT":
|
||||
if mostCloudy == "" {
|
||||
mostCloudy = "SCT"
|
||||
}
|
||||
case "FEW":
|
||||
if mostCloudy == "" {
|
||||
mostCloudy = "FEW"
|
||||
}
|
||||
case "CLR", "SKC":
|
||||
if mostCloudy == "" {
|
||||
mostCloudy = "CLR"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch mostCloudy {
|
||||
case "BKN", "VV":
|
||||
return 3
|
||||
case "SCT":
|
||||
return 2
|
||||
case "FEW":
|
||||
return 1
|
||||
case "CLR":
|
||||
return 0
|
||||
default:
|
||||
return model.WMOUnknown
|
||||
}
|
||||
}
|
||||
238
internal/sources/openmeteo/observation.go
Normal file
238
internal/sources/openmeteo/observation.go
Normal file
@@ -0,0 +1,238 @@
|
||||
package openmeteo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/model"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/standards"
|
||||
)
|
||||
|
||||
// ObservationSource polls an Open-Meteo endpoint and emits one Observation event.
|
||||
//
|
||||
// Typical URL shape (you provide this via config):
|
||||
//
|
||||
// https://api.open-meteo.com/v1/forecast?latitude=...&longitude=...¤t=temperature_2m,relative_humidity_2m,weather_code,wind_speed_10m,wind_direction_10m,wind_gusts_10m,surface_pressure,pressure_msl&timezone=GMT
|
||||
type ObservationSource struct {
|
||||
name string
|
||||
url string
|
||||
userAgent string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewObservationSource(cfg config.SourceConfig) (*ObservationSource, error) {
|
||||
if strings.TrimSpace(cfg.Name) == "" {
|
||||
return nil, fmt.Errorf("openmeteo_observation: name is required")
|
||||
}
|
||||
if cfg.Params == nil {
|
||||
return nil, fmt.Errorf("openmeteo_observation %q: params are required (need params.url)", cfg.Name)
|
||||
}
|
||||
|
||||
// Open-Meteo needs only a URL; everything else is optional.
|
||||
url, ok := cfg.ParamString("url", "URL")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("openmeteo_observation %q: params.url is required", cfg.Name)
|
||||
}
|
||||
|
||||
// Open-Meteo doesn't require a special User-Agent, but including one is polite.
|
||||
// If the caller doesn't provide one, we supply a reasonable default.
|
||||
ua := cfg.ParamStringDefault("weatherfeeder (open-meteo client)", "user_agent", "userAgent")
|
||||
|
||||
return &ObservationSource{
|
||||
name: cfg.Name,
|
||||
url: url,
|
||||
userAgent: ua,
|
||||
client: &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *ObservationSource) Name() string { return s.name }
|
||||
|
||||
// Kind is used for routing/policy. Note that the TYPE is domain-agnostic (event.Kind).
|
||||
func (s *ObservationSource) Kind() event.Kind { return event.Kind("observation") }
|
||||
|
||||
func (s *ObservationSource) Poll(ctx context.Context) ([]event.Event, error) {
|
||||
obs, effectiveAt, eventID, err := s.fetchAndParse(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Make EffectiveAt a stable pointer.
|
||||
effectiveAtCopy := effectiveAt
|
||||
|
||||
e := event.Event{
|
||||
ID: eventID,
|
||||
Kind: s.Kind(),
|
||||
Source: s.name,
|
||||
EmittedAt: time.Now().UTC(),
|
||||
EffectiveAt: &effectiveAtCopy,
|
||||
|
||||
// Optional but useful for downstream consumers once multiple event types exist.
|
||||
Schema: "weather.observation.v1",
|
||||
|
||||
// The payload domain-specific (model.WeatherObservation).
|
||||
// feedkit treats this as opaque.
|
||||
Payload: obs,
|
||||
}
|
||||
|
||||
if err := e.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []event.Event{e}, nil
|
||||
}
|
||||
|
||||
// ---- Open-Meteo JSON parsing ----
|
||||
|
||||
type omResponse struct {
|
||||
Latitude float64 `json:"latitude"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
Timezone string `json:"timezone"`
|
||||
UTCOffsetSeconds int `json:"utc_offset_seconds"`
|
||||
Elevation float64 `json:"elevation"`
|
||||
|
||||
Current omCurrent `json:"current"`
|
||||
}
|
||||
|
||||
type omCurrent struct {
|
||||
Time string `json:"time"` // e.g. "2026-01-10T12:30"
|
||||
Interval int `json:"interval"`
|
||||
Temperature2m float64 `json:"temperature_2m"`
|
||||
RelativeHumidity2m float64 `json:"relative_humidity_2m"`
|
||||
WeatherCode int `json:"weather_code"`
|
||||
|
||||
WindSpeed10m float64 `json:"wind_speed_10m"` // km/h
|
||||
WindDirection10m float64 `json:"wind_direction_10m"` // degrees
|
||||
WindGusts10m float64 `json:"wind_gusts_10m"` // km/h
|
||||
|
||||
Precipitation float64 `json:"precipitation"`
|
||||
|
||||
SurfacePressure float64 `json:"surface_pressure"` // hPa
|
||||
PressureMSL float64 `json:"pressure_msl"` // hPa
|
||||
|
||||
CloudCover float64 `json:"cloud_cover"`
|
||||
ApparentTemperature float64 `json:"apparent_temperature"`
|
||||
IsDay int `json:"is_day"`
|
||||
}
|
||||
|
||||
func (s *ObservationSource) fetchAndParse(ctx context.Context) (model.WeatherObservation, time.Time, string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", s.url, nil)
|
||||
if err != nil {
|
||||
return model.WeatherObservation{}, time.Time{}, "", err
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", s.userAgent)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
res, err := s.client.Do(req)
|
||||
if err != nil {
|
||||
return model.WeatherObservation{}, time.Time{}, "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||
return model.WeatherObservation{}, time.Time{}, "", fmt.Errorf("openmeteo_observation %q: HTTP %s", s.name, res.Status)
|
||||
}
|
||||
|
||||
var parsed omResponse
|
||||
if err := json.NewDecoder(res.Body).Decode(&parsed); err != nil {
|
||||
return model.WeatherObservation{}, time.Time{}, "", err
|
||||
}
|
||||
|
||||
// Parse current.time.
|
||||
// Open-Meteo "time" commonly looks like "YYYY-MM-DDTHH:MM" (no timezone suffix).
|
||||
// We'll interpret it in the timezone returned by the API (best-effort).
|
||||
t, err := parseOpenMeteoTime(parsed.Current.Time, parsed.Timezone, parsed.UTCOffsetSeconds)
|
||||
if err != nil {
|
||||
return model.WeatherObservation{}, time.Time{}, "", fmt.Errorf("openmeteo_observation %q: parse time %q: %w", s.name, parsed.Current.Time, err)
|
||||
}
|
||||
|
||||
// Normalize to UTC inside the domain model; presentation can localize later.
|
||||
effectiveAt := t.UTC()
|
||||
|
||||
// Measurements
|
||||
tempC := parsed.Current.Temperature2m
|
||||
rh := parsed.Current.RelativeHumidity2m
|
||||
wdir := parsed.Current.WindDirection10m
|
||||
wsKmh := parsed.Current.WindSpeed10m
|
||||
wgKmh := parsed.Current.WindGusts10m
|
||||
|
||||
surfacePa := parsed.Current.SurfacePressure * 100.0
|
||||
mslPa := parsed.Current.PressureMSL * 100.0
|
||||
|
||||
elevM := parsed.Elevation
|
||||
|
||||
// Canonical condition (WMO)
|
||||
isDay := parsed.Current.IsDay == 1
|
||||
wmo := model.WMOCode(parsed.Current.WeatherCode)
|
||||
canonicalText := standards.WMOText(wmo, &isDay)
|
||||
|
||||
obs := model.WeatherObservation{
|
||||
// Open-Meteo isn't a station feed; we’ll label this with a synthetic identifier.
|
||||
StationID: fmt.Sprintf("OPENMETEO(%.5f,%.5f)", parsed.Latitude, parsed.Longitude),
|
||||
StationName: "Open-Meteo",
|
||||
|
||||
Timestamp: effectiveAt,
|
||||
|
||||
// Canonical conditions
|
||||
ConditionCode: wmo,
|
||||
ConditionText: canonicalText,
|
||||
IsDay: &isDay,
|
||||
|
||||
// Provider evidence (Open-Meteo does not provide a separate raw description here)
|
||||
ProviderRawDescription: "",
|
||||
|
||||
// Human-facing fields:
|
||||
// Populate TextDescription with canonical text so downstream output remains consistent.
|
||||
TextDescription: canonicalText,
|
||||
|
||||
TemperatureC: &tempC,
|
||||
RelativeHumidityPercent: &rh,
|
||||
|
||||
WindDirectionDegrees: &wdir,
|
||||
WindSpeedKmh: &wsKmh,
|
||||
WindGustKmh: &wgKmh,
|
||||
|
||||
BarometricPressurePa: &surfacePa,
|
||||
SeaLevelPressurePa: &mslPa,
|
||||
|
||||
ElevationMeters: &elevM,
|
||||
}
|
||||
|
||||
// Build a stable event ID.
|
||||
// Open-Meteo doesn't supply a unique ID, so we key by source + effective time.
|
||||
eventID := fmt.Sprintf("openmeteo:%s:%s", s.name, effectiveAt.Format(time.RFC3339Nano))
|
||||
|
||||
return obs, effectiveAt, eventID, nil
|
||||
}
|
||||
|
||||
func parseOpenMeteoTime(s string, tz string, utcOffsetSeconds int) (time.Time, error) {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return time.Time{}, fmt.Errorf("empty time")
|
||||
}
|
||||
|
||||
// Typical Open-Meteo format: "2006-01-02T15:04"
|
||||
const layout = "2006-01-02T15:04"
|
||||
|
||||
// Best effort: try to load the timezone as an IANA name.
|
||||
// Examples Open-Meteo might return: "GMT", "America/Chicago".
|
||||
if tz != "" {
|
||||
if loc, err := time.LoadLocation(tz); err == nil {
|
||||
return time.ParseInLocation(layout, s, loc)
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: use the offset seconds to create a fixed zone.
|
||||
// (If offset is 0, this is UTC.)
|
||||
loc := time.FixedZone("open-meteo", utcOffsetSeconds)
|
||||
return time.ParseInLocation(layout, s, loc)
|
||||
}
|
||||
438
internal/sources/openweather/observation.go
Normal file
438
internal/sources/openweather/observation.go
Normal file
@@ -0,0 +1,438 @@
|
||||
package openweather
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/model"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/standards"
|
||||
)
|
||||
|
||||
// ObservationSource polls the OpenWeatherMap "Current weather" endpoint and emits one Observation event.
|
||||
//
|
||||
// Typical URL shape (you provide this via config):
|
||||
//
|
||||
// https://api.openweathermap.org/data/2.5/weather?lat=...&lon=...&appid=...&units=metric
|
||||
//
|
||||
// Unit notes:
|
||||
// - If `units` is omitted, OpenWeather uses "standard" units (temp Kelvin, wind m/s).
|
||||
// - `units=metric` => temp Celsius, wind m/s.
|
||||
// - `units=imperial` => temp Fahrenheit, wind mph.
|
||||
//
|
||||
// weatherd normalizes to:
|
||||
// - TemperatureC in °C
|
||||
// - WindSpeedKmh in km/h
|
||||
// - Pressure in Pa (OpenWeather provides hPa)
|
||||
type ObservationSource struct {
|
||||
name string
|
||||
url string
|
||||
userAgent string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewObservationSource(cfg config.SourceConfig) (*ObservationSource, error) {
|
||||
if strings.TrimSpace(cfg.Name) == "" {
|
||||
return nil, fmt.Errorf("openweather_observation: name is required")
|
||||
}
|
||||
if cfg.Params == nil {
|
||||
return nil, fmt.Errorf("openweather_observation %q: params are required (need params.url)", cfg.Name)
|
||||
}
|
||||
|
||||
// Driver-specific settings live under cfg.Params to keep feedkit domain-agnostic.
|
||||
url, ok := cfg.ParamString("url", "URL")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("openweather_observation %q: params.url is required", cfg.Name)
|
||||
}
|
||||
|
||||
// Optional User-Agent.
|
||||
ua := cfg.ParamStringDefault("weatherfeeder (openweather client)", "user_agent", "userAgent")
|
||||
|
||||
return &ObservationSource{
|
||||
name: cfg.Name,
|
||||
url: url,
|
||||
userAgent: ua,
|
||||
client: &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *ObservationSource) Name() string { return s.name }
|
||||
|
||||
// Kind is used for routing/policy.
|
||||
func (s *ObservationSource) Kind() event.Kind { return event.Kind("observation") }
|
||||
|
||||
func (s *ObservationSource) Poll(ctx context.Context) ([]event.Event, error) {
|
||||
obs, eventID, err := s.fetchAndParse(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// EffectiveAt is optional. If we have a real observation timestamp, use it.
|
||||
// We intentionally take a copy so the pointer is stable and not tied to a struct field.
|
||||
var effectiveAt *time.Time
|
||||
if !obs.Timestamp.IsZero() {
|
||||
t := obs.Timestamp
|
||||
effectiveAt = &t
|
||||
}
|
||||
|
||||
e := event.Event{
|
||||
ID: eventID,
|
||||
Kind: s.Kind(),
|
||||
Source: s.name,
|
||||
EmittedAt: time.Now().UTC(),
|
||||
EffectiveAt: effectiveAt,
|
||||
|
||||
Schema: "weather.observation.v1",
|
||||
Payload: obs,
|
||||
}
|
||||
|
||||
if err := e.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []event.Event{e}, nil
|
||||
}
|
||||
|
||||
// --- OpenWeather JSON parsing (minimal subset) ---
|
||||
|
||||
type owmResponse struct {
|
||||
Coord struct {
|
||||
Lon float64 `json:"lon"`
|
||||
Lat float64 `json:"lat"`
|
||||
} `json:"coord"`
|
||||
|
||||
Weather []struct {
|
||||
ID int `json:"id"`
|
||||
Main string `json:"main"`
|
||||
Description string `json:"description"`
|
||||
Icon string `json:"icon"` // e.g. "04d" or "01n"
|
||||
} `json:"weather"`
|
||||
|
||||
Main struct {
|
||||
Temp float64 `json:"temp"`
|
||||
Pressure float64 `json:"pressure"` // hPa
|
||||
Humidity float64 `json:"humidity"` // %
|
||||
SeaLevel *float64 `json:"sea_level"` // hPa (optional)
|
||||
} `json:"main"`
|
||||
|
||||
Visibility *float64 `json:"visibility"` // meters (optional)
|
||||
|
||||
Wind struct {
|
||||
Speed float64 `json:"speed"` // units depend on `units=...`
|
||||
Deg *float64 `json:"deg"`
|
||||
Gust *float64 `json:"gust"` // units depend on `units=...`
|
||||
} `json:"wind"`
|
||||
|
||||
Clouds struct {
|
||||
All *float64 `json:"all"` // cloudiness %
|
||||
} `json:"clouds"`
|
||||
|
||||
Dt int64 `json:"dt"` // unix seconds, UTC
|
||||
|
||||
Sys struct {
|
||||
Country string `json:"country"`
|
||||
Sunrise int64 `json:"sunrise"` // unix, UTC
|
||||
Sunset int64 `json:"sunset"` // unix, UTC
|
||||
} `json:"sys"`
|
||||
|
||||
Timezone int `json:"timezone"` // seconds offset from UTC
|
||||
ID int64 `json:"id"` // city id
|
||||
Name string `json:"name"` // city name
|
||||
Cod int `json:"cod"`
|
||||
}
|
||||
|
||||
func (s *ObservationSource) fetchAndParse(ctx context.Context) (model.WeatherObservation, string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", s.url, nil)
|
||||
if err != nil {
|
||||
return model.WeatherObservation{}, "", err
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", s.userAgent)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
res, err := s.client.Do(req)
|
||||
if err != nil {
|
||||
return model.WeatherObservation{}, "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||
return model.WeatherObservation{}, "", fmt.Errorf("openweather_observation %q: HTTP %s", s.name, res.Status)
|
||||
}
|
||||
|
||||
var parsed owmResponse
|
||||
if err := json.NewDecoder(res.Body).Decode(&parsed); err != nil {
|
||||
return model.WeatherObservation{}, "", err
|
||||
}
|
||||
|
||||
// Timestamp: dt is unix seconds, UTC.
|
||||
ts := time.Unix(parsed.Dt, 0).UTC()
|
||||
|
||||
// Primary weather condition: OpenWeather returns a list; we treat [0] as primary.
|
||||
// If missing, we degrade gracefully.
|
||||
owmID := 0
|
||||
rawDesc := ""
|
||||
icon := ""
|
||||
if len(parsed.Weather) > 0 {
|
||||
owmID = parsed.Weather[0].ID
|
||||
rawDesc = strings.TrimSpace(parsed.Weather[0].Description)
|
||||
icon = strings.TrimSpace(parsed.Weather[0].Icon)
|
||||
}
|
||||
|
||||
// Day/night inference:
|
||||
// - Prefer icon suffix if present ("d" or "n")
|
||||
// - Else fall back to sunrise/sunset bounds
|
||||
var isDay *bool
|
||||
if icon != "" {
|
||||
last := icon[len(icon)-1]
|
||||
switch last {
|
||||
case 'd':
|
||||
v := true
|
||||
isDay = &v
|
||||
case 'n':
|
||||
v := false
|
||||
isDay = &v
|
||||
}
|
||||
}
|
||||
if isDay == nil && parsed.Sys.Sunrise > 0 && parsed.Sys.Sunset > 0 {
|
||||
v := parsed.Dt >= parsed.Sys.Sunrise && parsed.Dt < parsed.Sys.Sunset
|
||||
isDay = &v
|
||||
}
|
||||
|
||||
// Units handling based on the request URL.
|
||||
unitSystem := getUnitsFromURL(s.url)
|
||||
|
||||
// Temperature normalization to Celsius.
|
||||
tempC := normalizeTempToC(parsed.Main.Temp, unitSystem)
|
||||
|
||||
// Humidity is already percent.
|
||||
rh := parsed.Main.Humidity
|
||||
|
||||
// Pressure hPa -> Pa
|
||||
surfacePa := parsed.Main.Pressure * 100.0
|
||||
var seaLevelPa *float64
|
||||
if parsed.Main.SeaLevel != nil {
|
||||
v := (*parsed.Main.SeaLevel) * 100.0
|
||||
seaLevelPa = &v
|
||||
}
|
||||
|
||||
// Wind speed normalization to km/h
|
||||
wsKmh := normalizeSpeedToKmh(parsed.Wind.Speed, unitSystem)
|
||||
|
||||
var wgKmh *float64
|
||||
if parsed.Wind.Gust != nil {
|
||||
v := normalizeSpeedToKmh(*parsed.Wind.Gust, unitSystem)
|
||||
wgKmh = &v
|
||||
}
|
||||
|
||||
// Visibility in meters (already matches our model)
|
||||
var visM *float64
|
||||
if parsed.Visibility != nil {
|
||||
v := *parsed.Visibility
|
||||
visM = &v
|
||||
}
|
||||
|
||||
// Map OpenWeather condition IDs -> canonical WMO code (our internal vocabulary).
|
||||
wmo := mapOpenWeatherToWMO(owmID)
|
||||
|
||||
// Canonical text from our shared table.
|
||||
canonicalText := standards.WMOText(wmo, isDay)
|
||||
|
||||
// Icon URL (optional).
|
||||
iconURL := ""
|
||||
if icon != "" {
|
||||
iconURL = fmt.Sprintf("https://openweathermap.org/img/wn/%s@2x.png", icon)
|
||||
}
|
||||
|
||||
stationID := ""
|
||||
if parsed.ID != 0 {
|
||||
stationID = fmt.Sprintf("OPENWEATHER(%d)", parsed.ID)
|
||||
} else {
|
||||
stationID = fmt.Sprintf("OPENWEATHER(%.5f,%.5f)", parsed.Coord.Lat, parsed.Coord.Lon)
|
||||
}
|
||||
|
||||
stationName := strings.TrimSpace(parsed.Name)
|
||||
if stationName == "" {
|
||||
stationName = "OpenWeatherMap"
|
||||
}
|
||||
|
||||
obs := model.WeatherObservation{
|
||||
StationID: stationID,
|
||||
StationName: stationName,
|
||||
Timestamp: ts,
|
||||
|
||||
// Canonical internal representation
|
||||
ConditionCode: wmo,
|
||||
ConditionText: canonicalText,
|
||||
IsDay: isDay,
|
||||
|
||||
// Provider evidence for troubleshooting mappings
|
||||
ProviderRawDescription: rawDesc,
|
||||
|
||||
// Human-facing legacy fields: we populate with canonical text for consistency
|
||||
TextDescription: canonicalText,
|
||||
IconURL: iconURL,
|
||||
|
||||
TemperatureC: &tempC,
|
||||
|
||||
WindDirectionDegrees: parsed.Wind.Deg,
|
||||
WindSpeedKmh: &wsKmh,
|
||||
WindGustKmh: wgKmh,
|
||||
|
||||
BarometricPressurePa: &surfacePa,
|
||||
SeaLevelPressurePa: seaLevelPa,
|
||||
VisibilityMeters: visM,
|
||||
|
||||
RelativeHumidityPercent: &rh,
|
||||
}
|
||||
|
||||
// Stable event ID: key by source + timestamp.
|
||||
eventID := fmt.Sprintf("openweather:%s:%s", s.name, obs.Timestamp.UTC().Format(time.RFC3339Nano))
|
||||
|
||||
return obs, eventID, nil
|
||||
}
|
||||
|
||||
func getUnitsFromURL(raw string) string {
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
return "standard"
|
||||
}
|
||||
q := u.Query()
|
||||
units := strings.TrimSpace(strings.ToLower(q.Get("units")))
|
||||
if units == "" {
|
||||
return "standard"
|
||||
}
|
||||
switch units {
|
||||
case "standard", "metric", "imperial":
|
||||
return units
|
||||
default:
|
||||
return "standard"
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeTempToC(v float64, unitSystem string) float64 {
|
||||
switch unitSystem {
|
||||
case "metric":
|
||||
// Already °C
|
||||
return v
|
||||
case "imperial":
|
||||
// °F -> °C
|
||||
return (v - 32.0) * 5.0 / 9.0
|
||||
default:
|
||||
// "standard" => Kelvin -> °C
|
||||
return v - 273.15
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeSpeedToKmh(v float64, unitSystem string) float64 {
|
||||
switch unitSystem {
|
||||
case "imperial":
|
||||
// mph -> km/h
|
||||
return v * 1.609344
|
||||
default:
|
||||
// m/s -> km/h
|
||||
return v * 3.6
|
||||
}
|
||||
}
|
||||
|
||||
// mapOpenWeatherToWMO maps OpenWeather weather condition IDs into your internal WMO code vocabulary.
|
||||
//
|
||||
// This is an approximate semantic mapping between two different code systems.
|
||||
// Your current canonical WMO table is intentionally small and text-focused,
|
||||
// so we map into that set (0/1/2/3/45/48/51/.../99) conservatively.
|
||||
func mapOpenWeatherToWMO(owmID int) model.WMOCode {
|
||||
switch {
|
||||
// 2xx Thunderstorm
|
||||
case owmID >= 200 && owmID <= 232:
|
||||
return model.WMOCode(95)
|
||||
|
||||
// 3xx Drizzle
|
||||
case owmID >= 300 && owmID <= 321:
|
||||
if owmID == 300 {
|
||||
return model.WMOCode(51)
|
||||
}
|
||||
if owmID == 302 {
|
||||
return model.WMOCode(55)
|
||||
}
|
||||
return model.WMOCode(53)
|
||||
|
||||
// 5xx Rain
|
||||
case owmID >= 500 && owmID <= 531:
|
||||
// 511 is "freezing rain"
|
||||
if owmID == 511 {
|
||||
return model.WMOCode(67)
|
||||
}
|
||||
|
||||
// showers bucket (520-531)
|
||||
if owmID >= 520 && owmID <= 531 {
|
||||
if owmID == 520 {
|
||||
return model.WMOCode(80)
|
||||
}
|
||||
if owmID == 522 {
|
||||
return model.WMOCode(82)
|
||||
}
|
||||
return model.WMOCode(81)
|
||||
}
|
||||
|
||||
// normal rain intensity
|
||||
if owmID == 500 {
|
||||
return model.WMOCode(61)
|
||||
}
|
||||
if owmID == 501 {
|
||||
return model.WMOCode(63)
|
||||
}
|
||||
if owmID >= 502 && owmID <= 504 {
|
||||
return model.WMOCode(65)
|
||||
}
|
||||
return model.WMOCode(63)
|
||||
|
||||
// 6xx Snow
|
||||
case owmID >= 600 && owmID <= 622:
|
||||
if owmID == 600 {
|
||||
return model.WMOCode(71)
|
||||
}
|
||||
if owmID == 601 {
|
||||
return model.WMOCode(73)
|
||||
}
|
||||
if owmID == 602 {
|
||||
return model.WMOCode(75)
|
||||
}
|
||||
|
||||
// Snow showers bucket (620-622)
|
||||
if owmID == 620 {
|
||||
return model.WMOCode(85)
|
||||
}
|
||||
if owmID == 621 || owmID == 622 {
|
||||
return model.WMOCode(86)
|
||||
}
|
||||
|
||||
return model.WMOCode(73)
|
||||
|
||||
// 7xx Atmosphere (mist/smoke/haze/dust/fog/etc.)
|
||||
case owmID >= 701 && owmID <= 781:
|
||||
return model.WMOCode(45)
|
||||
|
||||
// 800 Clear
|
||||
case owmID == 800:
|
||||
return model.WMOCode(0)
|
||||
|
||||
// 80x Clouds
|
||||
case owmID == 801:
|
||||
return model.WMOCode(1)
|
||||
case owmID == 802:
|
||||
return model.WMOCode(2)
|
||||
case owmID == 803 || owmID == 804:
|
||||
return model.WMOCode(3)
|
||||
|
||||
default:
|
||||
return model.WMOUnknown
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user