7680e162562afe16c44d2e7e2dd6f3ee07f45e2b
[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                 }
34         }
35
36         def open(LinkedHashMap metaData) {
37                 if (valve != "open") {
38                         def task = timers.runAfter(metaData["delay"]) {
39                                 println("the valve with id:$id is open!")
40                                 this.valveLatestValue = "open"
41                                 this.valve = "open"
42                                 sendEvent([name: "contact", 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 (valve != "closed") {
50                         println("the valve with id:$id is closed!")
51                         this.valveLatestValue = "closed"
52                         this.valve = "closed"
53                         sendEvent([name: "contact", 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 (valve != "closed") {
60                         def task = timers.runAfter(metaData["delay"]) {
61                                 println("the valve with id:$id is closed!")
62                                 this.valveLatestValue = "closed"
63                                 this.valve = "closed"
64                                 sendEvent([name: "contact", 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 valve with id:$id is $value!")
73                 this.valveLatestValue = value
74                 this.valve = value
75         }
76         
77         def currentValue(String deviceFeature) {
78                 if (deviceFeature == "valve") {
79                         return valve
80                 }
81         }
82
83         def latestValue(String deviceFeature) {
84                 if (deviceFeature == "valve") {
85                         return valveLatestValue
86                 }
87         }
88 }