3981f5466b6bc0bf373f42dbc6194a740c63f9cc
[smartthings-infrastructure.git] / Methods / schedule.groovy
1 /////////////////////////////////////////////////////////////////////
2 ////schedule(time, nameOfFunction as String)
3 def schedule(String time, String nameOfFunction) {
4         def _inputTime = time.split(':')
5         Date date = new Date()  
6         def _currentTime = date.format("HH:mm:ss").split(':')
7
8         //Convert input time and current time to minutes
9         def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60
10         def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2])
11         def delay
12
13         if (inputTime < currentTime) {
14                 delay = 24*60*60-inputTime+currentTime
15         } else {
16                 delay = inputTime-currentTime
17         }
18
19         timersFuncList.add(nameOfFunction)
20         timersList.add(new SimulatedTimer())
21         def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*1000*0) {
22                 "$nameOfFunction"()
23         }
24 }
25 ////schedule(time, nameOfFunction as Closure)
26 def schedule(String time, Closure nameOfFunction) {
27         def _inputTime = time.split(':')
28         Date date = new Date()  
29         def _currentTime = date.format("HH:mm:ss").split(':')
30
31         //Convert input time and current time to minutes
32         def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60
33         def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2])
34         def delay
35
36         if (inputTime < currentTime) {
37                 delay = 24*60*60-inputTime+currentTime
38         } else {
39                 delay = inputTime-currentTime
40         }
41
42         if (timersFuncList.contains(nameOfFunction)) {
43                 timersList[timersFuncList.indexOf(nameOfFunction)].cancel()
44                 def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds*0, nameOfFunction)
45         } else {
46                 timersFuncList.add(nameOfFunction)
47                 timersList.add(new SimulatedTimer())
48                 def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds*0, nameOfFunction)
49         }
50 }