Fixing bug in Mobile Presence class
[smartthings-infrastructure.git] / Switch / Switches.groovy
index f5e59e524698c82e32f4e401ec5a22edd6dbeb26..4729adf4e59840c404be776750c746f2915035b2 100644 (file)
@@ -1,78 +1,41 @@
 //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()
        }
 }