diff --git a/cmd/now.go b/cmd/now.go index f3090b0..8d11a09 100644 --- a/cmd/now.go +++ b/cmd/now.go @@ -36,7 +36,7 @@ func now(cmd *cobra.Command, args []string) { fmt.Fprintln(os.Stderr, "No location or coordinates provided.") os.Exit(1) } - n := &yr.NowForecast{} + n := &yr.Forecast{} if len(args) == 0 { n, err = c.NowCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil) cobra.CheckErr(err) diff --git a/yr/yr.go b/yr/yr.go index b3fb070..00b2730 100644 --- a/yr/yr.go +++ b/yr/yr.go @@ -64,14 +64,14 @@ func windDirection(d float64) string { } } -type NowForecast struct { +type Forecast struct { nominatim.LookupResult Temperature float64 `json:"temperature"` // In celsius Percipitation float64 `json:"percipitation"` // In mm Wind wind `json:"wind"` // In m/s } -func (c *Client) Now(q string) (*NowForecast, error) { +func (c *Client) Now(q string) (*Forecast, error) { coords, err := c.nom.Lookup(q) if err != nil { return nil, err @@ -83,7 +83,30 @@ func (c *Client) Now(q string) (*NowForecast, error) { }, &coords.Location) } -func (c *Client) NowCoords(coords *nominatim.Coordinates, location *string) (*NowForecast, error) { +func (c *Client) NowCoords(coords *nominatim.Coordinates, location *string) (*Forecast, error) { + forecasts, err := c.ForecastCoords(coords, location) + if err != nil { + return nil, err + } + + now := forecasts[0] + + return &now, nil +} + +func (c *Client) Forecast(q string) ([]Forecast, error) { + coords, err := c.nom.Lookup(q) + if err != nil { + return nil, err + } + + return c.ForecastCoords(&nominatim.Coordinates{ + Latitude: coords.Latitude, + Longitude: coords.Longitude, + }, &coords.Location) +} + +func (c *Client) ForecastCoords(coords *nominatim.Coordinates, location *string) ([]Forecast, error) { f, err := c.met.Forecast(coords.Latitude, coords.Longitude, nil) if err != nil { return nil, err @@ -109,23 +132,40 @@ func (c *Client) NowCoords(coords *nominatim.Coordinates, location *string) (*No slices.SortFunc(ts, sortTimeSeries) - latest := ts[0].Data + forecasts := []Forecast{} - return &NowForecast{ - LookupResult: nominatim.LookupResult{ - Location: *location, - Coordinates: nominatim.Coordinates{ - Latitude: coords.Latitude, - Longitude: coords.Longitude, + lookupResult := &nominatim.LookupResult{ + Location: *location, + Coordinates: nominatim.Coordinates{ + Latitude: coords.Latitude, + Longitude: coords.Longitude, + }, + } + + for _, t := range ts { + percipitation := 0.0 + + if t.Data.Next1_Hours != nil { + percipitation = t.Data.Next1_Hours.Details.PrecipitationAmount + } else if t.Data.Next6_Hours != nil { + percipitation = t.Data.Next6_Hours.Details.PrecipitationAmount + } else if t.Data.Next12_Hours != nil { + percipitation = t.Data.Next12_Hours.Details.PrecipitationAmount + } + + forecasts = append(forecasts, Forecast{ + LookupResult: *lookupResult, + Temperature: t.Data.Instant.Details.AirTemperature, + Percipitation: float64(percipitation), + Wind: wind{ + Speed: t.Data.Instant.Details.WindSpeed, + Direction: t.Data.Instant.Details.WindFromDirection, }, - }, - Temperature: latest.Instant.Details.AirTemperature, - Percipitation: latest.Next1_Hours.Details.PrecipitationAmount, - Wind: wind{ - Speed: latest.Instant.Details.WindSpeed, - Direction: latest.Instant.Details.WindFromDirection, - }, - }, nil + }) + + } + + return forecasts, nil } func sortTimeSeries(a, b met.Timeseries) int {