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 switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
45 //Methods for closures
46 def count(Closure Input) {
52 def each(Closure Input) {
55 def find(Closure Input) {
58 def sort(Closure Input) {
61 def collect(Closure Input) {
62 switches.collect(Input)
66 def setLevel(int level) {
68 switches[0].setLevel(level)
72 switchLatestValue = "on"
78 def on(LinkedHashMap metaData) {
79 def task = timers.runAfter(metaData["delay"]) {
80 switchLatestValue = "on"
88 switchLatestValue = "off"
94 def off(LinkedHashMap metaData) {
95 def task = timers.runAfter(metaData["delay"]) {
96 switchLatestValue = "off"
104 def setValue(LinkedHashMap eventDataMap) {
105 if (eventDataMap["value"] != switches[0].switchState) {
106 this.switchState = eventDataMap["value"]
107 this.switchLatestValue = eventDataMap["value"]
108 switches[0].setValue(eventDataMap["value"])
109 sendEvent(eventDataMap)
114 def currentValue(String deviceFeature) {
115 switches[0].currentValue(deviceFeature)
118 def latestValue(String deviceFeature) {
119 switches[0].latestValue(deviceFeature)