Changes in classes: new concept for latest value + all types of events generated...
[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) {
23                 this.sendEvent = sendEvent              
24                 this.deviceNumbers = deviceNumbers
25                 this.motionSensors = []
26
27                 def init = Verify.getBoolean()
28                 if (init) {
29                         this.motion = "inactive"
30                         this.motionLatestValue = "inactive"
31                 } else {
32                         this.motion = "active"
33                         this.motionLatestValue = "active"
34                 }
35                 motionSensors.add(new MotionSensor(id, label, displayName, this.motion, this.motionLatestValue))
36         }
37
38         //By Model Checker
39         def setValue(LinkedHashMap eventDataMap) {
40                 if (eventDataMap["value"] != motionSensors[0].motion) {
41                         this.motionLatestValue = eventDataMap["value"]
42                         this.motion = eventDataMap["value"]
43                         this.currentMotion = eventDataMap["value"]
44                         motionSensors[0].setValue(eventDataMap["value"])
45                         sendEvent(eventDataMap)
46                 }
47         }
48
49         //Methods for closures
50         def count(Closure Input) {
51                 motionSensors.count(Input)
52         }
53         def size() {
54                 motionSensors.size()
55         }
56         def each(Closure Input) {
57                 motionSensors.each(Input)
58         }
59         def find(Closure Input) {
60                 motionSensors.find(Input)
61         }
62         def sort(Closure Input) {
63                 motionSensors.sort(Input)
64         }
65         def collect(Closure Input) {
66                 motionSensors.collect(Input)
67         }
68
69
70         def currentValue(String deviceFeature) {
71                 motionSensors[0].currentValue(deviceFeature)//It is called if we have only one device
72         }
73
74         def latestValue(String deviceFeature) {
75                 motionSensors[0].latestValue(deviceFeature)//It is called if we have only one device
76         }
77
78         def statesSince(String info, Date dateObj) {
79                 return motionSensors[0].statesSince(info, dateObj)
80         }
81
82         def getAt(int ix) {
83                 motionSensors[ix]
84         }
85 }