Infrastructure compatible with 2 types of switches.(Normal switches and otherVsGeneri...
[smartthings-infrastructure.git] / MotionSensor / MotionSensors.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 MotionSensors {
9         private int deviceNumbers
10         private List motionSensors
11         def sendEvent
12
13         //For one device(We cannot have obj.id)-> We should have obj[0].id
14         private String id = "motionSensorID0"
15         private String label = "motionSensor0"
16         private String displayName = "motionSensor0"
17         private String motion = "inactive"
18         private String currentMotion = "inactive"
19         private String motionLatestValue = "inactive"
20
21                 
22         MotionSensors(Closure sendEvent, int deviceNumbers, boolean init) {
23                 this.sendEvent = sendEvent              
24                 this.deviceNumbers = deviceNumbers
25                 this.motionSensors = []
26
27                 if (init) {
28                         this.motion = "inactive"
29                         this.currentMotion = "inactive"
30                         this.motionLatestValue = "inactive"
31                 } else {
32                         this.motion = "active"
33                         this.currentMotion = "active"
34                         this.motionLatestValue = "active"
35                 }
36                 motionSensors.add(new MotionSensor(id, label, displayName, this.motion, this.motionLatestValue))
37         }
38
39         //By Model Checker
40         def setValue(LinkedHashMap eventDataMap) {
41                 if (eventDataMap["value"] != motionSensors[0].motion) {
42                         this.motionLatestValue = eventDataMap["value"]
43                         this.motion = eventDataMap["value"]
44                         this.currentMotion = eventDataMap["value"]
45                         motionSensors[0].setValue(eventDataMap["value"])
46                         sendEvent(eventDataMap)
47                 }
48         }
49
50         //Methods for closures
51         def count(Closure Input) {
52                 motionSensors.count(Input)
53         }
54         def size() {
55                 motionSensors.size()
56         }
57         def each(Closure Input) {
58                 motionSensors.each(Input)
59         }
60         def find(Closure Input) {
61                 motionSensors.find(Input)
62         }
63         def sort(Closure Input) {
64                 motionSensors.sort(Input)
65         }
66         def collect(Closure Input) {
67                 motionSensors.collect(Input)
68         }
69         
70         def currentState(String deviceFeature) {
71                 currentValue(deviceFeature)
72         }
73
74         def currentValue(String deviceFeature) {
75                 motionSensors[0].currentValue(deviceFeature)//It is called if we have only one device
76         }
77
78         def latestValue(String deviceFeature) {
79                 motionSensors[0].latestValue(deviceFeature)//It is called if we have only one device
80         }
81
82         def statesSince(String info, Date dateObj) {
83                 return motionSensors[0].statesSince()
84         }
85
86         def eventsSince(Date dateObj) {
87                 return motionSensors[0].statesSince()
88         }
89
90         def getAt(int ix) {
91                 motionSensors[ix]
92         }
93 }