itd/weather.go

248 lines
5.8 KiB
Go
Raw Permalink Normal View History

2022-02-22 00:18:52 +00:00
package main
import (
"context"
2022-02-22 00:18:52 +00:00
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"go.elara.ws/itd/infinitime"
2023-04-21 02:54:58 +00:00
"go.elara.ws/logger/log"
2022-02-22 00:18:52 +00:00
)
// METResponse represents a response from
// the MET Norway API
type METResponse struct {
Properties struct {
Timeseries []struct {
Time time.Time
Data METData
}
}
}
// METData represents data in a METResponse
type METData struct {
Instant struct {
Details struct {
AirPressure float32 `json:"air_pressure_at_sea_level"`
Temperature float32 `json:"air_temperature"`
2022-02-22 00:18:52 +00:00
DewPoint float32 `json:"dew_point_temperature"`
CloudAreaFraction float32 `json:"cloud_area_fraction"`
FogAreaFraction float32 `json:"fog_area_fraction"`
RelativeHumidity float32 `json:"relative_humidity"`
UVIndex float32 `json:"ultraviolet_index_clear_sky"`
WindDirection float32 `json:"wind_from_direction"`
WindSpeed float32 `json:"wind_speed"`
}
}
NextHour struct {
Summary struct {
SymbolCode string `json:"symbol_code"`
}
Details struct {
PrecipitationAmount float32 `json:"precipitation_amount"`
}
} `json:"next_1_hours"`
Next6Hours struct {
Details struct {
MaxTemp float32 `json:"air_temperature_max"`
MinTemp float32 `json:"air_temperature_min"`
}
} `json:"next_6_hours"`
2022-02-22 00:18:52 +00:00
}
// OSMData represents lat/long data from
// OpenStreetMap Nominatim
type OSMData []struct {
Lat string `json:"lat"`
Lon string `json:"lon"`
}
var sendWeatherCh = make(chan struct{}, 1)
func sleepCtx(ctx context.Context, d time.Duration) {
select {
case <-time.After(d):
case <-ctx.Done():
}
}
func initWeather(ctx context.Context, wg WaitGroup, dev *infinitime.Device) error {
if !k.Bool("weather.enabled") {
return nil
}
2022-02-22 00:18:52 +00:00
// Get location based on string in config
lat, lon, err := getLocation(ctx, k.String("weather.location"))
2022-02-22 00:18:52 +00:00
if err != nil {
return err
}
timer := time.NewTimer(time.Hour)
wg.Add(1)
2022-02-22 00:18:52 +00:00
go func() {
defer wg.Done("weather")
2022-02-22 00:18:52 +00:00
for {
select {
case _, ok := <-ctx.Done():
if !ok {
return
}
default:
}
2022-02-22 00:18:52 +00:00
// Attempt to get weather
data, err := getWeather(ctx, lat, lon)
2022-02-22 00:18:52 +00:00
if err != nil {
log.Warn("Error getting weather data").Err(err).Send()
2022-02-22 00:18:52 +00:00
// Wait 15 minutes before retrying
sleepCtx(ctx, 15*time.Minute)
2022-02-22 00:18:52 +00:00
continue
}
// Get current data
current := data.Properties.Timeseries[0]
currentData := current.Data.Instant.Details
icon := parseSymbol(current.Data.NextHour.Summary.SymbolCode)
if icon == infinitime.WeatherIconClear {
switch {
2024-04-14 04:36:57 +00:00
case currentData.CloudAreaFraction > 50:
icon = infinitime.WeatherIconHeavyClouds
2024-04-14 04:36:57 +00:00
case currentData.CloudAreaFraction == 50:
icon = infinitime.WeatherIconClouds
case currentData.CloudAreaFraction > 0:
icon = infinitime.WeatherIconFewClouds
}
2022-02-22 00:18:52 +00:00
}
err = dev.SetCurrentWeather(infinitime.CurrentWeather{
Time: time.Now(),
CurrentTemp: currentData.Temperature,
MaxTemp: current.Data.Next6Hours.Details.MaxTemp,
MinTemp: current.Data.Next6Hours.Details.MinTemp,
Location: k.String("weather.location"),
Icon: icon,
2022-02-22 00:18:52 +00:00
})
if err != nil {
log.Error("Error setting weather").Err(err).Send()
2022-02-22 00:18:52 +00:00
}
2022-02-24 05:22:03 +00:00
// Reset timer to 1 hour
timer.Stop()
2022-02-22 00:18:52 +00:00
timer.Reset(time.Hour)
2022-02-24 05:22:03 +00:00
// Wait for timer to fire or manual update signal
2022-02-22 00:18:52 +00:00
select {
case <-timer.C:
case <-sendWeatherCh:
case <-ctx.Done():
return
2022-02-22 00:18:52 +00:00
}
}
}()
return nil
}
// getLocation returns the latitude and longitude
// given a location
func getLocation(ctx context.Context, loc string) (lat, lon float64, err error) {
2022-02-22 00:18:52 +00:00
// Create request URL and perform GET request
reqURL := fmt.Sprintf("https://nominatim.openstreetmap.org/search.php?q=%s&format=jsonv2", url.QueryEscape(loc))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
if err != nil {
return
}
2024-06-04 22:00:42 +00:00
req.Header.Set("User-Agent", fmt.Sprintf("ITD/%s gitea.elara.ws/Elara6331/itd", strings.TrimSpace(version)))
res, err := http.DefaultClient.Do(req)
2022-02-22 00:18:52 +00:00
if err != nil {
return
}
// Decode JSON from response into OSMData
data := OSMData{}
err = json.NewDecoder(res.Body).Decode(&data)
if err != nil {
return
}
// If no data points
if len(data) == 0 {
return
}
// Get first data point
out := data[0]
// Attempt to parse latitude
lat, err = strconv.ParseFloat(out.Lat, 64)
if err != nil {
return
}
// Attempt to parse longitude
lon, err = strconv.ParseFloat(out.Lon, 64)
if err != nil {
return
}
return
}
// getWeather gets weather data given a latitude and longitude
func getWeather(ctx context.Context, lat, lon float64) (*METResponse, error) {
2022-02-22 00:18:52 +00:00
// Create new GET request
req, err := http.NewRequestWithContext(
ctx,
2022-02-22 00:18:52 +00:00
http.MethodGet,
fmt.Sprintf(
"https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=%.2f&lon=%.2f",
lat,
lon,
),
nil,
)
if err != nil {
return nil, err
}
// Set identifying user agent as per NMI requirements
2024-06-04 22:00:42 +00:00
req.Header.Set("User-Agent", fmt.Sprintf("ITD/%s gitea.elara.ws/Elara6331/itd", strings.TrimSpace(version)))
2022-02-22 00:18:52 +00:00
// Perform request
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
// Decode JSON from response to METResponse struct
out := &METResponse{}
err = json.NewDecoder(res.Body).Decode(out)
if err != nil {
return nil, err
}
return out, nil
}
// parseSymbol determines what weather icon a symbol code codes for.
func parseSymbol(symCode string) infinitime.WeatherIcon {
2022-02-22 00:18:52 +00:00
switch {
case strings.Contains(symCode, "lightrain"):
return infinitime.WeatherIconRain
2022-02-22 00:18:52 +00:00
case strings.Contains(symCode, "rain"):
return infinitime.WeatherIconCloudsWithRain
case strings.Contains(symCode, "snow"),
strings.Contains(symCode, "sleet"):
return infinitime.WeatherIconSnow
case strings.Contains(symCode, "thunder"):
return infinitime.WeatherIconThunderstorm
2022-02-22 00:18:52 +00:00
default:
return infinitime.WeatherIconClear
2022-02-22 00:18:52 +00:00
}
}