Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[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                 def initLevel = Verify.getIntFromList(30, 50, 70)
43                 this.currentLevel = initLevel
44                 def init = Verify.getBoolean()
45                 if (init) {
46                         this.switchState = "off"
47                         this.currentSwitch = "off"
48                         this.switchLatestValue = "off"
49                 } else {
50                         this.switchState = "on"
51                         this.currentSwitch = "on"
52                         this.switchLatestValue = "on"
53                 }
54
55                 switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
56         }
57
58         //Methods for closures
59         def count(Closure Input) {
60                 switches.count(Input)
61         }
62         def size() {
63                 switches.size()
64         }
65         def each(Closure Input) {
66                 switches.each(Input)
67         }
68         def find(Closure Input) {
69                 switches.find(Input)
70         }
71         def collect(Closure Input) {
72                 switches.collect(Input)
73         }
74
75         //By Apps
76         def setLevel(int level) {
77                 switches[0].setLevel(level)
78                 currentLevel = level
79         }
80
81         def on() {
82                 switches[0].on()
83                 switchLatestValue = switchState
84                 switchState = "on"
85                 currentSwitch = "on"
86         }
87
88         def on(LinkedHashMap metaData) {
89                 def task = timers.runAfter(metaData["delay"]) {
90                         switches[0].on()
91                         switchLatestValue = switchState
92                         switchState = "on"
93                         currentSwitch = "on"
94                 }
95         }
96
97         def off() {
98                 switches[0].off()
99                 switchLatestValue = switchState
100                 switchState = "off"
101                 currentSwitch = "off"
102         }
103
104         def off(LinkedHashMap metaData) {
105                 def task = timers.runAfter(metaData["delay"]) {
106                         switches[0].off()
107                         switchLatestValue = switchState
108                         switchState = "off"
109                         currentSwitch = "off"
110                 }
111         }
112
113         //By Model Checker
114         def setValue(LinkedHashMap eventDataMap) {
115                 if (eventDataMap["value"] != switches[0].switchState) {
116                         switches[0].setValue(eventDataMap["value"])
117                         this.switchState = switches[0].switchState
118                         this.switchLatestValue = switches[0].switchLatestValue
119                         sendEvent(eventDataMap)
120                 }
121         }
122
123
124         def currentValue(String deviceFeature) {
125                 switches[0].currentValue(deviceFeature)
126         }
127
128         def latestValue(String deviceFeature) {
129                 switches[0].latestValue(deviceFeature)
130         }
131
132         def getAt(int ix) {
133                 switches[ix]
134         }
135 }