2026-01-14 11:18:21 -06:00
|
|
|
|
// FILE: ./internal/sources/nws/observation.go
|
2026-01-13 18:14:21 -06:00
|
|
|
|
package nws
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/ejr/feedkit/config"
|
|
|
|
|
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
2026-01-15 08:58:56 -06:00
|
|
|
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/common"
|
2026-01-13 18:14:21 -06:00
|
|
|
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/standards"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// ObservationSource polls an NWS station observation endpoint and emits a RAW observation Event.
|
|
|
|
|
|
//
|
2026-01-13 18:14:21 -06:00
|
|
|
|
// 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) {
|
2026-01-15 09:43:22 -06:00
|
|
|
|
const driver = "nws_observation"
|
2026-01-13 18:14:21 -06:00
|
|
|
|
|
2026-01-15 09:43:22 -06:00
|
|
|
|
c, err := common.RequireHTTPSourceConfig(driver, cfg)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &ObservationSource{
|
2026-01-15 09:43:22 -06:00
|
|
|
|
name: c.Name,
|
|
|
|
|
|
url: c.URL,
|
|
|
|
|
|
userAgent: c.UserAgent,
|
|
|
|
|
|
client: common.NewHTTPClient(common.DefaultHTTPTimeout),
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *ObservationSource) Name() string { return s.name }
|
|
|
|
|
|
|
|
|
|
|
|
// Kind is used for routing/policy.
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// We keep Kind canonical (observation) even for raw events; Schema differentiates raw vs canonical.
|
2026-01-13 18:14:21 -06:00
|
|
|
|
func (s *ObservationSource) Kind() event.Kind { return event.Kind("observation") }
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// Poll fetches NWS "latest observation" and emits exactly one RAW Event.
|
|
|
|
|
|
// The RAW payload is json.RawMessage and Schema is standards.SchemaRawNWSObservationV1.
|
2026-01-13 18:14:21 -06:00
|
|
|
|
func (s *ObservationSource) Poll(ctx context.Context) ([]event.Event, error) {
|
2026-01-14 11:18:21 -06:00
|
|
|
|
raw, meta, err := s.fetchRaw(ctx)
|
2026-01-13 18:14:21 -06:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// Event.ID must be set BEFORE normalization (feedkit requires it).
|
2026-01-15 10:17:56 -06:00
|
|
|
|
// Prefer NWS-provided "id" (stable URL). Fallback to a stable computed key.
|
2026-01-14 11:18:21 -06:00
|
|
|
|
eventID := strings.TrimSpace(meta.ID)
|
|
|
|
|
|
if eventID == "" {
|
|
|
|
|
|
ts := meta.ParsedTimestamp
|
|
|
|
|
|
if ts.IsZero() {
|
|
|
|
|
|
ts = time.Now().UTC()
|
|
|
|
|
|
}
|
|
|
|
|
|
station := strings.TrimSpace(meta.StationID)
|
|
|
|
|
|
if station == "" {
|
|
|
|
|
|
station = "UNKNOWN"
|
|
|
|
|
|
}
|
|
|
|
|
|
eventID = fmt.Sprintf("nws:observation:%s:%s:%s", s.name, station, ts.UTC().Format(time.RFC3339Nano))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// EffectiveAt is optional; for observations it’s naturally the observation timestamp.
|
2026-01-13 18:14:21 -06:00
|
|
|
|
var effectiveAt *time.Time
|
2026-01-14 11:18:21 -06:00
|
|
|
|
if !meta.ParsedTimestamp.IsZero() {
|
|
|
|
|
|
t := meta.ParsedTimestamp.UTC()
|
2026-01-13 18:14:21 -06:00
|
|
|
|
effectiveAt = &t
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-15 09:43:22 -06:00
|
|
|
|
return common.SingleRawEvent(
|
|
|
|
|
|
s.Kind(),
|
|
|
|
|
|
s.name,
|
|
|
|
|
|
standards.SchemaRawNWSObservationV1,
|
|
|
|
|
|
eventID,
|
|
|
|
|
|
effectiveAt,
|
|
|
|
|
|
raw,
|
|
|
|
|
|
)
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// ---- RAW fetch + minimal metadata decode ----
|
2026-01-13 18:14:21 -06:00
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// observationMeta is a *minimal* decode of the NWS payload used only to build
|
|
|
|
|
|
// a stable Event.ID and a useful EffectiveAt for the envelope.
|
|
|
|
|
|
type observationMeta struct {
|
|
|
|
|
|
ID string `json:"id"`
|
2026-01-13 18:14:21 -06:00
|
|
|
|
Properties struct {
|
2026-01-14 11:18:21 -06:00
|
|
|
|
StationID string `json:"stationId"`
|
|
|
|
|
|
Timestamp string `json:"timestamp"`
|
2026-01-13 18:14:21 -06:00
|
|
|
|
} `json:"properties"`
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// Convenience fields populated after decode.
|
|
|
|
|
|
ParsedTimestamp time.Time `json:"-"`
|
|
|
|
|
|
StationID string `json:"-"`
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
func (s *ObservationSource) fetchRaw(ctx context.Context) (json.RawMessage, observationMeta, error) {
|
2026-01-15 08:58:56 -06:00
|
|
|
|
b, err := common.FetchBody(ctx, s.client, s.url, s.userAgent, "application/geo+json, application/json")
|
2026-01-13 18:14:21 -06:00
|
|
|
|
if err != nil {
|
2026-01-15 08:58:56 -06:00
|
|
|
|
return nil, observationMeta{}, fmt.Errorf("nws_observation %q: %w", s.name, err)
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
raw := json.RawMessage(b)
|
2026-01-13 18:14:21 -06:00
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
var meta observationMeta
|
|
|
|
|
|
if err := json.Unmarshal(b, &meta); err != nil {
|
|
|
|
|
|
// If metadata decode fails, still return raw; envelope will fall back to computed ID.
|
|
|
|
|
|
return raw, observationMeta{}, nil
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
meta.StationID = strings.TrimSpace(meta.Properties.StationID)
|
2026-01-13 18:14:21 -06:00
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
tsStr := strings.TrimSpace(meta.Properties.Timestamp)
|
|
|
|
|
|
if tsStr != "" {
|
|
|
|
|
|
if t, err := time.Parse(time.RFC3339, tsStr); err == nil {
|
|
|
|
|
|
meta.ParsedTimestamp = t
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
return raw, meta, nil
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|