Interleaving events.
[smartthings-infrastructure.git] / AccelerationSensor / AccelerationSensor.groovy
1 //Create a class for acceleration sensor
2 package AccelerationSensor
3 import Timer.SimulatedTimer
4
5 public class AccelerationSensor {
6         private String id
7         private String label
8         private String displayName
9         private String acceleration
10         private String currentAcceleration
11         private String accelerationLatestValue
12         private List states = []
13         private List timeOfStates = []
14
15         AccelerationSensor(String id, String label, String displayName, String acceleration, String accelerationLatestValue) {
16                 this.id = id
17                 this.label = label
18                 this.displayName = displayName
19                 this.acceleration = acceleration
20                 this.currentAcceleration = acceleration
21                 this.accelerationLatestValue = accelerationLatestValue
22         }
23
24         def setValue(String value) {
25                 println("the acceleration sensor with id:$id is triggered to $value!")
26                 this.accelerationLatestValue = value
27                 this.acceleration = value
28                 this.currentAcceleration = value
29                 this.states.add(value)
30                 this.timeOfStates.add(System.currentTimeMillis())
31         }
32
33         def statesSince(String info, Date dateObj) {
34                 def List happenedStates = []
35                 def sinceThen = dateObj.time
36                 for (int i = 0;i < timeOfStates.size();i++) {
37                         if (timeOfStates[i]>=sinceThen)
38                                 happenedStates.add(states[i])
39                 }
40                 return happenedStates
41         }
42
43         
44         def currentValue(String deviceFeature) {
45                 if (deviceFeature == "acceleration") {
46                         return acceleration
47                 }
48         }
49
50         def latestValue(String deviceFeature) {
51                 if (deviceFeature == "acceleration") {
52                         return accelerationLatestValue
53                 }
54         }
55 }