1 //Create a class for switch device
3 import Timer.SimulatedTimer
6 import gov.nasa.jpf.vm.Verify
9 public class Switches {
15 //If we have only one device
16 private String id = "switchID0"
17 private String label = "switch0"
18 private String displayName = "switch0"
19 private String switchState = "off"
20 private String currentSwitch = "off"
21 private int currentLevel = 50
22 private String switchLatestValue = "off"
24 Switches(Closure sendEvent, int deviceNumbers, boolean init) {
25 this.sendEvent = sendEvent
26 this.timers = new SimulatedTimer()
27 this.deviceNumbers = deviceNumbers
31 this.switchState = "off"
32 this.currentSwitch = "off"
33 this.switchLatestValue = "off"
34 this.currentLevel = 50
36 this.switchState = "on"
37 this.currentSwitch = "on"
38 this.switchLatestValue = "on"
39 this.currentLevel = 60
41 switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
44 //Methods for closures
45 def count(Closure Input) {
51 def each(Closure Input) {
54 def find(Closure Input) {
57 def sort(Closure Input) {
60 def collect(Closure Input) {
61 switches.collect(Input)
65 def eventsSince(Date dateObj, LinkedHashMap metaData) {
66 return switches[0].eventsSince()
69 def setLevel(int level) {
71 switches[0].setLevel(level)
75 switchLatestValue = "on"
81 def on(LinkedHashMap metaData) {
82 def task = timers.runAfter(metaData["delay"]) {
83 switchLatestValue = "on"
91 switchLatestValue = "off"
97 def off(LinkedHashMap metaData) {
98 def task = timers.runAfter(metaData["delay"]) {
99 switchLatestValue = "off"
101 currentSwitch = "off"
107 def setValue(LinkedHashMap eventDataMap) {
108 if (eventDataMap["value"] != switches[0].switchState) {
109 this.switchState = eventDataMap["value"]
110 this.switchLatestValue = eventDataMap["value"]
111 switches[0].setValue(eventDataMap["value"])
112 sendEvent(eventDataMap)
117 def currentValue(String deviceFeature) {
118 switches[0].currentValue(deviceFeature)
121 def latestValue(String deviceFeature) {
122 switches[0].latestValue(deviceFeature)