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