/* It includes methods related to SampleEntity */ #endregion #region Methods public static WeatherStatus GetCityWeatherStatus(string cityName) { try { HttpWebRequest myReq8 = DAT.DataService.CreateRequest(DAT.DataStatics.DomainForService, DAT.DataStatics.UserNameForService, DAT.DataStatics.PasswordForService, DAT.DataStatics.saportServiceURL(DAT.DataStatics.saportServiceGetCityWeatherStatus), false, true, "application/json; charset=UTF-8", "application/json; odata=verbose", "POST"); string parsedContent8 = "{\"CityName\":\"" + (string.IsNullOrEmpty(cityName) ? weatherDefaultCity : cityName) + "\"}"; UTF8Encoding encoding8 = new UTF8Encoding(); Byte[] bytes8 = encoding8.GetBytes(parsedContent8); myReq8.ContentLength = bytes8.Length; Stream newStream8 = myReq8.GetRequestStream(); newStream8.Write(bytes8, 0, bytes8.Length); newStream8.Close(); WebResponse response8 = myReq8.GetResponse(); string responseString8 = DAT.DataService.RestfulReader(response8); var serializer = new JavaScriptSerializer(); WeatherObject weatherObj = serializer.Deserialize <WeatherObject>(responseString8); WeatherStatus weatherStatus = weatherObj.Data; if (weatherStatus == null) { weatherStatus = GetDefaultWeatherStatus(); } return(weatherStatus); } catch (Exception ex) { EXP.RedirectToErrorPage(ex.Message); return(null); } }
public void HandleMqttMessage(MqttApplicationMessageReceivedEventArgs eventArguments) { var message = eventArguments.ApplicationMessage; if (message.Topic.ToLower() == $"{_weatherAdapterInitializationArgument.TopicPrefix}weather/{_weatherAdapterInitializationArgument.Identifier}") { var decodedString = _encodingService.Decode(eventArguments.ApplicationMessage.Payload); try { var payload = _jsonSerializerService.Deserialize <WeatherAdapterPayload>(decodedString); if (payload != null) { _status = new WeatherStatus { Temperature = payload.Temperature, Humidity = payload.Humidity, Pressure = payload.Pressure, TimeStamp = DateTime.Now }; Console.WriteLine($"Serialized: {decodedString}"); Console.WriteLine(_status); } } catch (Exception e) { Console.WriteLine("Deserialize failed"); Console.WriteLine(e); Console.WriteLine(e.StackTrace); // log message } } }
public static WeatherStatus DeserializeXMLToWeathers(string filefolder, string fileName, string cityName) { try { string fileLocation = AppDomain.CurrentDomain.BaseDirectory + "\\" + filefolder + "\\" + fileName; List <WeatherStatus> weatherStatuses = new List <WeatherStatus>(); WeatherStatus cityWeatherStatus = new WeatherStatus(); if (HLP.CheckIfFileExists(fileLocation)) { System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <WeatherStatus>)); System.IO.FileStream file = System.IO.File.OpenRead(fileLocation); weatherStatuses = (List <WeatherStatus>)writer.Deserialize(file); cityWeatherStatus = weatherStatuses.Single(x => x.City == cityName); file.Close(); } else { weatherStatuses = QueryAllWeatherStatusesForXml(); System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <WeatherStatus>)); System.IO.FileStream file = System.IO.File.Create(fileLocation); writer.Serialize(file, weatherStatuses); file.Close(); } return(weatherStatuses.Single(x => x.City == cityName)); } catch (Exception ex) { EXP.RedirectToErrorPage(ex.Message); return(null); } }
private WeatherStatus GetWeatherStatus() { var result = this.GetWeather(); var weatherStatus = new WeatherStatus(result); return(weatherStatus); }
public static string GetWeatherImageUrl(WeatherStatus weatherStat) { try { string periodOfDay = (DateTime.Now.Hour > 19 && DateTime.Now.Hour <= 23) || (DateTime.Now.Hour >= 0 && DateTime.Now.Hour <= 5) ? "pm" : "am"; return(string.Format(weatherImgeUrl, periodOfDay == "am" ? "day" : "night", weatherStat.WeatherInfo.StatusImage, weatherStat.WeatherInfo.Status)); } catch (Exception ex) { EXP.RedirectToErrorPage(ex.Message); return(null); } }
private void UpdateWeatherStatusDisplay(object sender, WeatherStatus weather) { Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { this.SunsetTimeTextBlock.Text = weather.SunsetTime.ToString("h:mm tt"); this.WindSpeedTextBlock.Text = weather.WindSpeed.ToString(); this.MainIconTextBlock.Text = weather.IconCode; this.TempTextBlock.Text = weather.Temperature.ToString() + "°"; this.DetailedSumaryTextBlock.Text = weather.DetailedSummary; this.DayTextBlock01.Text = weather.DailySummary[1].ShortDayName; this.IconTextBlock01.Text = weather.DailySummary[1].IconCode; this.HighTempDay01.Text = weather.DailySummary[1].HightTemp.ToString(); this.LowTempDay01.Text = weather.DailySummary[1].LowTemp.ToString(); this.DayTextBlock02.Text = weather.DailySummary[2].ShortDayName; this.IconTextBlock02.Text = weather.DailySummary[2].IconCode; this.HighTempDay02.Text = weather.DailySummary[2].HightTemp.ToString(); this.LowTempDay02.Text = weather.DailySummary[2].LowTemp.ToString(); this.DayTextBlock03.Text = weather.DailySummary[3].ShortDayName; this.IconTextBlock03.Text = weather.DailySummary[3].IconCode; this.HighTempDay03.Text = weather.DailySummary[3].HightTemp.ToString(); this.LowTempDay03.Text = weather.DailySummary[3].LowTemp.ToString(); this.DayTextBlock04.Text = weather.DailySummary[4].ShortDayName; this.IconTextBlock04.Text = weather.DailySummary[4].IconCode; this.HighTempDay04.Text = weather.DailySummary[4].HightTemp.ToString(); this.LowTempDay04.Text = weather.DailySummary[4].LowTemp.ToString(); this.DayTextBlock05.Text = weather.DailySummary[5].ShortDayName; this.IconTextBlock05.Text = weather.DailySummary[5].IconCode; this.HighTempDay05.Text = weather.DailySummary[5].HightTemp.ToString(); this.LowTempDay05.Text = weather.DailySummary[5].LowTemp.ToString(); this.DayTextBlock06.Text = weather.DailySummary[6].ShortDayName; this.IconTextBlock06.Text = weather.DailySummary[6].IconCode; this.HighTempDay06.Text = weather.DailySummary[6].HightTemp.ToString(); this.LowTempDay06.Text = weather.DailySummary[6].LowTemp.ToString(); this.DayTextBlock07.Text = weather.DailySummary[7].ShortDayName; this.IconTextBlock07.Text = weather.DailySummary[7].IconCode; this.HighTempDay07.Text = weather.DailySummary[7].HightTemp.ToString(); this.LowTempDay07.Text = weather.DailySummary[7].LowTemp.ToString(); } ); }
public static WeatherStatus GetDefaultWeatherStatus() { try { WeatherStatus defaultWeatherStat = new WeatherStatus() { City = weatherDefaultCity, WeatherInfo = new WeatherInfo() { ObservedAt = "", Status = "Partly Cloudy", StatusImage = "116", Temperature = "20" } }; return(defaultWeatherStat); } catch (Exception ex) { EXP.RedirectToErrorPage(ex.Message); return(null); } }
public static List <WeatherStatus> QueryAllWeatherStatusesForXml() { try { List <WeatherStatus> statuses = new List <WeatherStatus>(); foreach (var item in Enum.GetValues(typeof(WeatherCityName))) { WeatherStatus status = item.ToString() == "İstanbul" ? GetCityWeatherStatus("istanbul") : GetCityWeatherStatus(item.ToString()); statuses.Add(status); } return(statuses); } catch (Exception ex) { EXP.RedirectToErrorPage(ex.Message); return(null); } }
private WeatherStatus ParseWeather(string input) { input = input.Replace(" ", "").Trim(); string weatherIco = String_GetLeft(input, "🌡").Trim(); string Temp = GetBetween(input, "🌡", "🌬").Trim(); string Wind = String_GetRight_Last(input, "🌬").Trim(); string WindDirection = ""; //Emoji双字符,会遗留一个空白字符,需根据第二个字符是否为风向来判断 if (!Char.IsDigit(Wind.ToCharArray()[1])) //如果不是数字,那就是风向了 { WindDirection = Wind.Substring(1, 1); //风向 Wind = Wind.Substring(2); //去掉风向之后的风速 } var re = new WeatherStatus(); re.WeatherIco = weatherIco + " "; switch (weatherIco) { case "☀️": re.WeatherColor = Colors.Yellow; break; case "⛅️": re.WeatherColor = Colors.Orange; break; case "🌦": re.WeatherColor = Colors.Blue; break; case "⛈️": re.WeatherColor = Colors.Blue; break; case "🌩": re.WeatherColor = Colors.Yellow; break; case "☁️": re.WeatherColor = Colors.Gray; break; case "🌧": re.WeatherColor = Colors.DarkBlue; break; case "🌁": re.WeatherColor = Colors.Gray; break; case "🌫": re.WeatherColor = Colors.White; break; case "❄️": re.WeatherColor = Colors.White; break; case "🌨": re.WeatherColor = Colors.White; break; default: re.WeatherColor = Colors.White; break; } re.Temp = "🌡" + Temp + " "; //SubString从第二个取的原因是Emoji字符会遗留一个空白字符 if (!int.TryParse(String_GetLeft(Temp.Substring(1), "°C"), out int temp)) { temp = 0; } if (temp < -20) { re.TempColor = Colors.DarkBlue; } if (temp < -10) { re.TempColor = Colors.MidnightBlue; } else if (temp < 10) { re.TempColor = Colors.Blue; } else if (temp < 20) { re.TempColor = Colors.DarkTurquoise; } else if (temp < 30) { re.TempColor = Colors.Aqua; } else if (temp < 35) { re.TempColor = Colors.Yellow; } else if (temp < 40) { re.TempColor = Colors.Orange; } else if (temp < 50) { re.TempColor = Colors.OrangeRed; } else if (temp < 60) { re.TempColor = Colors.Red; } else { re.TempColor = Colors.DarkRed; } re.Wind = "🌬" + WindDirection + Wind + " "; if (!int.TryParse(String_GetLeft(Wind, "km/h"), out int wind)) { wind = 0; } if (wind < 1) { re.WindLevel = "0"; re.WindColor = Colors.White; } else if (wind <= 5) { re.WindLevel = "1"; re.WindColor = Colors.AliceBlue; } else if (wind <= 19) { re.WindLevel = "2"; re.WindColor = Colors.Aquamarine; } else if (wind <= 28) { re.WindLevel = "3"; re.WindColor = Colors.Aqua; } else if (wind <= 38) { re.WindLevel = "4"; re.WindColor = Colors.Cyan; } else if (wind <= 49) { re.WindLevel = "5"; re.WindColor = Colors.DeepSkyBlue; } else if (wind <= 61) { re.WindLevel = "6"; re.WindColor = Colors.CornflowerBlue; } else if (wind <= 74) { re.WindLevel = "7"; re.WindColor = Colors.DodgerBlue; } else if (wind <= 88) { re.WindLevel = "8"; re.WindColor = Colors.Blue; } else if (wind <= 102) { re.WindLevel = "9"; re.WindColor = Colors.MediumBlue; } else if (wind <= 117) { re.WindLevel = "10"; re.WindColor = Colors.DarkBlue; } else if (wind <= 134) { re.WindLevel = "11"; re.WindColor = Colors.MidnightBlue; } else if (wind <= 149) { re.WindLevel = "12"; re.WindColor = Colors.Navy; } else if (wind <= 166) { re.WindLevel = "13"; re.WindColor = Colors.Navy; } else if (wind <= 183) { re.WindLevel = "14"; re.WindColor = Colors.Navy; } else if (wind <= 201) { re.WindLevel = "15"; re.WindColor = Colors.Navy; } else if (wind <= 220) { re.WindLevel = "16"; re.WindColor = Colors.Navy; } else { re.WindLevel = "17"; re.WindColor = Colors.Navy; } return(re); }
string GetSmallWeatherStatusString(WeatherStatus weatherStatus) { string url = "http://www.webxml.com.cn/images/weather/"; switch (weatherStatus) { case WeatherStatus.Weizhi: url += "nothing.gif"; break; case WeatherStatus.Qing: url += "0.gif"; break; case WeatherStatus.Duoyun: url += "1.gif"; break; case WeatherStatus.Yin: url += "2.gif"; break; case WeatherStatus.Zhenyu: url += "3.gif"; break; case WeatherStatus.Leizhenyu: url += "4.gif"; break; case WeatherStatus.LeizhenyuBingpao: url += "5.gif"; break; case WeatherStatus.Yujiaxue: url += "6.gif"; break; case WeatherStatus.Xiaoyu: url += "7.gif"; break; case WeatherStatus.Zhongyu: url += "8.gif"; break; case WeatherStatus.Dayu: url += "9.gif"; break; case WeatherStatus.Baoyu: url += "10.gif"; break; case WeatherStatus.Dabaoyu: url += "11.gif"; break; case WeatherStatus.Tedabaoyu: url += "12.gif"; break; case WeatherStatus.Zhenxue: url += "13.gif"; break; case WeatherStatus.Xiaoxue: url += "14.gif"; break; case WeatherStatus.Zhongxue: url += "15.gif"; break; case WeatherStatus.Daxue: url += "16.gif"; break; case WeatherStatus.Baoxue: url += "17.gif"; break; case WeatherStatus.Wu: url += "18.gif"; break; case WeatherStatus.Dongyu: url += "19.gif"; break; case WeatherStatus.Shachenbao: url += "20.gif"; break; case WeatherStatus.XiaoyuZhuangZhongyu: url += "21.gif"; break; case WeatherStatus.ZhongyuZhuangDayu: url += "22.gif"; break; case WeatherStatus.DayuZhuangBaoyu: url += "23.gif"; break; case WeatherStatus.BaoyuZhuangDabaoyu: url += "24.gif"; break; case WeatherStatus.DabaoyuZhuangTedabaoyu: url += "25.gif"; break; case WeatherStatus.XiaoxueZhuangZhongxue: url += "26.gif"; break; case WeatherStatus.ZhongxueZhuangDaxue: url += "27.gif"; break; case WeatherStatus.DaxueZhuangBaoxue: url += "28.gif"; break; case WeatherStatus.Fuchen: url += "29.gif"; break; case WeatherStatus.Yasha: url += "30.gif"; break; case WeatherStatus.Qiangshachenbao: url += "31.gif"; break; case WeatherStatus.Xiaodaozhongyu: url += "21.gif"; break; case WeatherStatus.Zhongdaodayu: url += "22.gif"; break; case WeatherStatus.Dadaobaoyu: url += "23.gif"; break; case WeatherStatus.Xiaodaozhongxue: url += "26.gif"; break; case WeatherStatus.Zhongdaodaxue: url += "27.gif"; break; case WeatherStatus.Dadaobaoxue: url += "28.gif"; break; case WeatherStatus.Xiaozhenyu: url += "3.gif"; break; case WeatherStatus.Yintian: url += "2.gif"; break; case WeatherStatus.Mai: url += "18.gif"; break; case WeatherStatus.Wumai: url += "18.gif"; break; default: url += "nothing.gif"; break; } return(url); }
string GetBigWeatherStatusString(WeatherStatus weatherStatus) { string url = "http://www.webxml.com.cn/images/weather/"; switch (weatherStatus) { case WeatherStatus.Weizhi: url += "a_nothing.gif"; break; case WeatherStatus.Qing: url += "a_0.gif"; break; case WeatherStatus.Duoyun: url += "a_1.gif"; break; case WeatherStatus.Yin: url += "a_2.gif"; break; case WeatherStatus.Zhenyu: url += "a_3.gif"; break; case WeatherStatus.Leizhenyu: url += "a_4.gif"; break; case WeatherStatus.LeizhenyuBingpao: url += "a_5.gif"; break; case WeatherStatus.Yujiaxue: url += "a_6.gif"; break; case WeatherStatus.Xiaoyu: url += "a_7.gif"; break; case WeatherStatus.Zhongyu: url += "a_8.gif"; break; case WeatherStatus.Dayu: url += "a_9.gif"; break; case WeatherStatus.Baoyu: url += "a_10.gif"; break; case WeatherStatus.Dabaoyu: url += "a_11.gif"; break; case WeatherStatus.Tedabaoyu: url += "a_12.gif"; break; case WeatherStatus.Zhenxue: url += "a_13.gif"; break; case WeatherStatus.Xiaoxue: url += "a_14.gif"; break; case WeatherStatus.Zhongxue: url += "a_15.gif"; break; case WeatherStatus.Daxue: url += "a_16.gif"; break; case WeatherStatus.Baoxue: url += "a_17.gif"; break; case WeatherStatus.Wu: url += "a_18.gif"; break; case WeatherStatus.Dongyu: url += "a_19.gif"; break; case WeatherStatus.Shachenbao: url += "a_20.gif"; break; case WeatherStatus.XiaoyuZhuangZhongyu: url += "a_21.gif"; break; case WeatherStatus.ZhongyuZhuangDayu: url += "a_22.gif"; break; case WeatherStatus.DayuZhuangBaoyu: url += "a_23.gif"; break; case WeatherStatus.BaoyuZhuangDabaoyu: url += "a_24.gif"; break; case WeatherStatus.DabaoyuZhuangTedabaoyu: url += "a_25.gif"; break; case WeatherStatus.XiaoxueZhuangZhongxue: url += "a_26.gif"; break; case WeatherStatus.ZhongxueZhuangDaxue: url += "a_27.gif"; break; case WeatherStatus.DaxueZhuangBaoxue: url += "a_28.gif"; break; case WeatherStatus.Fuchen: url += "a_29.gif"; break; case WeatherStatus.Yasha: url += "a_30.gif"; break; case WeatherStatus.Qiangshachenbao: url += "a_31.gif"; break; case WeatherStatus.Xiaodaozhongyu: url += "a_21.gif"; break; case WeatherStatus.Zhongdaodayu: url += "a_22.gif"; break; case WeatherStatus.Dadaobaoyu: url += "a_23.gif"; break; case WeatherStatus.Xiaodaozhongxue: url += "a_26.gif"; break; case WeatherStatus.Zhongdaodaxue: url += "a_27.gif"; break; case WeatherStatus.Dadaobaoxue: url += "a_28.gif"; break; case WeatherStatus.Xiaozhenyu: url += "a_3.gif"; break; case WeatherStatus.Yintian: url += "a_2.gif"; break; case WeatherStatus.Mai: url += "a_18.gif"; break; case WeatherStatus.Wumai: url += "a_18.gif"; break; default: url += "a_nothing.gif"; break; } return(url); }
private void Analysis7DayData(byte[] contentBytes) { using (MemoryStream stream = new MemoryStream(contentBytes)) using (StreamReader reader = new StreamReader(stream)) { string content = reader.ReadToEnd(); MatchCollection matches = Regex.Matches(content, "<ul class=\"t clearfix\">.*?</ul>", RegexOptions.Singleline); if (matches.Count > 0) { matches = Regex.Matches(matches[0].Value, "<li class=\"sky skyid.*?</li>", RegexOptions.Singleline); if (matches.Count == 7) { List <string> dayList = new List <string>(); List <string> infoList = new List <string>(); List <string> tempList = new List <string>(); List <string> windList = new List <string>(); List <WeatherStatus> statusList = new List <WeatherStatus>(); MatchCollection matches_temp; foreach (Match match in matches) { //日期:31日(今日) matches_temp = Regex.Matches(match.Value, "<h1>.*?</h1>"); if (matches_temp.Count > 0) { dayList.Add(RemoveAngleBrackets(matches_temp[0].Value)); } else { dayList.Add(""); } //天气信息:晴 matches_temp = Regex.Matches(match.Value, "<p.*class=\"wea\".*?</p>"); if (matches_temp.Count > 0) { infoList.Add(RemoveAngleBrackets(matches_temp[0].Value)); WeatherStatus status = GetWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value)); if (status == WeatherStatus.Weizhi) { status = GetUnkownWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value)); } statusList.Add(status); } else { dayList.Add(""); statusList.Add(WeatherStatus.Weizhi); } //温度信息:4/-1℃ //风力信息:3-4级转3级 matches_temp = Regex.Matches(match.Value, "<span>.*?</span>"); string temperature = ""; string wind = ""; if (matches_temp.Count > 0) { temperature = RemoveAngleBrackets(matches_temp[0].Value) + "℃"; } matches_temp = Regex.Matches(match.Value, "<i>.*?</i>"); if (matches_temp.Count >= 1) { temperature = string.IsNullOrEmpty(temperature) ? RemoveAngleBrackets(matches_temp[0].Value) : string.Format("{0}/{1}", temperature, RemoveAngleBrackets(matches_temp[0].Value)); } if (matches_temp.Count >= 2) { wind = RemoveAngleBrackets(matches_temp[1].Value); } tempList.Add(temperature); windList.Add(wind); } this.Day_1To7 = dayList.ToArray(); this.Info_1To7 = infoList.ToArray(); this.Temperature_1To7 = tempList.ToArray(); this.Wind_1To7 = windList.ToArray(); this.WeatherStatus_1To7 = statusList.ToArray(); } } } }
private void AnalysisMoreData(byte[] contentBytes) { using (MemoryStream stream = new MemoryStream(contentBytes)) using (StreamReader reader = new StreamReader(stream)) //---- { string content = reader.ReadToEnd(); MatchCollection matches = Regex.Matches(content, "<ul class=\"t clearfix\">.*?</ul>", RegexOptions.Singleline); if (matches.Count > 0) { matches = Regex.Matches(matches[0].Value, "<li.*?</li>", RegexOptions.Singleline); if (matches.Count == 8) { List <string> dayList = new List <string>(); List <string> infoList = new List <string>(); List <string> tempList = new List <string>(); List <string> wind1List = new List <string>(); List <string> wind2List = new List <string>(); List <WeatherStatus> statusList = new List <WeatherStatus>(); MatchCollection matches_temp; foreach (Match match in matches) { //日期:31日(今日) matches_temp = Regex.Matches(match.Value, "<span class=\"time\">.*?</span>"); if (matches_temp.Count > 0) { dayList.Add(RemoveAngleBrackets(matches_temp[0].Value)); } else { dayList.Add(""); } //天气信息:晴 matches_temp = Regex.Matches(match.Value, "<span class=\"wea\">.*?</span>"); if (matches_temp.Count > 0) { infoList.Add(RemoveAngleBrackets(matches_temp[0].Value)); WeatherStatus status = GetWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value)); if (status == WeatherStatus.Weizhi) { status = GetUnkownWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value)); } statusList.Add(status); } else { dayList.Add(""); statusList.Add(WeatherStatus.Weizhi); } //温度信息:4/-1℃ //风力信息:3-4级转3级 matches_temp = Regex.Matches(match.Value, "<span class=\"tem\".*?</span>"); if (matches_temp.Count > 0) { tempList.Add(RemoveAngleBrackets(RemoveAngleBrackets(matches_temp[0].Value))); } else { tempList.Add(""); } matches_temp = Regex.Matches(match.Value, "<span class=\"wind\">.*?</span>"); if (matches_temp.Count > 0) { wind1List.Add(RemoveAngleBrackets(matches_temp[0].Value)); } else { wind1List.Add(""); } matches_temp = Regex.Matches(match.Value, "<span class=\"wind1\">.*?</span>"); if (matches_temp.Count > 0) { wind2List.Add(RemoveAngleBrackets(matches_temp[0].Value)); } else { wind2List.Add(""); } } this.Day_7To15 = dayList.ToArray(); this.Info_7To15 = infoList.ToArray(); this.Temperature_7To15 = tempList.ToArray(); this.Wind1_7To15 = wind1List.ToArray(); this.Wind2_7To15 = wind2List.ToArray(); this.WeatherStatus_7To15 = statusList.ToArray(); } } } //---- }