1 //Create a class for switch level
3 import Timer.SimulatedTimer
6 import gov.nasa.jpf.vm.Verify
8 public class SwitchLevels {
14 //If we have only one device
15 private String id = "switchLevelID0"
16 private String label = "switchLevel0"
17 private String displayName = "switchLevel0"
18 private int level = 50
20 private String switchState = "off"
21 private String currentSwitch = "off"
22 private String switchLatestValue = "off"
24 SwitchLevels(Closure sendEvent, int deviceNumbers) {
25 this.sendEvent = sendEvent
26 this.timers = new SimulatedTimer()
27 this.deviceNumbers = deviceNumbers
28 this.switchLevels = []
30 /*def initLevel = Verify.getIntFromList(30, 50, 70)
31 this.level = initLevel
32 def init = Verify.getBoolean()
34 this.switchState = "off"
35 this.currentSwitch = "off"
36 this.switchLatestValue = "off"
38 this.switchState = "on"
39 this.currentSwitch = "on"
40 this.switchLatestValue = "on"
42 switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.switchState, this.switchLatestValue))
45 //Methods for closures
46 def count(Closure Input) {
47 switchLevels.count(Input)
52 def each(Closure Input) {
53 switchLevels.each(Input)
55 def find(Closure Input) {
56 switchLevels.find(Input)
58 def sort(Closure Input) {
59 switchLevels.sort(Input)
61 def collect(Closure Input) {
62 switchLevels.collect(Input)
66 def setLevel(int level) {
67 if (this.level != level) {
68 switchLevels[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"
102 switchLevels[0].off()
107 def setValue(LinkedHashMap eventDataMap) {
108 if (eventDataMap["name"] == "switch") {
109 if (eventDataMap["value"] != switchLevels[0].switchState) {
110 this.switchState = eventDataMap["value"]
111 this.switchLatestValue = eventDataMap["value"]
112 this.currentSwitch = eventDataMap["value"]
113 switchLevels[0].setValue(eventDataMap["value"], "switch")
114 sendEvent(eventDataMap)
116 } else if (eventDataMap["name"] == "level") {
117 if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
118 this.level = eventDataMap["value"].toInteger()
119 this.rate = eventDataMap["value"].toInteger()
120 switchLevels[0].setValue(eventDataMap["value"], "level")
121 sendEvent(eventDataMap)