Example #1
0
        protected virtual SortedDictionary <DateTime, OWMForecastSummary> SummarizeForecast(OWMForecast forecast)
        {
            var ret = new SortedDictionary <DateTime, OWMForecastSummary>();

            foreach (OWMWeatherState state in forecast.WeatherStates)
            {
                DateTimeOffset timestamp = DateTimeOffset.FromUnixTimeSeconds(state.UnixTimestamp);
                DateTime       date      = timestamp.Date;

                OWMForecastSummary curSummary;
                if (ret.TryGetValue(date, out curSummary))
                {
                    curSummary.MaxTempKelvin = Math.Max(curSummary.MaxTempKelvin, state.Main.MaximumTemperatureKelvin);
                    curSummary.MinTempKelvin = Math.Min(curSummary.MinTempKelvin, state.Main.MinimumTemperatureKelvin);
                }
                else
                {
                    curSummary = new OWMForecastSummary
                    {
                        MaxTempKelvin = state.Main.MaximumTemperatureKelvin,
                        MinTempKelvin = state.Main.MinimumTemperatureKelvin,
                        WeatherStates = new List <string>(),
                    };
                    ret[date] = curSummary;
                }

                foreach (OWMWeather weather in state.Weathers)
                {
                    if (!curSummary.WeatherStates.Contains(weather.Main))
                    {
                        curSummary.WeatherStates.Add(weather.Main);
                    }
                }
            }

            return(ret);
        }
Example #2
0
        public string GetWeatherDescriptionForCoordinates(decimal latitudeDegNorth, decimal longitudeDegEast)
        {
            if (!CheckCooldownEnough(2))
            {
                return("OpenWeatherMap is on cooldown. :(");
            }

            string weatherUri = string.Format(
                CultureInfo.InvariantCulture,
                "https://api.openweathermap.org/data/2.5/weather?lat={0}&lon={1}&appid={2}",
                latitudeDegNorth, longitudeDegEast, Config.ApiKey
                );
            var    currentWeather = new OWMWeatherState();
            string err            = GetAndPopulateJson(weatherUri, currentWeather);

            RegisterForCooldown();
            if (err != null)
            {
                return(err);
            }

            string forecastUri = string.Format(
                CultureInfo.InvariantCulture,
                "https://api.openweathermap.org/data/2.5/forecast?lat={0}&lon={1}&appid={2}",
                latitudeDegNorth, longitudeDegEast, Config.ApiKey
                );
            var forecast = new OWMForecast();

            err = GetAndPopulateJson(forecastUri, forecast);
            RegisterForCooldown();
            if (err != null)
            {
                return(err);
            }

            var builder = new StringBuilder();

            // weather status
            if (currentWeather.Weathers.Count > 0)
            {
                builder.Append(currentWeather.Weathers[0].Main);
            }

            // current temperature
            if (builder.Length > 0)
            {
                builder.Append(", ");
            }
            builder.AppendFormat(
                CultureInfo.InvariantCulture,
                "{0:0} \u00B0C",
                KelvinToCelsius(currentWeather.Main.TemperatureKelvin)
                );

            // current humidity
            if (builder.Length > 0)
            {
                builder.Append(", ");
            }
            builder.AppendFormat(
                CultureInfo.InvariantCulture,
                "{0}% humidity",
                currentWeather.Main.HumidityPercent
                );

            if (forecast.WeatherStates.Count > 0)
            {
                if (builder.Length > 0)
                {
                    builder.Append("; ");
                }
                builder.Append("forecast: ");

                SortedDictionary <DateTime, OWMForecastSummary> summarized = SummarizeForecast(forecast);
                string forecastString = summarized
                                        .Select(kvp => string.Format(
                                                    CultureInfo.InvariantCulture,
                                                    "{0} {1}.{2:00}. {3} {4:0}\u2013{5:0} \u00B0C",
                                                    kvp.Key.DayOfWeek.ToString().Substring(0, 2),
                                                    kvp.Key.Day,
                                                    kvp.Key.Month,
                                                    kvp.Value.WeatherStates.StringJoin("/"),
                                                    KelvinToCelsius(kvp.Value.MinTempKelvin),
                                                    KelvinToCelsius(kvp.Value.MaxTempKelvin)
                                                    ))
                                        .StringJoin(", ");
                builder.Append(forecastString);
            }

            return("OpenWeatherMap: " + builder.ToString());
        }