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 = "on"
21 private String currentSwitch = "on"
22 private String switchLatestValue = "on"
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)
74 def setLevel(long level) {
75 if (this.level != level) {
76 switchLevels[0].setLevel(level)
83 switchLatestValue = "on"
89 def on(LinkedHashMap metaData) {
90 def task = timers.runAfter(metaData["delay"]) {
91 switchLatestValue = "on"
99 switchLatestValue = "off"
101 currentSwitch = "off"
102 switchLevels[0].off()
105 def off(LinkedHashMap metaData) {
106 def task = timers.runAfter(metaData["delay"]) {
107 switchLatestValue = "off"
109 currentSwitch = "off"
110 switchLevels[0].off()
115 def setValue(LinkedHashMap eventDataMap) {
116 if (eventDataMap["name"] == "switch") {
117 if (eventDataMap["value"] != switchLevels[0].switchState) {
118 this.switchState = eventDataMap["value"]
119 this.switchLatestValue = eventDataMap["value"]
120 this.currentSwitch = eventDataMap["value"]
121 switchLevels[0].setValue(eventDataMap["value"], "switch")
122 sendEvent(eventDataMap)
124 } else if (eventDataMap["name"] == "level") {
125 if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
126 this.level = eventDataMap["value"].toInteger()
127 this.rate = eventDataMap["value"].toInteger()
128 switchLevels[0].setValue(eventDataMap["value"], "level")
129 sendEvent(eventDataMap)
134 def currentValue(String deviceFeature) {
135 switchLevels[0].currentValue(deviceFeature)
138 def latestValue(String deviceFeature) {
139 switchLevels[0].latestValue(deviceFeature)