-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBellProfile.java
499 lines (419 loc) · 14.1 KB
/
BellProfile.java
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
package intelliBell;
import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Timer;
/**
* This class stores all the details pertaining to the ringing of the bells
* These details include start and end of academic year, end semester exams and entrance exams
* for both the semesters as well the timings to ring for each of them.
* The dates of holidays for the year are stored and on those days bell(s) will not ring.
* During the academic year bell(s) will not ring on sundays, but if an exam is scheduled on a sunday then
* bell(s) ring(s) on that day.
* These details are given by the user through the CreateNewProfile class and are stored in object of this
* BellProfile type. It implements the interface Serializable for storing the objects of this class in a file.
* Every time this program is run with a profile name, a file in that name is read which contains the object
* of this class with the required information to be used for ringing of the bell(s).
* @author varshaneya
* @see Serializable
* @see CreateNewProfile
*/
public class BellProfile implements Serializable{
//serial`VersionID for serialisation
private static final long serialVersionUID = -905136585127639302L;
//name of the profile
private String name;
//Calendar arrays to store the start and end dates
private Calendar academicYrStart[];
private Calendar academicYrEnd[];
private Calendar endSemStart[];
private Calendar endSemEnd[];
private Calendar entranceExamStart;
private Calendar entranceExamEnd;
//ArrayList to store a list of holidays
private ArrayList<GregorianCalendar> holidays;
//ArrayList to store a list of IP addresses of Arduino devices attached to the bells
private ArrayList<String> ip;
//ArrayList to store list different ringing timings
private ArrayList<String> academicYrTimings;
private ArrayList<String> academicYrDuration;
private ArrayList<String> endSemTimings;
private ArrayList<String> endSemDuration;
private ArrayList<String> entraceExamTimings;
private ArrayList<String> entraceExamDuration;
//converts Calendar entry to dd-MM-yyyy format
private SimpleDateFormat sdf;
//stores the current ringing profile whether exam day, academic year or no ring
private int ringStatus;
//contains duration and timings of the current ringing status
private ArrayList<String> timings = null;
private ArrayList<String> duration = null;
//total number of timing slots in the above ringing day
private int totalTimeSlots;
//array of Timer objects one per IP for scheduling a ring through SendMessage class which extends TimerTask
private Timer timer[];
//index of the next timing to be rung, in the ArrayList timings
private int nextSlot;
//static and final variables which stand for a particular part of the year
public static final int oddSem=0;
public static final int evenSem=1;
public static final int entranceTime = 1;
public static final int endSemTime = 2;
public static final int academicYrTime = 3;
/**
* Constructor to initialise and allocate memory for various class members
*/
public BellProfile() {
academicYrStart=new Calendar[2];
academicYrEnd=new Calendar[2];
endSemStart=new Calendar[2];
endSemEnd=new Calendar[2];
ip = new ArrayList<String>();
endSemTimings = new ArrayList<String>();
endSemDuration = new ArrayList<String>();
entraceExamTimings = new ArrayList<String>();
entraceExamDuration = new ArrayList<String>();
academicYrTimings = new ArrayList<String>();
academicYrDuration = new ArrayList<String>();
holidays = new ArrayList<GregorianCalendar>();
sdf = new SimpleDateFormat("dd-MM-yyyy");
name = new String();
}
/**
* Sets the name of the profile
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns the name of the current profile
* @return name
*/
public String getName() {
return this.name;
}
/**
* Checks holidays class member to see if today is listed as holiday or not.
* @param cal1
* @return true if today is a holiday, false if today is not a holiday
*/
public boolean isHoliday(Calendar cal1) {
for(Calendar cal2:holidays)
if((cal1.get(Calendar.YEAR)==cal2.get(Calendar.YEAR))&&(cal1.get(Calendar.MONTH)== cal2.get(Calendar.MONTH))&&(cal1.get(Calendar.DAY_OF_MONTH)== cal2.get(Calendar.DAY_OF_MONTH)))
return true;
return false;
}
/**
* Checks which ring status pertains to today and assigns timings and duration class members
* with the respective timings and duration lists.
* Ring status corresponds to
* 0 = Don't ring today
* 1 = Entrance exam
* 2 = End semester exam
* 3 = academic year (Should not ring on sundays and holidays during academic year)
* @return ringStatus
*/
public int ringToday() {
Calendar cal;
try {
cal = TimeSync.getInternetTime();
} catch (IOException e) {
cal = Calendar.getInstance();
}
if(entranceExamStart.before(cal) && entranceExamEnd.after(cal)) {
ringStatus= entranceTime;
timings= entraceExamTimings;
duration=entraceExamDuration;
totalTimeSlots=entraceExamTimings.size();
}
else if((endSemStart[oddSem].before(cal)&&endSemEnd[oddSem].after(cal))||(endSemStart[evenSem].before(cal)&&endSemEnd[evenSem].after(cal))) {
ringStatus= endSemTime;
timings = endSemTimings;
duration=endSemDuration;
totalTimeSlots=endSemTimings.size();
}
else if((academicYrStart[oddSem].before(cal)&&academicYrEnd[oddSem].after(cal))||(academicYrStart[evenSem].before(cal)&&academicYrEnd[evenSem].after(cal))) {
ringStatus= academicYrTime;
timings = academicYrTimings;
duration=academicYrDuration;
totalTimeSlots=academicYrTimings.size();
}
else
ringStatus=0;
if(isHoliday(cal))
ringStatus= 0;
return ringStatus;
}
/**
* @return totalTimeSlots class member which contains the number of rings to be rung.
*/
public int getTotalTimeSlots() {
return totalTimeSlots;
}
/**
* With respect to the current hour and minute, it calculates number of ringing slots passed and should
* not be scheduled to ring.
* @param nowHr
* @param nowMin
* @return number of ringing slots passed
*/
public int calTimeSlotsPassed(int nowHr,int nowMin)
{
int timeSlotsPassed = 0;
int hr,min;
for(String time:timings)
{
hr = Integer.parseInt(time.substring(0,time.indexOf(":")));
min = Integer.parseInt(time.substring(time.indexOf(":")+1));
if((nowHr>hr) || (nowHr == hr && nowMin > min))
++timeSlotsPassed;
}
return timeSlotsPassed;
}
/**
* Calculates the delay in ringing of the bells for the subsequent timings and stores them in array delay
* with respect to the current time.
* @param delay[]
* @param nowHr
* @param nowMin
* @param nowSec
* @param nextSlot
*/
public void calTime2Sleep(int[] delay,int nowHr,int nowMin,int nowSec,int nextSlot)
{
int nextHr,nextMin;
int current,upcoming;
for(int i=nextSlot,j=0;i<totalTimeSlots;i++,j++) {
nextHr = Integer.parseInt(timings.get(i).substring(0,timings.get(i).indexOf(":")));
nextMin = Integer.parseInt(timings.get(i).substring(timings.get(i).indexOf(":")+1));
current = (nowHr*3600*1000)+(nowMin*60*1000);//+(nowSec*1000);
upcoming = (nextHr*3600*1000)+(nextMin*60*1000);
if (current < upcoming)
delay[j]= (upcoming-current) - (nowSec*1000);
else if(current == upcoming)
delay[j] = 0;
}
}
/**
* Invokes the schedule method of each of timer object corresponding to a particular IP, and
* schedules the future rings for each of them.
* An array of timer objects is created by createTimer() method.
* @param delay[]
* @param tmpNextSlot
*/
public void scheduleRing(int[] delay,int tmpNextSlot)
{
createTimer();
nextSlot = tmpNextSlot;
for(int ring:delay) {
for(String s:ip)
timer[ip.indexOf(s)].schedule(new SendMessage(s,duration.get(tmpNextSlot)),ring);
++tmpNextSlot;
}
}
/**
* Delays the already scheduled ringing by offset minutes.
* @param offset
*/
public void rescheduleRing(int offset) {
int status = ringToday();
int nowTime[]=new int[3];
if(status !=0 && !(status == BellProfile.academicYrTime && TimeSync.getDay() == Calendar.SUNDAY)) {
TimeSync.getTime(nowTime);
try {
Thread.sleep((60-nowTime[2]+2)*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
totalTimeSlots = getTotalTimeSlots();
nextSlot = calTimeSlotsPassed(nowTime[0],nowTime[1]);
if(nextSlot != totalTimeSlots) {
int delay[] = new int[totalTimeSlots-nextSlot];
calTime2Sleep(delay,nowTime[0],nowTime[1],nowTime[2],nextSlot);
for(int i=0;i<delay.length;i++)
delay[i]+=(offset*60*1000);
for(int i=0;i<ip.size();i++) {
timer[i].cancel();
timer[i].purge();
}
scheduleRing(delay,nextSlot);
}
}
}
/**
*
* @return total number of bells to ring.
*/
public int getNoOfBells() {
return ip.size();
}
/**
*
* @return ArrayList containing IP addresses of the bells.
*/
public ArrayList<String> getIP() {
return ip;
}
/**
* Adds a new IP address to class member ip.
* @param IP
*/
public void addIP(String IP){
ip.add(IP);
}
/**
*
* @return start of entrance examination as String object.
*/
public String getEntranceExamStart() {
return sdf.format(entranceExamStart.getTime()) ;
}
/**
*
* @return end of entrance examination as String object.
*/
public String getEntranceExamEnd() {
return sdf.format(entranceExamEnd.getTime()) ;
}
/**
*
* @param sem
* @return start of end semester examination as String object when semester is given as input.
*/
public String getEndSemStart(int sem) {
return sdf.format(endSemStart[sem].getTime()) ;
}
/**
*
* @param sem
* @return end of end semester examination as String object when semester is given as input.
*/
public String getEndSemEnd(int sem) {
return sdf.format(endSemEnd[sem].getTime()) ;
}
/**
*
* @param sem
* @return start of academic year as String object when semester is given as input.
*/
public String getAcademicYrStart(int sem) {
return sdf.format(academicYrStart[sem].getTime()) ;
}
/**
*
* @param sem
* @return end of academic year as String object when semester is given as input.
*/
public String getAcademicYrEnd(int sem) {
return sdf.format(academicYrEnd[sem].getTime()) ;
}
/**
* Adds ringing time and ringing duration for end semester exams.
* @param hr
* @param min
* @param duration
*/
public void addEndSemTimings(Integer hr,Integer min,String duration) {
endSemTimings.add(hr.toString()+":"+min.toString());
endSemDuration.add(duration);
}
/**
* Adds ringing time and ringing duration for entrance exams.
* @param hr
* @param min
* @param duration
*/
public void addEntraceExamTimings(Integer hr,Integer min,String duration) {
entraceExamTimings.add(hr.toString()+":"+min.toString());
entraceExamDuration.add(duration);
}
/**
* Adds ringing time and ringing duration for academic year.
* @param hr
* @param min
* @param duration
*/
public void addAcademicYrTimings(Integer hr,Integer min,String duration) {
academicYrTimings.add(hr.toString()+":"+min.toString());
academicYrDuration.add(duration);
}
/**
* Adds holidays in an academic year.
* @param day
* @param month
* @param year
*/
public void addHolidays(int day,int month,int year) {
holidays.add(new GregorianCalendar(year,month,day));
}
/**
* Adds start of academic year for a particular semester denoted by integer "sem".
* @param day
* @param month
* @param year
* @param sem
*/
public void addAcademicYrStart(int day,int month,int year,int sem) {
academicYrStart[sem]=new GregorianCalendar(year, month, day,0,0,0);
}
/**
* Adds end of academic year for a particular semester denoted by integer "sem".
* @param day
* @param month
* @param year
* @param sem
*/
public void addAcademicYrEnd(int day,int month,int year,int sem) {
academicYrEnd[sem]=new GregorianCalendar(year, month, day,23,59,59);
}
/**
* Adds start of end semester exams for a particular semester denoted by integer "sem".
* @param day
* @param month
* @param year
* @param sem
*/
public void addEndSemStart(int day,int month,int year,int sem) {
endSemStart[sem]=new GregorianCalendar(year, month, day,0,0,0);
}
/**
* Adds end of end semester exams for a particular semester denoted by integer "sem".
* @param day
* @param month
* @param year
* @param sem
*/
public void addEndSemEnd(int day,int month,int year,int sem) {
endSemEnd[sem]=new GregorianCalendar(year, month, day,23,59,59);
}
/**
* Adds start of entrance exams for a year.
* @param day
* @param month
* @param year
*/
public void addEntranceExamStart(int day,int month,int year) {
entranceExamStart=new GregorianCalendar(year, month, day,0,0,0);
}
/**
* Adds end of entrance exams for a year.
* @param day
* @param month
* @param year
*/
public void addEntranceExamEnd(int day,int month,int year) {
entranceExamEnd=new GregorianCalendar(year, month, day,23,59,59);
}
/**
* Creates an array of Timer objects with size of array equal to number of bells.
*/
public void createTimer() {
timer = new Timer[getNoOfBells()];
for(int i=0;i<ip.size();i++)
timer[i]=new Timer(ip.get(i));
}
}