Fixing a bug: should have been pushed, not push.
[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, boolean init) {
19                 this.sendEvent = sendEvent              
20                 this.deviceNumbers = deviceNumbers
21                 this.stepSensors = []
22
23                 if (init) {
24                         this.goal = 50
25                         this.steps = 0
26                 } else {
27                         this.goal = 60
28                         this.steps = 1
29                 }
30                 stepSensors.add(new StepSensor(id, label, displayName, this.steps, this.goal))
31         }
32
33         //By Model Checker
34         def setValue(LinkedHashMap eventDataMap) {
35                 if (eventDataMap["name"] == "steps") {
36                         if (eventDataMap["value"].toInteger() != stepSensors[0].steps) {
37                                 this.steps = eventDataMap["value"].toInteger()
38                                 stepSensors[0].setValue(eventDataMap["value"], "steps")
39                                 sendEvent(eventDataMap)
40                         }
41                 } else if (eventDataMap["name"] == "goal") {
42                         if (eventDataMap["value"].toInteger() != stepSensors[0].goal) {
43                                 this.goal = eventDataMap["value"].toInteger()
44                                 stepSensors[0].setValue(eventDataMap["value"], "goal")
45                                 sendEvent(eventDataMap)
46                         }
47                 }
48         }
49
50         //Methods for closures
51         def count(Closure Input) {
52                 stepSensors.count(Input)
53         }
54         def size() {
55                 stepSensors.size()
56         }
57         def each(Closure Input) {
58                 stepSensors.each(Input)
59         }
60         def find(Closure Input) {
61                 stepSensors.find(Input)
62         }
63         def sort(Closure Input) {
64                 stepSensors.sort(Input)
65         }
66         def collect(Closure Input) {
67                 stepSensors.collect(Input)
68         }
69
70
71         def currentValue(String deviceFeature) {
72                 stepSensors[0].currentValue(deviceFeature)//It is called if we have only one device
73         }
74
75         def latestValue(String deviceFeature) {
76                 stepSensors[0].currentValue(deviceFeature)//It is called if we have only one device
77         }
78
79         def getAt(int ix) {
80                 stepSensors[ix]
81         }
82 }