-
Notifications
You must be signed in to change notification settings - Fork 6
/
DCS-930L.groovy
361 lines (285 loc) · 10.9 KB
/
DCS-930L.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
/**
* D-Link DCS-930L v1.0.1
* Modified from Generic Camera Device v1.0.07102014
*
* Copyright 2014 [email protected]
* Modified 2015 blebson
*
* 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.
*
*/
metadata {
definition (name: "DCS-930L", author: "blebson") {
capability "Image Capture"
capability "Sensor"
capability "Switch"
capability "Switch Level"
capability "Refresh"
attribute "hubactionMode", "string"
command "motionOn"
command "motionOff"
}
preferences {
input("CameraIP", "string", title:"Camera IP Address", description: "Please enter your camera's IP Address", required: true, displayDuringSetup: true)
input("CameraPort", "string", title:"Camera Port", description: "Please enter your camera's Port", defaultValue: 80 , required: true, displayDuringSetup: true)
input("CameraUser", "string", title:"Camera User", description: "Please enter your camera's username", required: false, displayDuringSetup: true)
input("CameraPassword", "password", title:"Camera Password", description: "Please enter your camera's password", required: false, displayDuringSetup: true)
}
simulator {
}
tiles {
carouselTile("cameraDetails", "device.image", width: 3, height: 2) { }
standardTile("take", "device.image", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "take", label: "Take", action: "Image Capture.take", icon: "st.camera.camera", backgroundColor: "#FFFFFF", nextState:"taking"
state "taking", label:'Taking', action: "", icon: "st.camera.take-photo", backgroundColor: "#53a7c0"
state "image", label: "Take", action: "Image Capture.take", icon: "st.camera.camera", backgroundColor: "#FFFFFF", nextState:"taking"
}
standardTile("refresh", "command.refresh", inactiveLabel: false) {
state "default", label:'refresh', action:"refresh.refresh", icon:"st.secondary.refresh-icon"
}
standardTile("motion", "device.switch", width: 1, height: 1, canChangeIcon: false) {
state "off", label: 'Motion Off', action: "switch.on", icon: "st.motion.motion.inactive", backgroundColor: "#ccffcc", nextState: "toggle"
state "toggle", label:'toggle', action: "", icon: "st.motion.motion.inactive", backgroundColor: "#53a7c0"
state "on", label: 'Motion On', action: "switch.off", icon: "st.motion.motion.active", backgroundColor: "#EE0000", nextState: "toggle"
}
controlTile("levelSliderControl", "device.level", "slider", height: 1, width: 3, inactiveLabel: false, range:"(0..100)") {
state "level", action:"switch level.setLevel"
}
main "motion"
details(["cameraDetails", "take", "motion", "refresh", "levelSliderControl"])
}
}
def parse(String description) {
log.debug "Parsing '${description}'"
def map = [:]
def retResult = []
def descMap = parseDescriptionAsMap(description)
def msg = parseLanMessage(description)
//log.debug "status ${msg.status}"
//log.debug "data ${msg.data}"
//Image
if (descMap["bucket"] && descMap["key"]) {
putImageInS3(descMap)
}
else if (descMap["headers"] && descMap["body"]){
def body = new String(descMap["body"].decodeBase64())
log.debug "Body: ${body}"
}
if (msg.body) {
//log.debug "Motion Enabled: ${msg.body.contains("enable=yes")}"
//log.debug "Motion Disabled: ${msg.body.contains("enable=no")}"
//log.debug "PIR Enabled: ${msg.body.contains("pir=yes")}"
//log.debug "PIR Disabled: ${msg.body.contains("pir=no")}"
if (msg.body.contains("MotionDetectionEnable=1")) {
log.debug "Motion is on"
sendEvent(name: "switch", value: "on");
}
else if (msg.body.contains("MotionDetectionEnable=0")) {
log.debug "Motion is off"
sendEvent(name: "switch", value: "off");
}
if(msg.body.contains("MotionDetectionSensitivity="))
{
//log.debug msg.body
String[] lines = msg.body.split( '\n' )
//log.debug lines[2]
String[] sensitivity = lines[2].split( '=' )
//log.debug sensitivity[1]
int[] senseValue = sensitivity[1].toInteger()
//log.debug senseValue
sendEvent(name: "level", value: "${senseValue[0]}")
//sendEvent(name: "switch.setLevel", value: "${senseValue}")
}
}
}
// handle commands
def take() {
def userpassascii = "${CameraUser}:${CameraPassword}"
def userpass = "Basic " + userpassascii.encodeAsBase64().toString()
def host = CameraIP
def hosthex = convertIPtoHex(host)
def porthex = convertPortToHex(CameraPort)
device.deviceNetworkId = "$hosthex:$porthex"
log.debug "The device id configured is: $device.deviceNetworkId"
def path = "/image/jpeg.cgi"
log.debug "path is: $path"
def headers = [:]
headers.put("HOST", "$host:$CameraPort")
headers.put("Authorization", userpass)
log.debug "The Header is $headers"
def method = "GET"
log.debug "The method is $method"
try {
def hubAction = new physicalgraph.device.HubAction(
method: method,
path: path,
headers: headers
)
hubAction.options = [outputMsgToS3:true]
log.debug hubAction
hubAction
}
catch (Exception e) {
log.debug "Hit Exception $e on $hubAction"
}
}
def motionCmd(int motion)
{
def userpassascii = "${CameraUser}:${CameraPassword}"
def userpass = "Basic " + userpassascii.encodeAsBase64().toString()
def host = CameraIP
def hosthex = convertIPtoHex(host)
def porthex = convertPortToHex(CameraPort)
device.deviceNetworkId = "$hosthex:$porthex"
log.debug "The device id configured is: $device.deviceNetworkId"
def headers = [:]
headers.put("HOST", "$host:$CameraPort")
headers.put("Authorization", userpass)
log.debug "The Header is $headers"
def path = "/motion.cgi?MotionDetectionEnable=${motion}&ConfigReboot=No"
log.debug "path is: $path"
try {
def hubAction = new physicalgraph.device.HubAction(
method: "GET",
path: path,
headers: headers
)
log.debug hubAction
return hubAction
}
catch (Exception e) {
log.debug "Hit Exception $e on $hubAction"
}
}
def sensitivityCmd(int percent)
{
def userpassascii = "${CameraUser}:${CameraPassword}"
def userpass = "Basic " + userpassascii.encodeAsBase64().toString()
def host = CameraIP
def hosthex = convertIPtoHex(host)
def porthex = convertPortToHex(CameraPort)
device.deviceNetworkId = "$hosthex:$porthex"
log.debug "The device id configured is: $device.deviceNetworkId"
log.debug "Sensitivity is ${percent}"
def path = "/motion.cgi?MotionDetectionSensitivity=${percent}&ConfigReboot=No"
log.debug "path is: $path"
def headers = [:]
headers.put("HOST", "$host:$CameraPort")
headers.put("Authorization", userpass)
log.debug "The Header is $headers"
try {
def hubAction = new physicalgraph.device.HubAction(
method: "GET",
path: path,
headers: headers
)
log.debug hubAction
return hubAction
}
catch (Exception e) {
log.debug "Hit Exception $e on $hubAction"
}
}
def putImageInS3(map) {
log.debug "firing s3"
def s3ObjectContent
try {
def imageBytes = getS3Object(map.bucket, map.key + ".jpg")
if(imageBytes)
{
s3ObjectContent = imageBytes.getObjectContent()
def bytes = new ByteArrayInputStream(s3ObjectContent.bytes)
storeImage(getPictureName(), bytes)
}
}
catch(Exception e) {
log.error e
}
finally {
//Explicitly close the stream
if (s3ObjectContent) { s3ObjectContent.close() }
}
}
def parseDescriptionAsMap(description) {
description.split(",").inject([:]) { map, param ->
def nameAndValue = param.split(":")
map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
}
}
private getPictureName() {
def pictureUuid = java.util.UUID.randomUUID().toString().replaceAll('-', '')
log.debug pictureUuid
def picName = device.deviceNetworkId.replaceAll(':', '') + "_$pictureUuid" + ".jpg"
return picName
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
log.debug "IP address entered is $ipAddress and the converted hex code is $hex"
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
log.debug hexport
return hexport
}
private Integer convertHexToInt(hex) {
Integer.parseInt(hex,16)
}
private String convertHexToIP(hex) {
log.debug("Convert hex to ip: $hex")
[convertHexToInt(hex[0..1]),convertHexToInt(hex[2..3]),convertHexToInt(hex[4..5]),convertHexToInt(hex[6..7])].join(".")
}
private getHostAddress() {
def parts = device.deviceNetworkId.split(":")
log.debug device.deviceNetworkId
def ip = convertHexToIP(parts[0])
def port = convertHexToInt(parts[1])
return ip + ":" + port
}
def on() {
log.debug "Enabling motion detection"
return motionCmd(1)
}
def off() {
log.debug "Disabling motion detection"
return motionCmd(0)
}
def setLevel(percent) {
log.debug "Executing 'setLevel'"
return sensitivityCmd(percent)
}
def refresh(){
log.debug "Refresh"
def userpassascii = "${CameraUser}:${CameraPassword}"
def userpass = "Basic " + userpassascii.encodeAsBase64().toString()
def host = CameraIP
def hosthex = convertIPtoHex(host)
def porthex = convertPortToHex(CameraPort)
device.deviceNetworkId = "$hosthex:$porthex"
log.debug "The device id configured is: $device.deviceNetworkId"
def path = "/motion.cgi"
log.debug "path is: $path"
def headers = [:]
headers.put("HOST", "$host:$CameraPort")
headers.put("Authorization", userpass)
log.debug "The Header is $headers"
try {
def hubAction = new physicalgraph.device.HubAction(
method: "GET",
path: path,
headers: headers
)
log.debug hubAction
return hubAction
}
catch (Exception e) {
log.debug "Hit Exception $e on $hubAction"
}
}