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