Пример #1
0
        private WeatherForcast GetBy_APIxu(String QueryVar)
        {
            WeatherForcast ThisWeather = new WeatherForcast();

            try {
                DateTime LastCashedOn  = getLastProcessedDateTime(QueryVar);
                DateTime MaxCashedTime = LastCashedOn.AddMinutes(30);

                if (MaxCashedTime > DateTime.Now)
                {
                    return(CashedWeather(QueryVar));
                }

                String URL      = $"http://api.apixu.com/v1/forecast.json?key=773b7eabe1a848ca94164724172008&q={QueryVar}&days=5";
                String JsonData = getWeatherJson(URL);
                if (String.IsNullOrEmpty(JsonData))
                {
                    return(CashedWeather(QueryVar));
                }
                else
                {
                    APIxu.WeatherModel APIxuWeather = JsonConvert.DeserializeObject <APIxu.WeatherModel>(JsonData);
                    ThisWeather.City     = APIxuWeather.location.region;
                    ThisWeather.Country  = APIxuWeather.location.country;
                    ThisWeather.Forecast = APIxu.Pipe.GetForcast(APIxuWeather.forecast);
                    ThisWeather.Today    = ThisWeather.Forecast[0];
                    APIxu.Pipe.SetToday(APIxuWeather.current, ThisWeather.Today);
                    SaveWeatherCashe(ThisWeather, QueryVar);
                }
            } catch {
                ThisWeather.Today.ConditionText = "Error";
            }
            return(ThisWeather);
        }
Пример #2
0
        private void SaveWeatherCashe(WeatherForcast ThisWeather, String LatLngFolder = "")
        {
            //save the cached file
            String Country = ThisWeather.Country.Replace(" ", "");
            String City    = ThisWeather.City.Replace(" ", "");

            String FileDir = String.IsNullOrEmpty(LatLngFolder) ?
                             Path.Combine(ApplicationPah, Country, City) :
                             Path.Combine(ApplicationPah, LatLngFolder);


            String LastCachedFileRef = Path.Combine(FileDir, "CachedDate.txt");
            String LastCachedFile    = Path.Combine(FileDir, "CachedWeather.json");

            String TheTime = DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss");
            String json    = JsonConvert.SerializeObject(ThisWeather);

            lock (_lockObject) {
                if (!Directory.Exists(FileDir))
                {
                    Directory.CreateDirectory(FileDir);
                }
                if (File.Exists(LastCachedFileRef))
                {
                    File.Delete(LastCachedFileRef);
                }
                if (File.Exists(LastCachedFile))
                {
                    File.Delete(LastCachedFile);
                }
                System.IO.File.WriteAllText(LastCachedFileRef, TheTime);
                //Save Json
                System.IO.File.WriteAllText(LastCachedFile, json);
            }
        }
Пример #3
0
        private WeatherForcast GetByIP_OpenWeatherMap(String IPAddress)
        {
            WeatherForcast ThisWeather = new WeatherForcast();

            try {
                CityInfo ThisCity = GetCityByIP(IPAddress);

                DateTime LastCashedOn  = getLastProcessedDateTime(ThisCity.Country, ThisCity.City);
                DateTime MaxCashedTime = LastCashedOn.AddMinutes(30);
                if (MaxCashedTime > DateTime.Now)
                {
                    return(CashedWeather(ThisCity.Country, ThisCity.City));
                }

                //Get todays weather and forecast
                String URL = APIUrl + "forecast/daily?APIKey=" + APIKey + "&cnt=5&units=metric&q=" + ThisCity.City + "," + ThisCity.Country;
                ThisWeather.Country = ThisCity.Country;
                ThisWeather.City    = ThisCity.City;
                String JsonData = getWeatherJson(URL);
                if (String.IsNullOrEmpty(JsonData))
                {
                    return(CashedWeather(ThisCity.City, ThisCity.Country));
                }
                else
                {
                    dynamic WeatherJson = JObject.Parse(JsonData);
                    SetWeatherInfo(ThisWeather, WeatherJson);
                    //SetWeatherStation(ThisWeather.Today, ThisCity.Lat, ThisCity.Lng);
                }
                SaveWeatherCashe(ThisWeather);
            } catch {
                //ThisWeather.Today.ConditionText = "Error";
            }
            return(ThisWeather);
        }
