Commit #4
[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                 this.id = id
17                 this.displayName = displayName
18                 this.switchCurrentValue = switchCurrentValue
19                 this.switchLatestValue = switchLatestValue
20         }
21
22         //By Apps
23         def on() {
24                 println("the switch with id:$id is on!")
25                 this.switchLatestValue = this.switchCurrentValue
26                 this.switchCurrentValue = "on"
27                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
28                     displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
29         }
30
31         def on(LinkedHashMap metaData) {
32                 def task = timers.runAfter(metaData["delay"]) {
33                         println("the switch with id:$id is on!")
34                         this.switchLatestValue = this.switchCurrentValue
35                         this.switchCurrentValue = "on"
36                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
37                             displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
38                 }
39         }
40
41         def off() {
42                 println("the switch with id:$id is off!")
43                 this.switchLatestValue = this.switchCurrentValue
44                 this.switchCurrentValue = "off"
45                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
46                     displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
47         }
48
49         def off(LinkedHashMap metaData) {
50                 def task = timers.runAfter(metaData["delay"]) {
51                         println("the switch with id:$id is off!")
52                         this.switchLatestValue = this.switchCurrentValue
53                         this.switchCurrentValue = "off"
54                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
55                             displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
56                 }
57         }
58
59         //By Model Checker
60         def setValue(String value) {
61                 println("the switch with id:$id is $value!")
62                 this.switchLatestValue = this.switchCurrentValue
63                 this.switchCurrentValue = value
64         }
65         
66         def currentValue(String deviceFeature) {
67                 if (deviceFeature == "switch") {
68                         return switchCurrentValue
69                 }
70         }
71
72         def latestValue(String deviceFeature) {
73                 if (deviceFeature == "switch") {
74                         return switchLatestValue
75                 }
76         }
77 }