Commit #3
[smartthings-infrastructure.git] / Switch / switching.groovy
1 //Create a class for switch device
2 package Switch
3
4 public class switching{
5         List switches
6         int count
7         def Timers
8
9         switching(int count) {
10                 this.Timers = new Timer()
11                 this.count = count
12                 if (count == 1) {
13                         switches = [new switches(0, "switch0", "off", "off")]
14                 } else if (count == 2) {
15                         switches = [new switches(0, "switch0", "off", "off"),new switches(1, "switch1", "off", "off")]
16                 } else if (count == 3) {
17                         switches = [new switches(0, "switch0", "off", "off"),new switches(1, "switch1", "off", "off"),new switches(2, "switch2", "off", "off")]
18                 }
19         }
20
21         def on() {
22                 if (count == 1) {
23                         switches[0].on()
24                 } else {
25                         switches*.on()
26                 }
27         }
28
29         def on(LinkedHashMap LHM) {
30                 if (count == 1) {
31                         def task = Timers.runAfter(LHM["delay"]) {
32                                 switches[0].on()
33                         }
34                 } else {
35                         def task = Timers.runAfter(LHM["delay"]) {
36                                 switches*.on()
37                         }
38                 }
39         }
40
41         def off() {
42                 if (count == 1) {
43                         switches[0].off()       
44                 } else {
45                         switches*.off()
46                 }
47         }
48
49         def off(LinkedHashMap LHM) {
50                 if (count == 1) {
51                         def task = Timers.runAfter(LHM["delay"]) {
52                                 switches[0].off()
53                         }
54                 } else {
55                         def task = Timers.runAfter(LHM["delay"]) {
56                                 switches*.off()
57                         }
58                 }
59         }
60
61         def currentValue(String S) {
62                 if (count == 1) {
63                         switches[0].currentValue(S)
64                 } else {
65                         switches*.currentValue(S)
66                 }
67         }
68
69         def latestValue(String S) {
70                 if (count == 1) {
71                         switches[0].latestValue(S)
72                 } else {
73                         switches*.latestValue(S)
74                 }
75         }
76
77         def getAt(int ix) {
78                 switches[ix]
79         }
80 }