Exemple #1
0
        protected async Task SetAndUpdatePreferredLocation()
        {
            CurrentLocation.Copy(City.NoData);

            CitySetupInfo city = _settings.Settings.LocationsList?.Find(loc => loc.Id == _settings.Settings.LocationCode);

            if (city == null)
            {
                return;
            }

            bool result      = false;
            City newLocation = new City(city);

            try
            {
                result = await ServiceRegistration.Get <IWeatherCatcher>().GetLocationData(newLocation).ConfigureAwait(false);
            }
            catch (Exception) { }

            CurrentLocation.Copy(newLocation);
            ServiceRegistration.Get <ILogger>().Info(result
                                                ? "CurrentWeatherModel: Loaded weather data for {0}, {1}"
                                                : "CurrentWeatherModel: Failed to load weather data for {0}, {1}",
                                                     CurrentLocation.Name, CurrentLocation.Id);
        }
        protected void SetAndUpdatePreferredLocation()
        {
            WeatherSettings settings = ServiceRegistration.Get <ISettingsManager>().Load <WeatherSettings>();

            if (settings == null || settings.LocationsList == null)
            {
                return;
            }

            CitySetupInfo city = settings.LocationsList.Find(loc => loc.Id == settings.LocationCode);

            if (city == null)
            {
                return;
            }
            bool result = false;

            try
            {
                City newLocation = new City(city);
                if (ServiceRegistration.Get <IWeatherCatcher>().GetLocationData(newLocation))
                {
                    CurrentLocation.Copy(newLocation);
                    result = true;
                }
            }
            catch (Exception)
            { }

            ServiceRegistration.Get <ILogger>().Info(result ?
                                                     "CurrentWeatherModel: Loaded weather data for {0}, {1}" : "WeatherModel: Failed to load weather data for {0}, {1}",
                                                     CurrentLocation.Name, CurrentLocation.Id);
        }
        private static void AddListItem(ItemsList list, CitySetupInfo city)
        {
            if (city == null)
            {
                return;
            }
            ListItem item = new ListItem();

            item.SetLabel("Name", city.Name);
            item.SetLabel("Id", city.Id);
            item.SetLabel("Detail", city.Detail);
            list.Add(item);
        }
        /// <summary>
        /// this will add a location
        /// to the _locationsExposed and _locations list
        /// </summary>
        /// <param name="item"></param>
        public void AddLocation(ListItem item)
        {
            // Don't add it if it's already in there
            if (_locationsExposed.Any(i => i["Id"].Equals(item["Id"])))
            {
                return;
            }

            _locationsExposed.Add(item);
            // Create a CitySetupObject and add it to the loctions list
            CitySetupInfo c = new CitySetupInfo(item["Name"], item["Id"])
            {
                Detail = item["Detail"]
            };

            _locations.Add(c);
            _locationsExposed.FireChange();
        }
Exemple #5
0
        /// <summary>
        /// Adds a single city to locations list.
        /// </summary>
        /// <param name="loc">city setup info</param>
        private City AddCityToLocations(CitySetupInfo loc)
        {
            if (loc == null)
            {
                return(null);
            }

            City city = new City(loc);

            _locations.Add(city);

            ListItem item = new ListItem();

            item.SetLabel("Name", loc.Name);
            item.SetLabel("Id", loc.Id);
            item.AdditionalProperties[KEY_CITY] = city;
            _locationsList.Add(item);
            return(city);
        }
        public async Task <List <CitySetupInfo> > FindLocationsByName(string name)
        {
            await new SynchronizationContextRemover();

            var         client      = new OpenWeatherMapClient(GetKey());
            var         parts       = name.Split(',');
            bool        isCoord     = false;
            Coordinates coordinates = new Coordinates();
            double      lat;
            double      lon;

            if (parts.Length == 2 &&
                double.TryParse(parts[0], NumberStyles.Any, CultureInfo.InvariantCulture, out lat) &&
                double.TryParse(parts[1], NumberStyles.Any, CultureInfo.InvariantCulture, out lon))
            {
                coordinates.Latitude  = lat;
                coordinates.Longitude = lon;
                isCoord = true;
            }

            var location = isCoord ?
                           await client.Search.GetByCoordinates(coordinates, _metricSystem, _language).ConfigureAwait(false) :
                           await client.Search.GetByName(name, _metricSystem, _language).ConfigureAwait(false);

            var  cities    = new List <CitySetupInfo>();
            bool useCoords = location.Count > 1;

            foreach (var city in location.List)
            {
                var setup = new CitySetupInfo(city.City.Name, city.City.Id.ToString(), SERVICE_NAME);
                if (useCoords)
                {
                    setup.Detail = string.Format("Lat. {0:F2} Lon. {1:F2}", city.City.Coordinates.Latitude, city.City.Coordinates.Longitude);
                }
                setup.Grabber = GetServiceName();
                cities.Add(setup);
            }
            return(cities);
        }
