1 //Create a class for SmartThing
5 import gov.nasa.jpf.vm.Verify
7 public class SmartThing {
8 def sendEventSmartThings
10 String labelSmartThing
11 String displayNameSmartThing
12 HashMap<String, String> deviceValueSmartThing = new HashMap<String, String>()
13 HashMap<String, Integer> deviceIntValueSmartThing = new HashMap<String, Integer>()
14 List<String> possibleValuesSmartThings = new ArrayList<String>()
16 // Method for handling events
17 def setValue(LinkedHashMap eventDataMap) {
18 def name = eventDataMap["name"]
19 def tmpID = eventDataMap["deviceId"]
20 def value = eventDataMap["value"]
22 if (deviceValueSmartThing.containsKey(name)) {
23 if (!value.equals(deviceValueSmartThing.get(name))) {
24 deviceValueSmartThing.put(name, value)
25 println("the $name of the $displayNameSmartThing with id:$tmpID is triggered to $value!")
26 sendEventSmartThings(eventDataMap)
28 } else if (deviceIntValueSmartThing.containsKey(name)) {
29 if (!value.equals(deviceIntValueSmartThing.get(name))) {
30 deviceIntValueSmartThing.put(name, value)
31 println("the $name of the $displayNameSmartThing with id:$tmpID is triggered to $value!")
32 sendEventSmartThings(eventDataMap)
35 println("the $name of the $displayNameSmartThing with id:$tmpID is triggered to $value!")
36 sendEventSmartThings(eventDataMap)
45 if (labelSmartThing.equals("humidity") || labelSmartThing.equals("temperature")) {
52 def sendCurrentValue() {
53 def evtTemp = [[name: labelSmartThing, value: deviceIntValueSmartThing.get(labelSmartThing), deviceId: idSmartThing, descriptionText: "",
54 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
55 def init = Verify.getInt(0,1)
57 if (init == 0) {// return empty set
59 } else if (init == 1) {// send one open event
67 def sendPossibleValues() {
68 def evtA = [[name: labelSmartThing, value: possibleValuesSmartThings[0], deviceId: idSmartThing, descriptionText: "",
69 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
70 def evtB = [[name: labelSmartThing, value: possibleValuesSmartThings[1], deviceId: idSmartThing, descriptionText: "",
71 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
72 def init = Verify.getInt(0,4)
74 if (init == 0) {// return empty set
76 } else if (init == 1) {// send one event A
81 } else if (init == 2) {// send two events A
89 } else if (init == 3) {// send one event B
94 } else if (init == 4) {// send two events B
105 // Methods to set values
106 def action(String newValue, String feature) {
107 if (!deviceValueSmartThing.get(feature).equals(newValue)) {
108 deviceValueSmartThing.put(feature, newValue)
109 println("$feature of the $displayNameSmartThing with id:$idSmartThing is changed to $newValue!")
110 sendEventSmartThings([name: feature, value: newValue, deviceId: idSmartThing, descriptionText: "",
111 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
115 def action(int newValue, String feature) {
116 if (!deviceIntValueSmartThing.get(feature).equals(newValue)) {
117 deviceIntValueSmartThing.put(feature, newValue)
118 println("$feature of the $displayNameSmartThing with id:$idSmartThing is changed to $newValue!")
119 sendEventSmartThings([name: feature, value: newValue, deviceId: idSmartThing, descriptionText: "",
120 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
124 // Methods to return values
125 def propertyMissing(String currentProperty) {
126 String property = currentProperty
127 if (property.contains("current")) // Check to see if we have currentXXX or xxx
128 property = property.substring(7,8).toLowerCase()+property.substring(8);
130 if (deviceValueSmartThing.containsKey(property)) {
131 return deviceValueSmartThing.get(property)
132 } else if (deviceIntValueSmartThing.containsKey(property)) {
133 return deviceIntValueSmartThing.get(property)
135 println("This capability does not support this property!")
139 def currentState(String deviceFeature) {
140 return [rawDateCreated: [time: System.currentTimeMillis()]]
143 def currentValue(String deviceFeature) {
144 if (deviceValueSmartThing.containsKey(deviceFeature)) {
145 return deviceValueSmartThing.get(deviceFeature)
146 } else if (deviceIntValueSmartThing.containsKey(deviceFeature)) {
147 return deviceIntValueSmartThing.get(deviceFeature)
149 println("Wrong device feature is sent to this method!")
153 def latestValue(String deviceFeature) {
154 currentValue(deviceFeature)