-
Notifications
You must be signed in to change notification settings - Fork 100
/
st_foscam_universal.groovy
545 lines (473 loc) · 18.6 KB
/
st_foscam_universal.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/**
* Foscam Universal Device
*
* Copyright 2014 skp19
*
* modified 2015-06-04 : thrash99er - changed bool comparsions from string to bool i.e. "true" to true
*
*
* 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: "Foscam Universal Device", namespace: "skp19", author: "skp19") {
capability "Polling"
capability "Image Capture"
attribute "alarmStatus", "string"
attribute "ledStatus", "string"
attribute "hubactionMode", "string"
command "alarmOn"
command "alarmOff"
command "toggleAlarm"
command "toggleLED"
command "ledOn"
command "ledOff"
command "ledAuto"
command "left"
command "right"
command "up"
command "down"
command "cruisemap1"
command "cruisemap2"
command "stopCruise"
command "preset1"
command "preset2"
command "preset3"
}
preferences {
input("ip", "string", title:"Camera IP Address", description: "Camera IP Address", required: true, displayDuringSetup: true)
input("port", "string", title:"Camera Port", description: "Camera Port", defaultValue: 80 , required: true, displayDuringSetup: true)
input("username", "string", title:"Camera Username", description: "Camera Username", required: true, displayDuringSetup: true)
input("password", "password", title:"Camera Password", description: "Camera Password", required: true, displayDuringSetup: true)
input("hdcamera", "bool", title:"HD Foscam Camera? (9xxx Series)", description: "Type of Foscam Camera", required: true, displayDuringSetup: true)
input("mirror", "bool", title:"Mirror? (Not required for HD cameras)", description: "Camera Mirrored?")
input("flip", "bool", title:"Flip? (Not required for HD cameras)", description: "Camera Flipped?")
input("preset1", "text", title: "Preset 1 (For HD cameras only)", description: "Name of your first preset position")
input("preset2", "text", title: "Preset 2 (For HD cameras only)", description: "Name of your second preset position")
input("preset3", "text", title: "Preset 3 (For HD cameras only)", description: "Name of your third preset position")
input("cruisemap1", "text", title: "Cruise Map 1 (For HD cameras only. Non-HD cameras will default to Horizontal.)", description: "Name of your first cruise map", defaultValue: "Horizontal")
input("cruisemap2", "text", title: "Cruise Map 2 (For HD cameras only. Non-HD cameras will default to Vertical.)", description: "Name of your second cruise map", defaultValue: "Vertical")
}
tiles {
carouselTile("cameraDetails", "device.image", width: 3, height: 2) { }
standardTile("camera", "device.alarmStatus", width: 1, height: 1, canChangeIcon: true, inactiveLabel: true, canChangeBackground: true) {
state "off", label: "off", action: "toggleAlarm", icon: "st.camera.dropcam-centered", backgroundColor: "#FFFFFF"
state "on", label: "on", action: "toggleAlarm", icon: "st.camera.dropcam-centered", backgroundColor: "#53A7C0"
}
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("alarmStatus", "device.alarmStatus", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "off", label: "off", action: "toggleAlarm", icon: "st.quirky.spotter.quirky-spotter-sound-off", backgroundColor: "#FFFFFF"
state "on", label: "on", action: "toggleAlarm", icon: "st.quirky.spotter.quirky-spotter-sound-on", backgroundColor: "#53A7C0"
}
standardTile("ledStatus", "device.ledStatus", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "auto", label: "auto", action: "toggleLED", icon: "st.Lighting.light13", backgroundColor: "#53A7C0"
state "off", label: "off", action: "toggleLED", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
state "on", label: "on", action: "toggleLED", icon: "st.Lighting.light11", backgroundColor: "#FFFF00"
state "manual", label: "manual", action: "toggleLED", icon: "st.Lighting.light13", backgroundColor: "#FFFF00"
}
standardTile("ledAuto", "device.ledStatus", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "auto", label: "auto", action: "ledAuto", icon: "st.Lighting.light11", backgroundColor: "#53A7C0"
state "off", label: "auto", action: "ledAuto", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
state "on", label: "auto", action: "ledAuto", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
state "manual", label: "auto", action: "ledAuto", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
}
standardTile("ledOn", "device.ledStatus", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "auto", label: "on", action: "ledOn", icon: "st.Lighting.light11", backgroundColor: "#FFFFFF"
state "off", label: "on", action: "ledOn", icon: "st.Lighting.light11", backgroundColor: "#FFFFFF"
state "on", label: "on", action: "ledOn", icon: "st.Lighting.light11", backgroundColor: "#FFFF00"
state "manual", label: "on", action: "ledOn", icon: "st.Lighting.light11", backgroundColor: "#00FF00"
}
standardTile("ledOff", "device.ledStatus", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "auto", label: "off", action: "ledOff", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
state "off", label: "off", action: "ledOff", icon: "st.Lighting.light13", backgroundColor: "#53A7C0"
state "on", label: "off", action: "ledOff", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
state "manual", label: "off", action: "ledOff", icon: "st.Lighting.light13", backgroundColor: "#00FF00"
}
standardTile("preset1", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "preset1", label: "preset 1", action: "preset1", icon: ""
}
standardTile("preset2", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "preset2", label: "preset 2", action: "preset2", icon: ""
}
standardTile("preset3", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "preset3", label: "preset 3", action: "preset3", icon: ""
}
standardTile("cruisemap1", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "cruisemap1", label: "Cruise Map 1", action: "cruisemap1", icon: ""
}
standardTile("cruisemap2", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "cruisemap2", label: "Cruise Map 2", action: "cruisemap2", icon: ""
}
standardTile("stopcruise", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "stopcruise", label: "Stop Cruise", action: "stopCruise", icon: ""
}
standardTile("left", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "left", label: "left", action: "left", icon: ""
}
standardTile("right", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "right", label: "right", action: "right", icon: ""
}
standardTile("up", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "up", label: "up", action: "up", icon: "st.thermostat.thermostat-up"
}
standardTile("down", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "down", label: "down", action: "down", icon: "st.thermostat.thermostat-down"
}
standardTile("stop", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "stop", label: "", action: "stopCruise", icon: "st.sonos.stop-btn"
}
standardTile("refresh", "device.alarmStatus", inactiveLabel: false, decoration: "flat") {
state "refresh", action:"polling.poll", icon:"st.secondary.refresh"
}
standardTile("blank", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "blank", label: "", action: "", icon: "", backgroundColor: "#FFFFFF"
}
main "camera"
//details(["cameraDetails", "take", "blank", "alarmStatus", "ledAuto", "ledOn", "ledOff", "refresh"]) //**Uncomment this line and comment out the next line to hide the PTZ controls
details(["cameraDetails", "take", "blank", "alarmStatus", "ledAuto", "ledOn", "ledOff", "preset1", "preset2", "preset3", "cruisemap1", "cruisemap2", "stopcruise", "blank", "up", "blank", "left", "stop", "right", "blank", "down", "blank", "refresh"])
}
}
//TAKE PICTURE
def take() {
log.debug("Taking Photo")
sendEvent(name: "hubactionMode", value: "s3");
if(hdcamera == true) {
hubGet("cmd=snapPicture2")
}
else {
hubGet("/snapshot.cgi?")
}
}
//END TAKE PICTURE
//ALARM ACTIONS
def toggleAlarm() {
log.debug "Toggling Alarm"
if(device.currentValue("alarmStatus") == "on") {
alarmOff()
}
else {
alarmOn()
}
}
def alarmOn() {
log.debug "Enabling Alarm"
sendEvent(name: "alarmStatus", value: "on");
if(hdcamera == true) {
hubGet("cmd=setMotionDetectConfig&isEnable=1")
}
else {
hubGet("/set_alarm.cgi?motion_armed=1&")
}
}
def alarmOff() {
log.debug "Disabling Alarm"
sendEvent(name: "alarmStatus", value: "off");
if(hdcamera == true) {
hubGet("cmd=setMotionDetectConfig&isEnable=0")
}
else {
hubGet("/set_alarm.cgi?motion_armed=0&")
}
}
//END ALARM ACTIONS
//LED ACTIONS
//Toggle LED's
def toggleLED() {
log.debug("Toggle LED")
if(device.currentValue("ledStatus") == "auto") {
ledOn()
}
else if(device.currentValue("ledStatus") == "on") {
ledOff()
}
else {
ledAuto()
}
}
def ledOn() {
log.debug("LED changed to: on")
sendEvent(name: "ledStatus", value: "on");
if(hdcamera == true) {
delayBetween([hubGet("cmd=setInfraLedConfig&mode=1"), hubGet("cmd=openInfraLed")])
}
else {
hubGet("/decoder_control.cgi?command=95&")
}
}
def ledOff() {
log.debug("LED changed to: off")
sendEvent(name: "ledStatus", value: "off");
if(hdcamera == "true") {
delayBetween([hubGet("cmd=setInfraLedConfig&mode=1"), hubGet("cmd=closeInfraLed")])
}
else {
hubGet("/decoder_control.cgi?command=94&")
}
}
def ledAuto() {
log.debug("LED changed to: auto")
sendEvent(name: "ledStatus", value: "auto");
if(hdcamera == true) {
hubGet("cmd=setInfraLedConfig&mode=0")
}
else {
hubGet("/decoder_control.cgi?command=95&")
}
}
//END LED ACTIONS
//PRESET ACTIONS
def preset1() {
log.debug("Preset 1 Selected - ${preset1}")
if(hdcamera == true) {
hubGet("cmd=ptzGotoPresetPoint&name=${preset1}")
}
else {
hubGet("/decoder_control.cgi?command=31&")
}
}
def preset2() {
log.debug("Preset 2 Selected - ${preset2}")
if(hdcamera == true) {
hubGet("cmd=ptzGotoPresetPoint&name=${preset2}")
}
else {
hubGet("/decoder_control.cgi?command=33&")
}
}
def preset3() {
log.debug("Preset 3 Selected - ${preset3}")
if(hdcamera == true) {
hubGet("cmd=ptzGotoPresetPoint&name=${preset3}")
}
else {
hubGet("/decoder_control.cgi?command=35&")
}
}
//END PRESET ACTIONS
//CRUISE ACTIONS
def cruisemap1() {
log.debug("Cruise Map 1 Selected - ${cruisemap1}")
if(hdcamera == true) {
hubGet("cmd=ptzStartCruise&mapName=${cruisemap1}")
}
else {
hubGet("/decoder_control.cgi?command=28&")
}
}
def cruisemap2() {
log.debug("Cruise Map 2 Selected - ${cruisemap2}")
if(hdcamera == true) {
hubGet("cmd=ptzStartCruise&mapName=${cruisemap2}")
}
else {
hubGet("/decoder_control.cgi?command=26&")
}
}
def stopCruise() {
log.debug("Stop Cruise")
if(hdcamera == true) {
hubGet("cmd=ptzStopRun")
}
else {
delayBetween([hubGet("/decoder_control.cgi?command=29&"), hubGet("/decoder_control.cgi?command=27&")])
}
}
//END CRUISE ACTIONS
//PTZ CONTROLS
def left() {
if(hdcamera == "true") {
delayBetween([hubGet("cmd=ptzMoveLeft"), hubGet("cmd=ptzStopRun")])
}
else {
if(mirror == "true") {
hubGet("/decoder_control.cgi?command=4&onestep=1&")
}
else {
hubGet("/decoder_control.cgi?command=6&onestep=1&")
}
}
}
def right() {
if(hdcamera == "true") {
delayBetween([hubGet("cmd=ptzMoveRight"), hubGet("cmd=ptzStopRun")])
}
else {
if(mirror == "true") {
hubGet("/decoder_control.cgi?command=6&onestep=1&")
}
else {
hubGet("/decoder_control.cgi?command=4&onestep=1&")
}
}
}
def up() {
if(hdcamera == true) {
delayBetween([hubGet("cmd=ptzMoveUp"), hubGet("cmd=ptzStopRun")])
}
else {
if(flip == true) {
hubGet("/decoder_control.cgi?command=2&onestep=1&")
}
else {
hubGet("/decoder_control.cgi?command=0&onestep=1&")
}
}
}
def down() {
if(hdcamera == true) {
delayBetween([hubGet("cmd=ptzMoveDown"), hubGet("cmd=ptzStopRun")])
}
else {
if(flip == true) {
hubGet("/decoder_control.cgi?command=0&onestep=1&")
}
else {
hubGet("/decoder_control.cgi?command=2&onestep=1&")
}
}
}
//END PTZ CONTROLS
def poll() {
sendEvent(name: "hubactionMode", value: "local");
//Poll Motion Alarm Status and IR LED Mode
if(hdcamera == true) {
delayBetween([hubGet("cmd=getMotionDetectConfig"), hubGet("cmd=getInfraLedConfig")])
}
else {
hubGet("/get_params.cgi?")
}
}
private getLogin() {
if(hdcamera == true) {
return "usr=${username}&pwd=${password}&"
}
else {
return "user=${username}&pwd=${password}"
}
}
private hubGet(def apiCommand) {
//Setting Network Device Id
def iphex = convertIPtoHex(ip)
def porthex = convertPortToHex(port)
device.deviceNetworkId = "$iphex:$porthex"
log.debug "Device Network Id set to ${iphex}:${porthex}"
log.debug("Executing hubaction on " + getHostAddress())
def uri = ""
if(hdcamera == true) {
uri = "/cgi-bin/CGIProxy.fcgi?" + getLogin() + apiCommand
}
else {
uri = apiCommand + getLogin()
}
log.debug uri
def hubAction = new physicalgraph.device.HubAction(
method: "GET",
path: uri,
headers: [HOST:getHostAddress()]
)
if(device.currentValue("hubactionMode") == "s3") {
hubAction.options = [outputMsgToS3:true]
sendEvent(name: "hubactionMode", value: "local");
}
hubAction
}
//Parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
def map = [:]
def retResult = []
def descMap = parseDescriptionAsMap(description)
//Image
if (descMap["bucket"] && descMap["key"]) {
putImageInS3(descMap)
}
//Status Polling
else if (descMap["headers"] && descMap["body"]) {
def body = new String(descMap["body"].decodeBase64())
if(hdcamera == true) {
def langs = new XmlSlurper().parseText(body)
def motionAlarm = "$langs.isEnable"
def ledMode = "$langs.mode"
//Get Motion Alarm Status
if(motionAlarm == "0") {
log.info("Polled: Alarm Off")
sendEvent(name: "alarmStatus", value: "off");
}
else if(motionAlarm == "1") {
log.info("Polled: Alarm On")
sendEvent(name: "alarmStatus", value: "on");
}
//Get IR LED Mode
if(ledMode == "0") {
log.info("Polled: LED Mode Auto")
sendEvent(name: "ledStatus", value: "auto")
}
else if(ledMode == "1") {
log.info("Polled: LED Mode Manual")
sendEvent(name: "ledStatus", value: "manual")
}
}
else {
if(body.find("alarm_motion_armed=0")) {
log.info("Polled: Alarm Off")
sendEvent(name: "alarmStatus", value: "off")
}
else if(body.find("alarm_motion_armed=1")) {
log.info("Polled: Alarm On")
sendEvent(name: "alarmStatus", value: "on")
}
//The API does not provide a way to poll for LED status on 8xxx series at the moment
}
}
}
def parseDescriptionAsMap(description) {
description.split(",").inject([:]) { map, param ->
def nameAndValue = param.split(":")
map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
}
}
def putImageInS3(map) {
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() }
}
}
private getPictureName() {
def pictureUuid = java.util.UUID.randomUUID().toString().replaceAll('-', '')
"image" + "_$pictureUuid" + ".jpg"
}
private getHostAddress() {
return "${ip}:${port}"
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
return hexport
}