b869773031db6367270e65edbc88d3291137f6a9
[smartthings-infrastructure.git] / SmartThing / SmartThing.groovy
1 //Create a class for SmartThing
2 package SmartThing
3
4 //JPF's Verify API
5 import gov.nasa.jpf.vm.Verify
6
7 public class SmartThing {
8         def sendEventSmartThings
9         String idSmartThing
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>()
15
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"]
21
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)
27                         }
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)
33                         }
34                 } else {
35                         println("the $name of the $displayNameSmartThing with id:$tmpID is triggered to $value!")
36                         sendEventSmartThings(eventDataMap)
37                 }
38         }
39
40         def statesSince() {
41                 eventsSince()
42         }
43
44         def eventsSince() {
45                 if (labelSmartThing.equals("humidity") || labelSmartThing.equals("temperature")) {
46                         sendCurrentValue()
47                 } else {
48                         sendPossibleValues()
49                 }
50         }
51
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)
56                 def evtToSend = []
57                 if (init == 0) {// return empty set
58                         return evtToSend
59                 } else if (init == 1) {// send one open event
60                         evtTemp.each{
61                                 evtToSend.add(it)
62                         }
63                         return evtToSend
64                 }
65         }
66
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)
73                 def evtToSend = []
74                 if (init == 0) {// return empty set
75                         return evtToSend
76                 } else if (init == 1) {// send one event A
77                         evtA.each{
78                                 evtToSend.add(it)
79                         }
80                         return evtToSend
81                 } else if (init == 2) {// send two events A
82                         evtA.each{
83                                 evtToSend.add(it)
84                         }
85                         evtA.each{
86                                 evtToSend.add(it)
87                         }
88                         return evtToSend
89                 } else if (init == 3) {// send one event B
90                         evtB.each{
91                                 evtToSend.add(it)
92                         }
93                         return evtToSend
94                 } else if (init == 4) {// send two events B
95                         evtB.each{
96                                 evtToSend.add(it)
97                         }
98                         evtB.each{
99                                 evtToSend.add(it)
100                         }
101                         return evtToSend
102                 }
103         }
104
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"}'])
112                 }
113         }
114
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"}'])
121                 }
122         }
123
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);
129
130                 if (deviceValueSmartThing.containsKey(property)) {
131                         return deviceValueSmartThing.get(property)
132                 } else if (deviceIntValueSmartThing.containsKey(property)) {
133                         return deviceIntValueSmartThing.get(property)
134                 } else {
135                         println("This capability does not support this property!")
136                 }
137         }
138
139         def currentState(String deviceFeature) {
140                 return [rawDateCreated: [time: System.currentTimeMillis()]]
141         }
142         
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)      
148                 } else {
149                         println("Wrong device feature is sent to this method!")
150                 }
151         }
152
153         def latestValue(String deviceFeature) {
154                 currentValue(deviceFeature)
155         }
156 }