Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[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
13         MotionSensor(String id, String label, String displayName, String motion, String motionLatestValue) {
14                 this.id = id
15                 this.label = label
16                 this.displayName = displayName
17                 this.motion = motion
18                 this.currentMotion = motion
19                 this.motionLatestValue = motionLatestValue
20         }
21
22         def setValue(String value) {
23                 println("the motion sensor with id:$id is triggered to $value!")
24                 this.motionLatestValue = value
25                 this.motion = value
26                 this.currentMotion = value
27         }
28
29         def statesSince() {
30                 def evtActive = [[name: "motion", value: "active", deviceId: "motionSensorID0", descriptionText: "",
31                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
32                 def evtInactive = [[name: "motion", value: "inactive", deviceId: "motionSensorID0", descriptionText: "",
33                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
34                 def init = Verify.getInt(0,4)
35                 def evtToSend = []
36                 if (init == 0) {//return empty set
37                         return evtToSend
38                 } else if (init == 1) {//send one active event
39                         evtActive.each{
40                                 evtToSend.add(it)
41                         }
42                         return evtToSend
43                 } else if (init == 2) {//send two active events
44                         evtActive.each{
45                                 evtToSend.add(it)
46                         }
47                         evtActive.each{
48                                 evtToSend.add(it)
49                         }
50                         return evtToSend
51                 } else if (init == 3) {//send one inactive event
52                         evtInactive.each{
53                                 evtToSend.add(it)
54                         }
55                         return evtToSend
56                 } else if (init == 4) {//send two inactive events
57                         evtInactive.each{
58                                 evtToSend.add(it)
59                         }
60                         evtInactive.each{
61                                 evtToSend.add(it)
62                         }
63                         return evtToSend
64                 }
65         }
66
67         def currentState(String deviceFeature) {
68                 currentValue(deviceFeature)
69         }
70
71         
72         def currentValue(String deviceFeature) {
73                 if (deviceFeature == "motion") {
74                         return motion
75                 }
76         }
77
78         def latestValue(String deviceFeature) {
79                 if (deviceFeature == "motion") {
80                         return motionLatestValue
81                 }
82         }
83 }