Commit #9: More classes + Extractor with Rahmadi's editions + Fixing some bugs
[smartthings-infrastructure.git] / MotionSensor / MotionSensor.groovy
1 //Create a class for presence sensor
2 package MotionSensor
3 import Timer.SimulatedTimer
4
5 public class MotionSensor {
6         private String id
7         private String label
8         private String displayName
9         private String motion
10         private String currentMotion
11         private String motionLatestValue
12         private List states = []
13         private List timeOfStates = []
14
15         MotionSensor(String id, String label, String displayName, String motion, String motionLatestValue) {
16                 this.id = id
17                 this.label = label
18                 this.displayName = displayName
19                 this.motion = motion
20                 this.currentMotion = motion
21                 this.motionLatestValue = motionLatestValue
22         }
23
24         def setValue(String value) {
25                 this.motionLatestValue = motion
26                 println("the motion sensor with id:$id is triggered to $value!")
27                 this.motion = value
28                 this.currentMotion = 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 == "motion") {
46                         return motion
47                 }
48         }
49
50         def latestValue(String deviceFeature) {
51                 if (deviceFeature == "motion") {
52                         return motionLatestValue
53                 }
54         }
55 }