2024-04-27 21:10:24 +02:00
|
|
|
package widget
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
|
|
|
|
"github.com/glanceapp/glance/internal/assets"
|
|
|
|
"github.com/glanceapp/glance/internal/feed"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Weather struct {
|
|
|
|
widgetBase `yaml:",inline"`
|
|
|
|
Location string `yaml:"location"`
|
2024-05-02 20:54:20 +02:00
|
|
|
ShowAreaName bool `yaml:"show-area-name"`
|
2024-04-27 21:10:24 +02:00
|
|
|
HideLocation bool `yaml:"hide-location"`
|
2024-05-13 14:08:58 +02:00
|
|
|
HourFormat string `yaml:"hour-format"`
|
2024-04-29 17:29:11 +02:00
|
|
|
Units string `yaml:"units"`
|
2024-04-27 21:10:24 +02:00
|
|
|
Place *feed.PlaceJson `yaml:"-"`
|
|
|
|
Weather *feed.Weather `yaml:"-"`
|
|
|
|
TimeLabels [12]string `yaml:"-"`
|
|
|
|
}
|
|
|
|
|
2024-05-13 14:08:58 +02:00
|
|
|
var timeLabels12h = [12]string{"2am", "4am", "6am", "8am", "10am", "12pm", "2pm", "4pm", "6pm", "8pm", "10pm", "12am"}
|
2024-05-17 20:28:15 +02:00
|
|
|
var timeLabels24h = [12]string{"02:00", "04:00", "06:00", "08:00", "10:00", "12:00", "14:00", "16:00", "18:00", "20:00", "22:00", "00:00"}
|
2024-04-27 21:10:24 +02:00
|
|
|
|
|
|
|
func (widget *Weather) Initialize() error {
|
|
|
|
widget.withTitle("Weather").withCacheOnTheHour()
|
2024-05-13 14:08:58 +02:00
|
|
|
|
2024-05-28 18:45:02 +02:00
|
|
|
if widget.Location == "" {
|
|
|
|
return fmt.Errorf("location must be specified for weather widget")
|
|
|
|
}
|
|
|
|
|
2024-05-13 14:08:58 +02:00
|
|
|
if widget.HourFormat == "" || widget.HourFormat == "12h" {
|
|
|
|
widget.TimeLabels = timeLabels12h
|
|
|
|
} else if widget.HourFormat == "24h" {
|
|
|
|
widget.TimeLabels = timeLabels24h
|
2024-05-17 19:37:11 +02:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("invalid hour format '%s' for weather widget, must be either 12h or 24h", widget.HourFormat)
|
2024-05-13 14:08:58 +02:00
|
|
|
}
|
2024-04-27 21:10:24 +02:00
|
|
|
|
2024-04-29 17:29:11 +02:00
|
|
|
if widget.Units == "" {
|
|
|
|
widget.Units = "metric"
|
|
|
|
} else if widget.Units != "metric" && widget.Units != "imperial" {
|
|
|
|
return fmt.Errorf("invalid units '%s' for weather, must be either metric or imperial", widget.Units)
|
|
|
|
}
|
|
|
|
|
2024-04-27 21:10:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (widget *Weather) Update(ctx context.Context) {
|
2024-05-28 18:45:02 +02:00
|
|
|
if widget.Place == nil {
|
|
|
|
place, err := feed.FetchPlaceFromName(widget.Location)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
widget.withError(err).scheduleEarlyUpdate()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.Place = place
|
|
|
|
}
|
|
|
|
|
2024-04-29 17:29:11 +02:00
|
|
|
weather, err := feed.FetchWeatherForPlace(widget.Place, widget.Units)
|
2024-04-27 21:10:24 +02:00
|
|
|
|
|
|
|
if !widget.canContinueUpdateAfterHandlingErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.Weather = weather
|
|
|
|
}
|
|
|
|
|
|
|
|
func (widget *Weather) Render() template.HTML {
|
|
|
|
return widget.render(widget, assets.WeatherTemplate)
|
|
|
|
}
|