aa3b2c52f841001c55416f8c5845bc71c2e0297d
[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                 if (this.currentLevel != level) {
32                         println("the switch with id:$id is setted to level $level!")
33                         this.currentLevel = level
34                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
35                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
36                 }
37         }
38
39         def on() {
40                 if (this.switchState != "on") {
41                         println("the switch with id:$id is on!")
42                         this.switchLatestValue = "on"
43                         this.switchState = "on"
44                         this.currentSwitch = "on"
45                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
46                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
47                         sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
48                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
49                 }
50         }
51
52         def on(LinkedHashMap metaData) {
53                 if (this.switchState != "on") {
54                         def task = timers.runAfter(metaData["delay"]) {
55                                 println("the switch with id:$id is on!")
56                                 this.switchLatestValue = "on"
57                                 this.switchState = "on"
58                                 this.currentSwitch = "on"
59                                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
60                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
61                                 sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
62                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
63                         }
64                 }
65         }
66
67         def off() {
68                 if (this.switchState != "off") {
69                         println("the switch with id:$id is off!")
70                         this.switchLatestValue = "off"
71                         this.switchState = "off"
72                         this.currentSwitch = "off"
73                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
74                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
75                         sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
76                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
77                 }
78         }
79
80         def off(LinkedHashMap metaData) {
81                 if (this.switchState != "off") {
82                         def task = timers.runAfter(metaData["delay"]) {
83                                 println("the switch with id:$id is off!")
84                                 this.switchLatestValue = "off"
85                                 this.switchState = "off"
86                                 this.currentSwitch = "off"
87                                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
88                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
89                                 sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
90                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
91                         }
92                 }
93         }
94
95         //By Model Checker
96         def setValue(String value) {
97                 println("the switch with id:$id is $value!")
98                 this.switchLatestValue = value
99                 this.switchState = value
100                 this.currentSwitch = value
101         }
102         
103         def currentValue(String deviceFeature) {
104                 if (deviceFeature == "switch") {
105                         return switchState
106                 }
107         }
108
109         def latestValue(String deviceFeature) {
110                 if (deviceFeature == "switch") {
111                         return switchLatestValue
112                 }
113         }
114 }