Commit #5
[smartthings-infrastructure.git] / Switch / Switching.groovy
1 //Create a class for switch device
2 package Switch
3
4 public class Switching{
5         int deviceNumbers       
6         List switches
7         def timers
8         def sendEvent
9
10         Switching(Closure sendEvent, int deviceNumbers) {
11                 this.sendEvent = sendEvent
12                 this.timers = new Timer()
13                 timers.cancel() //Timer is ready to use
14                 this.deviceNumbers = deviceNumbers
15                 this.switches = []
16                 if (deviceNumbers == 1) {
17                         switches = [new Switches(sendEvent, 0, "switch0", "off", "off")]
18                 } else if (deviceNumbers == 2) {
19                         switches = [new Switches(sendEvent, 0, "switch0", "off", "off"), new Switches(sendEvent, 1, "switch1", "off", "off")]
20                 } else if (deviceNumbers == 3) {
21                         switches = [new Switches(sendEvent, 0, "switch0", "off", "off"), new Switches(sendEvent, 1, "switch1", "off", "off")
22                                    ,new Switches(sendEvent, 2, "switch2", "off", "off")]
23                 }
24                 println("salam")
25         }
26
27         //By Apps
28         def on() {
29                 switches*.on()
30         }
31
32         def on(LinkedHashMap metaData) {
33                 def task = timers.runAfter(metaData["delay"]) {
34                         switches*.on()
35                 }
36         }
37
38         def off() {
39                 switches*.off()
40         }
41
42         def off(LinkedHashMap metaData) {
43                 def task = timers.runAfter(metaData["delay"]) {
44                         switches*.off()
45                 }
46         }
47
48         //By Model Checker
49         def setValue(LinkedHashMap eventDataMap) {
50                 switches[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
51                 sendEvent(eventDataMap)
52         }
53
54
55         def currentValue(String deviceFeature) {
56                 if (deviceNumbers == 1)
57                         switches[0].currentValue(deviceFeature)
58                 else
59                         switches*.currentValue(deviceFeature)
60         }
61
62         def latestValue(String deviceFeature) {
63                 if (deviceNumbers == 1)
64                         switches[0].latestValue(deviceFeature)
65                 else
66                         switches*.latestValue(deviceFeature)
67         }
68
69         def getAt(int ix) {
70                 switches[ix]
71         }
72 }