-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoscam.groovy
382 lines (329 loc) · 12.8 KB
/
foscam.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
/**
* Foscam Universal Device
*
* Copyright 2014 skp19
* Modified by Joshua Seidel, 2014
*
*/
metadata {
definition (name: "Foscam Universal Device", namespace: "blueharford", author: "skp19,blueharford") {
capability "Polling"
capability "Image Capture"
attribute "ledStatus", "string"
attribute "hubactionMode", "string"
command "toggleLED"
command "ledOn"
command "ledOff"
command "ledAuto"
command "left"
command "right"
command "up"
command "down"
}
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?")
}
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("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("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("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
//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
//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
}