Removing not required events in device handlers.
[smartthings-infrastructure.git] / RelaySwitch / RelaySwitch.groovy
1 //Create a class for relay switch device
2 package RelaySwitch
3 import Timer.SimulatedTimer
4
5 public class RelaySwitch {
6         private String id
7         private String label
8         private String displayName
9         private String switchState
10         private String currentSwitch
11         private String switchLatestValue
12         def sendEvent   
13         def timers
14         
15
16         RelaySwitch(Closure sendEvent, String id, String label, String displayName, String switchState, String currentSwitch, String switchLatestValue) {
17                 this.sendEvent = sendEvent
18                 this.timers = new SimulatedTimer()
19                 this.currentSwitch = currentSwitch
20                 this.id = id
21                 this.label = label
22                 this.displayName = displayName
23                 this.switchState = switchState
24                 this.switchLatestValue = switchLatestValue
25         }
26
27         //By Apps
28         def on() {
29                 if (switchState != "on") {
30                         println("the relay switch with id:$id is on!")
31                         this.switchLatestValue = "on"
32                         this.switchState = "on"
33                         this.currentSwitch = "on"
34                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
35                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
36                 }
37         }
38
39         def on(LinkedHashMap metaData) {
40                 if (switchState != "on") {
41                         def task = timers.runAfter(metaData["delay"]) {
42                                 println("the relay switch with id:$id is on!")
43                                 this.switchLatestValue = "on"
44                                 this.switchState = "on"
45                                 this.currentSwitch = "on"
46                                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
47                                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
48                         }
49                 }
50         }
51
52         def off() {
53                 if (switchState != "off") {
54                         println("the relay switch with id:$id is off!")
55                         this.switchLatestValue = "off"
56                         this.switchState = "off"
57                         this.currentSwitch = "off"
58                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
59                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
60                 }
61         }
62
63         def off(LinkedHashMap metaData) {
64                 if (switchState != "off") {
65                         def task = timers.runAfter(metaData["delay"]) {
66                                 println("the relay switch with id:$id is off!")
67                                 this.switchLatestValue = "off"
68                                 this.switchState = "off"
69                                 this.currentSwitch = "off"
70                                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
71                                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
72                         }
73                 }
74         }
75
76         //By Model Checker
77         def setValue(String value) {
78                 println("the relay switch with id:$id is $value!")
79                 this.switchLatestValue = value
80                 this.switchState = value
81                 this.currentSwitch = value
82         }
83         
84         def currentValue(String deviceFeature) {
85                 if (deviceFeature == "switch") {
86                         return switchState
87                 }
88         }
89
90         def latestValue(String deviceFeature) {
91                 if (deviceFeature == "switch") {
92                         return switchLatestValue
93                 }
94         }
95 }