refactor: Add Forecast method to yr

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-10-02 19:51:44 +02:00
parent 798faa786b
commit b350835718
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
2 changed files with 59 additions and 19 deletions

View file

@ -36,7 +36,7 @@ func now(cmd *cobra.Command, args []string) {
fmt.Fprintln(os.Stderr, "No location or coordinates provided.") fmt.Fprintln(os.Stderr, "No location or coordinates provided.")
os.Exit(1) os.Exit(1)
} }
n := &yr.NowForecast{} n := &yr.Forecast{}
if len(args) == 0 { if len(args) == 0 {
n, err = c.NowCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil) n, err = c.NowCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil)
cobra.CheckErr(err) cobra.CheckErr(err)

View file

@ -64,14 +64,14 @@ func windDirection(d float64) string {
} }
} }
type NowForecast struct { type Forecast struct {
nominatim.LookupResult nominatim.LookupResult
Temperature float64 `json:"temperature"` // In celsius Temperature float64 `json:"temperature"` // In celsius
Percipitation float64 `json:"percipitation"` // In mm Percipitation float64 `json:"percipitation"` // In mm
Wind wind `json:"wind"` // In m/s 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) coords, err := c.nom.Lookup(q)
if err != nil { if err != nil {
return nil, err return nil, err
@ -83,7 +83,30 @@ func (c *Client) Now(q string) (*NowForecast, error) {
}, &coords.Location) }, &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) f, err := c.met.Forecast(coords.Latitude, coords.Longitude, nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -109,23 +132,40 @@ func (c *Client) NowCoords(coords *nominatim.Coordinates, location *string) (*No
slices.SortFunc(ts, sortTimeSeries) slices.SortFunc(ts, sortTimeSeries)
latest := ts[0].Data forecasts := []Forecast{}
return &NowForecast{ lookupResult := &nominatim.LookupResult{
LookupResult: nominatim.LookupResult{ Location: *location,
Location: *location, Coordinates: nominatim.Coordinates{
Coordinates: nominatim.Coordinates{ Latitude: coords.Latitude,
Latitude: coords.Latitude, Longitude: coords.Longitude,
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, return forecasts, nil
Direction: latest.Instant.Details.WindFromDirection,
},
}, nil
} }
func sortTimeSeries(a, b met.Timeseries) int { func sortTimeSeries(a, b met.Timeseries) int {