forked from SmartThingsCommunity/SmartThingsPublic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neptune-apex-connect.src
322 lines (292 loc) · 11.1 KB
/
neptune-apex-connect.src
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
/**
* Neptune Apex (Connect)
*
* Copyright 2017 James Andariese
*
* 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.
*
*/
definition(
name: "Neptune Apex (Connect)",
namespace: "jamesandariese",
author: "James Andariese",
description: "Neptune Apex (Connect)",
category: "",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]") {
appSetting "username"
appSetting "password"
}
preferences {
page(name: "page1", "title": "Connect", nextPage: "page2", uninstall: true, install: false)
page(name: "page2", "title": "Select", install: true, uninstall: true)
}
def page1() {
atomicState.done = null
dynamicPage(name: "page1", "title": "Connect", nextPage: "page2", uninstall: true, install: false) {
section("Connection") {
input("name": "ip", type: "text", title: "IP", description: "A static IP is recommended")
input("name": "username", type: "text", title: "Username", description: "(not your Apex Fusion username)")
input("name": "password", type: "password", "title": "Password", description: "(not your Apex Fusion password)")
}
section("Setup Requirements") {
paragraph("You will need to have enabled LAN access on your controller for this to work.\nLAN access or Classic Dashboard is not the same as Apex Fusion and must be setup separately.")
href(name: "classicDashboard",
title: "Classic Dashboard Setup",
required: false,
style: "external",
url: "http://support.neptunesystems.com/support/solutions/articles/3000017576-how-to-access-your-classic-dashboard-pc",
description: "Tap to view setup instructions for Apex Classic Dashboard (LAN access)")
}
}
}
def page2() {
if (atomicState.done == null) {
log.debug "first run. sending off hub action"
atomicState.done = false
def auth = "$username:$password".bytes.encodeBase64()
def params = [
"headers": [
"HOST": "$ip:80",
"Authorization": "Basic $auth",
],
"method": "GET",
"path": "/cgi-bin/status.json",
]
log.debug params
sendHubCommand(new physicalgraph.device.HubAction(params, "$ip:80", [callback: handleHubStatusResponseDuringSetup]))
atomicState.done = false
}
if (!atomicState.done) {
log.debug "still waiting"
dynamicPage(name: "page2", title: "Connecting", install:false, uninstall: false, refreshInterval: 1) {
section("Connecting") {
paragraph("Connecting to your Apex controller. Please be patient")
}
}
} else {
log.debug "all done waiting"
if (!atomicState.success) {
dynamicPage(name: "page2", title: "Connecting", install:false, uninstall: false) {
section("Error") {
paragraph("There was an error connecting to your Apex controller. Your IP or credentials may have been wrong. Here's what the controller responded with:")
}
section("Raw Output") {
paragraph(atomicState.message)
}
}
} else {
def objects = [
"feed_0": [type: "feedMode", name: "Feed Mode A", did: "feed_0"],
"feed_1": [type: "feedMode", name: "Feed Mode B", did: "feed_1"],
"feed_2": [type: "feedMode", name: "Feed Mode C", did: "feed_2"],
"feed_3": [type: "feedMode", name: "Feed Mode D", did: "feed_3"],
]
dynamicPage(name: "page2", title: "Connecting", install:true, uninstall: false) {
log.debug "adding feed mode switches and buttons"
section("Feed Mode") {
paragraph("Switches which turn feed mode off when switched off")
input(name: "did_feed_0", title: "Feed Mode A Switch", type: "bool")
input(name: "did_feed_1", title: "Feed Mode B Switch", type: "bool")
input(name: "did_feed_2", title: "Feed Mode C Switch", type: "bool")
input(name: "did_feed_3", title: "Feed Mode D Switch", type: "bool")
}
def switchesForTypes = { sectionName, typefilter, objlist ->
log.debug "adding switches for $sectionName, $typefilter"
//section(sectionName) {
objlist.each {
def name = "did_${it['did']}"
if (it['type'] == typefilter) {
objects[it['did']] = it
def dfv = settings[name]
if (!dfv) {
dfv = false
}
log.debug "Default value for $name is $dfv"
input(name: name, title: it['name'], type: "bool", defaultValue: dfv)
}
}
//}
}
section("Temp Probes") {
switchesForTypes("Temp Probes", "Temp", atomicState.status["istat"]["inputs"])
}
section("Outlets") {
switchesForTypes("Outlets", "outlet", atomicState.status["istat"]["outputs"])
}
section("pH Probes") {
switchesForTypes("pH Probes", "pH", atomicState.status["istat"]["inputs"])
}
section("Amps") {
switchesForTypes("Amps", "Amps", atomicState.status["istat"]["inputs"])
}
section("Cool") {
paragraph("That's all!")
}
atomicState.objects = objects
}
}
}
}
def handleHubStatusResponseDuringSetup(resp) {
log.debug resp
if (resp.json == null || !resp.json.containsKey("istat")) {
atomicState.success = false
atomicState.message = resp.body
} else {
atomicState.status = resp.json
atomicState.success = true
}
atomicState.done = true
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
subscribe(location, null, locationHandler, [filterEvents:false])
def objects = atomicState.objects
log.debug "objects: $objects"
if (objects == null) {
log.debug "there are no objects to setup. skipping child device setup step"
return
}
def existingDevices = [:]
getChildDevices().each {
if (settings["did_" + it.device.deviceNetworkId] == false) {
log.debug "removing no longer used ${it.device.deviceNetworkId}"
try {
deleteChildDevice(it.device.deviceNetworkId)
} catch(e) {
log.debug "Error deleting unwanted child ${it.device.deviceNetworkId}: $e"
}
} else {
existingDevices[it.device.deviceNetworkId] = true
}
}
def typeMap = [
"Temp": "Neptune Apex Temp Probe",
"pH": "Neptune Apex pH Probe",
"feedMode": "Neptune Apex Feed Mode",
"Amps": "Neptune Apex Amps Probe",
"outlet": "Neptune Apex Outlet",
]
objects.each { dni, obj ->
if (settings["did_" + dni] && !existingDevices[dni]) {
log.debug "addChildDevice(${typeMap[obj['type']]}, $dni, null, [label: ${obj['name']}])"
addChildDevice(typeMap[obj['type']], dni, null, [label: obj['name']])
}
}
runEvery1Minute(poll)
}
def poll() {
log.debug "Polling"
def auth = "$username:$password".bytes.encodeBase64()
def params = [
"headers": [
"HOST": "$ip:80",
"Authorization": "Basic $auth",
],
"method": "GET",
"path": "/cgi-bin/status.json",
]
log.debug params
sendHubCommand(new physicalgraph.device.HubAction(params, "$ip:80", [callback: handleHubStatusResponse]))
}
def handleHubStatusResponse(resp) {
log.debug "handleHubStatusResponse(resp.body)"
if (resp.json == null || !resp.json.containsKey("istat")) {
log.debug "Invalid hub status response: $resp.body"
return
}
log.debug "yo dawg"
resp.json['istat']['inputs'].each {
def child = getChildDevice(it['did'])
if (child) {
child.updateFromApex(it)
}
}
resp.json['istat']['outputs'].each {
def child = getChildDevice(it['did'])
if (child) {
child.updateFromApex(it)
}
}
getChildDevices().each {
log.debug "looking for feed_ in ${it.device.deviceNetworkId}"
log.debug "it.device.deviceNetworkId.startsWith('feed_') = ${it.device.deviceNetworkId.startsWith('feed_')}"
if (it.device.deviceNetworkId.startsWith("feed_")) {
it.updateFromApex(resp.json['istat']['feed'])
}
}
log.debug "all done in handleHubStatusResponse"
}
def feedMode(i) {
def auth = "$username:$password".bytes.encodeBase64()
def feedCycle = "Feed"
if (i == null) {
i = 0
feedCycle="Feed Cancel"
}
def params = [
"headers": [
"HOST": "$ip:80",
"Authorization": "Basic $auth",
"Content-Type": "application/x-www-form-urlencoded"
],
"method": "POST",
"path": "/cgi-bin/status.cgi",
"body": "FeedCycle=$feedCycle&FeedSel=$i&noResponse=1"
]
log.debug params
sendHubCommand(new physicalgraph.device.HubAction(params, "$ip:80", [callback: handleCommandResponse]))
}
def handleCommandResponse(resp) {
poll()
}
def setApexOutput(did, value) {
// 0 = auto
// 1 = off
// 2 = on
log.debug "setApexOutput($did, $value)"
def auth = "$username:$password".bytes.encodeBase64()
def child = getChildDevice(did)
def apexOutputName = child.currentApexOutputName
if (apexOutputName == null) {
log.debug "Couldn't find the current apexOutputName for $did"
}
def params = [
"headers": [
"HOST": "$ip:80",
"Authorization": "Basic $auth",
"Content-Type": "application/x-www-form-urlencoded"
],
"method": "POST",
"path": "/cgi-bin/status.cgi",
"body": "${apexOutputName}_state=${value}&noResponse=1"
]
log.debug params
sendHubCommand(new physicalgraph.device.HubAction(params, "$ip:80", [callback: handleCommandResponse]))
}
def outletOn(did) {
setApexOutput(did, 2)
}
def outletOff(did) {
setApexOutput(did, 1)
}
def outletAuto(did) {
setApexOutput(did, 0)
}