Exemple #1
0
        public async Task <ActionResult> UpdateConfirm(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Localization localization = db.Localizations.FirstOrDefault(l => l.Name == id);

            if (localization == null)
            {
                return(HttpNotFound());
            }

            DarkSky.Services.DarkSkyService   darkSkyService = new DarkSky.Services.DarkSkyService(apiKey);
            DarkSky.Models.OptionalParameters parameters     = new DarkSky.Models.OptionalParameters()
            {
                MeasurementUnits = "si",
                LanguageCode     = "pl"
            };

            var forecast = await darkSkyService.GetForecast(localization.Latitude, localization.Longitude, parameters);

            if (forecast?.IsSuccessStatus == true)
            {
                db.UpdateForecastForLocalization(id, forecast.Response);
            }

            return(RedirectToAction("Index"));
        }
        private static List <string> GetTemperatures()
        {
            var darkSky = new DarkSky.Services.DarkSkyService("API_KEY_HERE");

            var date = DateTime.Today.AddDays(-28);

            var temperatures = new List <string>();

            do
            {
                var forecast = darkSky.GetForecast(53.812174, -1.096165,
                                                   new OptionalParameters {
                    MeasurementUnits = "uk2", ForecastDateTime = date
                }).Result;

                var temperatureLow  = Math.Round(forecast.Response.Daily.Data[0].TemperatureMin.Value, 0, MidpointRounding.AwayFromZero);
                var temperatureHigh = Math.Round(forecast.Response.Daily.Data[0].TemperatureMax.Value, 0,
                                                 MidpointRounding.AwayFromZero);
                var temperatureAverage =
                    Math.Round(forecast.Response.Hourly.Data.Select(x => x.Temperature.Value).Average(), 0, MidpointRounding.AwayFromZero);

                temperatures.Add($"Date: {date.ToShortDateString()};   Min: {temperatureLow,2};   Max: {temperatureHigh,2};   Avg: {temperatureAverage,2}");

                date = date.AddDays(1);
            } while (date < DateTime.Today);

            return(temperatures);
        }
        public async Task <Forecast> Get()
        {
            var weather = new DarkSky.Services.DarkSkyService(_settings.Value.DARK_SKY_API_KEY);

            var forecast = await weather.GetForecast(_settings.Value.Latitude, _settings.Value.Longitude, new OptionalParameters {
                MeasurementUnits = "si",
            });

            return(forecast.Response);
        }
        public async Task <IViewComponentResult> InvokeAsync()
        {
            OptionalParameters param = new OptionalParameters();

            param.LanguageCode     = "tr";
            param.MeasurementUnits = "si";
            var             darkSky  = new DarkSky.Services.DarkSkyService("KEY");
            DarkSkyResponse forecast = await darkSky.GetForecast(40.1766256, 28.7582773, param);

            return(View("DarkSkyWidget", forecast));
        }
            private async Task <Forecast> GetForecast()
            {
                var darkSky          = new DarkSky.Services.DarkSkyService(_options.Value.ApiKey);
                var forecastResponse = await darkSky.GetForecast(_options.Value.Latitude, _options.Value.Longitude, new OptionalParameters
                {
                    MeasurementUnits = _options.Value.Unit,
                    LanguageCode     = _options.Value.LanguageCode
                });

                if (!forecastResponse.IsSuccessStatus)
                {
                    throw new Exception(forecastResponse.ResponseReasonPhrase);
                }

                return(forecastResponse.Response);
            }
Exemple #6
0
        public static async Task Forecast()
        {
            var apiKey   = File.ReadLines(@"d:\temp\darksky\apikey.txt").First();
            var darkSky  = new DarkSky.Services.DarkSkyService(apiKey);
            var forecast = await darkSky.GetForecast(52.99042, 8.81614);

            if (forecast?.IsSuccessStatus == true)
            {
                Console.WriteLine(forecast.Response.Currently.Summary);
            }
            else
            {
                Console.WriteLine("No current weather data");
            }
            Console.WriteLine(forecast.AttributionLine);
            Console.WriteLine(forecast.DataSource);
        }
Exemple #7
0
        public async Task <ActionResult> ConfirmUpdateAll()
        {
            DarkSky.Services.DarkSkyService   darkSkyService = new DarkSky.Services.DarkSkyService(apiKey);
            DarkSky.Models.OptionalParameters parameters     = new DarkSky.Models.OptionalParameters()
            {
                MeasurementUnits = "si",
                LanguageCode     = "pl"
            };

            var localizations = db.Localizations.ToList();

            foreach (var loc in localizations)
            {
                var forecast = await darkSkyService.GetForecast(loc.Latitude, loc.Longitude, parameters);

                if (forecast.IsSuccessStatus == true)
                {
                    db.UpdateForecastForLocalization(loc.Name, forecast.Response);
                }
            }

            return(RedirectToAction("Index"));
        }
