-
Notifications
You must be signed in to change notification settings - Fork 9
/
iBrew.groovy
266 lines (216 loc) · 7.02 KB
/
iBrew.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import groovy.json.JsonSlurper
metadata {
definition (name: "iBrew", namespace: "jonbur", author: "JB") {
capability "Polling"
capability "Refresh"
capability "Switch"
capability "Sensor"
capability "Temperature Measurement"
command "refresh"
command "subscribe"
attribute "network","string"
attribute "bin","string"
}
preferences {
input("ip", "text", title: "IP Address", description: "iBrew Server Address", required: true, displayDuringSetup: true)
input("port", "number", title: "Port Number", description: "Port Number (Default:80)", defaultValue: "2080", required: true, displayDuringSetup: true)
input("mac", "text", title: "MAC Address", description: "MAC address of server")
input("kettleip", "text", title: "Kettle IP address", description: "IP address of the kettle")
}
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
state "on", label:'${name}', action:"switch.off", icon:"st.Home.home30", backgroundColor:"#79b821", nextState:"turningOff"
state "off", label:'${name}', action:"switch.on", icon:"st.Home.home30", backgroundColor:"#ffffff", nextState:"turningOn"
state "turningOn", label:'${name}', action:"switch.off", icon:"st.Home.home30", backgroundColor:"#79b821", nextState:"turningOff"
state "turningOff", label:'${name}', action:"switch.on", icon:"st.Home.home30", backgroundColor:"#ffffff", nextState:"turningOn"
state "offline", label:'${name}', icon:"st.Home.home30", backgroundColor:"#ff0000"
}
standardTile("refresh", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh"
}
standardTile("subscribe", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
state "default", label:"", action:"subscribe", icon:"st.Appliances.appliances3"
}
standardTile("temperature", "device.temperature", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
state "default", label:'${currentValue}°', unit:"C", icon:"st.Weather.weather2"
}
}
}
def parse(String description) {
def map
def headerString
def bodyString
def slurper
def result
map = stringToMap(description)
headerString = new String(map.headers.decodeBase64())
if (headerString.contains("200 OK")) {
try {
bodyString = new String(map.body.decodeBase64())
} catch (Exception e) {
// Keep this log for debugging StringIndexOutOfBoundsException issue
log.error("Exception decoding bytes in response")
throw e
}
slurper = new JsonSlurper()
result = slurper.parseText(bodyString)
switch (result.status) {
case "ready":
sendEvent(name: 'switch', value: "off" as String)
break;
case "heating":
sendEvent(name: 'switch', value: "on" as String)
}
log.debug "temp = " + result.sensors.temperature.raw.celsius
sendEvent(name: "temperature", value: result.sensors.temperature.raw.celsius, unit: "C")
}
else {
//processes callbacks
bodyString = new String(map.body.decodeBase64())
slurper = new JsonSlurper()
result = slurper.parseText(bodyString)
log.debug result
log.debug result.kettleheater
if (result.kettleheater.toString() == "true") {
log.debug "The kettle is on"
sendEvent(name: 'switch', value: "on" as String)
}
log.debug result.kettlebusy
if (result.kettlebusy.toString() == "false") {
log.debug "The kettle is off"
sendEvent(name: 'switch', value: "off" as String)
}
sendEvent(name: "temperature", value: result.temperature, unit: "C")
log.debug "temp = " + result.temperature
}
parse
}
// handle commands
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
initialize()
}
def initialize() {
log.info "iBrew ${textVersion()} ${textCopyright()}"
ipSetup()
poll()
}
def on() {
log.debug "Executing 'on'"
ipSetup()
api('on')
}
def off() {
log.debug "Executing 'off'"
api('off')
}
def poll() {
log.debug "Executing 'poll'"
if (device.deviceNetworkId != null) {
api('refresh')
}
else {
sendEvent(name: 'status', value: "error" as String)
sendEvent(name: 'network', value: "Not Connected" as String)
log.debug "DNI: Not set"
}
}
def refresh() {
log.debug "Executing 'refresh'"
ipSetup()
api('refresh')
}
def subscribe (){
log.debug "Executing 'subscribe'"
ipSetup()
subscribeAction()
}
def api(String APICommand, success = {}) {
def APIPath
def hubAction
switch (APICommand) {
case "on":
APIPath = "/api/" + settings.kettleip + "/start"
log.debug "The start command was sent"
break;
case "off":
APIPath = "/api/" + settings.kettleip + "/stop"
log.debug "The stop command was sent"
break;
case "refresh":
APIPath = "/api/" + settings.kettleip + "/status"
log.debug "The Status Command was sent"
break;
}
switch (APICommand) {
case "refresh":
try {
hubAction = new physicalgraph.device.HubAction(
method: "GET",
path: APIPath,
headers: [HOST: "${settings.ip}:${settings.port}", Accept: "application/json"])
}
catch (Exception e) {
log.debug "Hit Exception $e on $hubAction"
}
break;
default:
try {
hubAction = [new physicalgraph.device.HubAction(
method: "GET",
path: APIPath,
headers: [HOST: "${settings.ip}:${settings.port}", Accept: "application/json"]
), delayAction(1000), api('refresh')]
}
catch (Exception e) {
log.debug "Hit Exception $e on $hubAction"
}
break;
}
return hubAction
}
private subscribeAction(callbackPath="") {
def address = device.hub.getDataValue("localIP") + ":" + device.hub.getDataValue("localSrvPortTCP")
def result = new physicalgraph.device.HubAction(
method: "SUBSCRIBE",
path: "/api/" + settings.kettleip + "/smartthings",
headers: [
HOST: "${settings.ip}:${settings.port}",
CALLBACK: "<http://${address}/notify$callbackPath>",
TIMEOUT: "Second-3600"])
sendHubCommand(result)
}
def ipSetup() {
def hosthex
def porthex
if (settings.ip) {
hosthex = convertIPtoHex(settings.ip)
}
if (settings.port) {
porthex = convertPortToHex(settings.port)
}
if (settings.mac) {
device.deviceNetworkId = settings.mac
}
}
private String convertIPtoHex(ip) {
String hexip = ip.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
return hexip
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
return hexport
}
private delayAction(long time) {
new physicalgraph.device.HubAction("delay $time")
}
private def textVersion() {
def text = "Version 0.2"
}
private def textCopyright() {
def text = "Copyright © 2016 JB"
}