Infrastructure compatible with 2 types of switches.(Normal switches and otherVsGeneri...
[smartthings-infrastructure.git] / MotionSensor / MotionSensor.groovy
1 //Create a class for presence sensor
2 package MotionSensor
3 import Timer.SimulatedTimer
4
5 //JPF's Verify API
6 import gov.nasa.jpf.vm.Verify
7
8 public class MotionSensor {
9         private String id
10         private String label
11         private String displayName
12         private String motion
13         private String currentMotion
14         private String motionLatestValue
15
16         MotionSensor(String id, String label, String displayName, String motion, String motionLatestValue) {
17                 this.id = id
18                 this.label = label
19                 this.displayName = displayName
20                 this.motion = motion
21                 this.currentMotion = motion
22                 this.motionLatestValue = motionLatestValue
23         }
24
25         def setValue(String value) {
26                 println("the motion sensor with id:$id is triggered to $value!")
27                 this.motionLatestValue = value
28                 this.motion = value
29                 this.currentMotion = value
30         }
31
32         def statesSince() {
33                 eventsSince()
34         }
35
36         def eventsSince() {
37                 def evtActive = [[name: "motion", value: "active", deviceId: "motionSensorID0", descriptionText: "",
38                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
39                 def evtInactive = [[name: "motion", value: "inactive", deviceId: "motionSensorID0", descriptionText: "",
40                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
41                 def init = Verify.getInt(0,4)
42                 def evtToSend = []
43                 if (init == 0) {//return empty set
44                         return evtToSend
45                 } else if (init == 1) {//send one active event
46                         evtActive.each{
47                                 evtToSend.add(it)
48                         }
49                         return evtToSend
50                 } else if (init == 2) {//send two active events
51                         evtActive.each{
52                                 evtToSend.add(it)
53                         }
54                         evtActive.each{
55                                 evtToSend.add(it)
56                         }
57                         return evtToSend
58                 } else if (init == 3) {//send one inactive event
59                         evtInactive.each{
60                                 evtToSend.add(it)
61                         }
62                         return evtToSend
63                 } else if (init == 4) {//send two inactive events
64                         evtInactive.each{
65                                 evtToSend.add(it)
66                         }
67                         evtInactive.each{
68                                 evtToSend.add(it)
69                         }
70                         return evtToSend
71                 }
72         }
73
74         def currentState(String deviceFeature) {
75                 currentValue(deviceFeature)
76         }
77
78         
79         def currentValue(String deviceFeature) {
80                 if (deviceFeature == "motion") {
81                         return motion
82                 }
83         }
84
85         def latestValue(String deviceFeature) {
86                 if (deviceFeature == "motion") {
87                         return motionLatestValue
88                 }
89         }
90 }