refactor: Add Forecast method to yr
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
parent
798faa786b
commit
b350835718
2 changed files with 59 additions and 19 deletions
|
@ -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)
|
||||
|
|
64
yr/yr.go
64
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{
|
||||
lookupResult := &nominatim.LookupResult{
|
||||
Location: *location,
|
||||
Coordinates: nominatim.Coordinates{
|
||||
Latitude: coords.Latitude,
|
||||
Longitude: coords.Longitude,
|
||||
},
|
||||
},
|
||||
Temperature: latest.Instant.Details.AirTemperature,
|
||||
Percipitation: latest.Next1_Hours.Details.PrecipitationAmount,
|
||||
}
|
||||
|
||||
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: latest.Instant.Details.WindSpeed,
|
||||
Direction: latest.Instant.Details.WindFromDirection,
|
||||
Speed: t.Data.Instant.Details.WindSpeed,
|
||||
Direction: t.Data.Instant.Details.WindFromDirection,
|
||||
},
|
||||
}, nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
return forecasts, nil
|
||||
}
|
||||
|
||||
func sortTimeSeries(a, b met.Timeseries) int {
|
||||
|
|
Loading…
Reference in a new issue