a555d6892e9865ed131a5b702ec8fd3b9a97964b
[smartthings-infrastructure.git] / ThreeAxis / ThreeAxis.groovy
1 //Create a class for three axis
2 package ThreeAxis
3
4 public class ThreeAxis {
5         // id, label, and display name of the device
6         StringBuilder id = new StringBuilder()
7         StringBuilder label = new StringBuilder()
8         StringBuilder displayName = new StringBuilder()
9         // Other variables
10         def sendEvent
11         LinkedHashMap currentThreeAxis
12         
13
14         ThreeAxis(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, LinkedHashMap currentThreeAxis) {
15                 // Initialization
16                 this.id = id
17                 this.label = label
18                 this.displayName = displayName
19                 this.currentThreeAxis = currentThreeAxis
20                 this.sendEvent = sendEvent
21         }
22
23         // Methods to set values
24         def setValue(LinkedHashMap eventDataMap) {
25                 def tmpID = id.toString()
26                 currentThreeAxis = new groovy.json.JsonSlurper().parseText(eventDataMap["value"])
27                 println("the three axis with id:$tmpID of cube is chagned to $currentThreeAxis!")
28                 sendEvent(eventDataMap)
29         }
30
31         // Methods to return values
32         def getCurrentThreeAxis() {
33                 return currentThreeAxis
34         }
35
36         def currentState(String deviceFeature) {
37                 currentValue(deviceFeature)
38         }
39
40         def latestValue(String deviceFeature) {
41                 currentValue(deviceFeature)
42         }
43
44         def currentValue(String deviceFeature) {
45                 if (deviceFeature == "threeAxis" || deviceFeature == "status")
46                         return currentThreeAxis
47         }
48 }