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