Exemple #8
0
        static void Main(string[] args)
        {
            var darkSky = new DarkSky.Services.DarkSkyService("API_KEY_HERE");

            var date = new DateTime(2020, 1, 1);

            do
            {
                var forecast = darkSky.GetForecast(53.812174, -1.096165,
                                                   new OptionalParameters {
                    MeasurementUnits = "uk2", ForecastDateTime = date
                }).Result;

                var temperatureLow  = Math.Round(forecast.Response.Daily.Data[0].TemperatureMin.Value, 0, MidpointRounding.AwayFromZero);
                var temperatureHigh = Math.Round(forecast.Response.Daily.Data[0].TemperatureMax.Value, 0,
                                                 MidpointRounding.AwayFromZero);
                var temperatureAverage =
                    Math.Round(forecast.Response.Hourly.Data.Select(x => x.Temperature.Value).Average(), 0, MidpointRounding.AwayFromZero);

                Console.WriteLine($"Date: {date.ToShortDateString()};   Min: {temperatureLow,2};   Max: {temperatureHigh,2};   Avg: {temperatureAverage,2}");

                date = date.AddDays(1);
            } while (date < DateTime.Today);
        }
        public async Task <WeatherItem> GetForecastAsync(IOptions <AppSettings> appSettings)
        {
            WeatherItem weatherForecast = new WeatherItem();

            string apiKey = appSettings.Value.DarkSkyAPIKey;

            var darkSky = new DarkSky.Services.DarkSkyService(apiKey);

            var forecast = await darkSky.GetForecast(51.4557, -2.583,
                                                     new OptionalParameters
            {
                MeasurementUnits = "si"
            });

            if (forecast?.IsSuccessStatus == true)
            {
                weatherForecast.CurrentFeelsLikeTemperature = Convert.ToInt32(forecast.Response.Currently.ApparentTemperature);
                weatherForecast.CurrentSummary     = forecast.Response.Currently.Summary;
                weatherForecast.CurrentTemperature = Convert.ToInt32(forecast.Response.Currently.Temperature);
                weatherForecast.CurrentIconPath    = "/images/" + forecast.Response.Currently.Icon.ToString() + ".svg";

                weatherForecast.MinutelySummary = forecast.Response.Minutely.Summary;

                List <HourData> hourData = new List <HourData>();

                foreach (var hour in forecast.Response.Hourly.Data.GetRange(1, 3))
                {
                    HourData hdata = new HourData();
                    hdata.Time    = DateTime.ParseExact(hour.DateTime.TimeOfDay.ToString(), "HH:mm:ss", new CultureInfo("en-GB")).ToString("HH:mm");
                    hdata.Summary = hour.Summary;
                    hdata.Temp    = Convert.ToInt32(hour.Temperature);
                    hdata.Icon    = "/images/" + hour.Icon.ToString() + ".svg";
                    hourData.Add(hdata);
                }

                weatherForecast.Hour = hourData;

                List <DayData> dayData = new List <DayData>();

                foreach (var day in forecast.Response.Daily.Data.GetRange(0, 4))
                {
                    DayData ddata = new DayData();
                    ddata.Day      = DateTime.ParseExact(day.DateTime.Date.ToString(), "dd/MM/yyyy HH:mm:ss", new CultureInfo("en-GB")).ToString("dd.MM yyyy");
                    ddata.Summary  = day.Summary;
                    ddata.Temp     = Convert.ToInt32(day.TemperatureLow);
                    ddata.TempLow  = Convert.ToInt32(day.TemperatureLow);
                    ddata.TempHigh = Convert.ToInt32(day.TemperatureHigh);
                    ddata.Icon     = "images/" + day.Icon.ToString() + ".svg";
                    dayData.Add(ddata);
                }

                weatherForecast.Day = dayData;

                weatherForecast.Alerts = forecast.Response.Alerts;
            }
            else
            {
                weatherForecast.CurrentSummary = "No current weather data";
            }

            return(weatherForecast);
        }
