Infrastruction modification
[smartthings-infrastructure.git] / Valve / Valve.groovy
1 //Create a class for valve
2 package Valve
3 import SmartThing.SmartThing
4
5 public class Valve 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 currentValve = new StringBuilder()
12         // Maps from features to values
13         HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
14
15         Valve(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentValve) {
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.currentValve = currentValve
27
28                 deviceValuesMap.put("valve", currentValve)
29         }
30
31         // Methods to set values
32         def open() {
33                 action(currentValve, "open", "contact")
34         }
35
36         def open(LinkedHashMap metaData) {
37                 open()
38         }
39
40         def close() {
41                 action(currentValve, "closed", "contact")
42         }
43
44         def close(LinkedHashMap metaData) {
45                 close()
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 valve with id:$tmpID is $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 getCurrentValve() {
60                 return currentValve.toString()
61         }
62 }