-
Notifications
You must be signed in to change notification settings - Fork 2
/
Shinobi-App.groovy
296 lines (262 loc) · 9.48 KB
/
Shinobi-App.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
/**
* Copyright 2020 Brad Sileo
*
* Shinobi NVR
*
* Author: Brad Sileo
*
*
* version: 0.9.6
*/
definition(
name: "Shinobi",
namespace: "bsileo",
author: "Brad Sileo",
description: "Connect your Shinobi NVR to Hubitat",
iconUrl: "",
iconX2Url: ""
)
preferences {
page(name: "mainPage", title: "Main")
page(name: "installPage", title: "Install")
page(name: "selectMonitors", title: "Select Monitors")
}
def mainPage() {
if(!state.accessToken) createAccessToken()
def install = state.monitors != null
return dynamicPage(name: "home", title: "<h1>Shinobi NVR Integration</h1>", refreshInterval: 0, install: install, uninstall: true) {
if (state.monitors) {
section("<h2>Current Installation</h2>") {
def url = getFullLocalApiServerUrl() + "/motion" + "?access_token=" + state.accessToken + "&mid={{MONITOR_ID}}&region={{REGION_NAME}}&confidence={{CONFIDENCE}}&name={{MONITOR_NAME}}&reason={{REASON}}"
def urlNo = getFullLocalApiServerUrl() + "/nomotion" + "?access_token=" + state.accessToken+ "&mid={{MONITOR_ID}}"
paragraph "Use this local URL in the Shinobi Global Detector Settings Webhook:\n\n <a target=\"_frame\" href=\"${url}" + "\">${url}</a>"
paragraph "Use this local URL in the Shinobi No Motion Webhook:\n\n <a target=\"_frame\" href=\"${urlNo}" + "\">${urlNo}</a>"
paragraph "Both WebHooks should be configured to use a PUT request type."
}
}
section("<h2>Install / Setup</h2>") {
href(name: "installPage", title: "", description: "Tap to setup the Shinobi Server and manage connected devices", required: false, page: "installPage")
}
section("<h2>Logging</h2>") {
input (
name: "loggingLevel",
title: "Live Logging Level:\nMessages with this level and higher will be logged to the IDE.",
type: "enum",
options: [
"None",
"Error",
"Warning",
"Info",
"Debug",
"Trace"
],
required: false
)
}
}
}
def installPage() {
return dynamicPage(name: "home", title: "Shinobi NVR Interface", refreshInterval: 0, install: false, uninstall: true, nextPage: selectMonitors) {
section("Setup NVR Connection") {
input(name:"controllerIP", type: "text", title: "Shinobi IP Address", required: true, displayDuringSetup: true, defaultValue:"192.168.1.100")
input(name:"controllerPort", type: "text", title: "Shinobi Port", required: true, displayDuringSetup: true, defaultValue:"8080")
input(name:"APIKey", type: "text", title: "Shinobi API Key", required: true, displayDuringSetup: true, defaultValue:"")
input(name:"GroupKey", type: "text", title: "Shinobi Group Key", required: true, displayDuringSetup: true, defaultValue:"")
}
}
}
def selectMonitors() {
unsubscribe()
atomicState.subscribed = false
def options = [:]
def devices = getMonitors()
devices.each {
def value = "${it.value.name}"
options["${it.key}"] = value
}
return dynamicPage(name: "selectMonitors", title: "Select the Monitors to Install", refreshInterval: 0, install: true, uninstall: true) {
section("Monitors", hideable:false, hidden:false) {
input(name: "selectedMonitors", title:"Select Monitors", type: "enum", required:true, multiple:true, description: "Tap to choose", params: params,
options: options, submitOnChange: true, width: 6)
}
}
}
def getMonitors() {
atomicState.monitors = null
def body = ""
def params = [
uri: "http://${settings.controllerIP}:${controllerPort}",
path: "/${settings.APIKey}/monitor/${settings.GroupKey}",
requestContentType: "application/json",
contentType: "application/json",
body:body
]
res = [:]
logger("getMonitors with ${params}","debug")
httpGet(params) { resp ->
logger("getMonitor/resp: ${resp.data}","debug")
// getMonitor/resp: [ok:false, msg:Not Authorized]
resp.data.each {
res[it.mid] = it
}
atomicState.monitors = res
}
return res
}
def getControllerParams() {
return [
uri: "http://${settings.controllerIP}:${controllerPort}",
APIKey: settings.APIKey,
groupKey: settings.GroupKey
]
}
def installed() {
state.loggingLevelIDE = (settings.configLoggingLevelIDE) ? settings.configLoggingLevelIDE : 'Debug'
getHubPlatform()
updateChildren()
}
def updated () {
state.loggingLevelIDE = (settings.configLoggingLevelIDE) ? settings.configLoggingLevelIDE : 'Debug'
updateChildren()
}
def uninstalled() {
removeChildren()
}
def removeChildren() {
getChildDevices().each {
deleteChildDevice(it.deviceNetworkID)
}
}
def refresh() {
def devices = getMonitors()
devices.each {
def child = getChild(it.key)
child?.parseRefresh(it.value)
}
}
mappings {
path("/motion") {
action: [
PUT: "motionHandler"
]
}
path("/nomotion") {
action: [
PUT: "noMotionHandler"
]
}
}
def motionHandler(request) {
def monitorID = params.mid
def child = getChild(monitorID)
child?.triggerMotion(params)
}
def noMotionHandler(request) {
def monitorID = params.mid
def child = getChild(monitorID)
child?.triggerNoMotion(params)
}
def getChild(monitorID) {
def childDNI = monitorID
return getChildDevice(childDNI)
}
def updateChildren() {
settings.selectedMonitors.each {
def child = getChild(it)
if (child) {
logger("Update Child ${it}","debug")
}
else {
def monitor = atomicState.monitors[it]
logger("Create new Monitor called ${monitor.mid}","info")
createMonitor(monitor)
}
}
getChildDevices().each {
def selected = false
settings.selectedMonitors.each { sMon ->
if (sMon == it.deviceNetworkId) {
selected = true
}
}
if (! selected) {
logger("Remove unselected monitor with ID ${it.deviceNetworkId}","warn")
deleteChildDevice(it.deviceNetworkId)
}
}
}
def createMonitor(monitor) {
logger("Create Monitor for ${monitor}","trace")
def hub = location.hubs[0]
d = addChildDevice("bsileo", "Shinobi Monitor", monitor.mid, hub.id, [
"label": "Shinobi Monitor ${monitor.name}",
"completedSetup" : true,
"data": [
name: monitor.name
]
])
}
// INTERNAL Methods
//*******************************************************
//* logger()
//*
//* Wrapper function for all logging.
//*******************************************************
private logger(msg, level = "debug") {
def lookup = [
"None" : 0,
"Error" : 1,
"Warning" : 2,
"Info" : 3,
"Debug" : 4,
"Trace" : 5]
def logLevel = lookup[settings.loggingLevel ? settings.loggingLevel : 'Debug']
switch(level) {
case "error":
if (logLevel >= 1) log.error msg
break
case "warn":
if (logLevel >= 2) log.warn msg
break
case "info":
if (logLevel >= 3) log.info msg
break
case "debug":
if (logLevel >= 4) log.debug msg
break
case "trace":
if (logLevel >= 5) log.trace msg
break
default:
log.debug msg
break
}
}
// **************************************************************************************************************************
// SmartThings/Hubitat Portability Library (SHPL)
// Copyright (c) 2019, Barry A. Burke (storageanarchy@gmail.com)
//
// The following 3 calls are safe to use anywhere within a Device Handler or Application
// - these can be called (e.g., if (getPlatform() == 'SmartThings'), or referenced (i.e., if (platform == 'Hubitat') )
// - performance of the non-native platform is horrendous, so it is best to use these only in the metadata{} section of a
// Device Handler or Application
//
private String getPlatform() { (physicalgraph?.device?.HubAction ? 'SmartThings' : 'Hubitat') } // if (platform == 'SmartThings') ...
private Boolean getIsST() { (physicalgraph?.device?.HubAction ? true : false) } // if (isST) ...
private Boolean getIsHE() { (hubitat?.device?.HubAction ? true : false) } // if (isHE) ...
//
// The following 3 calls are ONLY for use within the Device Handler or Application runtime
// - they will throw an error at compile time if used within metadata, usually complaining that "state" is not defined
// - getHubPlatform() ***MUST*** be called from the installed() method, then use "state.hubPlatform" elsewhere
// - "if (state.isST)" is more efficient than "if (isSTHub)"
//
private String getHubPlatform() {
if (state?.hubPlatform == null) {
state.hubPlatform = getPlatform() // if (hubPlatform == 'Hubitat') ... or if (state.hubPlatform == 'SmartThings')...
state.isST = state.hubPlatform.startsWith('S') // if (state.isST) ...
state.isHE = state.hubPlatform.startsWith('H') // if (state.isHE) ...
}
return state.hubPlatform
}
private Boolean getIsSTHub() { (state.isST) } // if (isSTHub) ...
private Boolean getIsHEHub() { (state.isHE) }