Infrastruction modification
[smartthings-infrastructure.git] / Thermostat / Thermostat.groovy
1 //Create a class for thermostat device
2 package Thermostat
3 import SmartThing.SmartThing
4
5 class Thermostat 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 numberical values
11         MutableInteger currentTemperature = new MutableInteger()
12         MutableInteger currentCoolingSetpoint = new MutableInteger()
13         MutableInteger currentHeatingSetpoint = new MutableInteger()
14         MutableInteger currentThermostatSetPoint = new MutableInteger()
15         // Features with string values
16         StringBuilder currentThermostatOperatingState = new StringBuilder()
17         StringBuilder currentThermostatFanMode = new StringBuilder()
18         StringBuilder currentThermostatMode = new StringBuilder()
19         StringBuilder currentClimateName = new StringBuilder()
20         // Maps from features to values
21         HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
22         HashMap<String, MutableInteger> deviceIntValuesMap = new HashMap<String, MutableInteger>()
23
24         Thermostat(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, MutableInteger currentTemperature, MutableInteger currentCoolingSetpoint, 
25                    MutableInteger currentHeatingSetpoint, MutableInteger currentThermostatSetPoint, StringBuilder currentThermostatOperatingState, StringBuilder currentThermostatFanMode, 
26                    StringBuilder currentThermostatMode, StringBuilder currentClimateName) {
27                 deviceValuesMap = deviceValueSmartThing
28                 deviceIntValuesMap = deviceIntValueSmartThing
29                 idSmartThing = id
30                 labelSmartThing = label
31                 displayNameSmartThing = displayName
32                 sendEventSmartThings = sendEvent
33
34                 // Initialization
35                 this.id = id
36                 this.label = label
37                 this.displayName = displayName
38                 this.currentTemperature = currentTemperature
39                 this.currentCoolingSetpoint = currentCoolingSetpoint
40                 this.currentHeatingSetpoint = currentHeatingSetpoint
41                 this.currentThermostatSetPoint = currentThermostatSetPoint
42                 this.currentThermostatOperatingState = currentThermostatOperatingState
43                 this.currentThermostatFanMode = currentThermostatFanMode
44                 this.currentThermostatMode = currentThermostatMode
45                 this.currentClimateName = currentClimateName
46
47                 deviceValuesMap.put("temperature", currentTemperature)
48                 deviceValuesMap.put("thermostatOperatingState", currentThermostatOperatingState)
49                 deviceValuesMap.put("thermostatFanMode", currentThermostatFanMode)
50                 deviceValuesMap.put("thermostatMode", currentThermostatMode)
51                 deviceIntValuesMap.put("coolingSetpoint", currentCoolingSetpoint)
52                 deviceIntValuesMap.put("heatingSetpoint", currentHeatingSetpoint)
53                 deviceIntValuesMap.put("thermostatSetpoint", currentThermostatSetPoint)
54         }
55
56         // Methods to set values
57         def setCoolingSetpoint(int newValue) {
58                 action(this.currentCoolingSetpoint, newValue, "coolingSetpoint")
59         }
60
61         def setCoolingSetpoint(String newValue) {
62                 setCoolingSetpoint(newValue.toInteger())
63         }
64
65         def setHeatingSetpoint(int newValue) {
66                 action(this.currentHeatingSetpoint, newValue, "heatingSetpoint")
67         }
68
69         def setHeatingSetpoint(String newValue) {
70                 setHeatingSetpoint(newValue.toInteger())
71         }
72
73         def setThermostatFanMode(String newValue) {
74                 action(this.currentThermostatFanMode, newValue, "thermostatFanMode")
75         }
76
77         def setThermostatMode(String newValue) {
78                 action(this.currentThermostatMode, newValue, "thermostatMode")
79         }
80
81         def cool() {
82                 action(this.currentThermostatMode, "cool", "thermostatMode")
83         }
84
85         def heat() {
86                 action(this.currentThermostatMode, "heat", "thermostatMode")
87         }
88
89         def auto() {
90                 action(this.currentThermostatMode, "auto", "thermostatMode")
91         }
92         
93         def emergencyHeat() {
94                 action(this.currentThermostatMode, "emergencyHeat", "thermostatMode")
95         }
96
97         def off() {
98                 action(this.currentThermostatMode, "off", "thermostatMode")
99         }
100
101         def setClimate(String info, String newValue) {
102                 action(currentClimateName, newValue, "climateName")
103         }
104
105         def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
106                 setHeatingSetpoint(heatingSetpoint)
107                 setCoolingSetpoint(coolingSetpoint)
108         }
109
110         def action(StringBuilder variable, String newValue, String feature) {
111                 if (!variable.toString().equals(newValue)) {
112                         String tmpID = id.toString()
113                         variable.replace(0, variable.length(), newValue)
114                         println("$feature of the thermostat with id:$tmpID is changed to $newValue!")
115                         sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
116                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
117                 }
118         }
119
120         def action(MutableInteger variable, int newValue, String feature) {
121                 if (!variable.getValue().equals(newValue)) {
122                         String tmpID = id.toString()
123                         variable.setValue(newValue)
124                         println("$feature for the thermostat with id:$tmpID is changed to $newValue!")
125                         sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
126                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
127                 }
128         }
129
130         // Methods to return values
131         def getCurrentTemperature() {
132                 return currentTemperature.getValue()
133         }
134
135         def getCurrentCoolingSetpoint() {
136                 return currentCoolingSetpoint.getValue()
137         }
138         
139         def getCurrentHeatingSetpoint() {
140                 return currentHeatingSetpoint.getValue()
141         }
142
143         def getCurrentThermostatSetPoint() {
144                 return currentThermostatSetPoint.getValue()
145         }
146
147         def getCurrentThermostatOperatingState() {
148                 return currentThermostatOperatingState.toString()
149         }
150
151         def getCurrentThermostatFanMode() {
152                 return currentThermostatFanMode.toString()
153         }
154
155         def getCurrentThermostatMode() {
156                 return currentThermostatMode.toString()
157         }
158
159         def getCurrentClimateName() {
160                 return currentClimateName.toString()
161         }
162 }