//Create a class for switch device
package Switch
+import SmartThing.SmartThings
-public class Switches {
- private int id = 0
- private String displayName
- private String switchCurrentValue
- private String switchLatestValue
- def sendEvent
- def timers
-
+public class Switches extends SmartThings {
+ List switches = new ArrayList()
- Switches(Closure sendEvent, int id, String displayName, String switchCurrentValue, String switchLatestValue) {
- this.sendEvent = sendEvent
- this.timers = new Timer()
+ Switches(Closure sendEvent, boolean init) {
+ switches = smartThings
- this.id = id
- this.displayName = displayName
- this.switchCurrentValue = switchCurrentValue
- this.switchLatestValue = switchLatestValue
+ // Initialization
+ String id = "switchID0"
+ String label = "switch"
+ String displayName = "switch"
+ String currentSwitch
+
+ if (init)
+ currentSwitch = "off"
+ else
+ currentSwitch = "on"
+
+ switches.add(new Switch(sendEvent, id, label, displayName, currentSwitch))
}
- //By Apps
+ // Methods to set values
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: []])
+ 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: []])
- }
+ 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: []])
+ 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: []])
- }
- }
-
- //By Model Checker
- def setValue(String value) {
- println("the switch with id:$id is $value!")
- this.switchLatestValue = this.switchCurrentValue
- this.switchCurrentValue = value
- }
-
- def currentValue(String deviceFeature) {
- if (deviceFeature == "switch") {
- return switchCurrentValue
- }
- }
-
- def latestValue(String deviceFeature) {
- if (deviceFeature == "switch") {
- return switchLatestValue
- }
+ off()
}
}