e6ac8646571f6c4580146f0cb8a2fb08f22339d1
[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 String id = "switchID0"
13         private String label = "switch0"
14         private String displayName = "switch0"
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, boolean init) {
21                 this.sendEvent = sendEvent
22                 this.timers = new SimulatedTimer()
23                 this.deviceNumbers = deviceNumbers
24                 this.switches = []
25
26                 if (init) {
27                         this.switchState = "off"
28                         this.currentSwitch = "off"
29                         this.switchLatestValue = "off"
30                         this.currentLevel = 50
31                 } else {
32                         this.switchState = "on"
33                         this.currentSwitch = "on"
34                         this.switchLatestValue = "on"
35                         this.currentLevel = 60
36                 }
37                 switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
38         }
39
40         //Methods for closures
41         def count(Closure Input) {
42                 switches.count(Input)
43         }
44         def size() {
45                 switches.size()
46         }
47         def each(Closure Input) {
48                 switches.each(Input)
49         }
50         def find(Closure Input) {
51                 switches.find(Input)
52         }
53         def sort(Closure Input) {
54                 switches.sort(Input)
55         }
56         def collect(Closure Input) {
57                 switches.collect(Input)
58         }
59
60         //By Apps
61         def setLevel(int level) {
62                 currentLevel = level
63                 switches[0].setLevel(level)
64         }
65
66         def on() {
67                 switchLatestValue = "on"
68                 switchState = "on"
69                 currentSwitch = "on"
70                 switches[0].on()
71         }
72
73         def on(LinkedHashMap metaData) {
74                 def task = timers.runAfter(metaData["delay"]) {
75                         switchLatestValue = "on"
76                         switchState = "on"
77                         currentSwitch = "on"
78                         switches[0].on()
79                 }
80         }
81
82         def off() {
83                 switchLatestValue = "off"
84                 switchState = "off"
85                 currentSwitch = "off"
86                 switches[0].off()
87         }
88
89         def off(LinkedHashMap metaData) {
90                 def task = timers.runAfter(metaData["delay"]) {
91                         switchLatestValue = "off"
92                         switchState = "off"
93                         currentSwitch = "off"
94                         switches[0].off()
95                 }
96         }
97
98         //By Model Checker
99         def setValue(LinkedHashMap eventDataMap) {
100                 if (eventDataMap["value"] != switches[0].switchState) {
101                         this.switchState = eventDataMap["value"]
102                         this.switchLatestValue = eventDataMap["value"]
103                         switches[0].setValue(eventDataMap["value"])
104                         sendEvent(eventDataMap)
105                 }
106         }
107
108
109         def currentValue(String deviceFeature) {
110                 switches[0].currentValue(deviceFeature)
111         }
112
113         def latestValue(String deviceFeature) {
114                 switches[0].latestValue(deviceFeature)
115         }
116
117         def getAt(int ix) {
118                 switches[ix]
119         }
120 }