From ff9f40e2f8939f3127365a75f46e31a160e613df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Wed, 2 Oct 2024 19:04:01 +0200 Subject: [PATCH] feat: Add windspeed and wind-direction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim Hårek Andreassen --- cmd/now.go | 5 ++++- yr/direction/direction.go | 12 ++++++++++++ yr/yr.go | 39 +++++++++++++++++++++++++++++++++++++++ yr/yr_test.go | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 yr/direction/direction.go create mode 100644 yr/yr_test.go diff --git a/cmd/now.go b/cmd/now.go index bf53fc0..69f97c7 100644 --- a/cmd/now.go +++ b/cmd/now.go @@ -58,9 +58,12 @@ func now(cmd *cobra.Command, args []string) { fmt.Printf( `Current forecast for %s Temperature: %.1f - Rain: %.1f`, + Rain: %.1f + Wind: %.1f from %s`, n.Location, n.Temperature, n.Percipitation, + n.Wind.Speed, + n.Wind.DirectionToString(), ) } diff --git a/yr/direction/direction.go b/yr/direction/direction.go new file mode 100644 index 0000000..ab445d7 --- /dev/null +++ b/yr/direction/direction.go @@ -0,0 +1,12 @@ +package direction + +const ( + NORTH = "↑" + SOUTH = "↓" + WEST = "←" + EAST = "→" + NORTH_WEST = "↖" + NORTH_EAST = "↗" + SOUTH_WEST = "↙" + SOUTH_EAST = "↘" +) diff --git a/yr/yr.go b/yr/yr.go index fea2300..ef8f74c 100644 --- a/yr/yr.go +++ b/yr/yr.go @@ -7,6 +7,7 @@ import ( "git.sr.ht/~timharek/yr-go/internal/met" "git.sr.ht/~timharek/yr-go/internal/nominatim" + "git.sr.ht/~timharek/yr-go/yr/direction" ) type Client struct { @@ -28,10 +29,44 @@ func New() (*Client, error) { return &Client{met: *met, nom: *nom}, nil } +type wind struct { + Speed float64 `json:"speed"` + Direction float64 `json:"direction"` +} + +func (w *wind) DirectionToString() string { + return windDirection(w.Direction) +} + +// Retruns unicode block arrow based on wind-direction +func windDirection(d float64) string { + switch { + case d == 0 || d == 360: + return direction.SOUTH + case d <= 45 && d < 90: + return direction.SOUTH_WEST + case d <= 90 && d < 135: + return direction.WEST + case d <= 135 && d < 180: + return direction.NORTH_WEST + case d <= 180 && d < 225: + return direction.NORTH + case d <= 225 && d < 270: + return direction.NORTH_EAST + case d <= 270 && d < 315: + return direction.EAST + case d <= 315 && d < 360: + return direction.SOUTH_EAST + default: + return "↔" + } +} + type NowForecast struct { nominatim.LookupResult Temperature float64 `json:"temperature"` Percipitation float64 `json:"percipitation"` + Wind wind `json:"wind"` } func (c *Client) Now(q string) (*NowForecast, error) { @@ -84,6 +119,10 @@ func (c *Client) NowCoords(coords *nominatim.Coordinates, location *string) (*No }, Temperature: latest.Instant.Details.AirTemperature, Percipitation: latest.Next1_Hours.Details.PrecipitationAmount, + Wind: wind{ + Speed: latest.Instant.Details.WindSpeed, + Direction: latest.Instant.Details.WindFromDirection, + }, }, nil } diff --git a/yr/yr_test.go b/yr/yr_test.go new file mode 100644 index 0000000..bb14257 --- /dev/null +++ b/yr/yr_test.go @@ -0,0 +1,36 @@ +package yr + +import ( + "testing" + + "git.sr.ht/~timharek/yr-go/yr/direction" + "github.com/stretchr/testify/assert" +) + +func TestWindDirection(t *testing.T) { + assert := assert.New(t) + + south := windDirection(0) + assert.Equal(direction.SOUTH, south, "south") + + southwest := windDirection(45) + assert.Equal(direction.SOUTH_WEST, southwest, "southwest") + + west := windDirection(90) + assert.Equal(direction.WEST, west, "west") + + northwest := windDirection(135) + assert.Equal(direction.NORTH_WEST, northwest, "northwest") + + north := windDirection(180) + assert.Equal(direction.NORTH, north, "north") + + northeast := windDirection(225) + assert.Equal(direction.NORTH_EAST, northeast, "northeast") + + east := windDirection(270) + assert.Equal(direction.EAST, east, "east") + + southeast := windDirection(315) + assert.Equal(direction.SOUTH_EAST, southeast, "southeast") +}