Skip to content

Commit

Permalink
Add wind direction and debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Pereira committed Sep 8, 2024
1 parent 7996aec commit 311d698
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions backends/open-meteo.com.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/schachmat/wego/iface"
"io"
"log"
"net/http"
"regexp"
"strings"
"time"

"github.com/schachmat/wego/iface"
)

type openmeteoConfig struct {
Expand All @@ -26,6 +27,7 @@ type curCond struct {
ApparentTemperature *float32 `json:"apparent_temperature"`
IsDay int `json:"is_day"`
WeatherCode int `json:"weather_code"`
WindDirection10M *int `json:"wind_direction_10m"`
}

type Daily struct {
Expand All @@ -47,6 +49,7 @@ type Hourly struct {
Temperature2M []*float32 `json:"temperature_2m"`
ApparentTemperature []*float32 `json:"apparent_temperature"`
WeatherCode []int `json:"weather_code"`
WindDirection10M []*int `json:"wind_direction_10m"`
}

type openmeteoResponse struct {
Expand Down Expand Up @@ -115,13 +118,13 @@ func (opmeteo *openmeteoConfig) parseDaily(dailyInfo Hourly) []iface.Day {

for ind, dayTime := range dailyInfo.Time {

//day := new(iface.Day)
cond := new(iface.Cond)

cond.Code = codemap[dailyInfo.WeatherCode[ind]]
cond.TempC = dailyInfo.Temperature2M[ind]
cond.FeelsLikeC = dailyInfo.ApparentTemperature[ind]
cond.Time = time.Unix(dayTime, 0)
cond.WinddirDegree = dailyInfo.WindDirection10M[ind]

if day == nil {
day = new(iface.Day)
Expand Down Expand Up @@ -153,6 +156,7 @@ func parseCurCond(current curCond) (ret iface.Cond) {

ret.TempC = current.Temperature2M
ret.FeelsLikeC = current.ApparentTemperature
ret.WinddirDegree = current.WindDirection10M
return ret

}
Expand All @@ -169,7 +173,7 @@ func (opmeteo *openmeteoConfig) Fetch(location string, numdays int) iface.Data {
if len(location) > 0 {
params = append(params, loc)
}
params = append(params, "current=temperature_2m,apparent_temperature,is_day,weather_code&hourly=temperature_2m,apparent_temperature,weather_code&daily=weather_code,temperature_2m_max,apparent_temperature_max,sunrise,sunset&timeformat=unixtime&forecast_days=3")
params = append(params, "current=temperature_2m,apparent_temperature,is_day,weather_code&hourly=temperature_2m,apparent_temperature,weather_code,wind_direction_10m&daily=weather_code,temperature_2m_max,apparent_temperature_max,sunrise,sunset&timeformat=unixtime&forecast_days=3")

requri := openmeteoURI + strings.Join(params, "&")

Expand All @@ -188,7 +192,8 @@ func (opmeteo *openmeteoConfig) Fetch(location string, numdays int) iface.Data {

if opmeteo.debug {
log.Println("Weather request:", requri)
log.Println("Weather response:", string(body))
b, _ := json.MarshalIndent(body, "", "\t")
fmt.Println("Weather response:", string(b))
}

var resp openmeteoResponse
Expand All @@ -207,8 +212,6 @@ func (opmeteo *openmeteoConfig) Fetch(location string, numdays int) iface.Data {
ret.Forecast = forecast
}

//jcart, _ := json.MarshalIndent(ret, "", "\t")
//fmt.Println(string(jcart))
return ret
}

Expand Down

0 comments on commit 311d698

Please sign in to comment.