Commit #9: More classes + Extractor with Rahmadi's editions + Fixing some bugs
[smartthings-infrastructure.git] / MotionSensor / MotionSensor.groovy
diff --git a/MotionSensor/MotionSensor.groovy b/MotionSensor/MotionSensor.groovy
new file mode 100644 (file)
index 0000000..825fe33
--- /dev/null
@@ -0,0 +1,55 @@
+//Create a class for presence sensor
+package MotionSensor
+import Timer.SimulatedTimer
+
+public class MotionSensor {
+       private String id
+       private String label
+       private String displayName
+       private String motion
+       private String currentMotion
+       private String motionLatestValue
+       private List states = []
+       private List timeOfStates = []
+
+       MotionSensor(String id, String label, String displayName, String motion, String motionLatestValue) {
+               this.id = id
+               this.label = label
+               this.displayName = displayName
+               this.motion = motion
+               this.currentMotion = motion
+               this.motionLatestValue = motionLatestValue
+       }
+
+       def setValue(String value) {
+               this.motionLatestValue = motion
+               println("the motion sensor with id:$id is triggered to $value!")
+               this.motion = value
+               this.currentMotion = value
+               this.states.add(value)
+               this.timeOfStates.add(System.currentTimeMillis())
+       }
+
+       def statesSince(String info, Date dateObj) {
+               def List happenedStates = []
+               def sinceThen = dateObj.time
+               for (int i = 0;i < timeOfStates.size();i++) {
+                       if (timeOfStates[i]>=sinceThen)
+                               happenedStates.add(states[i])
+               }
+               return happenedStates
+       }
+
+       
+       def currentValue(String deviceFeature) {
+               if (deviceFeature == "motion") {
+                       return motion
+               }
+       }
+
+       def latestValue(String deviceFeature) {
+               if (deviceFeature == "motion") {
+                       return motionLatestValue
+               }
+       }
+}