-
Notifications
You must be signed in to change notification settings - Fork 1
/
CurrentWeather.cs
116 lines (104 loc) · 3.29 KB
/
CurrentWeather.cs
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
114
115
116
using System;
using System.Collections.Generic;
using System.Net;
using System.Text.Json;
namespace app
{
class CurrentWeather
{
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Main
{
public double temp { get; set; }
public double feels_like { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
public int pressure { get; set; }
public int humidity { get; set; }
}
public class Wind
{
public double speed { get; set; }
public int deg { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Sys
{
public int type { get; set; }
public int id { get; set; }
public string country { get; set; }
public int sunrise { get; set; }
public int sunset { get; set; }
}
public class Root
{
public Coord coord { get; set; }
public List<Weather> weather { get; set; }
public string @base { get; set; }
public Main main { get; set; }
public int visibility { get; set; }
public Wind wind { get; set; }
public Clouds clouds { get; set; }
public int dt { get; set; }
public Sys sys { get; set; }
public int timezone { get; set; }
public int id { get; set; }
public string name { get; set; }
public int cod { get; set; }
}
public static bool CheckForInternetConnection()
{
try
{
using (WebClient client = new WebClient())
{
using (client.OpenRead("http://google.com/generate_204"))
{
return true;
}
}
}
catch
{
return false;
}
}
public Tuple<float, bool> GetWeather(string city)
{
if (!CheckForInternetConnection())
{
throw new WebException("No internet connection");
}
float temp = 0f;
bool weatherOK = false;
string weath = "";
using (WebClient web = new WebClient())
{
string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&units=metric&appid=aa768c8b184a85a2e18c5b8fa9736598", city);
string json = web.DownloadString(url);
var w = JsonSerializer.Deserialize<Root>(json);
temp = Convert.ToSingle(w.main.feels_like);
weath = w.weather[0].main;
}
if (weath == "Clear" || weath == "Clouds")
{
weatherOK = true;
}
return new Tuple<float, bool>(temp, weatherOK);
}
}
}