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