Exemple #1
0
 public ForecastModel(DateTime date, double tempeture, WeatherState state, WeatherUnit unit)
 {
     this.Date         = date;
     this.Tempeture    = tempeture;
     this.WeatherState = state;
     this.Unit         = unit;
 }
Exemple #2
0
 public WeatherDwarfModel(
     double currentTempeture,
     WeatherState currentState,
     DateTime currentDate,
     List <ForecastModel> forecasts,
     string locationName,
     WeatherUnit unit)
 {
     this.CurrentTempeture = currentTempeture;
     this.CurrentState     = currentState;
     this.CurrentDate      = currentDate;
     this.Forecasts        = forecasts;
     this.LocationName     = locationName;
     this.Unit             = unit;
 }
Exemple #3
0
        public async Task <Forecast> GetForecastAsync(string city, WeatherUnit unit = WeatherUnit.Metric)
        {
            if (_ratelimiter.IsRatelimited(nameof(WeatherApiService)))
            {
                return(null);
            }

            try
            {
                return(await _api.GetForecastAsync(city, unit.ToString().ToLower(), _apiKey));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
            }
            return(null);
        }
Exemple #4
0
        public WeatherModel()
        {
            if (Configuration.Instance.IsValid() == true)
            {
                _weatherApiKey = Configuration.Instance.ApiKey;
                _weatherLocationCode = Configuration.Instance.LocationCode;
                _weatherUnit = Configuration.Instance.WeatherUnit.GetValueOrDefault(WeatherUnit.Imperial);
                _weatherClockTimeFormat = Configuration.Instance.ClockTimeFormat.GetValueOrDefault(ClockTimeFormat.Hours12);
                _weatherRefreshRateInMinutes = Configuration.Instance.RefreshRateInMinutes.GetValueOrDefault(10);
                _weatherLanguage = Configuration.Instance.Language.GetValueOrDefault(Language.EN);
            }

            // set the correct language for the UI thread
            System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(Enum.GetName(typeof(Language), Configuration.Instance.Language));

            _weatherFileName = String.Format(Configuration.Instance.WeatherFileNamePattern, _weatherLocationCode, _weatherLanguage);
            _weatherFileName = _weatherFileName.Replace(":", "."); // small cleanup is needed for zmw location codes
            _weatherFileLocation = Path.Combine(Configuration.Instance.ConfigFileFolder, _weatherFileName);
            _weatherApiAddress = String.Format(Configuration.Instance.ApiUrlPattern, _weatherApiKey, _weatherLanguage, _weatherLocationCode);

            _xmlWeatherData = new XmlDocument();
            _dailyForecast = new ArrayListDataSet();
            _hourlyForecast = new ArrayListDataSet();

            _uiRefreshNeeded = true;
            _isLoaded = false;

            // refresh timer
            if (_weatherRefreshTimer == null)
            {
                _weatherRefreshTimer = new Timer(this);
                _weatherRefreshTimer.Interval = 10000; // 10 seconds interval
                _weatherRefreshTimer.Tick += delegate { LoadWeatherData(); };
            }
        }
        /// <summary>
        ///     为了给雨雪加上强度描述,请务必在GetPrecipitation()方法后调用
        /// </summary>
        /// <param name="unit"></param>
        /// <param name="weather"></param>
        private static void SetSkycon(WeatherUnit unit, string weather)
        {
            //CLEAR_DAY:晴天,CLEAR_NIGHT:晴夜,PARTLY_CLOUDY_DAY:多云,PARTLY_CLOUDY_NIGHT:多云
            //CLOUDY:阴,RAIN:雨,SNOW:雪,WIND:风,FOG:雾,HAZE:霾
            string skycon, icon;

            switch (weather)
            {
            case "CLEAR_DAY":
                skycon = "晴";
                icon   = "Assets/WeatherIcon/100.png";
                break;

            case "CLEAR_NIGHT":
                skycon = "晴";
                icon   = "Assets/WeatherIcon/100.png";
                break;

            case "PARTLY_CLOUDY_DAY":
                skycon = "多云";
                icon   = "Assets/WeatherIcon/103.png";
                break;

            case "PARTLY_CLOUDY_NIGHT":
                skycon = "多云";
                icon   = "Assets/WeatherIcon/103.png";
                break;

            case "CLOUDY":
                skycon = "阴";
                icon   = "Assets/WeatherIcon/101.png";
                break;

            case "RAIN":
                skycon = "雨";
                icon   = "Assets/WeatherIcon/305.png";
                break;

            case "SNOW":
                skycon = "雪";
                icon   = "Assets/WeatherIcon/400.png";
                break;

            case "WIND":
                skycon = "风";
                icon   = "Assets/WeatherIcon/205.png";
                break;

            case "FOG":
                skycon = "雾";
                icon   = "Assets/WeatherIcon/501.png";
                break;

            case "HAZE":
                skycon = "霾";
                icon   = "Assets/WeatherIcon/502.png";
                break;

            default:
                skycon = weather;
                icon   = "Assets/WeatherIcon/999.png";
                break;
            }
            if (skycon == "雪" || skycon == "雨" && unit.Precipitation[0] != '无')
            {
                skycon = unit.Precipitation[0] + skycon;
            }
            unit.Precipitation = unit.Precipitation.Remove(0, 1);
            unit.Skycon        = skycon;
            unit.Icon          = icon;
        }