c82587925e93dc58d473a03114673bd0e6ef2119
[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                 this.doorLatestValue = doorLatestValue
23         }
24
25         //By Apps
26         def open() {
27                 if (doorState != "open") {
28                         println("the door with id:$id is open!")
29                         this.doorLatestValue = "open"
30                         this.doorState = "open"
31                         sendEvent([name: "doorControl", value: "open", deviceId: this.id, descriptionText: "",
32                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
33                 }
34         }
35
36         def open(LinkedHashMap metaData) {
37                 if (doorState != "open") {
38                         def task = timers.runAfter(metaData["delay"]) {
39                                 println("the door with id:$id is open!")
40                                 this.doorLatestValue = "open"
41                                 this.doorState = "open"
42                                 sendEvent([name: "doorControl", value: "open", deviceId: this.id, descriptionText: "",
43                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
44                         }
45                 }
46         }
47
48         def close() {
49                 if (doorState != "closed") {
50                         println("the door with id:$id is closed!")
51                         this.doorLatestValue = "closed"
52                         this.doorState = "closed"
53                         sendEvent([name: "doorControl", value: "closed", deviceId: this.id, descriptionText: "",
54                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
55                 }
56         }
57
58         def close(LinkedHashMap metaData) {
59                 if (doorState != "closed") {
60                         def task = timers.runAfter(metaData["delay"]) {
61                                 println("the door with id:$id is closed!")
62                                 this.doorLatestValue = "closed"
63                                 this.doorState = "closed"
64                                 sendEvent([name: "doorControl", value: "closed", deviceId: this.id, descriptionText: "",
65                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
66                         }
67                 }
68         }
69
70         //By Model Checker
71         def setValue(String value) {
72                 println("the door with id:$id is $value!")
73                 this.doorLatestValue = value
74                 this.doorState = value
75         }
76         
77         def currentValue(String deviceFeature) {
78                 if (deviceFeature == "status") {
79                         return doorState
80                 }
81         }
82
83         def latestValue(String deviceFeature) {
84                 if (deviceFeature == "status") {
85                         return doorLatestValue
86                 }
87         }
88 }