public GetWeatherRespose GetWeather(GetWeatherRequest req) { var resp = new GetWeatherRespose(); var r = new Random(); int celsius = r.Next(-20, 50); if (req.TemperatureType == TemperatureType.Celsius) { resp.Temperature = celsius; } else { resp.Temperature = (212 - 32) / 100 * celsius + 32; } if (req.City == "Redmond") { resp.Condition = TemperatureCondition.Rainy; } else { resp.Condition = (TemperatureCondition)r.Next(0, 3); } return(resp); }
/// <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 UpdateSecondaryTileTask() { userService = UserService.GetInstance(); weatherService = WeatherService.GetInstance(); userRespose = new GetUserRespose(); userCityRespose = new GetUserCityRespose(); weatherRespose = new GetWeatherRespose(); weatherTypeRespose = new GetWeatherTypeRespose(); }
/// <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> /// <param name="cityId"></param> /// <returns></returns> public async Task <GetWeatherRespose> GetWeatherByClientAsync(string cityId) { string fileName = null; string filePath = null; GetWeatherRespose respose = null; for (int i = 0; i < 3; i++) { fileName = cityId + "_" + DateTime.Now.AddDays(-i).ToString("yyyyMMdd") + ".json"; filePath = "Temp\\" + fileName; bool x = await FileHelper.IsExistFileAsync(filePath).ConfigureAwait(false); if (x) { respose = await Weather.Utils.JsonSerializeHelper.JsonDeSerializeForFileAsync <GetWeatherRespose>(filePath).ConfigureAwait(false); break; } } return(respose); }
public MainPage() { this.navigationHelper = new NavigationHelper(this); this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required; userService = UserService.GetInstance(); weatherService = WeatherService.GetInstance(); settingService = SettingService.GetInstance(); colorService = ColorService.GetInstance(); userRespose = new GetUserRespose(); userCityRespose = new GetUserCityRespose(); weatherRespose = new GetWeatherRespose(); weatherTypeRespose = new GetWeatherTypeRespose(); settingAutoUpdateTimeRepose = new GetSettingAutoUpdateTimeRepose(); colorResponse = new GetColorRespose(); homePageModel = new HomePageModel(); this.InitializeComponent(); Loaded += MainPage_Loaded; }
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="weatherRespose"></param> /// <param name="getWeatherTypeRespose"></param> /// <param name="getUserRespose"></param> private void UpdateTile(GetWeatherRespose weatherRespose) { string tileXmlString = @"<tile>" + "<visual version='2'>" + "<binding template='TileWide310x150PeekImage02' fallback='TileWidePeekImage02'>" + "<image id='1' src='ms-appx:///" + weatherTypeRespose.WeatherTypes.Find(x => x.Code == weatherRespose.result.FirstOrDefault().now.cond.code).TileWidePic + "'/>" + "<text id='1'>" + weatherRespose.result.FirstOrDefault().basic.city + "</text>" + "<text id='2'>" + weatherRespose.result.FirstOrDefault().daily_forecast.FirstOrDefault().tmp.min + "°~" + weatherRespose.result.FirstOrDefault().daily_forecast.FirstOrDefault().tmp.max + "° " + weatherRespose.result.FirstOrDefault().now.cond.txt + "</text>" + "<text id='3'>" + weatherRespose.result.FirstOrDefault().now.wind.dir + " " + weatherRespose.result.FirstOrDefault().now.wind.sc + " 级</text>" + "<text id='4'>湿度: " + weatherRespose.result.FirstOrDefault().now.hum + "%</text>" + "<text id='5'>能见度: " + weatherRespose.result.FirstOrDefault().now.vis + "km</text>" + "</binding>" + "<binding template='TileSquare150x150PeekImageAndText01' fallback='TileSquarePeekImageAndText01'>" + "<image id='1' src='ms-appx:///" + weatherTypeRespose.WeatherTypes.Find(x => x.Code == weatherRespose.result.FirstOrDefault().now.cond.code).TileSquarePic + "'/>" + "<text id='1'>" + weatherRespose.result.FirstOrDefault().basic.city + "</text>" + "<text id='2'>" + weatherRespose.result.FirstOrDefault().now.tmp + "°</text>" + "<text id='3'>" + weatherRespose.result.FirstOrDefault().now.cond.txt + "</text>" + "<text id='4'>" + weatherRespose.result.FirstOrDefault().now.wind.dir + " " + weatherRespose.result.FirstOrDefault().now.wind.sc + "级</text>" + "</binding>" + "</visual>" + "</tile>"; TileHelper.UpdateTileNotificationsByXml(tileXmlString); }
/// <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("天气数据获取失败"); } }