public ActionResult DoQuery(QueryViewModel model) { try { QueryWeatherRequest request = new QueryWeatherRequest(); //request.GPSLocationInfo = gpsLocationInfo; request.WCCode = model.WCCode; request.QueryDateTime = DateTime.Now; QueryWeatherResponse response = QueryServices.GetWeatherInfo(request); response.CurrentDay.QueryDate = request.QueryDateTime.ToString("yyyy/MM/dd"); response.CurrentDay.AreaCode = model.AreaCode; response.CurrentDay.AreaName = CityAndAreaConfig.Instance.getAreaNameByAreaCode(model.AreaCode); response.CurrentDay.WCCode = model.WCCode; response.CurrentDay.CountryName = CityAndAreaConfig.Instance.getWCCountryNameByWCCode(model.WCCode); ViewBag.CurrentDay = response.CurrentDay; ViewBag.OtherDays = response.OthreDays; } catch (System.Exception ex) { return(View("Error")); } return(View("Result")); //return RedirectToAction("Result"); }
public JsonResult QueryWeatherInfo(string weatherCountyCode, string Date) { QueryWeatherRequest request = new QueryWeatherRequest(); request.WCCode = weatherCountyCode; QueryWeatherResponse response = QueryServices.GetWeatherInfo(request); return(Json(new { WeatherInfo = response, IsSuccess = true, Msg = "" })); }
public ActionResult DoQuery(QueryViewModel model) { try { if (model == null) { throw new ArgumentNullException("model"); } //GPSLocation gpsLocationInfo = GPSLocationService.GetClientGPSLocationInfo(); QueryWeatherRequest request = new QueryWeatherRequest(); //request.GPSLocationInfo = gpsLocationInfo; request.AreaCode = model.AreaCode; request.WCCode = model.WCCode; request.QueryDateTime = DateTime.Now; QueryWeatherResponse response = QueryServices.GetWeatherInfo(request); response.CurrentDay.QueryDate = request.QueryDateTime.ToString("yyyy/MM/dd"); response.CurrentDay.AreaCode = model.AreaCode; response.CurrentDay.AreaName = CityAndAreaConfig.Instance.getAreaNameByAreaCode(model.AreaCode); response.CurrentDay.WCCode = model.WCCode; response.CurrentDay.CountryName = CityAndAreaConfig.Instance.getWCCountryNameByWCCode(model.WCCode); ViewBag.CurrentDay = response.CurrentDay; if (response == null || response.CurrentDay == null) { ViewData["NoData"] = "true"; return(View("Index")); } else { ViewData["NoData"] = "false"; return(View("Result")); } } catch (System.Net.WebException ex) { //ViewBag.ErrMsg = "網路斷線或遠端伺服器錯誤,請通知管理者"; ErrorPageModel errModel = new ErrorPageModel { ErrorTitle = "Network Error", ErrorMsg = "網路斷線或遠端伺服器錯誤,請通知管理者" }; return(View("Error", errModel)); } catch (System.Exception ex) { return(View("Error")); } //return RedirectToAction("Result"); }
public void ParserWeatherXMLInfo_OtherDays_Count_Pass_Test() { XmlDocument xmlConditions = new XmlDocument(); xmlConditions.Load("http://weather.msn.com/data.aspx?weadegreetype=C&wealocations=wc:TWXX0019"); QueryWeatherRequest request = new QueryWeatherRequest(); request.AreaCode = "10"; request.WCCode = "TWXX0001"; QueryWeatherResponse response = ParserService.ParserWeatherXMLInfo(xmlConditions, request); Assert.AreEqual(response.OthreDays.Count, 5); }
public static QueryWeatherResponse GetWeatherInfo(QueryWeatherRequest request) { if (request == null) { throw new ArgumentNullException("request"); } XmlDocument xmlConditions = new XmlDocument(); xmlConditions.Load(string.Format(MSN_WEATHER_API_URL, request.WCCode)); //Parser MSN-Weather-info XML QueryWeatherResponse response = ParserService.ParserWeatherXMLInfo(xmlConditions, request); response.CurrentDay.AreaCode = request.AreaCode; response.CurrentDay.WCCode = request.WCCode; response.CurrentDay.Shortday = response.OthreDays[0].Shortday; return(response); }
public static QueryWeatherResponse ParserWeatherXMLInfo(XmlDocument XmlDoc, QueryWeatherRequest request) { if (XmlDoc == null) { throw new ArgumentNullException("XmlDoc"); } if (request == null) { throw new ArgumentNullException("request"); } QueryWeatherResponse respone = new QueryWeatherResponse(); if (XmlDoc.SelectSingleNode(@"weatherdata/weather/current") != null) { respone.CurrentDay = new WeatherInfo(); respone.CurrentDay.WCCode = XmlDoc.SelectSingleNode(@"weatherdata/weather").Attributes["weatherlocationcode"].InnerText.Split(':')[1]; respone.CurrentDay.Temperature = XmlDoc.SelectSingleNode(@"weatherdata/weather/current").Attributes["temperature"].InnerText; respone.CurrentDay.Skycode = XmlDoc.SelectSingleNode(@"weatherdata/weather/current").Attributes["skycode"].InnerText; respone.CurrentDay.Skytext = XmlDoc.SelectSingleNode(@"weatherdata/weather/current").Attributes["skytext"].InnerText; respone.CurrentDay.Observationtime = XmlDoc.SelectSingleNode(@"weatherdata/weather/current").Attributes["observationtime"].InnerText; respone.CurrentDay.Observationpoint = XmlDoc.SelectSingleNode(@"weatherdata/weather/current").Attributes["observationpoint"].InnerText; respone.CurrentDay.Feelslike = XmlDoc.SelectSingleNode(@"weatherdata/weather/current").Attributes["feelslike"].InnerText; respone.CurrentDay.Humidity = XmlDoc.SelectSingleNode(@"weatherdata/weather/current").Attributes["humidity"].InnerText; respone.CurrentDay.Windspeed = XmlDoc.SelectSingleNode(@"weatherdata/weather/current").Attributes["windspeed"].InnerText; respone.CurrentDay.Winddisplay = XmlDoc.SelectSingleNode(@"weatherdata/weather/current").Attributes["winddisplay"].InnerText; respone.CurrentDay.WeatherStatus = ChangeSkyCodeToWeatherStatus(respone.CurrentDay.Skycode); respone.OthreDays = new List <ForeseeWeatherInfo>(); for (int i = 0; i < XmlDoc.SelectNodes(@"weatherdata/weather/forecast").Count; i++) { ForeseeWeatherInfo weatherinfo = new ForeseeWeatherInfo(); weatherinfo.Low = XmlDoc.SelectNodes(@"weatherdata/weather/forecast")[i].Attributes["low"].InnerText; weatherinfo.High = XmlDoc.SelectNodes(@"weatherdata/weather/forecast")[i].Attributes["high"].InnerText; weatherinfo.Skycodeday = XmlDoc.SelectNodes(@"weatherdata/weather/forecast")[i].Attributes["skycodeday"].InnerText; weatherinfo.Skytextday = XmlDoc.SelectNodes(@"weatherdata/weather/forecast")[i].Attributes["skytextday"].InnerText; weatherinfo.Date = XmlDoc.SelectNodes(@"weatherdata/weather/forecast")[i].Attributes["date"].InnerText; weatherinfo.Day = XmlDoc.SelectNodes(@"weatherdata/weather/forecast")[i].Attributes["day"].InnerText; weatherinfo.Shortday = XmlDoc.SelectNodes(@"weatherdata/weather/forecast")[i].Attributes["shortday"].InnerText; weatherinfo.Precip = XmlDoc.SelectNodes(@"weatherdata/weather/forecast")[i].Attributes["precip"].InnerText; weatherinfo.WeatherStatus = ChangeSkyCodeToWeatherStatus(weatherinfo.Skycodeday); weatherinfo.AreaCode = request.AreaCode; weatherinfo.WCCode = request.WCCode; respone.OthreDays.Add(weatherinfo); } if (respone.OthreDays.Count > 0) { respone.CurrentDay.High = respone.OthreDays[0].High; respone.CurrentDay.Low = respone.OthreDays[0].Low; } return(respone); } else { throw new XMLNodeNotFoundException(); } }
static void Main(string[] args) { DateTime starttime; DateTime endtime; TimeSpan spentime; int index = 1; List <WCCountry> wccountries = CityAndAreaConfig.Instance.getWCCountryList(); try { Console.WriteLine("=============================================================================="); starttime = DateTime.Now; Console.WriteLine(string.Format("Program start : Time =>{0}", starttime.ToString("yyyy/MM/dd HH:mm:ss"))); Console.WriteLine("=============================================================================="); foreach (var item in wccountries) { Console.WriteLine(""); Console.WriteLine("=============================================================================="); Console.WriteLine(string.Format("Index = {0} , WCCode = {1} , AreaName = {2} , Name ={3}", index, item.WCCode, CityAndAreaConfig.Instance.getAreaNameByAreaCode(item.AreaCode), item.Name)); QueryWeatherRequest request = new QueryWeatherRequest { WCCode = item.WCCode, AreaCode = item.AreaCode }; QueryWeatherResponse response = QueryServices.GetWeatherInfo(request); if (response != null && response.CurrentDay != null) { Console.WriteLine(string.Format("天氣狀況:{0} , 地點:{1} , 溫度:{2} , 舒適度:{3} , 溫度範圍:{4} ~ {5} , 濕度:{6} , 風速:{7} , 觀測時間:{8}" , response.CurrentDay.WeatherStatus , CityAndAreaConfig.Instance.getAreaNameByAreaCode(item.AreaCode) + " - " + CityAndAreaConfig.Instance.getWCCountryNameByWCCode(item.WCCode) , response.CurrentDay.Temperature , response.CurrentDay.Feelslike , response.CurrentDay.Low , response.CurrentDay.High , response.CurrentDay.Humidity , response.CurrentDay.Windspeed , response.CurrentDay.Observationtime )); WeatherDataDAO.NewWeatherData(response.CurrentDay); //WeatherDataDAO.AddForeseeWeatherData(response.OthreDays); } index++; Console.WriteLine("=============================================================================="); } Console.WriteLine(""); Console.WriteLine("=============================================================================="); endtime = DateTime.Now; Console.WriteLine(string.Format("Program End : Time =>{0}", endtime.ToString("yyyy/MM/dd HH:mm:ss"))); spentime = endtime - starttime; Console.WriteLine("Count : {0} , Spend Time:{1} 分 {2} 秒", index, spentime.Minutes, spentime.Seconds); Console.WriteLine("=============================================================================="); } catch (System.Exception ex) { Console.WriteLine(string.Format("System Error : {0}", ex.ToString())); } }