bc9140cc4344c844f06a23bdf562ee654790c41f
[smartthings-infrastructure.git] / SwitchLevel / SwitchLevels.groovy
1 //Create a class for switch level
2 package SwitchLevel
3 import Timer.SimulatedTimer
4
5 public class SwitchLevels {
6         int deviceNumbers       
7         List switchLevels
8         def timers
9         def sendEvent
10
11         //If we have only one device
12         private String id = "switchLevelID0"
13         private String label = "switchLevel0"
14         private String displayName = "switchLevel0"
15         private int level = 50
16         private int currentLevel = 50
17         private int rate = 50
18         private int currentRate = 50
19         private int hue = 30
20         private int currentHue = 30
21         private int saturation = 70
22         private String switchState = "on"
23         private String currentSwitch = "on"
24         private String switchLatestValue = "on"
25
26         SwitchLevels(Closure sendEvent, int deviceNumbers, boolean init) {
27                 this.sendEvent = sendEvent
28                 this.timers = new SimulatedTimer()
29                 this.deviceNumbers = deviceNumbers
30                 this.switchLevels = []
31
32                 if (init) {
33                         this.level = 50
34                         this.currentLevel = 50
35                         this.rate = 50
36                         this.currentRate = 50
37                         this.hue = 30
38                         this.currentHue = 30
39                         this.saturation = 70
40                         this.switchState = "off"
41                         this.currentSwitch = "off"
42                         this.switchLatestValue = "off"
43                 } else {
44                         this.level = 60
45                         this.currentLevel = 60
46                         this.rate = 60
47                         this.currentRate = 60
48                         this.hue = 50
49                         this.currentHue = 50
50                         this.saturation = 90
51                         this.switchState = "on"
52                         this.currentSwitch = "on"
53                         this.switchLatestValue = "on"
54                 }
55                 switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.hue, this.saturation, this.switchState, this.switchLatestValue))
56         }
57
58         //Methods for closures
59         def count(Closure Input) {
60                 switchLevels.count(Input)
61         }
62         def size() {
63                 switchLevels.size()
64         }
65         def each(Closure Input) {
66                 switchLevels.each(Input)
67         }
68         def find(Closure Input) {
69                 switchLevels.find(Input)
70         }
71         def sort(Closure Input) {
72                 switchLevels.sort(Input)
73         }
74         def collect(Closure Input) {
75                 switchLevels.collect(Input)
76         }
77
78         //By Apps
79         def setColor(LinkedHashMap metaData) {
80                 if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
81                         this.level = metaData["level"]
82                         this.currentLevel = metaData["level"]
83                         this.rate = metaData["level"]
84                         this.currentRate = metaData["level"]
85                         this.hue = metaData["hue"]
86                         this.currentHue = metaData["hue"]
87                         this.saturation = metaData["saturation"]
88                         switchLevels[0].setColor(metaData)
89                 }
90         }
91         
92         def setLevel(String level) {
93                 def newLevel = level.toInteger()
94                 setLevel(newLevel)
95         }
96
97         def setLevel(int level) {
98                 if (this.level != level) {
99                         switchLevels[0].setLevel(level)
100                         this.level = level
101                         this.currentLevel = level
102                         this.rate = level
103                         this.currentRate = level
104                 }
105         }
106         
107         def setLevel(long level) {
108                 if (this.level != level) {
109                         switchLevels[0].setLevel(level)
110                         this.level = level
111                         this.rate = level
112                         this.currentLevel = level
113                         this.currentRate = level
114                 }
115         }
116
117         def on() {
118                 switchLatestValue = "on"
119                 switchState = "on"
120                 currentSwitch = "on"
121                 switchLevels[0].on()
122         }
123
124         def on(LinkedHashMap metaData) {
125                 def task = timers.runAfter(metaData["delay"]) {
126                         switchLatestValue = "on"
127                         switchState = "on"
128                         currentSwitch = "on"
129                         switchLevels[0].on()
130                 }
131         }
132
133         def off() {
134                 switchLatestValue = "off"
135                 switchState = "off"
136                 currentSwitch = "off"
137                 switchLevels[0].off()
138         }
139
140         def off(LinkedHashMap metaData) {
141                 def task = timers.runAfter(metaData["delay"]) {
142                         switchLatestValue = "off"
143                         switchState = "off"
144                         currentSwitch = "off"
145                         switchLevels[0].off()
146                 }
147         }
148
149         //By Model Checker
150         def setValue(LinkedHashMap eventDataMap) {
151                 if (eventDataMap["name"] == "switch") {
152                         if (eventDataMap["value"] != switchLevels[0].switchState) {
153                                 this.switchState = eventDataMap["value"]
154                                 this.switchLatestValue = eventDataMap["value"]
155                                 this.currentSwitch = eventDataMap["value"]
156                                 switchLevels[0].setValue(eventDataMap["value"], "switch")
157                                 sendEvent(eventDataMap)
158                         }
159                 } else if (eventDataMap["name"] == "level") {
160                         if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
161                                 this.level = eventDataMap["value"].toInteger()
162                                 this.currentLevel = eventDataMap["value"].toInteger()
163                                 this.currentRate = eventDataMap["value"].toInteger()
164                                 this.rate = eventDataMap["value"].toInteger()
165                                 switchLevels[0].setValue(eventDataMap["value"], "level")
166                                 sendEvent(eventDataMap)
167                         }
168                 }
169         }
170
171         def currentValue(String deviceFeature) {
172                 switchLevels[0].currentValue(deviceFeature)
173         }
174         
175         def latestValue(String deviceFeature) {
176                 switchLevels[0].latestValue(deviceFeature)
177         }
178         
179
180         def getAt(int ix) {
181                 switchLevels[ix]
182         }
183 }