Adding remaining devices!
[smartthings-infrastructure.git] / ContactSensor / ContactSensor.groovy
1 //Create a class for contact sensor
2 package ContactSensor
3 import Timer.SimulatedTimer
4
5 //JPF's Verify API
6 import gov.nasa.jpf.vm.Verify
7
8 public class ContactSensor {
9         private String id
10         private String label
11         private String displayName
12         private String contactState
13         private String currentContact   
14         private String latestValue
15         private String alarmState
16         private List events = []
17         private List timeOfEvents = []
18         
19
20         ContactSensor(String id, String label, String displayName, String contactState, String currentContact, String alarmState, String latestValue) {
21                 this.id = id
22                 this.label = label
23                 this.displayName = displayName
24                 this.contactState = contactState
25                 this.currentContact = currentContact
26                 this.latestValue = latestValue
27                 this.alarmState = alarmState
28         }
29
30         def eventsSince() {
31                 def evtOpen = [[name: "contact", value: "open", deviceId: "contactSensorID0", descriptionText: "",
32                                 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
33                 def evtClosed = [[name: "contact", value: "closed", deviceId: "contactSensorID0", descriptionText: "",
34                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
35                 def init = Verify.getInt(0,4)
36                 def evtToSend = []
37                 if (init == 0) {//return empty set
38                         return evtToSend
39                 } else if (init == 1) {//send one open event
40                         evtOpen.each{
41                                 evtToSend.add(it)
42                         }
43                         return evtToSend
44                 } else if (init == 2) {//send two open events
45                         evtOpen.each{
46                                 evtToSend.add(it)
47                         }
48                         evtOpen.each{
49                                 evtToSend.add(it)
50                         }
51                         return evtToSend
52                 } else if (init == 3) {//send one closed event
53                         evtClosed.each{
54                                 evtToSend.add(it)
55                         }
56                         return evtToSend
57                 } else if (init == 4) {//send two closed events
58                         evtClosed.each{
59                                 evtToSend.add(it)
60                         }
61                         evtClosed.each{
62                                 evtToSend.add(it)
63                         }
64                         return evtToSend
65                 }
66         }
67
68         def setValue(String value) {
69                 println("the contact sensor with id:$id is triggered to $value!")
70                 this.contactState = value
71                 this.currentContact = value
72                 this.latestValue = value
73         }
74
75         
76         def on() {
77                 println("the contact sensor with id:$id is armed!")
78                 this.alarmState = "armed"
79         }
80
81         def off() {
82                 println("the contact sensor with id:$id is not armed!")
83                 this.alarmState = "not armed"
84         }
85         
86         def currentValue(String deviceFeature) {
87                 if (deviceFeature == "contact") {
88                         return contactState
89                 }
90         }
91
92         def currentState(String deviceFeature) {
93                 if (deviceFeature == "contact") {
94                         return contactState
95                 }
96         }
97
98         def latestValue(String deviceFeature) {
99                 if (deviceFeature == "contact") {
100                         return latestValue
101                 }
102         }
103 }