Infrastruction modification
[smartthings-infrastructure.git] / RelaySwitch / RelaySwitch.groovy
1 //Create a class for relay switch device
2 package RelaySwitch
3 import SmartThing.SmartThing
4
5 public class RelaySwitch extends SmartThing {
6         // id, label, and display name of the device
7         StringBuilder id = new StringBuilder()
8         StringBuilder label = new StringBuilder()
9         StringBuilder displayName = new StringBuilder()
10         // Features with string values
11         StringBuilder currentSwitch = new StringBuilder()
12         // Maps from features to values
13         HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
14
15         RelaySwitch(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentSwitch) {
16                 deviceValuesMap = deviceValueSmartThing
17                 idSmartThing = id
18                 labelSmartThing = label
19                 displayNameSmartThing = displayName
20                 sendEventSmartThings = sendEvent
21
22                 // Initialization
23                 this.id = id
24                 this.label = label
25                 this.displayName = displayName
26                 this.currentSwitch = currentSwitch
27
28                 deviceValuesMap.put("switch", currentSwitch)
29         }
30
31         // Methods to set values
32         def on() {
33                 action(currentSwitch, "on", "switch")
34         }
35
36         def on(LinkedHashMap metaData) {
37                 on()
38         }
39
40         def off() {
41                 action(currentSwitch, "off", "switch")
42         }
43
44         def off(LinkedHashMap metaData) {
45                 off()
46         }
47
48         def action(StringBuilder variable, String newValue, String feature) {
49                 if (!variable.toString().equals(newValue)) {
50                         String tmpID = id.toString()
51                         variable.replace(0, variable.length(), newValue)
52                         println("$feature of the relay switch with id:$tmpID is changed to $newValue!")
53                         sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
54                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
55                 }
56         }
57
58         // Methods to return values
59         def getCurrentSwitch() {
60                 return currentSwitch.toString()
61         }
62 }