Changes in classes: new concept for latest value + all types of events generated...
[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"].toInteger() != stepSensors[0].steps) {
30                                 this.steps = eventDataMap["value"].toInteger()
31                                 stepSensors[0].setValue(eventDataMap["value"], "steps")
32                                 sendEvent(eventDataMap)
33                         }
34                 } else if (eventDataMap["name"] == "goal") {
35                         if (eventDataMap["value"].toInteger() != stepSensors[0].goal) {
36                                 this.goal = eventDataMap["value"].toInteger()
37                                 stepSensors[0].setValue(eventDataMap["value"], "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 sort(Closure Input) {
57                 stepSensors.sort(Input)
58         }
59         def collect(Closure Input) {
60                 stepSensors.collect(Input)
61         }
62
63
64         def currentValue(String deviceFeature) {
65                 stepSensors[0].currentValue(deviceFeature)//It is called if we have only one device
66         }
67
68         def getAt(int ix) {
69                 stepSensors[ix]
70         }
71 }