Skip to content

Commit

Permalink
feat: add weather description functionality and integrate into templa…
Browse files Browse the repository at this point in the history
…te functions

- Introduced GetWeatherDescriptionFunc to provide human-readable descriptions for weather codes.
- Updated GetTemplateFunctions to include the new weather description function, enhancing the template's capability to display weather information.
- This change improves user experience by offering clearer insights into weather conditions through descriptions.
  • Loading branch information
tphakala committed Jan 5, 2025
1 parent 5a36c63 commit 897468b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers

import (
"html/template"
"log"
"time"

"github.com/tphakala/birdnet-go/internal/suncalc"
Expand All @@ -12,7 +13,13 @@ import (
// GetWeatherIconFunc returns a function that returns an SVG icon for a given weather main
func (h *Handlers) GetWeatherIconFunc() func(weatherCode string, timeOfDay weather.TimeOfDay) template.HTML {
return func(weatherCode string, timeOfDay weather.TimeOfDay) template.HTML {
// Strip 'd' or 'n' suffix if present
if len(weatherCode) > 2 {
weatherCode = weatherCode[:2]
}
iconCode := weather.IconCode(weatherCode)
// debug
log.Printf("iconCode: %s, weatherCode: %s", iconCode, weatherCode)
return weather.GetWeatherIcon(iconCode, timeOfDay)
}
}
Expand All @@ -33,3 +40,15 @@ func (h *Handlers) CalculateTimeOfDay(noteTime time.Time, sunEvents suncalc.SunE
func (h *Handlers) TimeOfDayToInt(s string) weather.TimeOfDay {
return weather.StringToTimeOfDay(s)
}

// GetWeatherDescriptionFunc returns a function that returns a description for a given weather code
func (h *Handlers) GetWeatherDescriptionFunc() func(weatherCode string) string {
return func(weatherCode string) string {
// Strip 'd' or 'n' suffix if present
if len(weatherCode) > 2 {
weatherCode = weatherCode[:2]
}
iconCode := weather.IconCode(weatherCode)
return weather.GetIconDescription(iconCode)
}
}
1 change: 1 addition & 0 deletions internal/httpcontroller/template_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (s *Server) GetTemplateFunctions() template.FuncMap {
"getHourlyHeaderData": getHourlyHeaderData,
"getHourlyCounts": getHourlyCounts,
"sumHourlyCountsRange": sumHourlyCountsRange,
"weatherDescription": s.Handlers.GetWeatherDescriptionFunc(),
}
}

Expand Down

0 comments on commit 897468b

Please sign in to comment.