Infrastruction modification
[smartthings-infrastructure.git] / DoorControl / DoorControl.groovy
index c82587925e93dc58d473a03114673bd0e6ef2119..d0027273b4260ab0e18e27f22070ca050a722dd0 100644 (file)
@@ -1,88 +1,62 @@
 //Create a class for door control device
 package DoorControl
-import Timer.SimulatedTimer
-
-public class DoorControl {
-       private String id
-       private String label
-       private String displayName
-       private String doorState
-       private String doorLatestValue
-       def sendEvent   
-       def timers
-       
-
-       DoorControl(Closure sendEvent, String id, String label, String displayName, String doorState, String doorLatestValue) {
-               this.sendEvent = sendEvent
-               this.timers = new SimulatedTimer()
+import SmartThing.SmartThing
+
+public class DoorControl 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 currentDoorState = new StringBuilder()
+       // Maps from features to values
+       HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
+
+       DoorControl(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentDoorState) {
+               deviceValuesMap = deviceValueSmartThing
+               idSmartThing = id
+               labelSmartThing = label
+               displayNameSmartThing = displayName
+               sendEventSmartThings = sendEvent
+
+               // Initialization
                this.id = id
                this.label = label
                this.displayName = displayName
-               this.doorState = doorState
-               this.doorLatestValue = doorLatestValue
+               this.currentDoorState = currentDoorState
+
+               deviceValuesMap.put("status", currentDoorState)
        }
 
-       //By Apps
+       // Methods to set values
        def open() {
-               if (doorState != "open") {
-                       println("the door with id:$id is open!")
-                       this.doorLatestValue = "open"
-                       this.doorState = "open"
-                       sendEvent([name: "doorControl", value: "open", deviceId: this.id, descriptionText: "",
-                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+               action(currentDoorState, "open", "status")
        }
 
        def open(LinkedHashMap metaData) {
-               if (doorState != "open") {
-                       def task = timers.runAfter(metaData["delay"]) {
-                               println("the door with id:$id is open!")
-                               this.doorLatestValue = "open"
-                               this.doorState = "open"
-                               sendEvent([name: "doorControl", value: "open", deviceId: this.id, descriptionText: "",
-                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-                       }
-               }
+               open()
        }
 
        def close() {
-               if (doorState != "closed") {
-                       println("the door with id:$id is closed!")
-                       this.doorLatestValue = "closed"
-                       this.doorState = "closed"
-                       sendEvent([name: "doorControl", value: "closed", deviceId: this.id, descriptionText: "",
-                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+               action(currentDoorState, "closed", "status")
        }
 
        def close(LinkedHashMap metaData) {
-               if (doorState != "closed") {
-                       def task = timers.runAfter(metaData["delay"]) {
-                               println("the door with id:$id is closed!")
-                               this.doorLatestValue = "closed"
-                               this.doorState = "closed"
-                               sendEvent([name: "doorControl", value: "closed", deviceId: this.id, descriptionText: "",
-                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-                       }
-               }
+               close()
        }
 
-       //By Model Checker
-       def setValue(String value) {
-               println("the door with id:$id is $value!")
-               this.doorLatestValue = value
-               this.doorState = value
-       }
-       
-       def currentValue(String deviceFeature) {
-               if (deviceFeature == "status") {
-                       return doorState
+       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 door 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 == "status") {
-                       return doorLatestValue
-               }
+       // Methods to return values
+       def getCurrentDoorState() {
+               return currentDoorState.toString()
        }
 }