Exemple #10
0
        async Task GetForecast(double longitude, double latitude)
        {
            var darkSky  = new DarkSky.Services.DarkSkyService("a1c059ee2f1d05ad7900a749d26c3082");
            var forecast = await darkSky.GetForecast(latitude, longitude);

            double[] temp              = new double[49];
            double[] cloudCoverage     = new double[49];
            double[] humidity          = new double[49];
            double[] precipProbability = new double[49];
            string[] summary           = new string[49];
            int[]    uvIndex           = new int[49];

            if (forecast?.IsSuccessStatus == true)
            {
                List <DataPoint> r = forecast.Response.Hourly.Data;
                int    time        = r[10].DateTime.Hour;       //10 hours from now
                string pmoram      = "am";
                if (time > 12)
                {
                    time  -= 12;
                    pmoram = "pm";
                }
                if (time == 0)
                {
                    time = 12;
                }
                string tenhours = time.ToString() + " " + pmoram;
                nowLabel.StringValue = "Now to " + tenhours;
                int sixteenHoursLater = (time + 16) % 12;
                if (sixteenHoursLater == 0)
                {
                    sixteenHoursLater = 12;
                }
                if (time < 6)
                {
                    if (pmoram == "pm")
                    {
                        pmoram = "am";
                    }
                    else
                    {
                        pmoram = "pm";
                    }
                }
                string later = sixteenHoursLater.ToString();

                laterLabel.StringValue = "From " + tenhours + " to " + later + " " + pmoram;
                int most = 0;
                Dictionary <string, int> sum = new Dictionary <string, int>();

                int i;
                for (i = 0; i < 49; i++)
                {
                    temp[i]              = (double)r[i].Temperature;
                    cloudCoverage[i]     = (double)r[i].CloudCover;
                    humidity[i]          = (double)r[i].Humidity;
                    precipProbability[i] = (double)r[i].PrecipProbability;
                    summary[i]           = r[i].Summary;
                    uvIndex[i]           = (int)r[i].UvIndex;
                }
                for (i = 0; i < 11; i++)
                {
                    if (i == 0)
                    {
                        sum.Add(summary[i], 1);
                    }
                    if (!sum.ContainsKey(summary[i]))
                    {
                        sum.Add(summary[i], 1);
                    }
                    else
                    {
                        sum[summary[i]] += 1;
                    }
                    avgTemp          += temp[i];
                    avgCloudCoverage += cloudCoverage[i];
                    avgHumidity      += humidity[i];
                    avgPrecipProb    += precipProbability[i];
                    avgUV            += uvIndex[i];
                }
                if (sum.Count >= 1)
                {
                    foreach (KeyValuePair <string, int> x in sum)
                    {
                        if (x.Value > most)
                        {
                            most       = x.Value;
                            avgSummary = x.Key;
                        }
                    }
                }
                avgTemp          /= 11;
                avgCloudCoverage /= 11;
                avgHumidity      /= 11;
                avgPrecipProb    /= 11;
                avgUV            /= 11;
                if (avgSummary == "Clear")
                {
                    avgSummary = "Clear Skies";
                }
                nowWeatherLabel.StringValue = Math.Round(avgTemp, 2).ToString() + "℉" + GetEmoji(avgSummary) + avgSummary;

                sum.Clear();
                for (i = 11; i < 27; i++)
                {
                    if (i == 11)
                    {
                        sum.Add(summary[i], 1);
                    }
                    if (!sum.ContainsKey(summary[i]))
                    {
                        sum.Add(summary[i], 1);
                    }
                    else
                    {
                        sum[summary[i]] += 1;
                    }
                    avgTempLater          += temp[i];
                    avgCloudCoverageLater += cloudCoverage[i];
                    avgHumidityLater      += humidity[i];
                    avgPrecipProbLater    += precipProbability[i];
                    avgUVLater            += uvIndex[i];
                }
                avgTempLater          /= 16;
                avgCloudCoverageLater /= 16;
                avgHumidityLater      /= 16;
                avgPrecipProbLater    /= 16;
                avgUVLater            /= 16;
                most = 0;
                if (sum.Count >= 1)
                {
                    foreach (KeyValuePair <string, int> x in sum)
                    {
                        if (x.Value > most)
                        {
                            most            = x.Value;
                            avgSummaryLater = x.Key;
                        }
                    }
                }
                if (avgSummaryLater == "Clear")
                {
                    avgSummaryLater = "Clear Skies";
                }
                laterWeatherLabel.StringValue = Math.Round(avgTempLater, 2).ToString() + "℉" + GetEmoji(avgSummaryLater) + avgSummaryLater;
            }
            else
            {
                nowWeatherLabel.StringValue   = "No current weather data";
                laterWeatherLabel.StringValue = "";
            }
        }