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