Changes in classes: new concept for latest value + all types of events generated...
[smartthings-infrastructure.git] / Valve / Valve.groovy
1 //Create a class for valve
2 package Valve
3 import Timer.SimulatedTimer
4
5 public class Valve {
6         private String id
7         private String label
8         private String displayName
9         private String valve
10         private String valveLatestValue
11         def sendEvent   
12         def timers
13         
14
15         Valve(Closure sendEvent, String id, String label, String displayName, String valve, String valveLatestValue) {
16                 this.sendEvent = sendEvent
17                 this.timers = new SimulatedTimer()
18                 this.id = id
19                 this.label = label
20                 this.displayName = displayName
21                 this.valve = valve
22                 this.valveLatestValue = valveLatestValue
23         }
24
25         //By Apps
26         def open() {
27                 if (valve != "open") {
28                         println("the valve with id:$id is open!")
29                         this.valveLatestValue = "open"
30                         this.valve = "open"
31                         sendEvent([name: "contact", value: "open", deviceId: this.id, descriptionText: "",
32                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
33                         sendEvent([name: "contact.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 (valve != "open") {
40                         def task = timers.runAfter(metaData["delay"]) {
41                                 println("the valve with id:$id is open!")
42                                 this.valveLatestValue = "open"
43                                 this.valve = "open"
44                                 sendEvent([name: "contact", value: "open", deviceId: this.id, descriptionText: "",
45                                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
46                                 sendEvent([name: "contact.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 (valve != "closed") {
54                         println("the valve with id:$id is closed!")
55                         this.valveLatestValue = "closed"
56                         this.valve = "closed"
57                         sendEvent([name: "contact", value: "closed", deviceId: this.id, descriptionText: "",
58                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
59                         sendEvent([name: "contact.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 (valve != "closed") {
66                         def task = timers.runAfter(metaData["delay"]) {
67                                 println("the valve with id:$id is closed!")
68                                 this.valveLatestValue = "closed"
69                                 this.valve = "closed"
70                                 sendEvent([name: "contact", value: "closed", deviceId: this.id, descriptionText: "",
71                                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
72                                 sendEvent([name: "contact.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 valve with id:$id is $value!")
81                 this.valveLatestValue = value
82                 this.valve = value
83         }
84         
85         def currentValue(String deviceFeature) {
86                 if (deviceFeature == "valve") {
87                         return valve
88                 }
89         }
90
91         def latestValue(String deviceFeature) {
92                 if (deviceFeature == "valve") {
93                         return valveLatestValue
94                 }
95         }
96 }