diff --git a/loxone_codes.go b/loxone_codes.go index b1e3670..29b5738 100644 --- a/loxone_codes.go +++ b/loxone_codes.go @@ -1,10 +1,77 @@ package main +// https://github.com/sarnau/Inside-The-Loxone-Miniserver/blob/master/Code/LoxoneWeather.py +// +// 1 Clear, cloudless sky (Loxone: Wolkenlos) +// 2 Clear, few cirrus (Loxone: Wolkenlos) +// 3 Clear with cirrus (Loxone: Heiter) +// 4 Clear with few low clouds (Loxone: Heiter) +// 5 Clear with few low clouds and few cirrus (Loxone: Heiter) +// 6 Clear with few low clouds and cirrus (Loxone: Heiter) +// 7 Partly cloudy (Loxone: Heiter) +// 8 Partly cloudy and few cirrus (Loxone: Heiter) +// 9 Partly cloudy and cirrus (Loxone: Wolkig) +// +// 10 Mixed with some thunderstorm clouds possible (Loxone: Wolkig) +// 11 Mixed with few cirrus with some thunderstorm clouds possible (Loxone: Wolkig) +// 12 Mixed with cirrus and some thunderstorm clouds possible (Loxone: Wolkig) +// 13 Clear but hazy (Loxone: Wolkenlos) +// 14 Clear but hazy with few cirrus (Loxone: Heiter) +// 15 Clear but hazy with cirrus (Loxone: Heiter) +// 16 Fog/low stratus clouds (Loxone: Nebel) +// 17 Fog/low stratus clouds with few cirrus (Loxone: Nebel) +// 18 Fog/low stratus clouds with cirrus (Loxone: Nebel) +// 19 Mostly cloudy (Loxone: Stark bewölkt) +// 20 Mostly cloudy and few cirrus (Loxone: Stark bewölkt) +// 21 Mostly cloudy and cirrus (Loxone: Stark bewölkt) +// 22 Overcast (Loxone: Bedeckt) +// 23 Overcast with rain (Loxone: Regen) +// 24 Overcast with snow (Loxone: Schneefall) +// 25 Overcast with heavy rain (Loxone: Starker Regen) +// 26 Overcast with heavy snow (Loxone: Starker Schneefall) +// 27 Rain, thunderstorms likely (Loxone: Kräftiges Gewitter) +// 28 Light rain, thunderstorms likely (Loxone: Gewitter) +// 29 Storm with heavy snow (Loxone: Starker Schneeschauer) +// 30 Heavy rain, thunderstorms likely (Loxone: Kräftiges Gewitter) +// 31 Mixed with showers (Loxone: Leichter Regenschauer) +// 32 Mixed with snow showers (Loxone: Leichter Schneeschauer) +// 33 Overcast with light rain (Loxone: Leichter Regen) +// 34 Overcast with light snow (Loxone: Leichter Schneeschauer) +// 35 Overcast with mixture of snow and rain (Loxone: Schneeregen) const ( - Clear = 1 - ClearFewCirrus = 2 - ClearWithCirrus = 3 - PartlyCloud = 7 - MostlyCloudy = 19 - Overcast = 22 + ClearSky = 1 + ClearFewCirrus = 2 + ClearWithCirrus = 3 + ClearWithFewLowClouds = 4 + ClearWithFewLowCloudsAndFewCirrus = 5 + ClearWithFewLowCloudsAndCirrus = 6 + PartlyCloudy = 7 + PartlyCloudyAndFewCirrus = 8 + PartlyCloudyAndCirrus = 9 + MixedWithSomeThunderstormClouds = 10 + MixedWithFewCirrusAndThunderstorm = 11 + MixedWithCirrusAndThunderstorm = 12 + ClearButHazy = 13 + ClearButHazyWithFewCirrus = 14 + ClearButHazyWithCirrus = 15 + FogLowStratusClouds = 16 + FogLowStratusCloudsWithFewCirrus = 17 + FogLowStratusCloudsWithCirrus = 18 + MostlyCloudy = 19 + MostlyCloudyAndFewCirrus = 20 + MostlyCloudyAndCirrus = 21 + Overcast = 22 + OvercastWithRain = 23 + OvercastWithSnow = 24 + OvercastWithHeavyRain = 25 + OvercastWithHeavySnow = 26 + RainThunderstormsLikely = 27 + LightRainThunderstormsLikely = 28 + StormWithHeavySnow = 29 + HeavyRainThunderstormsLikely = 30 + MixedWithShowers = 31 + MixedWithSnowShowers = 32 + OvercastWithLightRain = 33 + OvercastWithLightSnow = 34 + OvercastWithMixtureOfSnowAndRain = 35 ) diff --git a/service.go b/service.go index 134f68b..d7ef1ad 100644 --- a/service.go +++ b/service.go @@ -6,6 +6,8 @@ import ( "github.com/rs/zerolog/log" "io/ioutil" "net/http" + "net/http/httputil" + "net/url" "os" "strconv" "strings" @@ -71,25 +73,31 @@ func (s *Service) GetForecast(coord string, asl string) (interface{}, error) { } func (s *Service) downloadReport(longitude, latitude float64) (*Tomorrow_Forecast, error) { - path := - fmt.Sprintf("https://api.tomorrow.io/v4/forecast?location=%.3f,%.2f&apikey=%s", latitude, longitude, s.apiKey) - request, err := http.NewRequest("GET", path, nil) + request, err := http.NewRequest("GET", "https://api.tomorrow.io/v4/weather/forecast", nil) if err != nil { return nil, err } q := request.URL.Query() - q.Add("timesteps", "1h") + q.Add("location", fmt.Sprintf("%.3f,%.3f", latitude, longitude)) + q.Add("apikey", s.apiKey) + q.Add("timesteps", "hourly") q.Add("units", "metric") - request.URL.RawQuery = q.Encode() + decoded, _ := url.QueryUnescape(q.Encode()) + request.URL.RawQuery = decoded resp, err := http.Get(request.URL.String()) if err != nil { return nil, err } + dump, _ := httputil.DumpResponse(resp, true) + log.Debug().Msg("api response received") + fmt.Printf("%s", dump) + if resp.StatusCode != 200 { + return nil, fmt.Errorf("invalid response with code %d, error: %s", resp.StatusCode, resp.Status) + } var result Tomorrow_Forecast defer resp.Body.Close() - //dump, _ := httputil.DumpResponse(resp, true) - //log.Debug().Msg("api response received") + json.NewDecoder(resp.Body).Decode(&result) return &result, nil diff --git a/tomorrow_icons.go b/tomorrow_icons.go index 46b2eb2..36a002f 100644 --- a/tomorrow_icons.go +++ b/tomorrow_icons.go @@ -1,73 +1,82 @@ package main -// 1 Clear, cloudless sky (Loxone: Wolkenlos) -// 2 Clear, few cirrus (Loxone: Wolkenlos) -// 3 Clear with cirrus (Loxone: Heiter) -// 4 Clear with few low clouds (Loxone: Heiter) -// 5 Clear with few low clouds and few cirrus (Loxone: Heiter) -// 6 Clear with few low clouds and cirrus (Loxone: Heiter) -// 7 Partly cloudy (Loxone: Heiter) -// 8 Partly cloudy and few cirrus (Loxone: Heiter) -// 9 Partly cloudy and cirrus (Loxone: Wolkig) -// -// 10 Mixed with some thunderstorm clouds possible (Loxone: Wolkig) -// 11 Mixed with few cirrus with some thunderstorm clouds possible (Loxone: Wolkig) -// 12 Mixed with cirrus and some thunderstorm clouds possible (Loxone: Wolkig) -// 13 Clear but hazy (Loxone: Wolkenlos) -// 14 Clear but hazy with few cirrus (Loxone: Heiter) -// 15 Clear but hazy with cirrus (Loxone: Heiter) -// 16 Fog/low stratus clouds (Loxone: Nebel) -// 17 Fog/low stratus clouds with few cirrus (Loxone: Nebel) -// 18 Fog/low stratus clouds with cirrus (Loxone: Nebel) -// 19 Mostly cloudy (Loxone: Stark bewölkt) -// 20 Mostly cloudy and few cirrus (Loxone: Stark bewölkt) -// 21 Mostly cloudy and cirrus (Loxone: Stark bewölkt) -// 22 Overcast (Loxone: Bedeckt) -// 23 Overcast with rain (Loxone: Regen) -// 24 Overcast with snow (Loxone: Schneefall) -// 25 Overcast with heavy rain (Loxone: Starker Regen) -// 26 Overcast with heavy snow (Loxone: Starker Schneefall) -// 27 Rain, thunderstorms likely (Loxone: Kräftiges Gewitter) -// 28 Light rain, thunderstorms likely (Loxone: Gewitter) -// 29 Storm with heavy snow (Loxone: Starker Schneeschauer) -// 30 Heavy rain, thunderstorms likely (Loxone: Kräftiges Gewitter) -// 31 Mixed with showers (Loxone: Leichter Regenschauer) -// 32 Mixed with snow showers (Loxone: Leichter Schneeschauer) -// 33 Overcast with light rain (Loxone: Leichter Regen) -// 34 Overcast with light snow (Loxone: Leichter Schneeschauer) -// 35 Overcast with mixture of snow and rain (Loxone: Schneeregen) -func fixTomorrow(report Tomorrow_Report) int { +// https://docs.tomorrow.io/reference/data-layers-weather-codes +//"0": "Unknown", +// "1000": "Clear, Sunny", +// "1100": "Mostly Clear", +// "1101": "Partly Cloudy", +// "1102": "Mostly Cloudy", +// "1001": "Cloudy", +// "2000": "Fog", +// "2100": "Light Fog", +// "4000": "Drizzle", +// "4001": "Rain", +// "4200": "Light Rain", +// "4201": "Heavy Rain", +// "5000": "Snow", +// "5001": "Flurries", +// "5100": "Light Snow", +// "5101": "Heavy Snow", +// "6000": "Freezing Drizzle", +// "6001": "Freezing Rain", +// "6200": "Light Freezing Rain", +// "6201": "Heavy Freezing Rain", +// "7000": "Ice Pellets", +// "7101": "Heavy Ice Pellets", +// "7102": "Light Ice Pellets", +// "8000": "Thunderstorm" - // https://docs.tomorrow.io/reference/data-layers-weather-codes - switch report.Values.WeatherCode { +func getWeatherCondition(code int) int { + switch code { + case 0: + return 0 case 1000: - return + return ClearSky + case 1100: + return ClearSky + case 1101: + return PartlyCloudy + case 1102: + return MostlyCloudy + case 1001: + return MostlyCloudy + case 2000: + return FogLowStratusClouds + case 2100: + return FogLowStratusClouds + case 4000: + return OvercastWithRain + case 4001: + return OvercastWithRain + case 4200: + return LightRainThunderstormsLikely + case 4201: + return HeavyRainThunderstormsLikely + case 5000: + return MostlyCloudyAndCirrus + case 5001: + return OvercastWithRain + case 5100: + return OvercastWithSnow + case 5101: + return StormWithHeavySnow + case 6000: + return OvercastWithMixtureOfSnowAndRain + case 6001: + return OvercastWithMixtureOfSnowAndRain + case 6200: + return OvercastWithMixtureOfSnowAndRain + case 6201: + return MixedWithSnowShowers + case 7000: + return ClearButHazyWithCirrus + case 7101: + return ClearButHazyWithCirrus + case 7102: + return ClearButHazyWithCirrus + case 8000: + return HeavyRainThunderstormsLikely + default: + return 0 } - var id = 0 - //"0": "Unknown", - // "1000": "Clear, Sunny", - // "1100": "Mostly Clear", - // "1101": "Partly Cloudy", - // "1102": "Mostly Cloudy", - // "1001": "Cloudy", - // "2000": "Fog", - // "2100": "Light Fog", - // "4000": "Drizzle", - // "4001": "Rain", - // "4200": "Light Rain", - // "4201": "Heavy Rain", - // "5000": "Snow", - // "5001": "Flurries", - // "5100": "Light Snow", - // "5101": "Heavy Snow", - // "6000": "Freezing Drizzle", - // "6001": "Freezing Rain", - // "6200": "Light Freezing Rain", - // "6201": "Heavy Freezing Rain", - // "7000": "Ice Pellets", - // "7101": "Heavy Ice Pellets", - // "7102": "Light Ice Pellets", - // "8000": "Thunderstorm" - return id - }