Infrastructure compatible for all groups instead of switches.
[smartthings-infrastructure.git] / SwitchLevel / SwitchLevel.groovy
1 //Create a class for switch level
2 package SwitchLevel
3 import Timer.SimulatedTimer
4
5 public class SwitchLevel {
6         private String id
7         private String label
8         private String displayName
9         private String switchState
10         private String currentSwitch
11         private int level
12         private int rate
13         private int hue
14         private int saturation
15         private String switchLatestValue
16         def sendEvent   
17         def timers
18         
19
20         SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, int hue, int saturation, String switchState, String switchLatestValue) {
21                 this.sendEvent = sendEvent
22                 this.timers = new SimulatedTimer()
23                 this.id = id
24                 this.label = label
25                 this.displayName = displayName
26                 this.level = level
27                 this.rate = level
28                 this.hue = hue
29                 this.saturation = saturation
30                 this.switchState = switchState
31                 this.currentSwitch = switchState
32                 this.switchLatestValue = switchLatestValue
33         }
34
35         //By Apps
36         def setColor(LinkedHashMap metaData) {
37                 if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
38                         this.level = metaData["level"]
39                         this.rate = metaData["level"]
40                         this.hue = metaData["hue"]
41                         this.saturation = metaData["saturation"]
42                         println("the switch with id:$id is setted to level $level and hue to $hue and saturation to $saturation!")
43                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
44                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
45                         sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
46                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
47                         sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
48                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
49                 }
50         }       
51
52         def setLevel(int level) {
53                 if (this.level != level) {
54                         println("the switch with id:$id is setted to level $level!")
55                         this.level = level
56                         this.rate = level
57                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
58                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
59                 }
60         }
61         
62         def setLevel(long level) {
63                 if (this.level != level) {
64                         println("the switch with id:$id is setted to level $level!")
65                         this.level = level
66                         this.rate = level
67                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
68                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
69                 }
70         }
71
72         def on() {
73                 if (this.switchState != "on") {
74                         println("the switch with id:$id is on!")
75                         this.switchLatestValue = "on"
76                         this.switchState = "on"
77                         this.currentSwitch = "on"
78                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
79                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
80                 }
81         }
82
83         def on(LinkedHashMap metaData) {
84                 if (this.switchState != "on") {
85                         def task = timers.runAfter(metaData["delay"]) {
86                                 println("the switch with id:$id is on!")
87                                 this.switchLatestValue = "on"
88                                 this.switchState = "on"
89                                 this.currentSwitch = "on"
90                                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
91                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
92                         }
93                 }
94         }
95
96         def off() {
97                 if (this.switchState != "off") {
98                         println("the switch with id:$id is off!")
99                         this.switchLatestValue = "off"
100                         this.switchState = "off"
101                         this.currentSwitch = "off"
102                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
103                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
104                 }
105         }
106
107         def off(LinkedHashMap metaData) {
108                 if (this.switchState != "off") {
109                         def task = timers.runAfter(metaData["delay"]) {
110                                 println("the switch with id:$id is off!")
111                                 this.switchLatestValue = "off"
112                                 this.switchState = "off"
113                                 this.currentSwitch = "off"
114                                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
115                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
116                         }
117                 }
118         }
119
120         //By Model Checker
121         def setValue(String value, String name) {
122                 if (name == "switch") {
123                         println("the switch with id:$id is $value!")
124                         this.switchLatestValue = value
125                         this.switchState = value
126                         this.currentSwitch = value
127                 } else if (name == "level") {
128                         println("the switch with id:$id is setted to level $value!")
129                         this.level = value.toInteger()
130                         this.rate = value.toInteger()
131                 }
132         }
133
134
135         def currentValue(String deviceFeature) {
136                 if (deviceFeature == "level") {
137                         return level
138                 } else if (deviceFeature == "switch") {
139                         return switchState
140                 }
141         }
142
143         def latestValue(String deviceFeature) {
144                 if (deviceFeature == "level") {
145                         return level
146                 } else if (deviceFeature == "switch") {
147                         return switchState
148                 }
149         }
150 }