-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
113 lines (87 loc) · 4.33 KB
/
Form1.cs
File metadata and controls
113 lines (87 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Net;
namespace PDC
{
public partial class WeatherApp : Form
{
public WeatherApp()
{
InitializeComponent();
}
//Api Key
string APIKey = "9956205bd19201ba74cb853779f8260e";
private void button1_Click(object sender, EventArgs e)
{
//For fetching the weather
getweather();
getforecast();
}
double lon;
double lat;
void getweather()
{
//For using online services like API
using (WebClient web = new WebClient())
{
//Api
string url = string.Format("https://api.openweathermap.org/data/2.5/weather?q={0}&APPID={1}",textBox1.Text,APIKey);
//Download data from Api
var json = web.DownloadString(url);
//Convert the json file of data into text format
WeatherInfo.root info = JsonConvert.DeserializeObject<WeatherInfo.root>(json);
//fetching the weather image icon from the api
pictureBox1.ImageLocation = "https://openweathermap.org/img/w/" +info.weather[0].icon +".png";
//for the weather condition
labcondition.Text = info.weather[0].main;
//for the details of the condition
labdeatails.Text = info.weather[0].description;
//fetching the sunrise time and then convert to local time
labsunrise.Text = convertDateTime(info.sys.sunrise).ToShortTimeString();
//fetching the sunset time and then convert to local time
labsunset.Text = convertDateTime(info.sys.sunset).ToShortTimeString();
//fetching wind speed and then converting in to string
labwinds.Text = info.wind.speed.ToString();
//fetching the pressure and then converting in to string
labpressure.Text = info.main.pressure.ToString();
//fetching the humidity and then converting in to string
labhumidiy.Text = info.main.humidity.ToString();
lon = info.coord.lon;
lat = info.coord.lat;
}
}
DateTime convertDateTime(long millisec)
{
// years, months, days, hours, minutes, seconds, miliseconds
DateTime day = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc).ToLocalTime();
day = day.AddSeconds(millisec).ToLocalTime(); //we add these millisec to this day of 1970 Unix timespam
//and then it returns us correct current time
//then we convert it to local time
return day;
}
void getforecast()
{
//For using online services like API
using (WebClient web = new WebClient())
{
//Api
string url = string.Format("https://api.openweathermap.org/data/2.5/forecast?lat={0}&lon={1}&exclude=current,minutely,hourly,alerts&appid={2}", lon, lat, APIKey);
//Download data from Api
var json = web.DownloadString(url);
WeatherForecast.Forecastinfo forecastinfo = JsonConvert.DeserializeObject<WeatherForecast.Forecastinfo>(json);
foreecast fuc;
for (int i=0; i<8; i++)
{
fuc = new foreecast();
fuc.picweathericon.ImageLocation = "https://openweathermap.org/img/w/" + forecastinfo.list[i].weather[0].icon + ".png";
fuc.labmainweather.Text = forecastinfo.list[i].weather[0].main;
fuc.labweatherdescription.Text = forecastinfo.list[i].weather[0].description;
fuc.labDT.Text = convertDateTime(forecastinfo.list[i].dt).DayOfWeek.ToString();
fuc.labdtime.Text = forecastinfo.list[i].dt_txt;
flowLayoutPanel1.Controls.Add(fuc);
}
}
}
}
}