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