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