-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerzoEsercizio.cs
406 lines (365 loc) · 12 KB
/
TerzoEsercizio.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using System;
using System.Collections.Generic;
using System.Net;
using System.Data;
using System.ServiceProcess;
using System.IO;
using System.Timers;
using System.Net.Http;
using System.Configuration;
using Newtonsoft.Json;
namespace TerzoEsercizio
{
public partial class TerzoEsercizio : ServiceBase
{
Timer timer;
int numberOfPlace = (Int32.Parse(ConfigurationManager.AppSettings["NumberOfPlace"]));
List<Place> places = new List<Place>();
List<string> appconfig =new List<string> { "FirstPlace", "SecondPlace", "ThirdPlace", "FourthPlace", "FifthPlace"};
public TerzoEsercizio()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WriteLogfile("Service is started") ;
timer = new Timer
{
Interval = (Int32.Parse(ConfigurationManager.AppSettings["TimeIntervall"]) * 60 * 1000)
};
for (int i = 0; i < numberOfPlace; i++)
{
AddPlace(i);
}
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Start();
}
public void AddPlace(int i)
{
places.Add(new Place(ConfigurationManager.AppSettings[appconfig[i].ToString()]));
}
private void OnElapsedTime(object sender, ElapsedEventArgs e)
{
try
{
timer.Stop();
for(int i = 0; i < numberOfPlace; i++)
{
GenerateWeatherInfo(sender, e, places[i]);
GenerateWeatherInfoForecast(places[i]) ;
}
}
catch (Exception ex)
{
WriteLogfile(e.ToString() ) ;
}
finally
{
timer.Start();
}
}
private void WriteLogfile(string message)
{
StreamWriter sw = null;
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\WindDataLogFile.txt", true); //AppDomain.CurrentDomain.BaseDirectory return [Application.exe location]
sw.WriteLine($"{DateTime.Now.ToString()} : {message}");
sw.Flush();
sw.Close();
}
private void GenerateWeatherInfo(object sender, ElapsedEventArgs e, Place place_)
{
string APIKey = "1c94af854cd256475701cd4d0ed5f520";
string url = string.Format("https://api.openweathermap.org/data/2.5/weather?q={0}&appid={1}", place_.name, APIKey);
try
{
HttpClient client = new HttpClient();
using (HttpResponseMessage response = client.GetAsync(url).Result)
{
using (HttpContent content = response.Content)
{
var json = content.ReadAsStringAsync().Result;
WeatherAPI weatherInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<WeatherAPI>(json);
place_.currentWeather = weatherInfo;
WriteLogfile($"{place_.name} Current Wind Speed and Temperature: {place_.currentWeather.wind.speed}, {place_.currentWeather.main.temp}");
}
}
}
catch (Exception ex)
{
WriteLogfile(ex.ToString());
}
}
private void GenerateWeatherInfoForecast( Place place_)
{
Coord coord = new Coord();
coord.lat = place_.currentWeather.coord.lat;
coord.lon = place_.currentWeather.coord.lon;
string APIKey = "0a0a67a57038fede56852d44452f4928";
string url = string.Format("https://api.openweathermap.org/data/2.5/onecall?lat={0}&lon={1}&exclude=current,minutely,hourly,alerts&units=metric&appid={2}", coord.lat, coord.lon, APIKey);
HttpClient client = new HttpClient();
using (HttpResponseMessage response = client.GetAsync(url).Result)
{
using (HttpContent content = response.Content)
{
var json = content.ReadAsStringAsync().Result;
WeatherForcastAPI weatherForcastAPI = Newtonsoft.Json.JsonConvert.DeserializeObject<WeatherForcastAPI>(json) ; //jsonSerialize
place_.forcastWeather = weatherForcastAPI;
printForecastWindTemp3NextDay(weatherForcastAPI);
}
}
void printForecastWindTemp3NextDay(WeatherForcastAPI weatherForcastAPI)
{
WriteLogfile($"{place_.name} Forecast Wind Speed NEXT 3 DAYS: { weatherForcastAPI.daily[0].wind_speed } ; {weatherForcastAPI.daily[1].wind_speed}; {weatherForcastAPI.daily[2].wind_speed}");
WriteLogfile($"{place_.name} Forecast Temperature NEXT 3 DAYS: {weatherForcastAPI.daily[0].temp.day}; {weatherForcastAPI.daily[1].temp.day}; {weatherForcastAPI.daily[2].temp.day}");
}
}
protected override void OnStop()
{
WriteLogfile("Service is stopped");
}
}
public class Place
{
public string name;
public WeatherAPI currentWeather;
public WeatherForcastAPI forcastWeather;
public Place(string name)
{
this.name = name;
currentWeather = new WeatherAPI();
forcastWeather = new WeatherForcastAPI();
}
}
public class WeatherForcastAPI
{
public double lat { get; set; }
public double lon { get; set; }
public string timezone { get; set; }
public int timezone_offset { get; set; }
public List<Daily> daily { get; set; }
public WeatherForcastAPI()
{
lat = 0.0;
lon = 0.0;
timezone = null;
timezone_offset = 0;
daily = new List<Daily>();
}
}
public class WeatherAPI
{
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 long dt { get; set; }
public Sys sys { get; set; }
public int timezone { get; set; }
public long id { get; set; }
public string name { get; set; }
public int cod { get; set; }
public WeatherAPI()
{
coord = new Coord();
weather = new List<Weather>();
@base = null;
main = new Main();
visibility = 0;
wind = new Wind();
clouds = new Clouds();
dt = 0;
sys = new Sys();
timezone = 0;
id = 0;
name = null;
cod = 0;
}
}
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
public Coord()
{
lon = 0.0;
lat = 0.0;
}
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
public Weather()
{
this.id = 0;
this.main = null;
this.description = null;
this.icon = null;
}
}
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 double pressure { get; set; }
public int humidity { get; set; }
public double sea_level { get; set; }
public double grnd_level { get; set; }
public Main()
{
this.temp = 0.0;
this.feels_like = 0.0;
this.temp_min = 0.0;
this.temp_max = 0.0;
this.pressure = 0.0;
this.humidity = 0;
this.sea_level = 0.0;
this.grnd_level = 0.0;
}
}
public class Wind
{
public double speed { get; set; }
public double deg { get; set; }
public double gust { get; set; }
public Wind(double _speed, double _deg, double _gust)
{
this.speed = _speed;
this.deg = _deg;
this.gust = _gust;
}
public Wind()
{
speed = 0.0;
deg = 0.0;
gust = 0.0;
}
}
public class Clouds
{
public int all { get; set; }
public Clouds()
{
all = 0;
}
}
public class Rain
{
public double h1 { get; set; }
public double h3 { get; set; }
public Rain()
{
h1 = 0.0;
h3 = 0.0;
}
}
public class Snow
{
public double h1 { get; set; }
public double h3 { get; set; }
public Snow()
{
h1 = 0.0;
h3= 0.0;
}
}
public class Sys
{
public int type { get; set; }
public long id { get; set; }
public double message { get; set; }
public string country { get; set; }
public long sunrise { get; set; }
public long sunset { get; set; }
public Sys()
{
type = 0;
id = 0;
message = 0.0;
country = null;
sunrise = 0;
sunset = 0;
}
}
public class Daily
{
public long dt { get; set; }
public long sunrise { set; get; }
public long sunset { set; get; }
public long moonrise { set; get; }
public long moonset { set; get; }
public float moon_phase { set; get; }
public Temp temp { get; set; }
public Feels_Like feels_like { get; set; }
public int pressure { get; set; }
public int humidity { get; set; }
public float dew_point { get; set; }
public float wind_speed { get; set; }
public int wind_deg { get; set; }
public List<Weather> weather { get; set; }
public int clouds { get; set; }
public float pop { get; set; }
public float rain { get; set; }
public float uvi { get; set; }
public Daily()
{
dt = 0;
sunrise = 0;
sunset = 0;
moonrise = 0;
moonset = 0;
moon_phase = 0;
temp = new Temp();
feels_like = new Feels_Like();
pressure = 0;
humidity = 0;
dew_point = 0;
wind_speed = 0;
wind_deg = 0;
weather = new List<Weather>();
clouds = 0;
pop = 0;
rain = 0;
uvi = 0;
}
}
public class Temp
{
public float day { get; set; }
public float min { get; set; }
public float max { get; set; }
public float night { get; set; }
public float eve { get; set; }
public float morn { get; set; }
public Temp()
{
day = 0;
min = 0;
max = 0;
night = 0;
eve = 0;
morn = 0;
}
}
public class Feels_Like
{
public float day { get; set; }
public float night { set; get; }
public float eve { set; get; }
public float morn { set; get; }
public Feels_Like()
{
day = 0;
night = 0;
eve = 0;
morn = 0;
}
}
}