Пример #4
0
        private WeatherForcast GetByLocation_OpenWeatherMap(Double Lat, Double Lng)
        {
            WeatherForcast ThisWeather = new WeatherForcast();

            try {
                DateTime LastCashedOn  = getLastProcessedDateTime(Lat, Lng);
                DateTime MaxCashedTime = LastCashedOn.AddMinutes(30);

                if (MaxCashedTime > DateTime.Now)
                {
                    return(CashedWeather(Lat, Lng));
                }

                String URL      = APIUrl + "forecast/daily?APIKey=" + APIKey + "&units=metric&cnt=5&lat=" + Lat + "&lon=" + Lng;
                String JsonData = getWeatherJson(URL);
                if (String.IsNullOrEmpty(JsonData))
                {
                    return(CashedWeather(Lat, Lng));
                }
                else
                {
                    dynamic WeatherJson = JObject.Parse(JsonData);
                    SetWeatherInfo(ThisWeather, WeatherJson);
                    ThisWeather.City    = WeatherJson.city.name;
                    ThisWeather.Country = WeatherJson.city.country;
                    SetWeatherStation(ThisWeather.Today, Lat, Lng);
                    String LatLngFolder = String.Format("#{0}#{1}", Convert.ToInt32(Lat), Convert.ToInt32(Lng));
                    SaveWeatherCashe(ThisWeather, LatLngFolder);
                }
            } catch  {
                ThisWeather.Today.ConditionText = "Error";
            }
            return(ThisWeather);
        }
Пример #5
0
        //[ChildActionOnly]
        //[OutputCache(Duration = 3600, VaryByCustom = "User")]
        public ActionResult LocalWeather()
        {
            var API = new Exponent.WeatherAPI();

            Exponent.WeatherForcast TodaysWeather = API.GetByIP(Request.UserHostAddress);
            //Exponent.WeatherForcast TodaysWeather = API.GetByIP("144.48.250.74");
            return(View(TodaysWeather));
        }
Пример #6
0
        private WeatherForcast CashedWeather_Core(String FolderName)
        {
            WeatherForcast tmp            = new WeatherForcast();
            String         LastCachedFile = Path.Combine(ApplicationPah, FolderName, "CachedWeather.json");

            if (File.Exists(LastCachedFile))
            {
                String JSonText = File.ReadAllText(LastCachedFile);
                try {
                    tmp = JsonConvert.DeserializeObject <WeatherForcast>(JSonText);
                } catch {
                    //nothing
                }
            }
            return(tmp);
        }
Пример #7
0
        public WeatherForcast GetByLocation(Double Lat, Double Lng)
        {
            WeatherForcast ThisWeather;

            switch (APIServer)
            {
            case "APIxu":
                ThisWeather = GetBy_APIxu($"{Lat},{Lng}");
                break;

            case "OpenWeatherMap":
                ThisWeather = GetByLocation_OpenWeatherMap(Lat, Lng);
                break;

            default:
                ThisWeather = new WeatherForcast();
                break;
            }
            return(ThisWeather);
        }
Пример #8
0
        public WeatherForcast GetByIP(String IPAddress)
        {
            WeatherForcast ThisWeather;

            IPAddress = Exponent.APIxu.IP.Fix(IPAddress);
            switch (APIServer)
            {
            case "APIxu":
                CityInfo ThisCity = GetCityByIP(IPAddress);
                ThisWeather = GetBy_APIxu(ThisCity.City);
                break;

            case "OpenWeatherMap":
                ThisWeather = GetByIP_OpenWeatherMap(IPAddress);
                break;

            default:
                ThisWeather = new WeatherForcast();
                break;
            }
            return(ThisWeather);
        }
Пример #9
0
        private void SetWeatherInfo(WeatherForcast ThisWeather, dynamic WeatherJson)
        {
            var      WeatherInfo = WeatherJson.list;
            DateTime LastForcast = DateTime.Now;

            for (var i = 0; i < WeatherInfo.Count; i++)
            {
                WeatherCondition ThisCondition = new WeatherCondition();
                try {
                    // Unix timestamp is seconds past epoch
                    Double          UnixTimeStamp = WeatherInfo[i].dt;
                    System.DateTime dtDateTime    = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                    ThisCondition.ConditionDate = dtDateTime.AddSeconds(UnixTimeStamp).ToLocalTime();
                    ThisCondition.ConditionText = WeatherInfo[i].weather[0].main;
                    ThisCondition.ConditionCode = WeatherInfo[i].weather[0].icon;
                    ThisCondition.Humidity      = WeatherInfo[i].humidity;
                    ThisCondition.Visibility    = 0;
                    ThisCondition.Pressure      = WeatherInfo[i].pressure;
                    ThisCondition.Temperature   = WeatherInfo[i].temp.day;
                    ThisCondition.High          = WeatherInfo[i].temp.max;
                    ThisCondition.Low           = WeatherInfo[i].temp.min;
                    ThisCondition.WindDirection = WeatherInfo[i].deg;
                    ThisCondition.WindSpeed     = WeatherInfo[i].speed * 3.6;

                    if (i == 0)
                    {
                        ThisWeather.Today = ThisCondition;
                    }
                } catch (Exception e) {
                    ThisCondition.ConditionText = e.Message;
                }
                ThisWeather.Forecast.Add(ThisCondition);
                if (i >= 5)
                {
                    return;
                }
            }
        }