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
21 private int saturation = 70
22 private String switchState = "on"
23 private String currentSwitch = "on"
24 private String switchLatestValue = "on"
26 SwitchLevels(Closure sendEvent, int deviceNumbers) {
27 this.sendEvent = sendEvent
28 this.timers = new SimulatedTimer()
29 this.deviceNumbers = deviceNumbers
30 this.switchLevels = []
32 /*def initLevel = Verify.getIntFromList(30, 50, 70)
33 this.level = initLevel
34 def init = Verify.getBoolean()
36 this.switchState = "off"
37 this.currentSwitch = "off"
38 this.switchLatestValue = "off"
40 this.switchState = "on"
41 this.currentSwitch = "on"
42 this.switchLatestValue = "on"
44 switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.hue, this.saturation, this.switchState, this.switchLatestValue))
47 //Methods for closures
48 def count(Closure Input) {
49 switchLevels.count(Input)
54 def each(Closure Input) {
55 switchLevels.each(Input)
57 def find(Closure Input) {
58 switchLevels.find(Input)
60 def sort(Closure Input) {
61 switchLevels.sort(Input)
63 def collect(Closure Input) {
64 switchLevels.collect(Input)
68 def setColor(LinkedHashMap metaData) {
69 if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
70 this.level = metaData["level"]
71 this.rate = metaData["level"]
72 this.hue = metaData["hue"]
73 this.saturation = metaData["saturation"]
74 switchLevels[0].setColor(metaData)
78 def setLevel(int level) {
79 if (this.level != level) {
80 switchLevels[0].setLevel(level)
86 def setLevel(long level) {
87 if (this.level != level) {
88 switchLevels[0].setLevel(level)
95 switchLatestValue = "on"
101 def on(LinkedHashMap metaData) {
102 def task = timers.runAfter(metaData["delay"]) {
103 switchLatestValue = "on"
111 switchLatestValue = "off"
113 currentSwitch = "off"
114 switchLevels[0].off()
117 def off(LinkedHashMap metaData) {
118 def task = timers.runAfter(metaData["delay"]) {
119 switchLatestValue = "off"
121 currentSwitch = "off"
122 switchLevels[0].off()
127 def setValue(LinkedHashMap eventDataMap) {
128 if (eventDataMap["name"] == "switch") {
129 if (eventDataMap["value"] != switchLevels[0].switchState) {
130 this.switchState = eventDataMap["value"]
131 this.switchLatestValue = eventDataMap["value"]
132 this.currentSwitch = eventDataMap["value"]
133 switchLevels[0].setValue(eventDataMap["value"], "switch")
134 sendEvent(eventDataMap)
136 } else if (eventDataMap["name"] == "level") {
137 if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
138 this.level = eventDataMap["value"].toInteger()
139 this.rate = eventDataMap["value"].toInteger()
140 switchLevels[0].setValue(eventDataMap["value"], "level")
141 sendEvent(eventDataMap)
146 def currentValue(String deviceFeature) {
147 switchLevels[0].currentValue(deviceFeature)
150 def latestValue(String deviceFeature) {
151 switchLevels[0].latestValue(deviceFeature)