示例#1
0
        public WeatherData GetWeather(string city, string country)
        {
            GlobalWeatherSoapClient soapClient = new GlobalWeatherSoapClient();
            string result = soapClient.GetWeather(city, country);

            XmlSerializer serializer = new XmlSerializer(typeof(CurrentWeather));
            MemoryStream  reader     = new MemoryStream(Encoding.Unicode.GetBytes(result));

            CurrentWeather weatherResult = (CurrentWeather)serializer.Deserialize(reader);

            var data = new WeatherData()
            {
                LocationName = weatherResult.Location,
                Conditions   = weatherResult.SkyConditions
            };

            string timeStamp = weatherResult.Time.Substring(weatherResult.Time.IndexOf("/") + 1)
                               .Replace('.', '-')
                               .Replace("UTC", "")
                               .TrimEnd();
            string formattedTime = timeStamp.Substring(0, timeStamp.Length - 2) + ":" + timeStamp.Substring(timeStamp.Length - 2);

            data.CurrentTime = Convert.ToDateTime(formattedTime);

            int    length  = weatherResult.Temperature.Length;
            string celsius = weatherResult.Temperature.Substring(weatherResult.Temperature.IndexOf("(") + 1, length - (weatherResult.Temperature.IndexOf(")") - 1));

            data.Temp.AddCelsius(Convert.ToDouble(celsius));

            return(data);
        }
示例#2
0
        public bool SaveCurrentWeatherValues(CurrentWeather currentWeather)
        {
            try
            {
                switch (APIConfiguration.DataSaveMethod)
                {
                case DataSaveMethod.FileSave:
                    FireEvent("Saving to the file system for zip code " + currentWeather.ZipCode);
                    // Concerned about using File append because of file locks
                    _fileSystem.File.AppendAllText(APIConfiguration.SaveFileLocation, currentWeather.CreateLogLine() + Environment.NewLine);
                    break;

                case DataSaveMethod.DatabaseSave:
                    FireEvent("Saving to the database for zip code " + currentWeather.ZipCode);
                    //TODO: add database code or wrapper
                    break;

                default:
                    FireEvent("Unknown save method for zip code " + currentWeather.ZipCode);
                    break;
                }
                return(true);
            }
            catch (Exception exp)
            {
                FireEvent($"Error trying to save the values for {currentWeather.ZipCode} {Environment.NewLine}Error: {exp.Message}");
                return(false);
            }
        }
示例#3
0
        /// <summary>
        /// Used to make a single call to the external API with one zip code and return a CurrentWeather object
        /// </summary>
        /// <param name="zipCode">5 digit code used to look up a city</param>
        /// <returns>CurrentWeather object with the values returned from the Weather service</returns>
        public async Task <CurrentWeather> GetWeatherObjectByZipAsync(string zipCode, string uri)
        {
            try
            {
                var currentWeather = new CurrentWeather()
                {
                    ZipCode = zipCode
                };

                var result = await GetWeatherByZipAsync(zipCode, uri);

                currentWeather.ParseJson(result);

                return(currentWeather);
            }
            catch (Exception)
            {
                throw;
            }
        }