Example #1
0
        public bool WeatherQuery(string provice, string city, out WeatherDetail detailResult)
        {
            try
            {
                HttpWebRequest request = WebRequest.CreateHttp(@"http://www.nmc.gov.cn/f/rest/province");

                HttpWebResponse             response        = request.GetResponse() as HttpWebResponse;
                Stream                      stream          = response.GetResponseStream();
                StreamReader                reader          = new StreamReader(stream);
                string                      content         = reader.ReadToEnd();
                List <Province>             provinceResult  = JsonConvert.DeserializeObject <List <Province> >(content);
                Dictionary <string, string> proviceNamedict = new Dictionary <string, string>();
                provinceResult.ForEach(x =>
                {
                    if (!proviceNamedict.Keys.Contains(x.name))
                    {
                        proviceNamedict.Add(x.name, x.code);
                    }
                });
                string str = string.Format(@"http://www.nmc.gov.cn/f/rest/province/{0}", proviceNamedict[provice]);
                request  = WebRequest.CreateHttp(str);
                response = request.GetResponse() as HttpWebResponse;
                stream   = response.GetResponseStream();
                reader   = new StreamReader(stream);
                content  = reader.ReadToEnd();
                List <City> cityResult = JsonConvert.DeserializeObject <List <City> >(content);
                Dictionary <string, string> cityNamedict = new Dictionary <string, string>();
                cityResult.ForEach(x =>
                {
                    if (!cityNamedict.Keys.Contains(x.city))
                    {
                        cityNamedict.Add(x.city, x.code);
                    }
                });
                string str1 = string.Format("http://www.nmc.gov.cn/f/rest/real/{0}", cityNamedict[city]);
                request      = WebRequest.CreateHttp(str1);
                response     = request.GetResponse() as HttpWebResponse;
                stream       = response.GetResponseStream();
                reader       = new StreamReader(stream);
                content      = reader.ReadToEnd();
                detailResult = JsonConvert.DeserializeObject <WeatherDetail>(content);
                return(true);
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
                detailResult = null;
                return(false);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                detailResult = null;
                return(false);
            }
        }
Example #2
0
        private void timer_weatherQuery_Tick(object state, EventArgs sender)//查询天气
        {
            WeatherClass  WC     = new WeatherClass();
            WeatherDetail detail = new WeatherDetail();

            if (WC.WeatherQuery(Client.ConfigClass.getInstance().getValue("province"), Client.ConfigClass.getInstance().getValue("city"), out detail))
            {
                WeatherUpdate.update(detail);
            }
            else
            {
                this.LB_NetState.Text      = "异常";
                this.LB_NetState.ForeColor = Color.Red;
            }
        }
Example #3
0
 private void Btn_Confirm_Click(object sender, EventArgs e)
 {
     if (this.dateTime.Value != null && this.cbo_weather.Text != null && this.tbo_temperature.Text != null && this.tbo_wind.Text != null)
     {
         float temperature;
         if (float.TryParse(this.tbo_temperature.Text, out temperature))
         {
             DateTime           dt        = this.dateTime.Value;
             string             weather   = this.cbo_weather.Text.ToString();
             string             wind      = this.tbo_wind.Text;
             Client.MySqlHelper mysql     = new Client.MySqlHelper();
             string             sqlString = string.Format("INSERT INTO tb_weather SET time='{0}',weather='{1}',temperature={2},wind='{3}';", dt, weather, temperature, wind);
             try
             {
                 mysql.ExecuteNonQuery(sqlString);
                 WeatherDetail detail = new WeatherDetail();
                 detail.weather             = new Weather();
                 detail.wind                = new Wind();
                 detail.publish_time        = dt.ToString();
                 detail.weather.temperature = temperature;
                 detail.wind.power          = wind;
                 detail.weather.info        = weather;
                 WeatherUpdate.update(detail);
                 this.tbo_temperature.Text = "";
                 this.tbo_wind.Text        = "";
                 this.cbo_weather.Text     = "";
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message);
                 MessageBox.Show("写入数据库失败");
             }
         }
         else
         {
             MessageBox.Show("请输入正确温度");
         }
     }
     else
     {
         MessageBox.Show("请输入全部信息");
     }
 }
Example #4
0
        public static void readWeatherDetail()
        {
            string        sqlString = string.Format("SELECT * FROM tb_weather WHERE uuid=(SELECT MAX(uuid) FROM tb_weather);");
            WeatherDetail detail    = new WeatherDetail();

            detail.weather = new Weather();
            detail.wind    = new Wind();
            try
            {
                DataTable dt = mysql.ExecuteDataTable(sqlString);
                detail.publish_time        = dt.Rows[0]["time"].ToString();
                detail.weather.info        = dt.Rows[0]["weather"].ToString();
                detail.wind.power          = dt.Rows[0]["wind"].ToString();
                detail.weather.temperature = float.Parse(dt.Rows[0]["temperature"].ToString());
                update(detail);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Example #5
0
        public static void update(WeatherDetail detail)
        {
            //天气
            LB_NetState.Text      = "测试中";
            LB_NetState.ForeColor = Color.White;
            LB_Weather.Text       = detail.weather.info;
            int index = -1;

            if (detail.weather.info.Contains("晴"))
            {
                index = 0;
            }
            else if (detail.weather.info.Contains("雨"))
            {
                index = 1;
            }
            else if (detail.weather.info.Contains("雪"))
            {
                index = 2;
            }
            else if (detail.weather.info.Contains("阴"))
            {
                index = 3;
            }
            else if (detail.weather.info.Contains("云"))
            {
                index = 4;
            }
            else if (detail.weather.info.Contains("雹"))
            {
                index = 5;
            }
            else if (detail.weather.info.Contains("雾") || detail.weather.info.Contains("霾"))
            {
                index = 6;
            }
            else if (detail.weather.info.Contains("沙"))
            {
                index = 7;
            }
            switch (index)
            {
            case 0:
                Pic_Weather.Image = Image.FromFile(@"img/weather_sunshine.jpg");
                break;

            case 1:
                Pic_Weather.Image = Image.FromFile(@"img/weather_rain.png");
                break;

            case 2:
                Pic_Weather.Image = Image.FromFile(@"img/weather_snow.png");
                break;

            case 3:
                Pic_Weather.Image = Image.FromFile(@"img/weather_Overcast.png");
                break;

            case 4:
                Pic_Weather.Image = Image.FromFile(@"img/weather_Cloudy2sunny.png");
                break;

            case 5:
                Pic_Weather.Image = Image.FromFile(@"img/weather_hail.png");
                break;

            case 6:
                Pic_Weather.Image = Image.FromFile(@"img/weather_Haze.png");
                break;

            case 7:
                Pic_Weather.Image = Image.FromFile(@"img/weather_sand.png");
                break;
            }
            //温度
            LB_Tem.Text = detail.weather.temperature.ToString() + "℃";
            //风力
            LB_WindPower.Text = detail.wind.power.ToString();
        }