Repository navigation
Expand file tree
/
Copy pathSeca.java
More file actions
637 lines (549 loc) · 19.8 KB
/
Copy pathSeca.java
File metadata and controls
637 lines (549 loc) · 19.8 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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package seca;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.Vector;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@Path("/seca")
public class Seca {
Connection myConn = null;
Statement myStmt = null;
ResultSet myRs = null;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/tableNiveau")
public String tableNiveau() {
JSONArray ja = new JSONArray();
try {
Class.forName("com.mysql.jdbc.Driver");
// 1. Get a connection to database
myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/secadenord", "root" , "");
// 2. Create a statement
myStmt = myConn.createStatement();
// 3. Execute SQL query
//in production replace query with : SELECT IDBOITIER, DATETRAJET, CARBURANT, TEMPERATURE FROM niveau where DATETRAJET in (select max(DATETRAJET) from niveau WHERE IDBOITIER IN(953136,953125,953134,953143,953146,953147,953149,953150,953152,953153) group by IDBOITIER)
//or query=SELECT IDBOITIER, DATETRAJET, CARBURANT, TEMPERATURE FROM niveau where DATE(DATETRAJET)=DATE(NOW()) AND IDBOITIER IN(953136,953125,953134,953143,953146,953147,953149,953150,953152,953153) group by IDBOITIER)
myRs = myStmt.executeQuery("SELECT IDBOITIER, DATETRAJET, CARBURANT, TEMPERATURE FROM niveau where DATE(DATETRAJET)='2018-03-22' AND IDBOITIER IN(953136,953125,953134,953143,953146,953147,953149,953150,953152,953153) group by IDBOITIER");
// 4. Process the result set
while (myRs.next()) {
JSONObject Personobj = new JSONObject();
Personobj.put("IDBOITIER", myRs.getString("IDBOITIER"));
Personobj.put("DATETRAJET", myRs.getString("DATETRAJET"));
Personobj.put("CARBURANT", myRs.getString("CARBURANT"));
Personobj.put("TEMPERATURE", myRs.getString("TEMPERATURE"));
ja.add(Personobj);
}
return ja.toString();
}
catch (Exception exc) {
exc.printStackTrace();
}
finally {
if (myRs != null) {
try {
myRs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myStmt != null) {
try {
myStmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myConn != null) {
try {
myConn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return ja.toString();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/tableDebit")
public String tableDebit() {
JSONArray ja = new JSONArray();
try {
Class.forName("com.mysql.jdbc.Driver");
// 1. Get a connection to database
myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/secadenord", "root" , "");
// 2. Create a statement
myStmt = myConn.createStatement();
// 3. Execute SQL query
//in production we may change the query to : SELECT IDBOITIER, DATETRAJET, CARBURANT, TEMPERATURE, AN3, AN4 FROM niveau where DATETRAJET in (select max(DATETRAJET) from niveau WHERE IDBOITIER IN(953145,953148,953151,953155,953157) group by IDBOITIER)
//or SELECT IDBOITIER, DATETRAJET, CARBURANT, TEMPERATURE, AN3, AN4 FROM niveau where DATE(DATETRAJET) =DATE(NOW()) AND IDBOITIER IN (953145,953148,953151,953155,953157) goup by idboitier
myRs = myStmt.executeQuery("SELECT IDBOITIER, DATETRAJET, CARBURANT, TEMPERATURE, AN3, AN4 FROM niveau where DATE(DATETRAJET) ='2018-03-22' AND IDBOITIER IN (953145,953148,953151,953155,953157) GROUP BY idboitier");
// 4. Process the result set
while (myRs.next()) {
JSONObject Personobj = new JSONObject();
Personobj.put("IDBOITIER", myRs.getString("IDBOITIER"));
Personobj.put("DATETRAJET", myRs.getString("DATETRAJET"));
Personobj.put("CARBURANT", myRs.getString("CARBURANT"));
Personobj.put("TEMPERATURE", myRs.getString("TEMPERATURE"));
Personobj.put("AN3", myRs.getString("AN3"));
Personobj.put("AN4", myRs.getString("AN4"));
ja.add(Personobj);
}
return ja.toString();
}
catch (Exception exc) {
exc.printStackTrace();
}
finally {
if (myRs != null) {
try {
myRs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myStmt != null) {
try {
myStmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myConn != null) {
try {
myConn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return ja.toString();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/courbeNiveau/{IDBOITIER}/{DATEDEBUT}/{DATEFIN}")
public String courbeNiveau(@PathParam("IDBOITIER") String IDBOITIER, @PathParam("DATEDEBUT") String DATEDEBUT, @PathParam("DATEFIN") String DATEFIN) {
JSONArray ja = new JSONArray();
JSONArray ja2 = new JSONArray();
JSONObject obj = new JSONObject();
try {
Class.forName("com.mysql.jdbc.Driver");
// 1. Get a connection to database
myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/secadenord", "root" , "");
// 2. Create a statement
myStmt = myConn.createStatement();
// 3. Execute SQL query
myRs = myStmt.executeQuery("SELECT IDBOITIER, DATETRAJET, CARBURANT, TEMPERATURE FROM niveau where DATE(DATETRAJET) BETWEEN CAST('"+DATEDEBUT+"' AS DATE) AND CAST('"+DATEFIN+"' AS DATE) and idboitier="+IDBOITIER);
// 4. Process the result set
while (myRs.next()) {
JSONObject courbeObj = new JSONObject();
courbeObj.put("y", myRs.getDouble("CARBURANT"));
courbeObj.put("x", myRs.getString("DATETRAJET"));
JSONObject courbeObj2 = new JSONObject();
courbeObj2.put("y", myRs.getDouble("TEMPERATURE"));
courbeObj2.put("x", myRs.getString("DATETRAJET"));
ja2.add(courbeObj2);
ja.add(courbeObj);
}
obj.put("aval", ja);
obj.put("amont", ja2);
return obj.toString();
}
catch (Exception exc) {
exc.printStackTrace();
}
finally {
if (myRs != null) {
try {
myRs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myStmt != null) {
try {
myStmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myConn != null) {
try {
myConn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return obj.toString();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/courbeDebit/{IDBOITIER}/{DATEDEBUT}/{DATEFIN}")
public String courbeDebit(@PathParam("IDBOITIER") String IDBOITIER, @PathParam("DATEDEBUT") String DATEDEBUT, @PathParam("DATEFIN") String DATEFIN) {
JSONArray ja1 = new JSONArray();
JSONArray ja2 = new JSONArray();
JSONArray ja3 = new JSONArray();
JSONArray ja4 = new JSONArray();
JSONObject mainobj = new JSONObject();
try {
Class.forName("com.mysql.jdbc.Driver");
// 1. Get a connection to database
myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/secadenord", "root" , "");
// 2. Create a statement
myStmt = myConn.createStatement();
// 3. Execute SQL query
myRs = myStmt.executeQuery("SELECT IDBOITIER, DATETRAJET, CARBURANT, TEMPERATURE, AN3, AN4 FROM niveau where DATE(DATETRAJET) BETWEEN CAST('"+DATEDEBUT+"' AS DATE) AND CAST('"+DATEFIN+"' AS DATE) and idboitier="+IDBOITIER);
// 4. Process the result set
while (myRs.next()) {
JSONObject Pointobj1 = new JSONObject();
JSONObject Pointobj2 = new JSONObject();
JSONObject Pointobj3 = new JSONObject();
JSONObject Pointobj4 = new JSONObject();
Pointobj1.put("x", myRs.getString("DATETRAJET"));
Pointobj2.put("x", myRs.getString("DATETRAJET"));
Pointobj3.put("x", myRs.getString("DATETRAJET"));
Pointobj4.put("x", myRs.getString("DATETRAJET"));
Pointobj1.put("y", myRs.getDouble("CARBURANT"));
Pointobj2.put("y", myRs.getDouble("TEMPERATURE"));
Pointobj3.put("y", myRs.getDouble("AN3"));
Pointobj4.put("y", myRs.getDouble("AN4"));
ja1.add(Pointobj1);
ja2.add(Pointobj2);
ja3.add(Pointobj3);
ja4.add(Pointobj4);
}
mainobj.put("courb1", ja1);
mainobj.put("courb2", ja2);
mainobj.put("courb3", ja3);
mainobj.put("courb4", ja4);
return mainobj.toString();
}
catch (Exception exc) {
exc.printStackTrace();
}
finally {
if (myRs != null) {
try {
myRs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myStmt != null) {
try {
myStmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myConn != null) {
try {
myConn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return mainobj.toString();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/tableTotal/{DateMesure}")
public String tableTotal(@PathParam("DateMesure") String DateMesure) {
Connection myConn = null;
Statement myStmt = null;
ResultSet myRs = null;
JSONObject obj = new JSONObject();
JSONArray ja = new JSONArray();
String boitiers[]= {"953145","953148","953151","953155","953157"};
Vector<String> dates = new Vector<String>(), entrees1 = new Vector<String>(), entrees2 = new Vector<String>(), entrees3 = new Vector<String>(), entrees4 = new Vector<String>();
Vector<Double> total1 = new Vector<Double>(), total2 = new Vector<Double>(), total3 = new Vector<Double>(), total4 = new Vector<Double>();
//String idboitier="9531456";
int a=0;
try {
// 1. Get a connection to database
Class.forName("com.mysql.jdbc.Driver");
myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/secadenord", "root" , "");
// 2. Create a statement
myStmt = myConn.createStatement();
// 3. Execute SQL query
// in production change query to : SELECT IDBOITIER, DATETRAJET, CARBURANT, TEMPERATURE, AN3, AN4 FROM niveau where DATE(DATETRAJET)='"+DateMesure+"' and IDBOITIER IN(953145,953148,953151,953155,953157) ORDER BY IDBOITIER"
myRs = myStmt.executeQuery("SELECT IDBOITIER, DATETRAJET, CARBURANT, TEMPERATURE, AN3, AN4 FROM niveau where DATE(DATETRAJET) ='2018-03-22' and IDBOITIER IN(953145,953148,953151,953155,953157) ORDER BY IDBOITIER");
// 4. Process the result set
for (int k=0; k<5;k++) {
total1.add(k, 0.0);
total2.add(k, 0.0);
total3.add(k, 0.0);
total4.add(k, 0.0);
}
for(int j=0; j<5;j++) {
boolean verif=false;
while (myRs.next()) {
if(myRs.getString("IDBOITIER").equals(boitiers[j])) {
verif=true;
//System.out.println("i am inside first while \n");
//System.out.println("dates.size()= "+dates.size()+"\n");
dates.add(myRs.getString("DATETRAJET"));
//System.out.println("dates.elementAt(0)= "+dates.elementAt(0)+"\n");
entrees1.add(myRs.getString("CARBURANT"));
//System.out.println("entrees1.elementAt(0)= "+entrees1.elementAt(0)+"\n");
entrees2.add(myRs.getString("TEMPERATURE"));
entrees3.add(myRs.getString("AN3"));
entrees4.add(myRs.getString("AN4"));
}
else {
myRs.previous();
break;
}
}
if(verif) {
//System.out.println("dates.elementAt(0)= "+dates.elementAt(0)+" a= "+a+"\n");
JSONObject boitierObj = new JSONObject();
boitierObj.put("IDBOITIER", boitiers[j]);
boitierObj.put("total1", total(dates,entrees1));
boitierObj.put("total2", total(dates,entrees2));
boitierObj.put("total3", total(dates,entrees3));
boitierObj.put("total4", total(dates,entrees4));
ja.add(boitierObj);
a++;
}
else {
a++;}
dates.clear();
entrees1.clear();
entrees2.clear();
entrees3.clear();
entrees4.clear();
}
/*System.out.println("dates.size= "+total1.size()+"\n");
System.out.println("dates.size= "+total2.size()+"\n");
System.out.println("dates.size= "+total3.size()+"\n");
System.out.println("dates.size= "+total4.size()+"\n");*/
/*System.out.println("*******total1*****");
for(int i=0; i< total1.size();i++) {
//System.out.println(total1.get(i)+"\n");
//System.out.println("*******total2*****");
System.out.println(total3.get(i)+"\n");
System.out.println("*******total3*****");
System.out.println(total3.get(j)+"\n");
System.out.println("*******total4*****");
System.out.println(total4.get(j)+"\n");
}*/
return ja.toString();
}
catch (Exception exc) {
exc.printStackTrace();
}
finally {
if (myRs != null) {
try {
myRs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myStmt != null) {
try {
myStmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myConn != null) {
try {
myConn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return ja.toString();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/courbeTotal/{IDBOITIER}/{dateDebut}/{dateFin}")
public String courbeTotal(@PathParam("dateDebut") String dateDebut, @PathParam("dateFin") String dateFin, @PathParam("IDBOITIER") String IDBOITIER) {
Connection myConn = null;
Statement myStmt = null;
ResultSet myRs = null;
JSONObject obj = new JSONObject();
JSONArray labels = new JSONArray();
String datetrajet= "" ;
Vector<String> dates = new Vector<String>(), entrees1 = new Vector<String>(), entrees2 = new Vector<String>(), entrees3 = new Vector<String>(), entrees4 = new Vector<String>();
Vector<Double> total1 = new Vector<Double>(), total2 = new Vector<Double>(), total3 = new Vector<Double>(), total4 = new Vector<Double>();
//String idboitier="9531456";
int a=0;
try {
// 1. Get a connection to database
Class.forName("com.mysql.jdbc.Driver");
myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/secadenord", "root" , "");
// 2. Create a statement
myStmt = myConn.createStatement();
// 3. Execute SQL query
myRs = myStmt.executeQuery("SELECT IDBOITIER, DATETRAJET, CARBURANT, TEMPERATURE, AN3, AN4 FROM niveau where DATE(DATETRAJET) BETWEEN CAST('"+dateDebut+"' AS DATE) AND CAST('"+dateFin+"' AS DATE) and idboitier="+IDBOITIER+" ORDER BY datetrajet ASC");
// 4. Process the result set
while(myRs.next()) {
String parts[]= myRs.getString("DATETRAJET").split(" ");
datetrajet=parts[0];
dates.add(myRs.getString("DATETRAJET"));
//System.out.println("dates.elementAt(0)= "+dates.elementAt(0)+"\n");
entrees1.add(myRs.getString("CARBURANT"));
//System.out.println("entrees1.elementAt(0)= "+entrees1.elementAt(0)+"\n");
entrees2.add(myRs.getString("TEMPERATURE"));
entrees3.add(myRs.getString("AN3"));
entrees4.add(myRs.getString("AN4"));
break;
}
while(myRs.next()) {
while(myRs.next() && datetrajet.equals(myRs.getString("DATETRAJET").split(" ")[0])) {
dates.add(myRs.getString("DATETRAJET"));
//System.out.println("dates.elementAt(0)= "+dates.elementAt(0)+"\n");
entrees1.add(myRs.getString("CARBURANT"));
//System.out.println("entrees1.elementAt(0)= "+entrees1.elementAt(0)+"\n");
entrees2.add(myRs.getString("TEMPERATURE"));
entrees3.add(myRs.getString("AN3"));
entrees4.add(myRs.getString("AN4"));
}
System.out.println("datetrajet= "+datetrajet+"\n");
if(!datetrajet.equals(dateFin)) {
//System.out.println("i am inside if 1");
labels.add(datetrajet);
datetrajet=myRs.getString("DATETRAJET").split(" ")[0];
total1.add(a, total(dates,entrees1));
total2.add(a, total(dates,entrees2));
total3.add(a, total(dates,entrees3));
total4.add(a, total(dates,entrees4));
a++;
dates.clear();
entrees1.clear();
entrees2.clear();
entrees3.clear();
entrees4.clear();
myRs.previous();
}
else {
//System.out.println("i am inside if 2");
labels.add(datetrajet);
total1.add(a, total(dates,entrees1));
total2.add(a, total(dates,entrees2));
total3.add(a, total(dates,entrees3));
total4.add(a, total(dates,entrees4));
break;
}
}
obj.put("labels", labels);
obj.put("total1", total1);
obj.put("total2", total2);
obj.put("total3", total3);
obj.put("total4", total4);
return obj.toString();
}
catch (Exception exc) {
exc.printStackTrace();
}
finally {
if (myRs != null) {
try {
myRs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myStmt != null) {
try {
myStmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (myConn != null) {
try {
myConn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return obj.toString();
}
private static double total(Vector<String> dates, Vector<String> entrees) {
double s=0.0;
// System.out.println("i am in total function!");
//System.out.println("dates.size="+dates.size());
HashMap<String, String> hmap = new HashMap<String, String>();
for(int k=0;k<dates.size();k++) {
hmap.put(dates.get(k), entrees.get(k));
}
Map<String, String> map = new TreeMap<String, String>(hmap);
Set set2 = map.entrySet();
Iterator iterator2 = set2.iterator();
String xOld = "";
while(iterator2.hasNext()) {
Map.Entry me2 = (Map.Entry)iterator2.next();
xOld = (String) me2.getKey();
break;
}
while(iterator2.hasNext()) {
Map.Entry me2 = (Map.Entry)iterator2.next();
Object xNew = me2.getKey();
java.util.Date x1 = null;
java.util.Date x2 = null;
try {
x1 = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SS").parse((String) xOld);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
x2 = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SS").parse((String) xNew);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double valeur = Double.parseDouble( ((String) me2.getValue()).replace(",",".") );
s+=(x2.getTime()-x1.getTime())*valeur;
xOld=(String) xNew;
//System.out.println("valeur= "+valeur+"\n");
//System.out.println("(x1.getTime()-x2.getTime())= "+(x2.getTime()-x1.getTime())+"\n");
//System.out.println("x1= "+x1+"\t x2= "+x2+"\n");
// System.out.print(me2.getKey() + ": ");
//System.out.println(me2.getValue());
}
return s;
}
}