1ceac32e84a3850693bbf0b25045d7430aef608e
[smartthings-infrastructure.git] / Lock / Lock.groovy
1 //Create a class for lock device
2 package Lock
3 import SmartThing.SmartThing
4
5 public class Lock 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 currentLock = new StringBuilder()
12         // Maps from features to values
13         HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
14
15
16         Lock(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentLock) {
17                 deviceValuesMap = deviceValueSmartThing
18                 idSmartThing = id
19                 labelSmartThing = label
20                 displayNameSmartThing = displayName
21                 sendEventSmartThings = sendEvent
22
23                 // Initialization
24                 this.id = id
25                 this.label = label
26                 this.displayName = displayName
27                 this.currentLock = currentLock
28
29                 deviceValuesMap.put("lock", currentLock)
30         }
31
32         // Methods to set values
33         def lock() {
34                 action(currentLock, "locked")
35         }
36
37         def lock(LinkedHashMap metaData) {
38                 lock()
39         }
40         
41         def unlock() {
42                 action(currentLock, "unlocked")
43         }
44
45         def unlock(LinkedHashMap metaData) {
46                 unlock()
47         }
48
49         def action(StringBuilder variable, String newValue) {
50                 if (!variable.toString().equals(newValue)) {
51                         String tmpID = id.toString()
52                         variable.replace(0, variable.length(), newValue)
53                         println("Lock with id:$tmpID is changed to $newValue!")
54                         sendEvent([name: "lock", value: newValue, deviceId: tmpID, descriptionText: "",
55                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
56                 }
57         }
58
59         // Methods to return values
60         def getCurrentLock() {
61                 return currentLock.toString()
62         }
63
64 }