Changes in classes: new concept for latest value + all types of events generated...
[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                         sendEvent([name: "doorControl.open", value: "open", deviceId: this.id, descriptionText: "",
34                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
35                 }
36         }
37
38         def open(LinkedHashMap metaData) {
39                 if (doorState != "open") {
40                         def task = timers.runAfter(metaData["delay"]) {
41                                 println("the door with id:$id is open!")
42                                 this.doorLatestValue = "open"
43                                 this.doorState = "open"
44                                 sendEvent([name: "doorControl", value: "open", deviceId: this.id, descriptionText: "",
45                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
46                                 sendEvent([name: "doorControl.open", value: "open", deviceId: this.id, descriptionText: "",
47                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
48                         }
49                 }
50         }
51
52         def close() {
53                 if (doorState != "closed") {
54                         println("the door with id:$id is closed!")
55                         this.doorLatestValue = "closed"
56                         this.doorState = "closed"
57                         sendEvent([name: "doorControl", value: "closed", deviceId: this.id, descriptionText: "",
58                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
59                         sendEvent([name: "doorControl.closed", value: "closed", deviceId: this.id, descriptionText: "",
60                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
61                 }
62         }
63
64         def close(LinkedHashMap metaData) {
65                 if (doorState != "closed") {
66                         def task = timers.runAfter(metaData["delay"]) {
67                                 println("the door with id:$id is closed!")
68                                 this.doorLatestValue = "closed"
69                                 this.doorState = "closed"
70                                 sendEvent([name: "doorControl", value: "closed", deviceId: this.id, descriptionText: "",
71                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
72                                 sendEvent([name: "doorControl.closed", value: "closed", deviceId: this.id, descriptionText: "",
73                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
74                         }
75                 }
76         }
77
78         //By Model Checker
79         def setValue(String value) {
80                 println("the door with id:$id is $value!")
81                 this.doorLatestValue = value
82                 this.doorState = value
83         }
84         
85         def currentValue(String deviceFeature) {
86                 if (deviceFeature == "status") {
87                         return doorState
88                 }
89         }
90
91         def latestValue(String deviceFeature) {
92                 if (deviceFeature == "status") {
93                         return doorLatestValue
94                 }
95         }
96 }