-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
532 lines (462 loc) · 14.5 KB
/
Controller.cpp
File metadata and controls
532 lines (462 loc) · 14.5 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*************************************************************************
Controller - description
-------------------
début : $DATE$
copyright : (C) $YEAR$ par $AUTHOR$
e-mail : $EMAIL$
*************************************************************************/
//---------- Réalisation de la classe <Controller> (fichier Controller.cpp) ------------
//---------------------------------------------------------------- INCLUDE
//-------------------------------------------------------- Include système
#include <iostream>
using namespace std;
//------------------------------------------------------ Include personnel
#include "Controller.h"
#include "Traitement.h"
#include "View.h"
#include "User.h"
#include "CoordGPS.h"
#include "Date.h"
#include "AirMeasurement.h"
#include "Sensor.h"
#include "GovernmentAgencyEmployee.h"
#include "PrivateIndividual.h"
#include "Provider.h"
//------------------------------------------------------------- Constantes
//----------------------------------------------------------------- PUBLIC
User *Controller::user;
//----------------------------------------------------- Méthodes publiques
// type Controller::Méthode ( liste des paramètres )
// Algorithme :
//
//{
//} //----- Fin de Méthode
void Controller::recupererSaisieMenu(int saisie)
// Algorithme :
//
{
if (GovernmentAgencyEmployee *u = dynamic_cast<GovernmentAgencyEmployee *>(user))
{
switch (saisie)
{
case 1:
{
CheckSensorFunctional();
break;
}
case 2:
{
CheckPrivateIndividual();
break;
}
case 3:
{
RankBySimilarity();
break;
}
case 4:
{
MesureMeanAirQuality();
break;
}
case 5:
{
MesureAirQuality();
break;
}
case 6:
{
ImpactAirCleaners();
break;
}
case 0:
{
delete user;
user = nullptr;
break;
}
default:
{
View::afficherErreur("Veuillez entrer un nombre valide.");
break;
}
}
}
else if (PrivateIndividual *u = dynamic_cast<PrivateIndividual *>(user))
{
switch (saisie)
{
case 1:
{
MesureMeanAirQuality();
break;
}
case 2:
{
RankBySimilarity();
break;
}
case 3:
{
MesureAirQuality();
break;
}
case 4:
{
ConsultPoints();
break;
}
case 0:
{
delete user;
user = nullptr;
break;
}
default:
{
View::afficherErreur("Veuillez entrer un nombre valide.");
break;
}
}
}
else if (Provider *u = dynamic_cast<Provider *>(user))
{
switch (saisie)
{
case 1:
{
ImpactAirCleaners();
break;
}
case 0:
{
delete user;
user = nullptr;
break;
}
default:
{
View::afficherErreur("Veuillez entrer un nombre valide.");
break;
}
}
}
chargerMenu();
return;
} //----- Fin de Méthode
void Controller::connexion(string login, string password)
// Algorithme :
//
{
string id = "A1"; // Fake id, same for everyone
// The user chooses which type of user he is
const int caseGovernment = 1;
const int casePrivate = 2;
const int caseProvider = 3;
int userType = View::entrerNombre("Enter the user (1: Government, 2: Private, 3: Provider)");
switch (userType)
{
case caseGovernment:
user = new GovernmentAgencyEmployee(id, login, password);
Controller::chargerMenu();
break;
case casePrivate:
user = new PrivateIndividual(id);
Controller::chargerMenu();
break;
case caseProvider:
user = new Provider(id);
Controller::chargerMenu();
break;
default:
View::afficherErreur("Veuillez entrer un nombre valide.");
View::MenuConnexion();
}
return;
} //----- Fin de Méthode
void Controller::chargerMenu()
// Algorithme :
//
{
if (!user)
{
View::MenuConnexion();
}
else
{
if (GovernmentAgencyEmployee *u = dynamic_cast<GovernmentAgencyEmployee *>(user))
{
View::MenuPrincipalGovernmentAgency();
}
else if (PrivateIndividual *u = dynamic_cast<PrivateIndividual *>(user))
{
View::MenuPrincipalPrive();
}
else if (Provider *u = dynamic_cast<Provider *>(user))
{
View::MenuPrincipalProvider();
}
}
return;
} //----- Fin de Méthode
void Controller::clean()
// Algorithme :
//
{
std::ofstream outFile("timing_results.txt", std::ios::trunc);
delete user;
} //----- Fin de ~Controller
//------------------------------------------------- Surcharge d'opérateurs
Controller &Controller::operator=(const Controller &unController)
// Algorithme :
//
{
} //----- Fin de operator =
//-------------------------------------------- Constructeurs - destructeur
Controller::Controller(const Controller &unController)
// Algorithme :
//
{
#ifdef MAP
cout << "Appel au constructeur de copie de <Controller>" << endl;
#endif
} //----- Fin de Controller (constructeur de copie)
Controller::Controller()
// Algorithme :
//
{
#ifdef MAP
cout << "Appel au constructeur de <Controller>" << endl;
#endif
user = nullptr;
} //----- Fin de Controller
Controller::~Controller()
// Algorithme :
//
{
#ifdef MAP
cout << "Appel au destructeur de <Controller>" << endl;
#endif
delete user;
} //----- Fin de ~Controller
//------------------------------------------------------------------ PRIVE
//----------------------------------------------------- Méthodes protégées
void Controller::MesureAirQuality()
// Algorithme :
//
{
CoordGPS *coordGPS = View::entrerCoord("Enter the GPS coordinates");
if ((abs(coordGPS->GetLat() > 90.0)) or (abs(coordGPS->GetLng() > 180)))
{
View::afficherErreur("The latitude must be between -90 and 90 and the longitude must be between -180 and 180");
return;
}
Date *date = View::entrerDate("Enter the date");
int an = date->GetAnnee();
int mois = date->GetMois();
int jour = date->GetJour();
int heure = date->GetHeure();
if ((mois < 0) or (mois > 12))
{
View::afficherErreur("Months must be between 1 and 12 included");
return;
}
if (jour < 0)
{
View::afficherErreur("Days must be positives");
return;
}
if ((heure < 0) or (heure > 23))
{
View::afficherErreur("Hours must be between 0 and 23 included");
return;
}
if (mois == 2)
{
if (((an % 4) == 0) and (jour > 29))
{
View::afficherErreur("February has only 29 days on bissextile years");
return;
}
if (((an % 4) != 0) and (jour > 28))
{
View::afficherErreur("February has only 28 days on not bissextile years");
return;
}
}
if ((mois % 2 == 1) and (jour > 31))
{
View::afficherErreur("Days must not exceed 31");
return;
}
if ((mois % 2 == 0) and (mois != 2) and (jour > 30))
{
View::afficherErreur("Days of some months must not exceed 30");
return;
}
std::chrono::_V2::system_clock::time_point start = std::chrono::high_resolution_clock::now();
AirMeasurement *airMeasurement = Traitement::calculateAirQualite(coordGPS, date);
std::chrono::_V2::system_clock::time_point end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
View::afficherTpsExec("calculateAirQualite", duration);
if (airMeasurement->GetO3() == -1 && airMeasurement->GetNO2() == -1 && airMeasurement->GetSO2() == -1 && airMeasurement->GetPM10() == -1)
{
View::afficherErreur("No data for the specified date.");
}
else
{
View::afficherMesureAirQuality(*airMeasurement);
}
}
void Controller::MesureMeanAirQuality()
// Algorithme :
//
{
CoordGPS *coordGPS = View::entrerCoord("Enter the GPS coordinates of the center of the area");
if ((abs(coordGPS->GetLat() > 90.0)) or (abs(coordGPS->GetLng() > 180)))
{
View::afficherErreur("The latitude must be between -90 and 90 and the longitude must be between -180 and 180");
return;
}
int radius = View::entrerNombre("Enter the radius (in km)");
if ((radius <= 0) or (radius > 200))
{
View::afficherErreur("The radius must be strictly positive and must not exceed 200");
return;
}
Date *dateBeginning = View::entrerDate("Enter the date 1 (beginning)");
int an1 = dateBeginning->GetAnnee();
int mois1 = dateBeginning->GetMois();
int jour1 = dateBeginning->GetJour();
int heure1 = dateBeginning->GetHeure();
Date *dateEnd = View::entrerDate("Enter the date 2 (end)");
int an2 = dateEnd->GetAnnee();
int mois2 = dateEnd->GetMois();
int jour2 = dateEnd->GetJour();
int heure2 = dateEnd->GetHeure();
if ((mois1 < 0) or (mois1 > 12) or (mois2 < 0) or (mois2 > 12))
{
View::afficherErreur("Months must be between 1 and 12 included");
return;
}
if ((jour1 < 0) or (jour2 < 0))
{
View::afficherErreur("Days must be positive");
return;
}
if ((heure1 < 0) or (heure1 > 23) or (heure2 < 0) or (heure2 > 23))
{
View::afficherErreur("Hours must be between 0 and 23 included");
return;
}
if (mois1 == 2)
{
if (((an1 % 4) == 0) and (jour1 > 29))
{
View::afficherErreur("February has only 29 days on bissextile years");
return;
}
if (((an1 % 4) != 0) and (jour1 > 28))
{
View::afficherErreur("February has only 28 days on not bissextile years");
return;
}
}
if (mois2 == 2)
{
if (((an2 % 4) == 0) and (jour2 > 29))
{
View::afficherErreur("February has only 29 days on bissextile years");
}
if (((an2 % 4) != 0) and (jour2 > 28))
{
View::afficherErreur("February has only 28 days on not bissextile years");
return;
}
}
if (((mois1 % 2 == 1) and (jour1 > 31)) or ((mois2 % 2 == 1) and (jour2 > 31)))
{
View::afficherErreur("Days must not exceed 31\r\n");
return;
}
if (((mois1 % 2 == 0) and (mois1 != 2) and (jour1 > 30)) or ((mois2 % 2 == 0) and (mois2 != 2) and (jour2 > 30)))
{
View::afficherErreur("Days of some months must not exceed 30\r\n");
return;
}
std::chrono::_V2::system_clock::time_point start = std::chrono::high_resolution_clock::now();
AirMeasurement *airMeasurement = Traitement::calculateMeanAirQualite(coordGPS, radius, dateBeginning, dateEnd);
std::chrono::_V2::system_clock::time_point end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
View::afficherTpsExec("calculateMeanAirQualite", duration);
if (airMeasurement->GetO3() == 0 && airMeasurement->GetNO2() == 0 && airMeasurement->GetPM10() == 0 && airMeasurement->GetSO2() == 0)
{
View::afficherErreur("No sensor in the specified area or no data for the specified dates.");
}
else
{
View::afficherMesureAirQuality(*airMeasurement);
}
}
void Controller::CheckSensorFunctional()
// Algorithme :
//
{
string sensorID = View::entrerString("Enter the sensor ID");
std::chrono::_V2::system_clock::time_point start;
std::chrono::_V2::system_clock::time_point end;
Sensor *sensor = Traitement::findSensorById(sensorID);
if (!sensor)
{
View::afficherErreur("The specified sensor doesn't exist.");
}
else
{
start = std::chrono::high_resolution_clock::now();
bool functional = Traitement::analyzeFunctionalState(sensor);
end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
View::afficherBool(functional);
View::afficherTpsExec("analyzeFunctionalState", duration);
}
}
void Controller::RankBySimilarity()
// Algorithme :
//
{
string sensorID = View::entrerString("Enter the sensor ID : \r\n");
Date *dateBeginning = View::entrerDate("Enter the date 1 (beginning) : \r\n");
Date *dateEnd = View::entrerDate("Enter the date 2 (end) : \r\n");
Sensor *sensor = Traitement::findSensorById(sensorID);
std::chrono::_V2::system_clock::time_point start = std::chrono::high_resolution_clock::now();
map<AirMeasurement, Sensor *> mapSensor = Traitement::rankingBySimilarity(sensor, dateBeginning, dateEnd);
std::chrono::_V2::system_clock::time_point end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
View::afficherTpsExec("analyzeFunctionalState", duration);
vector<Sensor> vectSensor;
// Utilisation de std::for_each pour copier les valeurs de la map dans le vecteur
for_each(mapSensor.begin(), mapSensor.end(),
[&vectSensor](const pair<AirMeasurement, Sensor *> &pair)
{
vectSensor.push_back(*pair.second);
});
View::afficherListe(vectSensor);
}
void Controller::ConsultPoints()
// Algorithme :
//
{
cout << "consult points" << endl; // temporaire
}
void Controller::ImpactAirCleaners()
// Algorithme :
//
{
cout << "impact cleaners" << endl; // temporaire
}
void Controller::CheckPrivateIndividual()
// Algorithme :
//
{
cout << "private individual fiability" << endl; // temporaire
}