7db6a168d8cecdbe1ab33404aa6ab33b1e54ec08
[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(String level) {
53                 def newLevel = level.toInteger()
54                 setLevel(newLevel)
55         }       
56
57         def setLevel(int level) {
58                 if (this.level != level) {
59                         println("the switch with id:$id is setted to level $level!")
60                         this.level = level
61                         this.rate = level
62                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
63                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
64                 }
65         }
66         
67         def setLevel(long level) {
68                 if (this.level != level) {
69                         println("the switch with id:$id is setted to level $level!")
70                         this.level = level
71                         this.rate = level
72                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
73                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
74                 }
75         }
76
77         def on() {
78                 if (this.switchState != "on") {
79                         println("the switch with id:$id is on!")
80                         this.switchLatestValue = "on"
81                         this.switchState = "on"
82                         this.currentSwitch = "on"
83                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
84                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
85                 }
86         }
87
88         def on(LinkedHashMap metaData) {
89                 if (this.switchState != "on") {
90                         def task = timers.runAfter(metaData["delay"]) {
91                                 println("the switch with id:$id is on!")
92                                 this.switchLatestValue = "on"
93                                 this.switchState = "on"
94                                 this.currentSwitch = "on"
95                                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
96                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
97                         }
98                 }
99         }
100
101         def off() {
102                 if (this.switchState != "off") {
103                         println("the switch with id:$id is off!")
104                         this.switchLatestValue = "off"
105                         this.switchState = "off"
106                         this.currentSwitch = "off"
107                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
108                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
109                 }
110         }
111
112         def off(LinkedHashMap metaData) {
113                 if (this.switchState != "off") {
114                         def task = timers.runAfter(metaData["delay"]) {
115                                 println("the switch with id:$id is off!")
116                                 this.switchLatestValue = "off"
117                                 this.switchState = "off"
118                                 this.currentSwitch = "off"
119                                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
120                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
121                         }
122                 }
123         }
124
125         //By Model Checker
126         def setValue(String value, String name) {
127                 if (name == "switch") {
128                         println("the switch with id:$id is $value!")
129                         this.switchLatestValue = value
130                         this.switchState = value
131                         this.currentSwitch = value
132                 } else if (name == "level") {
133                         println("the switch with id:$id is setted to level $value!")
134                         this.level = value.toInteger()
135                         this.rate = value.toInteger()
136                 }
137         }
138
139
140         def currentValue(String deviceFeature) {
141                 if (deviceFeature == "level") {
142                         return level
143                 } else if (deviceFeature == "switch") {
144                         return switchState
145                 }
146         }
147
148         def latestValue(String deviceFeature) {
149                 if (deviceFeature == "level") {
150                         return level
151                 } else if (deviceFeature == "switch") {
152                         return switchState
153                 }
154         }
155 }