X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=Switch%2FSwitches.groovy;h=a0e191cf4f61e4b92c33aafcf3670b273aa8946d;hb=33b28ef5e367fbf98724f874c6c7616c20d831d7;hp=dcf981fb51c9a5bd116b0aba04b28a1218422730;hpb=e967e66f8e5a414c24bc87c5146bade90e1759cc;p=smartthings-infrastructure.git diff --git a/Switch/Switches.groovy b/Switch/Switches.groovy index dcf981f..a0e191c 100644 --- a/Switch/Switches.groovy +++ b/Switch/Switches.groovy @@ -1,77 +1,124 @@ //Create a class for switch device package Switch +import Timer.SimulatedTimer public class Switches { - private int id = 0 - private String displayName - private String switchCurrentValue - private String switchLatestValue - def sendEvent + int deviceNumbers + List switches def timers - + def sendEvent - Switches(Closure sendEvent, int id, String displayName, String switchCurrentValue, String switchLatestValue) { + //If we have only one device + private String id = "switchID0" + private String label = "switch0" + private String displayName = "switch0" + private String switchState = "off" + private String currentSwitch = "off" + private int currentLevel = 50 + private String switchLatestValue = "off" + + Switches(Closure sendEvent, int deviceNumbers, boolean init) { this.sendEvent = sendEvent - this.timers = new Timer() - this.id = id - this.displayName = displayName - this.switchCurrentValue = switchCurrentValue - this.switchLatestValue = switchLatestValue + this.timers = new SimulatedTimer() + this.deviceNumbers = deviceNumbers + this.switches = [] + + if (init) { + this.switchState = "off" + this.currentSwitch = "off" + this.switchLatestValue = "off" + this.currentLevel = 50 + } else { + this.switchState = "on" + this.currentSwitch = "on" + this.switchLatestValue = "on" + this.currentLevel = 60 + } + switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue)) + } + + //Methods for closures + def count(Closure Input) { + switches.count(Input) + } + def size() { + switches.size() + } + def each(Closure Input) { + switches.each(Input) + } + def find(Closure Input) { + switches.find(Input) + } + def sort(Closure Input) { + switches.sort(Input) + } + def collect(Closure Input) { + switches.collect(Input) } //By Apps + def eventsSince(Date dateObj, LinkedHashMap metaData) { + return switches[0].eventsSince() + } + + def setLevel(int level) { + currentLevel = level + switches[0].setLevel(level) + } + def on() { - println("the switch with id:$id is on!") - this.switchLatestValue = this.switchCurrentValue - this.switchCurrentValue = "on" - sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + switchLatestValue = "on" + switchState = "on" + currentSwitch = "on" + switches[0].on() } def on(LinkedHashMap metaData) { def task = timers.runAfter(metaData["delay"]) { - println("the switch with id:$id is on!") - this.switchLatestValue = this.switchCurrentValue - this.switchCurrentValue = "on" - sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + switchLatestValue = "on" + switchState = "on" + currentSwitch = "on" + switches[0].on() } } def off() { - println("the switch with id:$id is off!") - this.switchLatestValue = this.switchCurrentValue - this.switchCurrentValue = "off" - sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + switchLatestValue = "off" + switchState = "off" + currentSwitch = "off" + switches[0].off() } def off(LinkedHashMap metaData) { def task = timers.runAfter(metaData["delay"]) { - println("the switch with id:$id is off!") - this.switchLatestValue = this.switchCurrentValue - this.switchCurrentValue = "off" - sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + switchLatestValue = "off" + switchState = "off" + currentSwitch = "off" + switches[0].off() } } //By Model Checker - def setValue(String value) { - println("the switch with id:$id is $value!") - this.switchLatestValue = this.switchCurrentValue - this.switchCurrentValue = value + def setValue(LinkedHashMap eventDataMap) { + if (eventDataMap["value"] != switches[0].switchState) { + this.switchState = eventDataMap["value"] + this.switchLatestValue = eventDataMap["value"] + switches[0].setValue(eventDataMap["value"]) + sendEvent(eventDataMap) + } } - + + def currentValue(String deviceFeature) { - if (deviceFeature == "switch") { - return switchCurrentValue - } + switches[0].currentValue(deviceFeature) } def latestValue(String deviceFeature) { - if (deviceFeature == "switch") { - return switchLatestValue - } + switches[0].latestValue(deviceFeature) + } + + def getAt(int ix) { + switches[ix] } }