Commit #4
[smartthings-infrastructure.git] / Switch / Switches.groovy
diff --git a/Switch/Switches.groovy b/Switch/Switches.groovy
new file mode 100644 (file)
index 0000000..dcf981f
--- /dev/null
@@ -0,0 +1,77 @@
+//Create a class for switch device
+package Switch
+
+public class Switches {
+       private int id = 0      
+       private String displayName
+       private String switchCurrentValue
+       private String switchLatestValue
+       def sendEvent   
+       def timers
+       
+
+       Switches(Closure sendEvent, int id, String displayName, String switchCurrentValue, String switchLatestValue) {
+               this.sendEvent = sendEvent
+               this.timers = new Timer()
+               this.id = id
+               this.displayName = displayName
+               this.switchCurrentValue = switchCurrentValue
+               this.switchLatestValue = switchLatestValue
+       }
+
+       //By Apps
+       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: []])
+       }
+
+       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: []])
+               }
+       }
+
+       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: []])
+       }
+
+       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
+               }
+       }
+}