Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[smartthings-infrastructure.git] / ColorControl / ColorControls.groovy
1 //Create a class for color control
2 package ColorControl
3 import Timer.SimulatedTimer
4
5
6 public class ColorControls {
7         private int deviceNumbers
8         private List colorControls
9         def sendEvent
10
11         //For one device(We cannot have obj.id)-> We should have obj[0].id
12         private String id = "colorControlID0"
13         private String label = "colorControl0"
14         private String displayName = "colorControl0"
15         private String color = "red"
16         private int hue = 50
17         private int saturation = 50
18         
19                 
20         ColorControls(Closure sendEvent, int deviceNumbers) {
21                 this.sendEvent = sendEvent
22                 this.deviceNumbers = deviceNumbers
23                 this.colorControls = []
24
25                 colorControls.add(new ColorControl(id, label, displayName, this.color, this.hue, this.saturation))
26         }
27
28         //Methods for closures
29         def count(Closure Input) {
30                 colorControls.count(Input)
31         }
32         def size() {
33                 colorControls.size()
34         }
35         def each(Closure Input) {
36                 colorControls.each(Input)
37         }
38         def find(Closure Input) {
39                 colorControls.find(Input)
40         }
41         def collect(Closure Input) {
42                 colorControls.collect(Input)
43         }
44
45         //By model checker
46         def setValue(LinkedHashMap eventDataMap) {
47                 if (eventDataMap["name"] == "color") {
48                         if (eventDataMap["value"] != colorControls[0].color) {
49                                 colorControls[0].setValue(eventDataMap["value"], "color")
50                                 this.color = colorControls[0].color
51                                 sendEvent(eventDataMap)
52                         }       
53                 } else if (eventDataMap["name"] == "hue") {
54                         if (eventDataMap["value"] != colorControls[0].hue) {
55                                 colorControls[0].setValue(eventDataMap["value"], "hue")
56                                 this.hue = colorControls[0].hue
57                                 sendEvent(eventDataMap)
58                         }
59                 } else {
60                         if (eventDataMap["value"] != colorControls[0].saturation) {
61                                 colorControls[0].setValue(eventDataMap["value"], "saturation")
62                                 this.saturation = colorControls[0].saturation
63                                 sendEvent(eventDataMap)
64                         }
65                 }
66         }
67
68
69         //methods
70         def setColor(String color) {
71                 colorControls[0].setColor(color)
72                 this.color = color      
73         }
74
75         def setHue(int hue) {
76                 colorControls[0].setHue(hue)
77                 this.hue = hue  
78         }
79
80         def setSaturation(int saturation) {
81                 colorControls[0].setSaturation(saturation)
82                 this.saturation = saturation    
83         }
84
85         def currentValue(String deviceFeature) {
86                 colorControls[0].currentValue(deviceFeature)
87         }
88
89         def getAt(int ix) {
90                 colorControls[ix]
91         }
92 }