/// <summary> /// 获取天气字符串 /// </summary> /// <param name="cityName"></param> /// <returns></returns> private async Task <string> GetWeatherString(string cityId) { IGetWeatherRequest request = GetWeatherRequestFactory.CreateGetWeatherRequest(GetWeatherMode.CityId, cityId); string requestUrl = request.GetRequestUrl(); string resposeString = await Weather.Utils.HttpHelper.GetUrlResposeAsnyc(requestUrl); return(resposeString.Replace("HeWeather data service 3.0", "result")); }
/// <summary> /// 获取天气 /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <GetWeatherRespose> GetWeatherAsync(IGetWeatherRequest request) { GetWeatherRespose respose = new GetWeatherRespose(); string requestUrl = request.GetRequestUrl(); string resposeString = await Weather.Utils.HttpHelper.GetUrlResposeAsnyc(requestUrl).ConfigureAwait(false); respose = Weather.Utils.JsonSerializeHelper.JsonDeserialize <GetWeatherRespose>(resposeString.Replace("HeWeather data service 3.0", "result")); return(respose); }
public static IGetWeatherRequest CreateGetWeatherRequest(GetWeatherMode mode,string args) { switch (mode) { case GetWeatherMode.City: getWeatherRequest = new GetWeatherByCityNameOrIdRequest(args); break; case GetWeatherMode.Gps: getWeatherRequest = new GetWeatherByGpsRequest(args.Split(',')[0],args.Split(',')[1]); break; case GetWeatherMode.Ip: getWeatherRequest = new GetWeatherByIpRequest(args); break; default: getWeatherRequest = new GetWeatherByCityNameOrIdRequest(args); break; } return getWeatherRequest; }
public static IGetWeatherRequest CreateGetWeatherRequest(GetWeatherMode mode, string args) { switch (mode) { case GetWeatherMode.City: getWeatherRequest = new GetWeatherByCityNameRequest(args); break; case GetWeatherMode.CityId: getWeatherRequest = new GetWeatherByCityIdRequest(args); break; case GetWeatherMode.CityIp: getWeatherRequest = new GetWeatherByIpRequest(args); break; default: getWeatherRequest = new GetWeatherByCityIdRequest(args); break; } return getWeatherRequest; }
public static IGetWeatherRequest CreateGetWeatherRequest(GetWeatherMode mode, string args) { switch (mode) { case GetWeatherMode.City: getWeatherRequest = new GetWeatherByCityNameRequest(args); break; case GetWeatherMode.CityId: getWeatherRequest = new GetWeatherByCityIdRequest(args); break; case GetWeatherMode.CityIp: getWeatherRequest = new GetWeatherByIpRequest(args); break; default: getWeatherRequest = new GetWeatherByCityIdRequest(args); break; } return(getWeatherRequest); }
private async Task <GetWeatherRespose> GetWeatherAsync(int isRefresh, Model.UserCity userCity, IGetWeatherRequest weatherRequest) { GetWeatherRespose weatherRespose = new GetWeatherRespose(); try { if (isRefresh == 1) { weatherRespose = await weatherService.GetWeatherAsync(weatherRequest); await weatherService.SaveWeather(weatherRespose, userCity.CityId.ToString()); } else { string filePath = StringHelper.GetTodayFilePath(userCity.CityId); if (!await FileHelper.IsExistFileAsync(filePath)) { //不存在当天的天气数据,就从网络获取数据 weatherRespose = await weatherService.GetWeatherAsync(weatherRequest); await DeleteFile(userCity.CityId); await weatherService.SaveWeather(weatherRespose, userCity.CityId.ToString()); } else { weatherRespose = await weatherService.GetWeatherByClientAsync(userCity.CityId.ToString()); } } return(weatherRespose); } catch (Exception) { return(weatherRespose); } }
/// <summary> /// 获取天气数据 /// </summary> /// <param name="cityId"></param> private async Task GetWeather(string cityId, int isRefresh) { Model.UserCity userCity = string.IsNullOrEmpty(cityId) ? userCityRespose.UserCities.FirstOrDefault(x => x.IsDefault == 1) : userCityRespose.UserCities.FirstOrDefault(x => x.CityId == cityId); IGetWeatherRequest weatherRequest = GetWeatherRequestFactory.CreateGetWeatherRequest(GetWeatherMode.CityId, userCity.CityId); //有网络 if (NetHelper.IsNetworkAvailable()) { if (userRespose.UserConfig.IsWifiUpdate == 0) { weatherRespose = await GetWeatherAsync(isRefresh, userCity, weatherRequest); } else { if (NetHelper.IsWifiConnection()) { weatherRespose = await GetWeatherAsync(isRefresh, userCity, weatherRequest); } else { weatherRespose.result = null; NotifyUser("Wifi未启动"); } } } else { weatherRespose = await weatherService.GetWeatherByClientAsync(userCity.CityId.ToString()); NotifyUser("请开启网络,以更新最新天气数据"); } if (weatherRespose != null) { var respose = weatherRespose.result.FirstOrDefault(); var aqi = respose.aqi; var now = respose.now; var basic = respose.basic; var daily_forecast = respose.daily_forecast; var hourly_forecast = respose.hourly_forecast; if (aqi != null) { homePageModel.Aqi = "空气质量: " + aqi.city.qlty; } homePageModel.City = basic.city; homePageModel.Daytmp = daily_forecast.FirstOrDefault().tmp.min + "° / " + daily_forecast.FirstOrDefault().tmp.max + "°"; homePageModel.Hum = "湿度: " + now.hum + " %"; homePageModel.Pres = now.pres + " hPa"; homePageModel.Tmp = now.tmp + "°"; homePageModel.Txt = now.cond.txt; homePageModel.Update = basic.update.loc + " 发布"; homePageModel.Vis = "能见度: " + now.vis + " km"; homePageModel.Wind = now.wind.dir + " " + now.wind.sc + " 级"; homePageModel.WeatherType = weatherTypeRespose.WeatherTypes.FirstOrDefault(x => x.Code == now.cond.code); List <ViewModel.DailyItem> dailyList = new List <ViewModel.DailyItem>(); foreach (var item in daily_forecast) { DailyItem daily = new DailyItem() { Date = GetDateStr(item.date), Image = weatherTypeRespose.WeatherTypes.FirstOrDefault(x => x.Code == item.cond.code_d).TileWidePic, Tmp = item.tmp.min + "° / " + item.tmp.max + "°", Txt = item.cond.txt_d }; dailyList.Add(daily); } homePageModel.DailyList = dailyList; List <ViewModel.HourlyItem> hourlyList = new List <ViewModel.HourlyItem>(); foreach (var item in hourly_forecast) { HourlyItem hourly = new HourlyItem() { Date = DateTime.Parse(item.date).ToString("HH:mm"), Hum = item.hum + " %", Tmp = item.tmp + "°", Wind = item.wind.dir + " " + item.wind.sc }; hourlyList.Add(hourly); } homePageModel.HourlyList = hourlyList; LayoutRoot.DataContext = null; LayoutRoot.DataContext = homePageModel; } else { NotifyUser("天气数据获取失败"); } }
private async Task<GetWeatherRespose> GetWeatherAsync(int isRefresh, Model.UserCity userCity, IGetWeatherRequest weatherRequest) { GetWeatherRespose weatherRespose = new GetWeatherRespose(); try { if (isRefresh == 1) { weatherRespose = await weatherService.GetWeatherAsync(weatherRequest); await weatherService.SaveWeather(weatherRespose, userCity.CityId.ToString()); } else { string filePath = StringHelper.GetTodayFilePath(userCity.CityId); if (!await FileHelper.IsExistFileAsync(filePath)) { //不存在当天的天气数据,就从网络获取数据 weatherRespose = await weatherService.GetWeatherAsync(weatherRequest); await DeleteFile(userCity.CityId); await weatherService.SaveWeather(weatherRespose, userCity.CityId.ToString()); } else { weatherRespose = await weatherService.GetWeatherByClientAsync(userCity.CityId.ToString()); } } return weatherRespose; } catch (Exception) { return weatherRespose; } }