Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[smartthings-infrastructure.git] / StepSensor / StepSensors.groovy
1 //Create a class for step sensor
2 package StepSensor
3 import Timer.SimulatedTimer
4
5 public class StepSensors {
6         private int deviceNumbers
7         private List stepSensors
8         def sendEvent
9
10         //For one device(We cannot have obj.id)-> We should have obj[0].id
11         private String id = "stepSensorID0"
12         private String label = "stepSensor0"
13         private String displayName = "stepSensor0"
14         private int goal = 1000
15         private int steps = 0
16
17                 
18         StepSensors(Closure sendEvent, int deviceNumbers) {
19                 this.sendEvent = sendEvent              
20                 this.deviceNumbers = deviceNumbers
21                 this.stepSensors = []
22
23                 stepSensors.add(new StepSensor(id, label, displayName, this.steps, this.goal))
24         }
25
26         //By Model Checker
27         def setValue(LinkedHashMap eventDataMap) {
28                 if (eventDataMap["name"] == "steps") {
29                         if (eventDataMap["value"] != stepSensors[0].steps) {
30                                 stepSensors[0].setValue(eventDataMap["value"], "steps")
31                                 this.steps = stepSensors[0].steps
32                                 sendEvent(eventDataMap)
33                         }
34                 } else if (eventDataMap["value"] == "goal") {
35                         if (eventDataMap["value"] != stepSensors[0].goal) {
36                                 stepSensors[0].setValue(eventDataMap["value"], "goal")
37                                 this.goal = stepSensors[0].goal
38                                 sendEvent(eventDataMap)
39                         }
40                 }
41         }
42
43         //Methods for closures
44         def count(Closure Input) {
45                 stepSensors.count(Input)
46         }
47         def size() {
48                 stepSensors.size()
49         }
50         def each(Closure Input) {
51                 stepSensors.each(Input)
52         }
53         def find(Closure Input) {
54                 stepSensors.find(Input)
55         }
56         def collect(Closure Input) {
57                 stepSensors.collect(Input)
58         }
59
60
61         def currentValue(String deviceFeature) {
62                 stepSensors[0].currentValue(deviceFeature)//It is called if we have only one device
63         }
64
65         def getAt(int ix) {
66                 stepSensors[ix]
67         }
68 }