Infrastruction modification
[smartthings-infrastructure.git] / Switch / Switch.groovy
1 //Create a class for switch device
2 package Switch
3 import SmartThing.SmartThing
4
5 public class Switch 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         // Possible values for eventsSince method
15         List<StringBuilder> possibleValues = new ArrayList<StringBuilder>();
16
17         Switch(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentSwitch) {
18                 deviceValuesMap = deviceValueSmartThing
19                 idSmartThing = id
20                 labelSmartThing = label
21                 displayNameSmartThing = displayName
22                 sendEventSmartThings = sendEvent
23                 possibleValuesSmartThings = possibleValues
24
25                 // Initialization
26                 this.id = id
27                 this.label = label
28                 this.displayName = displayName
29                 this.currentSwitch = currentSwitch
30                 possibleValues.add("on")
31                 possibleValues.add("off")
32
33                 deviceValuesMap.put("switch", currentSwitch)
34         }
35
36         // Methods to set values
37         def on() {
38                 action(currentSwitch, "on", "switch")
39         }
40
41         def on(LinkedHashMap metaData) {
42                 on()
43         }
44
45         def off() {
46                 action(currentSwitch, "off", "switch")
47         }
48
49         def off(LinkedHashMap metaData) {
50                 off()
51         }
52
53         def action(StringBuilder variable, String newValue, String feature) {
54                 if (!variable.toString().equals(newValue)) {
55                         String tmpID = id.toString()
56                         variable.replace(0, variable.length(), newValue)
57                         println("$feature of the light with id:$tmpID is changed to $newValue!")
58                         sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
59                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
60                 }
61         }
62
63         // Methods to return values
64         def getCurrentSwitch() {
65                 return currentSwitch.toString()
66         }
67 }