Skip to content

Commit fcae1cb

Browse files
committed
Clarify the weather units.
Signed-off-by: Katharine Berry <[email protected]>
1 parent ae07e53 commit fcae1cb

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

service/assistant/functions/weather.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func processDailyForecast(ctx context.Context, lat, lon float64, units string) i
150150
"qpf_snow": forecast.QpfSnow[i],
151151
}
152152
}
153+
response["temperatureUnit"] = forecast.TemperatureUnit
153154
//response["source"] = "The Weather Channel"
154155
return response
155156
}
@@ -180,7 +181,7 @@ func processHourlyForecast(ctx context.Context, lat, lon float64, units string)
180181
response = append(response, entry)
181182
}
182183
// the thing that is returned must not be an array.
183-
return map[string]any{"response": response}
184+
return map[string]any{"response": response, "temperatureUnit": hourly.TemperatureUnit}
184185
}
185186

186187
func processCurrentWeather(ctx context.Context, lat, lon float64, units string) interface{} {

service/assistant/util/weather/weather.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@ func mapUnit(unit string) (string, error) {
3434
return "", fmt.Errorf("unit must be one of 'imperial', 'metric', or 'uk hybrid'; not %q", unit)
3535
}
3636

37+
func mapWindSpeedUnit(unit string) string {
38+
switch unit {
39+
case "e", "h":
40+
return "mph"
41+
case "m":
42+
return "km/h"
43+
}
44+
return ""
45+
}
46+
47+
func mapTemperatureUnit(unit string) string {
48+
switch unit {
49+
case "e":
50+
return "°F"
51+
case "m", "h":
52+
return "°C"
53+
}
54+
return ""
55+
}
56+
3757
func GetDailyForecast(ctx context.Context, lat, lon float64, units string) (*Forecast, error) {
3858
units, err := mapUnit(units)
3959
if err != nil {
@@ -52,6 +72,8 @@ func GetDailyForecast(ctx context.Context, lat, lon float64, units string) (*For
5272
if err := json.NewDecoder(resp.Body).Decode(&forecast); err != nil {
5373
return nil, fmt.Errorf("error decoding response: %w", err)
5474
}
75+
forecast.WindSpeedUnit = mapWindSpeedUnit(units)
76+
forecast.TemperatureUnit = mapTemperatureUnit(units)
5577
return &forecast, nil
5678
}
5779

@@ -73,6 +95,8 @@ func GetCurrentConditions(ctx context.Context, lat, lon float64, units string) (
7395
if err := json.NewDecoder(resp.Body).Decode(&conditions); err != nil {
7496
return nil, fmt.Errorf("error decoding response: %w", err)
7597
}
98+
conditions.WindSpeedUnit = mapWindSpeedUnit(units)
99+
conditions.TemperatureUnit = mapTemperatureUnit(units)
76100
return &conditions, nil
77101
}
78102

@@ -94,6 +118,7 @@ func GetHourlyForecast(ctx context.Context, lat, lon float64, units string) (*Ho
94118
if err := json.NewDecoder(resp.Body).Decode(&hourly); err != nil {
95119
return nil, fmt.Errorf("error decoding response: %w", err)
96120
}
121+
hourly.TemperatureUnit = mapTemperatureUnit(units)
97122
return &hourly, nil
98123
}
99124

@@ -112,6 +137,8 @@ type Forecast struct {
112137
Qpf []float32 `json:"qpf"`
113138
QpfSnow []float32 `json:"qpfSnow"`
114139
DayParts []ForecastDayPart `json:"daypart"`
140+
WindSpeedUnit string `json:"windSpeedUnit"`
141+
TemperatureUnit string `json:"temperatureUnit"`
115142
}
116143

117144
type ForecastDayPart struct {
@@ -160,20 +187,23 @@ type CurrentConditions struct {
160187
TemperatureMax24Hour int `json:"temperatureMax24Hour"`
161188
TemperatureMin24Hour int `json:"temperatureMin24Hour"`
162189
TemperatureWindChill int `json:"temperatureWindChill"`
190+
TemperatureUnit string `json:"temperatureUnit"`
163191
UVIndex int `json:"uvIndex"`
164192
Visibility float32 `json:"visibility"`
165193
WindDirectionCardinal string `json:"windDirectionCardinal"`
166194
WindSpeed int `json:"windSpeed"`
195+
WindSpeedUnit string `json:"windSpeedUnit"`
167196
WindGust int `json:"windGust"`
168197
Description string `json:"wxPhraseLong"`
169198
IconCode int `json:"iconCode"`
170199
}
171200

172201
type HourlyForecast struct {
173-
WxPhraseLong []string `json:"wxPhraseLong"`
174-
Temperature []int `json:"temperature"`
175-
PrecipChance []int `json:"precipChance"`
176-
PrecipType []string `json:"precipType"`
177-
ValidTimeLocal []string `json:"validTimeLocal"`
178-
UVIndex []int `json:"uvIndex"`
202+
WxPhraseLong []string `json:"wxPhraseLong"`
203+
Temperature []int `json:"temperature"`
204+
PrecipChance []int `json:"precipChance"`
205+
PrecipType []string `json:"precipType"`
206+
ValidTimeLocal []string `json:"validTimeLocal"`
207+
UVIndex []int `json:"uvIndex"`
208+
TemperatureUnit string `json:"temperatureUnit"`
179209
}

0 commit comments

Comments
 (0)