1 //Create a class for switch device
3 import Timer.SimulatedTimer
6 import gov.nasa.jpf.vm.Verify
8 public class Switches {
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"
23 Switches(Closure sendEvent, int deviceNumbers) {
24 this.sendEvent = sendEvent
25 this.timers = new SimulatedTimer()
26 this.deviceNumbers = deviceNumbers
29 def initLevel = Verify.getIntFromList(30, 50, 70)
30 this.currentLevel = initLevel
31 def init = Verify.getBoolean()
33 this.switchState = "off"
34 this.currentSwitch = "off"
35 this.switchLatestValue = "off"
37 this.switchState = "on"
38 this.currentSwitch = "on"
39 this.switchLatestValue = "on"
42 def initLevel = Verify.getIntFromList(30, 50, 70)
43 this.currentLevel = initLevel
44 def init = Verify.getBoolean()
46 this.switchState = "off"
47 this.currentSwitch = "off"
48 this.switchLatestValue = "off"
50 this.switchState = "on"
51 this.currentSwitch = "on"
52 this.switchLatestValue = "on"
55 switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
58 //Methods for closures
59 def count(Closure Input) {
65 def each(Closure Input) {
68 def find(Closure Input) {
71 def collect(Closure Input) {
72 switches.collect(Input)
76 def setLevel(int level) {
77 switches[0].setLevel(level)
83 switchLatestValue = switchState
88 def on(LinkedHashMap metaData) {
89 def task = timers.runAfter(metaData["delay"]) {
91 switchLatestValue = switchState
99 switchLatestValue = switchState
101 currentSwitch = "off"
104 def off(LinkedHashMap metaData) {
105 def task = timers.runAfter(metaData["delay"]) {
107 switchLatestValue = switchState
109 currentSwitch = "off"
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)
124 def currentValue(String deviceFeature) {
125 switches[0].currentValue(deviceFeature)
128 def latestValue(String deviceFeature) {
129 switches[0].latestValue(deviceFeature)