Changes in classes: new concept for latest value + all types of events generated...
[smartthings-infrastructure.git] / ContactSensor / ContactSensor.groovy
1 //Create a class for contact sensor
2 package ContactSensor
3 import Timer.SimulatedTimer
4
5 public class ContactSensor {
6         private String id
7         private String label
8         private String displayName
9         private String contactState
10         private String currentContact   
11         private String latestValue
12         private String alarmState
13         private List events = []
14         private List timeOfEvents = []
15         
16
17         ContactSensor(String id, String label, String displayName, String contactState, String currentContact, String alarmState, String latestValue) {
18                 this.id = id
19                 this.label = label
20                 this.displayName = displayName
21                 this.contactState = contactState
22                 this.currentContact = currentContact
23                 this.latestValue = latestValue
24                 this.alarmState = alarmState
25         }
26
27         def setValue(String value) {
28                 println("the contact sensor with id:$id is triggered to $value!")
29                 this.contactState = value
30                 this.currentContact = value
31                 this.latestValue = value
32                 this.events.add(eventDataMap)
33                 this.timeOfEvents.add(System.currentTimeMillis())
34         }
35
36         def eventsSince(Date dateObj) {
37                 def List happenedEvents = []
38                 def sinceThen = dateObj.time
39                 for (int i = 0;i < timeOfEvents.size();i++) {
40                         if (timeOfEvents[i]>=sinceThen)
41                                 happenedEvents.add(events[i])
42                 }
43                 return happenedEvents
44         }
45         
46         def on() {
47                 println("the contact sensor with id:$id is armed!")
48                 this.alarmState = "armed"
49         }
50
51         def off() {
52                 println("the contact sensor with id:$id is not armed!")
53                 this.alarmState = "not armed"
54         }
55         
56         def currentValue(String deviceFeature) {
57                 if (deviceFeature == "contact") {
58                         return contactState
59                 }
60         }
61
62         def currentState(String deviceFeature) {
63                 if (deviceFeature == "contact") {
64                         return contactState
65                 }
66         }
67
68         def latestValue(String deviceFeature) {
69                 if (deviceFeature == "contact") {
70                         return latestValue
71                 }
72         }
73 }