-
Notifications
You must be signed in to change notification settings - Fork 42
/
RainMachine.SmartApp.groovy
427 lines (387 loc) · 11.6 KB
/
RainMachine.SmartApp.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
* RainMachine Service Manager SmartApp
*
* Author: Jason Mok
* Date: 2014-12-20
*
***************************
*
* Copyright 2014 Jason Mok
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
**************************
*
* REQUIREMENTS
* 1) This only works for firmware version 3.63 on RainMachine
* 2) You know your external IP address
* 3) You have forwarded port 80 (Currently does not work with SSL 443/18443, this is smartthings limitation).
* 4) You must have all scripts installed
*
**************************
*
* USAGE
* 1) Put this in SmartApp. Don't install until you have all other device types scripts added
* 2) Configure the first page which collects your ip address & port and password to log in to RainMachine
* 3) For each items you pick on the Programs/Zones page, it will create a device
* 4) Enjoy!
*
*/
definition(
name: "RainMachine",
namespace: "copy-ninja",
author: "Jason Mok",
description: "Connect your RainMachine to control your irrigation",
category: "SmartThings Labs",
iconUrl: "http://smartthings.copyninja.net/icons/[email protected]",
iconX2Url: "http://smartthings.copyninja.net/icons/[email protected]",
iconX3Url: "http://smartthings.copyninja.net/icons/[email protected]"
)
preferences {
page(name: "prefLogIn", title: "RainMachine")
page(name: "prefListProgramsZones", title: "RainMachine")
}
/* Preferences */
def prefLogIn() {
def showUninstall = ip_address != null && password != null && ip_port != null
return dynamicPage(name: "prefLogIn", title: "Connect to RainMachine", nextPage:"prefListProgramsZones", uninstall:showUninstall, install: false) {
section("Server Information"){
input("ip_address", "text", title: "IP Address/Host Name", description: "IP Address/Host Name of RainMachine")
input("ip_port", "text", title: "Port Number", description: "Forwarded port RainMachine")
}
section("Login Credentials"){
input("password", "password", title: "Password", description: "RainMachine password")
}
section("Server Polling"){
input("polling", "int", title: "Polling Interval (in minutes)", description: "in minutes", defaultValue: 5)
}
}
}
def prefListProgramsZones() {
if (forceLogin()) {
return dynamicPage(name: "prefListProgramsZones", title: "Programs/Zones", install:true, uninstall:true) {
section("Select which programs to use"){
input(name: "programs", type: "enum", required:false, multiple:true, metadata:[values:getProgramList()])
}
section("Select which zones to use"){
input(name: "zones", type: "enum", required:false, multiple:true, metadata:[values:getZoneList()])
}
}
}
}
/* Initialization */
def installed() {
log.info "installed()"
log.debug "Installed with settings: " + settings
unschedule()
forceLogin()
initialize()
}
def updated() {
log.info "updated()"
log.debug "Updated with settings: " + settings
state.polling = [
last: now(),
runNow: true
]
unschedule()
unsubscribe()
login()
initialize()
}
def uninstalled() {
def delete = getAllChildDevices()
delete.each { deleteChildDevice(it.deviceNetworkId) }
}
def initialize() {
log.info "initialize()"
// Get initial device status in state.data
refresh()
def progZones = []
def programList = [:]
def zoneList = [:]
def delete
// Collect programs and zones
if (settings.programs) {
if (settings.programs[0].size() > 1) {
progZones = settings.programs
} else {
progZones.add(settings.programs)
}
programList = getProgramList()
}
if (settings.zones) {
if (settings.zones[0].size() > 1) {
settings.zones.each { dni -> progZones.add(dni)}
} else {
progZones.add(settings.zones)
}
zoneList = getZoneList()
}
// Create device if selected and doesn't exist
progZones.each { dni ->
def childDevice = getChildDevice(dni)
def childDeviceAttrib = [:]
if (!childDevice) {
if (dni.contains("prog")) {
childDeviceAttrib = ["name": "RainMachine Program: " + programList[dni], "completedSetup": true]
} else if (dni.contains("zone")) {
childDeviceAttrib = ["name": "RainMachine Zone: " + zoneList[dni], "completedSetup": true]
}
addChildDevice("copy-ninja", "RainMachine", dni, null, childDeviceAttrib)
}
}
// Delete child devices that are not selected in the settings
if (!progZones) {
delete = getAllChildDevices()
} else {
delete = getChildDevices().findAll {
!progZones.contains(it.deviceNetworkId)
}
}
delete.each { deleteChildDevice(it.deviceNetworkId) }
// Schedule polling
schedule("0 0/" + (settings.polling.toInteger() > 0 )? settings.polling.toInteger() : 1 + " * * * ?", refresh )
}
/* Access Management */
private forceLogin() {
//Reset token and expiry
state.auth = [
expires_in: now() - 500,
access_token: ""
]
state.polling = [
last: now(),
runNow: true
]
state.data = [:]
state.pause = [:]
return doLogin()
}
private login() {
if (!(state.auth.expires_in > now())) {
return doLogin()
} else {
return true
}
}
private doLogin() {
// TODO: make call through hub later...
apiPost("/api/4/auth/login",[pwd: settings.password, remember: 1]) { response ->
if (response.status == 200) {
state.auth.expires_in = now() + response.data.expires_in
state.auth.access_token = response.data.access_token
return true
} else {
return false
}
}
}
// Listing all the programs you have in RainMachine
def getProgramList() {
def programsList = [:]
apiGet("/api/4/program") { response ->
if (response.status == 200) {
response.data.programs.each { program ->
if (program.uid) {
def dni = [ app.id, "prog", program.uid ].join('|')
def endTime = 0 //TODO: calculate time left for the program
programsList[dni] = program.name
state.data[dni] = [
status: program.status,
endTime: endTime
]
log.debug "Prog: " + dni + " Status : " + state.data[dni].status
}
}
}
}
return programsList
}
// Listing all the zones you have in RainMachine
def getZoneList() {
def zonesList = [:]
apiGet("/api/4/zone") { response ->
if (response.status == 200) {
response.data.zones.each { zone ->
def dni = [ app.id, "zone", zone.uid ].join('|')
def endTime = now + ((zone.remaining?:0) * 1000)
zonesList[dni] = zone.name
state.data[dni] = [
status: zone.state,
endTime: endTime
]
log.debug "Zone: " + dni + " Status : " + state.data[dni].status
}
}
}
return zonesList
}
// Updates devices
def updateDeviceData() {
log.info "updateDeviceData()"
// automatically checks if the token has expired, if so login again
if (login()) {
// Next polling time, defined in settings
def next = (state.polling.last?:0) + ( (settings.polling.toInteger() > 0 ? settings.polling.toInteger() : 1) * 60 * 1000)
log.debug "last: " + state.polling.last
log.debug "now: " + now()
log.debug "next: " + next
log.debug "RunNow: " + state.polling.runNow
if ((now() > next) || (state.polling.runNow)) {
// set polling states
state.polling = [
last: now(),
runNow: false
]
// Get all the program information
getProgramList()
// Get all the program information
getZoneList()
}
}
}
def pollAllChild() {
// get all the children and send updates
def childDevice = getAllChildDevices()
childDevice.each {
log.debug "Polling " + it.deviceNetworkId
it.poll()
}
}
// Returns UID of a Zone or Program
private getChildUID(child) {
return child.device.deviceNetworkId.split("\\|")[2]
}
// Returns Type of a Zone or Program
private getChildType(child) {
def childType = child.device.deviceNetworkId.split("\\|")[1]
if (childType == "prog") { return "program" }
if (childType == "zone") { return "zone" }
}
/* api connection */
// HTTP GET call
private apiGet(apiPath, callback = {}) {
def apiParams = [
uri: "http://" + settings.ip_address + ":" + settings.ip_port,
path: apiPath,
contentType: "application/json",
query: [ access_token: state.auth.access_token ]
]
log.debug "HTTP GET : " + apiParams
try {
httpGet(apiParams) { response ->
if (response.data.ErrorMessage) {
log.debug "API Error: $response.data"
}
callback(response)
}
}
catch (Error e) {
log.debug "API Error: $e"
}
}
// HTTP POST call
def apiPost(apiPath, apiBody, callback = {}) {
def apiParams = [
uri: "http://" + settings.ip_address + ":" + settings.ip_port,
path: apiPath,
contentType: "application/json",
query: [ access_token: state.auth.access_token ],
body: apiBody
]
log.debug "HTTP POST : " + apiParam
try {
httpPostJson("http://" + settings.ip_address + ":" + settings.ip_port + apiPath + "?access_token=" + state.auth.access_token, apiBody) { response ->
if (response.data.ErrorMessage) {
log.debug "API Error: $response.data"
}
callback(response)
}
}
catch (Error e) {
log.debug "API Error: $e"
}
}
/* for SmartDevice to call */
// Refresh data
def refresh() {
log.info "refresh()"
// Set initial polling run
state.polling = [
last: now(),
runNow: true
]
state.data = [:]
//Update Devices
updateDeviceData()
pause(1000)
pollAllChild()
}
// Get single device status
def getDeviceStatus(child) {
log.info "getDeviceStatus()"
//tries to get latest data if polling limitation allows
//updateDeviceData()
return state.data[child.device.deviceNetworkId].status
}
// Get single device ending time
def getDeviceEndTime(child) {
//tries to get latest data if polling limitation allows
updateDeviceData()
if (state.data[child.device.deviceNetworkId]) {
return state.data[child.device.deviceNetworkId].endTime
}
}
// Send command to start or stop
def sendCommand(child, apiCommand, apiTime) {
def childUID = getChildUID(child)
def childType = getChildType(child)
def commandSuccess = false
def zonesActive = false
def apiPath = "/api/4/" + childType + "/" + childUID + "/" + apiCommand
def apiBody = []
//Try to get the latest data first
updateDeviceData()
//Checks for any active running sprinklers before allowing another program to run
if (childType == "program") {
if (apiCommand == "start") {
state.data.each { dni, data -> if ((data.status == 1) || (data.status == 2)) { zonesActive = true }}
if (!zonesActive) {
apiPost(apiPath, [pid: childUID])
commandSuccess = true
} else {
commandSuccess = false
}
} else {
apiPost(apiPath, [pid: childUID])
commandSuccess = true
}
}
//Zones will require time
if (childType == "zone") {
apiPost(apiPath, [time: apiTime])
commandSuccess = true
}
//Forcefully get the latest data after waiting for 2 seconds
pause(2000)
refresh()
return commandSuccess
}
//Stop everything
def sendStopAll() {
def apiPath = "/api/4/watering/stopall"
def apiBody = [all: "true"]
apiPost(apiPath, apiBody)
//Forcefully get the latest data after waiting for 2 seconds
pause(2000)
refresh()
return true
}