Infrastructure compatible with swiches
[smartthings-infrastructure.git] / SmartThing / SmartThing.groovy
index 7f08910f9e940639cc6070a229dd20adeb0e8964..ec4907a79e9a795f86fb81d8853dfb83ccba950e 100644 (file)
@@ -4,52 +4,45 @@ package SmartThing
 //JPF's Verify API
 import gov.nasa.jpf.vm.Verify
 
-//Importing mutable integer class
-import MutableInteger.MutableInteger
-
-public class SmartThing {      
-       List nonStoredDevices = ["aeonKeyFob", "appTouch", "button", "momentary", "nfcTouch"] // Devices with no stored value
+public class SmartThing {
        def sendEventSmartThings
-
-       StringBuilder idSmartThing = new StringBuilder()
-       StringBuilder labelSmartThing = new StringBuilder()
-       StringBuilder displayNameSmartThing = new StringBuilder()
-       HashMap<String, StringBuilder> deviceValueSmartThing = new HashMap<String, StringBuilder>()
-       HashMap<String, MutableInteger> deviceIntValueSmartThing = new HashMap<String, MutableInteger>()
-       List<StringBuilder> possibleValuesSmartThings = new ArrayList<StringBuilder>();
+       String idSmartThing
+       String labelSmartThing
+       String displayNameSmartThing
+       HashMap<String, String> deviceValueSmartThing = new HashMap<String, String>()
+       HashMap<String, Integer> deviceIntValueSmartThing = new HashMap<String, Integer>()
+       List<String> possibleValuesSmartThings = new ArrayList<String>()
 
        // Method for handling events
-       def setValue(LinkedHashMap eventDataMap) {
-               def name = eventDataMap["name"]
-               def tmpID = eventDataMap["deviceId"]
-               def value = eventDataMap["value"]
+        def setValue(LinkedHashMap eventDataMap) {
+                def name = eventDataMap["name"]
+                def tmpID = eventDataMap["deviceId"]
+                def value = eventDataMap["value"]
 
-               if (deviceValueSmartThing.containsKey(name)) {
-                       StringBuilder tmpStr = deviceValueSmartThing.get(name)
-                       if (!value.equals(tmpStr.toString())) {
-                               tmpStr.replace(0, tmpStr.length(), value)
-                               println("the $name with id:$tmpID is triggered to $value!")
-                               sendEventSmartThings(eventDataMap)
-                       }
-               } else if (deviceIntValueSmartThing.containsKey(name)) {
-                       MutableInteger tmpInt = deviceIntValueSmartThing.get(name)
-                       if (!value.equals(tmpInt.getValue())) {
-                               tmpInt.setValue(value)
-                               println("the $name with id:$tmpID is triggered to $value!")
-                               sendEventSmartThings(eventDataMap)
-                       }
-               } else if (nonStoredDevices.contains(name)) {
-                       println("the $name with id:$tmpID is triggered to $value!")
-                       sendEventSmartThings(eventDataMap)
-               }
-       }
+                if (deviceValueSmartThing.containsKey(name)) {
+                        if (!value.equals(deviceValueSmartThing.get(name))) {
+                                deviceValueSmartThing.put(name, value)
+                                println("the $name of the $displayNameSmartThing with id:$tmpID is triggered to $value!")
+                                sendEventSmartThings(eventDataMap)
+                        }
+                } else if (deviceIntValueSmartThing.containsKey(name)) {
+                        if (!value.toInteger().equals(deviceIntValueSmartThing.get(name))) {
+                                deviceIntValueSmartThing.put(name, value.toInteger())
+                                println("the $name of the $displayNameSmartThing with id:$tmpID is triggered to $value!")
+                                sendEventSmartThings(eventDataMap)
+                        }
+                } else {
+                        println("the $name of the $displayNameSmartThing with id:$tmpID is triggered to $value!")
+                        sendEventSmartThings(eventDataMap)
+                }
+        }
 
        def statesSince() {
                eventsSince()
        }
 
        def eventsSince() {
-               if (labelSmartThing.toString().equals("humidity") || labelSmartThing.toString().equals("temperature")) {
+               if (labelSmartThing.equals("humidity") || labelSmartThing.equals("temperature")) {
                        sendCurrentValue()
                } else {
                        sendPossibleValues()
@@ -57,8 +50,7 @@ public class SmartThing {
        }
 
        def sendCurrentValue() {
-               def label = labelSmartThing.toString()
-               def evtTemp = [[name: label, value: deviceIntValueSmartThing.get(label).getValue(), deviceId: idSmartThing.toString(), descriptionText: "",
+               def evtTemp = [[name: labelSmartThing, value: deviceIntValueSmartThing.get(labelSmartThing).toString(), deviceId: idSmartThing, descriptionText: "",
                                displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
                def init = Verify.getInt(0,1)
                def evtToSend = []
@@ -73,9 +65,9 @@ public class SmartThing {
        }
 
        def sendPossibleValues() {
-               def evtA = [[name: labelSmartThing.toString(), value: possibleValuesSmartThings[0].toString(), deviceId: idSmartThing.toString(), descriptionText: "",
+               def evtA = [[name: labelSmartThing, value: possibleValuesSmartThings[0], deviceId: idSmartThing, descriptionText: "",
                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
-               def evtB = [[name: labelSmartThing.toString(), value: possibleValuesSmartThings[1].toString(), deviceId: idSmartThing.toString(), descriptionText: "",
+               def evtB = [[name: labelSmartThing, value: possibleValuesSmartThings[1], deviceId: idSmartThing, descriptionText: "",
                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
                def init = Verify.getInt(0,4)
                def evtToSend = []
@@ -110,16 +102,49 @@ public class SmartThing {
                }
        }
 
+       // Methods to set values
+       def action(String newValue, String feature) {
+               if (!deviceValueSmartThing.get(feature).equals(newValue)) {
+                       deviceValueSmartThing.put(feature, newValue)
+                       println("$feature of the $displayNameSmartThing with id:$idSmartThing is changed to $newValue!")
+                       sendEventSmartThings([name: feature, value: newValue, deviceId: idSmartThing, descriptionText: "",
+                                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+       }
+
+       def action(int newValue, String feature) {
+               if (!deviceIntValueSmartThing.get(feature).equals(newValue)) {
+                       deviceIntValueSmartThing.put(feature, newValue)
+                       println("$feature of the $displayNameSmartThing with id:$idSmartThing is changed to $newValue!")
+                       sendEventSmartThings([name: feature, value: newValue.toString(), deviceId: idSmartThing, descriptionText: "",
+                                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+       }
+
        // Methods to return values
+       def propertyMissing(String currentProperty) {
+               String property = currentProperty
+               if (property.contains("current")) // Check to see if we have currentXXX or xxx
+                       property = property.substring(7,8).toLowerCase()+property.substring(8);
+
+               if (deviceValueSmartThing.containsKey(property)) {
+                       return deviceValueSmartThing.get(property)
+               } else if (deviceIntValueSmartThing.containsKey(property)) {
+                       return deviceIntValueSmartThing.get(property)
+               } else {
+                       println("This capability does not support this property!")
+               }
+       }
+
        def currentState(String deviceFeature) {
                return [rawDateCreated: [time: System.currentTimeMillis()]]
        }
        
        def currentValue(String deviceFeature) {
                if (deviceValueSmartThing.containsKey(deviceFeature)) {
-                       return deviceValueSmartThing.get(deviceFeature).toString()
+                       return deviceValueSmartThing.get(deviceFeature)
                } else if (deviceIntValueSmartThing.containsKey(deviceFeature)) {
-                       return deviceIntValueSmartThing.get(deviceFeature).getValue()   
+                       return deviceIntValueSmartThing.get(deviceFeature)      
                } else {
                        println("Wrong device feature is sent to this method!")
                }
@@ -128,16 +153,4 @@ public class SmartThing {
        def latestValue(String deviceFeature) {
                currentValue(deviceFeature)
        }
-
-       def getId() {
-               return idSmartThing.toString()
-       }
-
-       def getLabel() {
-               return labelSmartThing.toString()
-       }
-       
-       def getDisplayName() {
-               return displayNameSmartThing.toString()
-       }
 }