public static ViewModel.CommonCityView ConvertToCommonCityView(Model.UserCity city, Model.WeatherType type, Model.Weather weather) { ViewModel.CommonCityView view = new ViewModel.CommonCityView(); view.CityId = city.CityId; view.CityName = city.CityName; view.TodayPic = (weather == null || type == null) ? null : type.TodayPic; view.Temp = weather == null ? null : weather.today.temperature; return(view); }
/// <summary> /// 通过网络获取天气 /// </summary> /// <param name="userCity"></param> /// <returns></returns> private async Task SetWeatherByNetTask(Model.UserCity userCity) { string resposeString = await GetWeatherString(userCity.CityId); GetWeatherRespose getWeatherRespose = Weather.Utils.JsonSerializeHelper.JsonDeserialize <GetWeatherRespose>(resposeString); UpdateTile(getWeatherRespose); await CreateFile(userCity.CityId, resposeString); }
/// <summary> /// 更新单个城市 /// </summary> /// <returns></returns> private async Task UpdateCity(Model.UserCity userCity) { string resposeString = await GetRealResposeString(userCity.CityId); GetWeatherRespose respose = Weather.Utils.JsonSerializeHelper.JsonDeserialize <GetWeatherRespose>(resposeString); string tileId = userCity.CityId + "_Weather"; if (SecondaryTileHelper.IsExists(tileId)) { UpdateSecondaryTile(respose, tileId); } await CreateFile(userCity.CityId, resposeString); }
/// <summary> /// 通过本地文件获取天气 /// </summary> /// <param name="userCity"></param> /// <returns></returns> private async Task GetWeatherByClientTask(Model.UserCity userCity) { GetWeatherRespose respose = await weatherService.GetWeatherByClientAsync(userCity.CityId.ToString()); // 同一天 if (respose.result.FirstOrDefault().daily_forecast.FirstOrDefault().date == DateTime.Now.ToString("yyyy-MM-dd")) { UpdateTile(respose); } else { UpdateTileByClientForTomorrow(respose.result.FirstOrDefault().daily_forecast.FirstOrDefault(x => x.date == DateTime.Now.ToString("yyyy-MM-dd")), userCity.CityName); } }
/// <summary> /// 通过本地文件获取天气 /// </summary> /// <param name="userCity"></param> /// <returns></returns> private async Task UpdateCityByClientTask(Model.UserCity userCity) { GetWeatherRespose respose = await weatherService.GetWeatherByClientAsync(userCity.CityId.ToString()); string tileId = userCity.CityId + "_Weather"; if (respose.result.FirstOrDefault().daily_forecast.FirstOrDefault().date == DateTime.Now.ToString("yyyy-MM-dd")) { UpdateSecondaryTile(respose, tileId); } else { UpdateTileByClientForTomorrow(tileId, respose.result.FirstOrDefault().daily_forecast.FirstOrDefault(x => x.date == DateTime.Now.ToString("yyyy-MM-dd")), userCity.CityName); } }
/// <summary> /// 通过本地更新天气 /// </summary> /// <returns></returns> private async Task UpdateWeatherByClientTask() { //允许自动更新所用城市 if (userRespose.UserConfig.IsAutoUpdateForCities == 1) { await UpdateAllCityByClientTask(); } else//允许更新默认城市 { if (userRespose.UserConfig.IsAutoUpdateForCity == 1) { //获取默认城市 Model.UserCity userCity = (from u in userCityRespose.UserCities where u.IsDefault == 1 select u).FirstOrDefault(); await UpdateCityByClientTask(userCity); } } }
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="cityName"></param> /// <returns></returns> public async Task <bool> UpdateUserCity(string cityName) { bool isAdd = false; Model.UserCity userCity = new Model.UserCity() { CityId = (from c in page.Cities where c.DistrictZH == cityName select c.ID).FirstOrDefault(), AddTime = DateTime.Now, CityName = cityName.Trim(), IsDefault = 1 }; GetUserCityRespose respose = new GetUserCityRespose(); List <Model.UserCity> userCityList = new List <Model.UserCity>(); userCityList.Add(userCity); respose.UserCities = userCityList; await userService.SaveUserCity(respose); isAdd = true; return(isAdd); }
/// <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("天气数据获取失败"); } }
/// <summary> /// 更新 /// </summary> /// <param name="cityName"></param> /// <returns></returns> public async Task<bool> UpdateUserCity(string cityName) { bool isAdd = false; Model.UserCity userCity = new Model.UserCity() { CityId = (from c in page.Cities where c.DistrictZH == cityName select c.ID).FirstOrDefault(), AddTime = DateTime.Now, CityName = cityName.Trim(), IsDefault = 1 }; GetUserCityRespose respose = new GetUserCityRespose(); List<Model.UserCity> userCityList = new List<Model.UserCity>(); userCityList.Add(userCity); respose.UserCities = userCityList; await userService.SaveUserCity(respose); isAdd = true; return isAdd; }
public async Task<bool> UpdateUserCity(string cityName) { bool isAdd = false; Model.UserCity userCity = new Model.UserCity() { CityId = (from c in page.Cities where c.District == cityName select c.Id).FirstOrDefault(), AddTime = DateTime.Now, CityName = cityName.Trim(), IsDefault = isNotFirst == false ? 1 : 0 }; if (!isNotFirst) { GetUserCityRespose respose = new GetUserCityRespose(); List<Model.UserCity> userCityList = new List<Model.UserCity>(); userCityList.Add(userCity); respose.UserCities = userCityList; await userService.SaveUserCity(SortUserCity(respose)); isAdd = true; } else { if (resposeUserCity.UserCities.Count < 5) { var count = resposeUserCity.UserCities.Count(x => x.CityName.Contains(cityName.Trim())); if (count == 0) { resposeUserCity.UserCities.Add(userCity); await userService.SaveUserCity(SortUserCity(resposeUserCity)); isAdd = true; } else { NotifyUser("该城市已加入常用城市列表"); } } else { NotifyUser("因资源有限,每人最多拥有5座常用城市"); } } return isAdd; }