feat: Add windspeed and wind-direction

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-10-02 19:04:01 +02:00
parent 36309212a7
commit ff9f40e2f8
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
4 changed files with 91 additions and 1 deletions

View file

@ -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(),
)
}

12
yr/direction/direction.go Normal file
View file

@ -0,0 +1,12 @@
package direction
const (
NORTH = "↑"
SOUTH = "↓"
WEST = "←"
EAST = "→"
NORTH_WEST = "↖"
NORTH_EAST = "↗"
SOUTH_WEST = "↙"
SOUTH_EAST = "↘"
)

View file

@ -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
}

36
yr/yr_test.go Normal file
View file

@ -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")
}