Skip to content

Commit

Permalink
⬆️ Upgrade to trafikinfo v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
daenney committed Jun 26, 2022
1 parent c8c4028 commit 6b3c0cd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module hemtjan.st/trafikvader
go 1.18

require (
code.dny.dev/trafikinfo v0.1.0
code.dny.dev/trafikinfo v0.3.0
lib.hemtjan.st v0.7.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
code.dny.dev/trafikinfo v0.1.0 h1:RT5SJUWPghgHpxCvYynrZ9OVBt/gq0J10zLoZC/MiVI=
code.dny.dev/trafikinfo v0.1.0/go.mod h1:vSLjxprxL9sILsFuPFpJicw8LenBsOS1tsMa4oVCIy4=
code.dny.dev/trafikinfo v0.3.0 h1:1IOuLCnA1qkbx4uyxutW6lmZDOPRwOiH70y+kg4cg8M=
code.dny.dev/trafikinfo v0.3.0/go.mod h1:vSLjxprxL9sILsFuPFpJicw8LenBsOS1tsMa4oVCIy4=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down
28 changes: 11 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,7 @@ func retrieve(ctx context.Context, client *http.Client, body []byte) (data, erro
type weatherstationResp struct {
Response struct {
Result []struct {
WeatherStation []*struct {
Name string `json:"Name"`
Measurement struct {
Air struct {
Temp float64 `json:"Temp"`
RelativeHumidity float64 `json:"RelativeHumidity"`
} `json:"Air"`
Precipitation struct {
Amount float64 `json:"Amount"`
} `json:"Precipitation"`
} `json:"Measurement"`
} `json:"WeatherStation"`
WeatherStation []trafikinfo.WeatherStation1Dot0 `json:"WeatherStation"`
} `json:"RESULT"`
} `json:"RESPONSE"`
}
Expand All @@ -226,14 +215,19 @@ func retrieve(ctx context.Context, client *http.Client, body []byte) (data, erro
return data{}, fmt.Errorf("failed to decode response: %w", err)
}

if len(wr.Response.Result) == 0 {
if len(wr.Response.Result[0].WeatherStation) == 0 {
return data{}, fmt.Errorf("station with ID: %s does not exist", *stationID)
}

precip := 0.0
if data := wr.Response.Result[0].WeatherStation[0].Measurement.Precipitation.Amount; data != nil {
precip = *data
}

return data{
name: wr.Response.Result[0].WeatherStation[0].Name,
tempC: wr.Response.Result[0].WeatherStation[0].Measurement.Air.Temp,
rhPct: wr.Response.Result[0].WeatherStation[0].Measurement.Air.RelativeHumidity,
precip: wr.Response.Result[0].WeatherStation[0].Measurement.Precipitation.Amount,
name: *wr.Response.Result[0].WeatherStation[0].Name,
tempC: *wr.Response.Result[0].WeatherStation[0].Measurement.Air.Temperature,
rhPct: *wr.Response.Result[0].WeatherStation[0].Measurement.Air.RelativeHumidity,
precip: precip,
}, nil
}

0 comments on commit 6b3c0cd

Please sign in to comment.