Exemple #7
0
    /// <summary>
    /// Adds a single city to locations list.
    /// </summary>
    /// <param name="loc">city setup info</param>
    private City AddCityToLocations(CitySetupInfo loc)
    {
      if (loc == null)
        return null;

      City city = new City(loc);
      _locations.Add(city);

      ListItem item = new ListItem();
      item.SetLabel("Name", loc.Name);
      item.SetLabel("Id", loc.Id);
      item.AdditionalProperties[KEY_CITY] = city;
      _locationsList.Add(item);
      return city;
    }
    /// <summary>
    /// Returns a new List of City objects if the searched
    /// location was found... the unique id (City.Id) and the
    /// location name will be set for each City...
    /// </summary>
    /// <param name="locationName">Name of the location to search for</param>
    /// <returns>New City List</returns>
    public List<CitySetupInfo> FindLocationsByName(string locationName)
    {
      // create list that will hold all found cities
      List<CitySetupInfo> locations = new List<CitySetupInfo>();

      Dictionary<string, string> args = new Dictionary<string, string>();
      args["query"] = locationName;
      args["num_of_results"] = "5";

      string url = BuildRequest("search.ashx", args);
      XPathDocument doc = WorldWeatherOnlineHelper.GetOnlineContent(url);
      XPathNavigator navigator = doc.CreateNavigator();
      XPathNodeIterator nodes = navigator.Select("/search_api/result");

      while (nodes.MoveNext())
      {
        List<string> details = new List<string>();
        CitySetupInfo city = new CitySetupInfo();
        XPathNavigator nodesNavigator = nodes.Current;
        if (nodesNavigator == null)
          continue;

        XPathNavigator areaNode = nodesNavigator.SelectSingleNode("areaName");
        if (areaNode != null)
          city.Name = areaNode.Value;

        XPathNavigator countryNode = nodesNavigator.SelectSingleNode("country");
        if (countryNode != null)
          city.Name += " (" + countryNode.Value + ")";

        // Build a unique key based on latitude & longitude
        XPathNavigator latNode = nodesNavigator.SelectSingleNode("latitude");
        XPathNavigator lonNode = nodesNavigator.SelectSingleNode("longitude");
        if (latNode != null && lonNode != null)
        {
          city.Id = latNode.Value + "," + lonNode.Value;
          details.Add(string.Format("{0:00.00}  {1:00.00}", latNode.ValueAsDouble, lonNode.ValueAsDouble));
        }

        // Get population info
        XPathNavigator populationNode = nodesNavigator.SelectSingleNode("population");
        if (populationNode != null && populationNode.ValueAsDouble > 0)
          details.Add(string.Format("Pop.: {0}", populationNode.Value));

        if (details.Count > 0)
          city.Detail = string.Format("({0})", string.Join(" / ", details.ToArray()));

        city.Grabber = GetServiceName();
        locations.Add(city);
      }
      return locations;
    }
Exemple #9
0
        /// <summary>
        /// Returns a new List of City objects if the searched
        /// location was found... the unique id (City.Id) and the
        /// location name will be set for each City...
        /// </summary>
        /// <param name="locationName">Name of the location to search for</param>
        /// <returns>New City List</returns>
        public List <CitySetupInfo> FindLocationsByName(string locationName)
        {
            // create list that will hold all found cities
            List <CitySetupInfo> locations = new List <CitySetupInfo>();

            Dictionary <string, string> args = new Dictionary <string, string>();

            args["query"]          = locationName;
            args["num_of_results"] = "5";

            string            url       = BuildRequest("search.ashx", args);
            XPathDocument     doc       = WorldWeatherOnlineHelper.GetOnlineContent(url);
            XPathNavigator    navigator = doc.CreateNavigator();
            XPathNodeIterator nodes     = navigator.Select("/search_api/result");

            while (nodes.MoveNext())
            {
                List <string>  details        = new List <string>();
                CitySetupInfo  city           = new CitySetupInfo();
                XPathNavigator nodesNavigator = nodes.Current;
                if (nodesNavigator == null)
                {
                    continue;
                }

                XPathNavigator areaNode = nodesNavigator.SelectSingleNode("areaName");
                if (areaNode != null)
                {
                    city.Name = areaNode.Value;
                }

                XPathNavigator countryNode = nodesNavigator.SelectSingleNode("country");
                if (countryNode != null)
                {
                    city.Name += " (" + countryNode.Value + ")";
                }

                // Build a unique key based on latitude & longitude
                XPathNavigator latNode = nodesNavigator.SelectSingleNode("latitude");
                XPathNavigator lonNode = nodesNavigator.SelectSingleNode("longitude");
                if (latNode != null && lonNode != null)
                {
                    city.Id = latNode.Value + "," + lonNode.Value;
                    details.Add(string.Format("{0:00.00}  {1:00.00}", latNode.ValueAsDouble, lonNode.ValueAsDouble));
                }

                // Get population info
                XPathNavigator populationNode = nodesNavigator.SelectSingleNode("population");
                if (populationNode != null && populationNode.ValueAsDouble > 0)
                {
                    details.Add(string.Format("Pop.: {0}", populationNode.Value));
                }

                if (details.Count > 0)
                {
                    city.Detail = string.Format("({0})", string.Join(" / ", details.ToArray()));
                }

                city.Grabber = GetServiceName();
                locations.Add(city);
            }
            return(locations);
        }
 private static void AddListItem(ItemsList list, CitySetupInfo city)
 {
   if (city == null)
     return;
   ListItem item = new ListItem();
   item.SetLabel("Name", city.Name);
   item.SetLabel("Id", city.Id);
   item.SetLabel("Detail", city.Detail);
   list.Add(item);
 }
    /// <summary>
    /// this will add a location
    /// to the _locationsExposed and _locations list
    /// </summary>
    /// <param name="item"></param>
    public void AddLocation(ListItem item)
    {
      // Don't add it if it's already in there
      if (_locationsExposed.Any(i => i["Id"].Equals(item["Id"])))
        return;

      _locationsExposed.Add(item);
      // Create a CitySetupObject and add it to the loctions list
      CitySetupInfo c = new CitySetupInfo(item["Name"], item["Id"]) { Detail = item["Detail"] };
      _locations.Add(c);
      _locationsExposed.FireChange();
    }