Commit #8: New version of extractor with running the preferences method make things...
[smartthings-infrastructure.git] / Switch / Switches.groovy
1 //Create a class for switch device
2 package Switch
3 import Timer.SimulatedTimer
4
5 public class Switches {
6         int deviceNumbers       
7         List switches
8         def timers
9         def sendEvent
10
11         //If we have only one device
12         private int id = 40
13         private String label = "switch"
14         private String displayName = "switch"
15         private String switchState = "off"
16         private String currentSwitch = "off"
17         private int currentLevel = 50
18         private String switchLatestValue = "off"
19
20         Switches(Closure sendEvent, int deviceNumbers) {
21                 this.sendEvent = sendEvent
22                 this.timers = new SimulatedTimer()
23                 this.deviceNumbers = deviceNumbers
24                 this.switches = []
25                 for (int i = 0;i < deviceNumbers;i++) {
26                         switches.add(new Switch(sendEvent, i+40, label+i.toString(), displayName+i.toString(), this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
27                 }
28         }
29
30         //Methods for closures
31         def count(Closure Input) {
32                 switches.count(Input)
33         }
34         def size() {
35                 switches.size()
36         }
37         def each(Closure Input) {
38                 switches.each(Input)
39         }
40
41         //By Apps
42         def setLevel(int level) {
43                 switches*.setLevel(level)
44         }
45
46         def on() {
47                 switches*.on()
48         }
49
50         def on(LinkedHashMap metaData) {
51                 def task = timers.runAfter(metaData["delay"]) {
52                         switches*.on()
53                 }
54         }
55
56         def off() {
57                 switches*.off()
58         }
59
60         def off(LinkedHashMap metaData) {
61                 def task = timers.runAfter(metaData["delay"]) {
62                         switches*.off()
63                 }
64         }
65
66         //By Model Checker
67         def setValue(LinkedHashMap eventDataMap) {
68                 switches[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
69                 if (deviceNumbers == 1)
70                         this.switchState = switches[eventDataMap["deviceId"]].switchState
71                         this.switchLatestValue = switches[eventDataMap["deviceId"]].switchLatestValue
72                 sendEvent(eventDataMap)
73         }
74
75
76         def currentValue(String deviceFeature) {
77                 if (deviceNumbers == 1)
78                         switches[0].currentValue(deviceFeature)
79                 else
80                         switches*.currentValue(deviceFeature)
81         }
82
83         def latestValue(String deviceFeature) {
84                 if (deviceNumbers == 1)
85                         switches[0].latestValue(deviceFeature)
86                 else
87                         switches*.latestValue(deviceFeature)
88         }
89
90         def getAt(int ix) {
91                 switches[ix]
92         }
93 }