示例#1
0
        private void Process()
        {
            #region CMD_ARGS_PROCESSING

            if (_cmdline["help"] != null)
            {
                Console.WriteLine("This app can show current Weather or the Weather forecast for chosen city. \n" +
                                  "There are the following parameters available: \n" +
                                  "--help - shows this manual. \n" +
                                  "--location - sets the city. Example: --location London. Without this parameter the app will use default city if set. \n" +
                                  "-d - sets number of days ahead. Example: -d 1. -d value can be from 0 (today) \n" +
                                  " to 5 (five days ahead). By default -d value is 0. Negative numbers will be replaced by 0.\n" +
                                  "-f - sets chosen city as default. Example: --location London -f. \n" +
                                  "All the parameters are not mandatory.");
                return;
            }

            string locationId;

            if (_cmdline["location"] != null)
            {
                _location  = _cmdline["location"];
                locationId = GetLocationId(_location);
            }
            else
            {
                locationId = GetCurrentLocation();
            }

            if (locationId == null)
            {
                Console.WriteLine("Please choose your city using --location command; you can also set any city as default using command -f.");
                return;
            }

            if (_cmdline["f"] != null)
            {
                _citiesRepository.SetFavourite(Int32.Parse(locationId));
            }

            WeatherType type = WeatherType.Current;

            if (_cmdline["d"] != null)
            {
                bool isInt = Int32.TryParse(_cmdline["d"], out _daysAhead);
                if (_daysAhead < 0)
                {
                    Console.WriteLine("-d value cannot be less than 0");
                    return;
                }

                if (_daysAhead > 5)
                {
                    Console.WriteLine("-d value can be from 0 to 5");
                    return;
                }
                if (_daysAhead > 0)
                {
                    type = WeatherType.Forecast;
                }
            }
            #endregion

            // It's reasonable to get forecast for other days once a day, and use one cached response within one day.
            if (type == WeatherType.Forecast)
            {
                if (CheckCache(int.Parse(locationId), type))
                {
                    return;
                }
            }

            _model = new TimeLimitProxy(locationId, _daysAhead);

            var weather = _model.GetWeather();

            if (weather == null)
            {
                // Current Weather may change during the day, so we need cash only if we cannot get actual info.
                CheckCache(int.Parse(locationId), type);
                return;
            }

            if (type == WeatherType.Forecast)
            {
                var weatherForecast = weather as WeatherForecast;
                var dayRequired     = (from day in weatherForecast.List where day.Date.Date == (DateTime.Today.AddDays(_daysAhead)) select day).ToList();
                weatherForecast.List = dayRequired;
                weather = weatherForecast;
            }

            var message = _view.ShowWeather(weather, type);

            _cashRepository.Add(_cashRepository.Create(weather, message, type, _daysAhead));
        }