4317849a30982993fd4c1d53fcf7092934f77d0f
[smartthings-infrastructure.git] / DoorControl / DoorControl.groovy
1 //Create a class for door control device
2 package DoorControl
3 import Timer.SimulatedTimer
4
5 public class DoorControl {
6         private String id
7         private String label
8         private String displayName
9         private String doorState
10         private String doorLatestValue
11         def sendEvent   
12         def timers
13         
14
15         DoorControl(Closure sendEvent, String id, String label, String displayName, String doorState, String doorLatestValue) {
16                 this.sendEvent = sendEvent
17                 this.timers = new SimulatedTimer()
18                 this.id = id
19                 this.label = label
20                 this.displayName = displayName
21                 this.doorState = doorState
22         }
23
24         //By Apps
25         def open() {
26                 println("the door with id:$id is open!")
27                 this.doorLatestValue = this.doorState
28                 this.doorState = "open"
29                 sendEvent([name: "doorControl", value: "open", deviceId: this.id, descriptionText: "",
30                     displayed: true, linkText: "", isStateChange: false, unit: "", data: [value: "open"]])
31         }
32
33         def open(LinkedHashMap metaData) {
34                 def task = timers.runAfter(metaData["delay"]) {
35                         println("the door with id:$id is open!")
36                         this.doorLatestValue = this.doorState
37                         this.doorState = "open"
38                         sendEvent([name: "doorControl", value: "open", deviceId: this.id, descriptionText: "",
39                             displayed: true, linkText: "", isStateChange: false, unit: "", data: [value: "open"]])
40                 }
41         }
42
43         def close() {
44                 println("the door with id:$id is closed!")
45                 this.doorLatestValue = this.doorState
46                 this.doorState = "closed"
47                 sendEvent([name: "doorControl", value: "closed", deviceId: this.id, descriptionText: "",
48                     displayed: true, linkText: "", isStateChange: false, unit: "", data: [value: "closed"]])
49         }
50
51         def close(LinkedHashMap metaData) {
52                 def task = timers.runAfter(metaData["delay"]) {
53                         println("the door with id:$id is closed!")
54                         this.doorLatestValue = this.doorState
55                         this.doorState = "closed"
56                         sendEvent([name: "doorControl", value: "closed", deviceId: this.id, descriptionText: "",
57                             displayed: true, linkText: "", isStateChange: false, unit: "", data: [value: "closed"]])
58                 }
59         }
60
61         //By Model Checker
62         def setValue(String value) {
63                 println("the door with id:$id is $value!")
64                 this.doorLatestValue = this.doorState
65                 this.doorState = value
66         }
67         
68         def currentValue(String deviceFeature) {
69                 if (deviceFeature == "status") {
70                         return doorState
71                 }
72         }
73
74         def latestValue(String deviceFeature) {
75                 if (deviceFeature == "status") {
76                         return doorLatestValue
77                 }
78         }
79 }