1364d41be180dceb7e6f9482b72bc439e8e95697
[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                 this.deviceNumbers = deviceNumbers
14                 this.switches = []
15                 int id = 0
16                 for (int i = 0;i < deviceNumbers;i++) {
17                         switches.add(new Switches(sendEvent, id, "switch"+id.toString(), "off", "off"))
18                         id = id+1
19                 }
20         }
21
22         //By Apps
23         def on() {
24                 switches*.on()
25         }
26
27         def on(LinkedHashMap metaData) {
28                 def task = timers.runAfter(metaData["delay"]) {
29                         switches*.on()
30                 }
31         }
32
33         def off() {
34                 switches*.off()
35         }
36
37         def off(LinkedHashMap metaData) {
38                 def task = timers.runAfter(metaData["delay"]) {
39                         switches*.off()
40                 }
41         }
42
43         //By Model Checker
44         def setValue(LinkedHashMap eventDataMap) {
45                 switches[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
46                 sendEvent(eventDataMap)
47         }
48
49
50         def currentValue(String deviceFeature) {
51                 if (deviceNumbers == 1)
52                         switches[0].currentValue(deviceFeature)
53                 else
54                         switches*.currentValue(deviceFeature)
55         }
56
57         def latestValue(String deviceFeature) {
58                 if (deviceNumbers == 1)
59                         switches[0].latestValue(deviceFeature)
60                 else
61                         switches*.latestValue(deviceFeature)
62         }
63
64         def getAt(int ix) {
65                 switches[ix]
66         }
67 }