f9337f9c6e83d2bb4c034608abbd2f07cd32a813
[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 String switchLatestValue
14         def sendEvent   
15         def timers
16         
17
18         SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, String switchState, String switchLatestValue) {
19                 this.sendEvent = sendEvent
20                 this.timers = new SimulatedTimer()
21                 this.id = id
22                 this.label = label
23                 this.displayName = displayName
24                 this.level = level
25                 this.rate = level
26                 this.switchState = switchState
27                 this.currentSwitch = switchState
28                 this.switchLatestValue = switchLatestValue
29         }
30
31         //By Apps
32         def setLevel(int level) {
33                 if (this.level != level) {
34                         println("the switch with id:$id is setted to level $level!")
35                         this.level = level
36                         this.rate = level
37                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
38                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
39                 }
40         }
41         
42         def setLevel(long level) {
43                 if (this.level != level) {
44                         println("the switch with id:$id is setted to level $level!")
45                         this.level = level
46                         this.rate = level
47                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
48                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
49                 }
50         }
51
52         def on() {
53                 if (this.switchState != "on") {
54                         println("the switch with id:$id is on!")
55                         this.switchLatestValue = "on"
56                         this.switchState = "on"
57                         this.currentSwitch = "on"
58                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
59                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
60                 }
61         }
62
63         def on(LinkedHashMap metaData) {
64                 if (this.switchState != "on") {
65                         def task = timers.runAfter(metaData["delay"]) {
66                                 println("the switch with id:$id is on!")
67                                 this.switchLatestValue = "on"
68                                 this.switchState = "on"
69                                 this.currentSwitch = "on"
70                                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
71                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
72                         }
73                 }
74         }
75
76         def off() {
77                 if (this.switchState != "off") {
78                         println("the switch with id:$id is off!")
79                         this.switchLatestValue = "off"
80                         this.switchState = "off"
81                         this.currentSwitch = "off"
82                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
83                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
84                 }
85         }
86
87         def off(LinkedHashMap metaData) {
88                 if (this.switchState != "off") {
89                         def task = timers.runAfter(metaData["delay"]) {
90                                 println("the switch with id:$id is off!")
91                                 this.switchLatestValue = "off"
92                                 this.switchState = "off"
93                                 this.currentSwitch = "off"
94                                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
95                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
96                         }
97                 }
98         }
99
100         //By Model Checker
101         def setValue(String value, String name) {
102                 if (name == "switch") {
103                         println("the switch with id:$id is $value!")
104                         this.switchLatestValue = value
105                         this.switchState = value
106                         this.currentSwitch = value
107                 } else if (name == "level") {
108                         println("the switch with id:$id is setted to level $value!")
109                         this.level = value.toInteger()
110                         this.rate = value.toInteger()
111                 }
112         }
113
114
115         def currentValue(String deviceFeature) {
116                 if (deviceFeature == "level") {
117                         return level
118                 } else if (deviceFeature == "switch") {
119                         return switchState
120                 }
121         }
122
123         def latestValue(String deviceFeature) {
124                 if (deviceFeature == "level") {
125                         return level
126                 } else if (deviceFeature == "switch") {
127                         return switchState
128                 }
129         }
130 }