Infrastruction modification
[smartthings-infrastructure.git] / RelaySwitch / RelaySwitch.groovy
index 7239f9d396faf1c5c8ad50e18f67974bff7d6ad2..82fdc4ca340ff60a9ef9309ccdfbc5b6b07b505b 100644 (file)
@@ -1,95 +1,62 @@
 //Create a class for relay switch device
 package RelaySwitch
-import Timer.SimulatedTimer
-
-public class RelaySwitch {
-       private String id
-       private String label
-       private String displayName
-       private String switchState
-       private String currentSwitch
-       private String switchLatestValue
-       def sendEvent   
-       def timers
-       
-
-       RelaySwitch(Closure sendEvent, String id, String label, String displayName, String switchState, String currentSwitch, String switchLatestValue) {
-               this.sendEvent = sendEvent
-               this.timers = new SimulatedTimer()
-               this.currentSwitch = currentSwitch
+import SmartThing.SmartThing
+
+public class RelaySwitch extends SmartThing {
+       // id, label, and display name of the device
+       StringBuilder id = new StringBuilder()
+       StringBuilder label = new StringBuilder()
+       StringBuilder displayName = new StringBuilder()
+       // Features with string values
+       StringBuilder currentSwitch = new StringBuilder()
+       // Maps from features to values
+       HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
+
+       RelaySwitch(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentSwitch) {
+               deviceValuesMap = deviceValueSmartThing
+               idSmartThing = id
+               labelSmartThing = label
+               displayNameSmartThing = displayName
+               sendEventSmartThings = sendEvent
+
+               // Initialization
                this.id = id
                this.label = label
                this.displayName = displayName
-               this.switchState = switchState
-               this.switchLatestValue = switchLatestValue
+               this.currentSwitch = currentSwitch
+
+               deviceValuesMap.put("switch", currentSwitch)
        }
 
-       //By Apps
+       // Methods to set values
        def on() {
-               if (switchState != "on") {
-                       println("the relay switch with id:$id is on!")
-                       this.switchLatestValue = "on"
-                       this.switchState = "on"
-                       this.currentSwitch = "on"
-                       sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
-                                 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+               action(currentSwitch, "on", "switch")
        }
 
        def on(LinkedHashMap metaData) {
-               if (switchState != "on") {
-                       def task = timers.runAfter(metaData["delay"]) {
-                               println("the relay switch with id:$id is on!")
-                               this.switchLatestValue = "on"
-                               this.switchState = "on"
-                               this.currentSwitch = "on"
-                               sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
-                                         displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-                       }
-               }
+               on()
        }
 
        def off() {
-               if (switchState != "off") {
-                       println("the relay switch with id:$id is off!")
-                       this.switchLatestValue = "off"
-                       this.switchState = "off"
-                       this.currentSwitch = "off"
-                       sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
-                                 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+               action(currentSwitch, "off", "switch")
        }
 
        def off(LinkedHashMap metaData) {
-               if (switchState != "off") {
-                       def task = timers.runAfter(metaData["delay"]) {
-                               println("the relay switch with id:$id is off!")
-                               this.switchLatestValue = "off"
-                               this.switchState = "off"
-                               this.currentSwitch = "off"
-                               sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
-                                         displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-                       }
-               }
+               off()
        }
 
-       //By Model Checker
-       def setValue(String value) {
-               println("the relay switch with id:$id is $value!")
-               this.switchLatestValue = value
-               this.switchState = value
-               this.currentSwitch = value
-       }
-       
-       def currentValue(String deviceFeature) {
-               if (deviceFeature == "switch") {
-                       return switchState
+       def action(StringBuilder variable, String newValue, String feature) {
+               if (!variable.toString().equals(newValue)) {
+                       String tmpID = id.toString()
+                       variable.replace(0, variable.length(), newValue)
+                       println("$feature of the relay switch with id:$tmpID is changed to $newValue!")
+                       sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
                }
        }
 
-       def latestValue(String deviceFeature) {
-               if (deviceFeature == "switch") {
-                       return switchLatestValue
-               }
+       // Methods to return values
+       def getCurrentSwitch() {
+               return currentSwitch.toString()
        }
 }