Commit #5
[smartthings-infrastructure.git] / Switch / Switches.groovy
1 //Create a class for switch device
2 package Switch
3
4 public class Switches {
5         private int id = 0      
6         private String displayName
7         private String switchCurrentValue
8         private String switchLatestValue
9         def sendEvent   
10         def timers
11         
12
13         Switches(Closure sendEvent, int id, String displayName, String switchCurrentValue, String switchLatestValue) {
14                 this.sendEvent = sendEvent
15                 this.timers = new Timer()
16                 timers.cancel() //Timer is ready to use
17                 this.id = id
18                 this.displayName = displayName
19                 this.switchCurrentValue = switchCurrentValue
20                 this.switchLatestValue = switchLatestValue
21         }
22
23         //By Apps
24         def on() {
25                 println("the switch with id:$id is on!")
26                 this.switchLatestValue = this.switchCurrentValue
27                 this.switchCurrentValue = "on"
28                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
29                     displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
30         }
31
32         def on(LinkedHashMap metaData) {
33                 def task = timers.runAfter(metaData["delay"]) {
34                         println("the switch with id:$id is on!")
35                         this.switchLatestValue = this.switchCurrentValue
36                         this.switchCurrentValue = "on"
37                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
38                             displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
39                 }
40         }
41
42         def off() {
43                 println("the switch with id:$id is off!")
44                 this.switchLatestValue = this.switchCurrentValue
45                 this.switchCurrentValue = "off"
46                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
47                     displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
48         }
49
50         def off(LinkedHashMap metaData) {
51                 def task = timers.runAfter(metaData["delay"]) {
52                         println("the switch with id:$id is off!")
53                         this.switchLatestValue = this.switchCurrentValue
54                         this.switchCurrentValue = "off"
55                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
56                             displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
57                 }
58         }
59
60         //By Model Checker
61         def setValue(String value) {
62                 println("the switch with id:$id is $value!")
63                 this.switchLatestValue = this.switchCurrentValue
64                 this.switchCurrentValue = value
65         }
66         
67         def currentValue(String deviceFeature) {
68                 if (deviceFeature == "switch") {
69                         return switchCurrentValue
70                 }
71         }
72
73         def latestValue(String deviceFeature) {
74                 if (deviceFeature == "switch") {
75                         return switchLatestValue
76                 }
77         }
78 }