Adding path explorations for initializations.
[smartthings-infrastructure.git] / Switch / Switches.groovy
1 //Create a class for switch device
2 package Switch
3 import Timer.SimulatedTimer
4
5 //JPF's Verify API
6 import gov.nasa.jpf.vm.Verify
7
8 public class Switches {
9         int deviceNumbers       
10         List switches
11         def timers
12         def sendEvent
13
14         //If we have only one device
15         private String id = "switchID0"
16         private String label = "switch0"
17         private String displayName = "switch0"
18         private String switchState = "off"
19         private String currentSwitch = "off"
20         private int currentLevel = 50
21         private String switchLatestValue = "off"
22
23         Switches(Closure sendEvent, int deviceNumbers) {
24                 this.sendEvent = sendEvent
25                 this.timers = new SimulatedTimer()
26                 this.deviceNumbers = deviceNumbers
27                 this.switches = []
28                 
29                 def initLevel = Verify.getIntFromList(30, 50, 70)
30                 this.currentLevel = initLevel
31                 def init = Verify.getBoolean()
32                 if (init) {
33                         this.switchState = "off"
34                         this.currentSwitch = "off"
35                         this.switchLatestValue = "off"
36                 } else {
37                         this.switchState = "on"
38                         this.currentSwitch = "on"
39                         this.switchLatestValue = "on"
40                 }
41
42                 switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
43         }
44
45         //Methods for closures
46         def count(Closure Input) {
47                 switches.count(Input)
48         }
49         def size() {
50                 switches.size()
51         }
52         def each(Closure Input) {
53                 switches.each(Input)
54         }
55         def find(Closure Input) {
56                 switches.find(Input)
57         }
58         def collect(Closure Input) {
59                 switches.collect(Input)
60         }
61
62         //By Apps
63         def setLevel(int level) {
64                 switches[0].setLevel(level)
65                 currentLevel = level
66         }
67
68         def on() {
69                 switches[0].on()
70                 switchLatestValue = switchState
71                 switchState = "on"
72                 currentSwitch = "on"
73         }
74
75         def on(LinkedHashMap metaData) {
76                 def task = timers.runAfter(metaData["delay"]) {
77                         switches[0].on()
78                         switchLatestValue = switchState
79                         switchState = "on"
80                         currentSwitch = "on"
81                 }
82         }
83
84         def off() {
85                 switches[0].off()
86                 switchLatestValue = switchState
87                 switchState = "off"
88                 currentSwitch = "off"
89         }
90
91         def off(LinkedHashMap metaData) {
92                 def task = timers.runAfter(metaData["delay"]) {
93                         switches[0].off()
94                         switchLatestValue = switchState
95                         switchState = "off"
96                         currentSwitch = "off"
97                 }
98         }
99
100         //By Model Checker
101         def setValue(LinkedHashMap eventDataMap) {
102                 if (eventDataMap["value"] != switches[0].switchState) {
103                         switches[0].setValue(eventDataMap["value"])
104                         this.switchState = switches[0].switchState
105                         this.switchLatestValue = switches[0].switchLatestValue
106                         sendEvent(eventDataMap)
107                 }
108         }
109
110
111         def currentValue(String deviceFeature) {
112                 switches[0].currentValue(deviceFeature)
113         }
114
115         def latestValue(String deviceFeature) {
116                 switches[0].latestValue(deviceFeature)
117         }
118
119         def getAt(int ix) {
120                 switches[ix]
121         }
122 }