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