forked from skp19/st_foscam_hd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
st_foscam_hd.groovy
430 lines (353 loc) · 13.8 KB
/
st_foscam_hd.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
/**
* Foscam HD
*
* Author: skp19
* Date: 6/18/14
*
* Example device type for HD Foscam cameras.
* Code based off of Foscam device type by brian@bevey.org.
* Heavily modified to work with the Foscam HD cameras.
*
* This device has the following functions:
* - Take a snapshot
* - Toggle the infrared lights
* - Enable/Disable motion alarm
* - Go to and set preset locations
* - Enable cruise maps
* - Control PTZ
* - Reboot
*
* Capability: Image Capture, Polling
* Custom Attributes: setStatus, alarmStatus, ledStatus
* Custom Commands: alarmOn, alarmOff, toggleAlarm, left, right, up, down,
* stop, set, preset, preset1, preset2, preset3, cruisemap1,
* cruisemap2, cruise, toggleLED, ledOn, ledOff, ledAuto
*/
preferences {
input("username", "text", title: "Username", description: "Your Foscam camera username")
input("password", "password", title: "Password", description: "Your Foscam camera password")
input("ip", "text", title: "IP address/Hostname", description: "The IP address or hostname of your Foscam camera")
input("port", "text", title: "Port", description: "The port of your Foscam camera")
input("preset1", "text", title: "Preset 1", description: "Name of your first preset position")
input("preset2", "text", title: "Preset 2", description: "Name of your second preset position")
input("preset3", "text", title: "Preset 3", description: "Name of your third preset position")
input("cruisemap1", "text", title: "Cruise Map 1", description: "Name of your first cruise map")
input("cruisemap2", "text", title: "Cruise Map 2", description: "Name of your second cruise map")
}
metadata {
definition (name: "Foscam HD") {
capability "Polling"
capability "Image Capture"
attribute "setStatus", "string"
attribute "alarmStatus", "string"
attribute "ledStatus", "string"
command "alarmOn"
command "alarmOff"
command "toggleAlarm"
command "left"
command "right"
command "up"
command "down"
command "stop"
command "set"
command "preset"
command "preset1"
command "preset2"
command "preset3"
command "cruisemap1"
command "cruisemap2"
command "cruise"
command "toggleLED"
command "ledOn"
command "ledOff"
command "ledAuto"
command "reboot"
}
tiles {
carouselTile("cameraDetails", "device.image", width: 3, height: 2) { }
standardTile("foscam", "device.alarmStatus", width: 1, height: 1, canChangeIcon: true, inactiveLabel: true, canChangeBackground: false) {
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("camera", "device.image", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "default", label: "", action: "Image Capture.take", icon: "st.camera.dropcam-centered", backgroundColor: "#FFFFFF"
}
standardTile("take", "device.image", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false, decoration: "flat") {
state "take", label: "", action: "Image Capture.take", icon: "st.secondary.take", nextState:"taking"
}
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("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("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("left", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "left", label: "left", action: "left", icon: ""
}
standardTile("stop", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "stop", label: "", action: "stop", icon: "st.sonos.stop-btn"
}
standardTile("right", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "right", label: "right", action: "right", icon: ""
}
standardTile("blank", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "blank", label: "", action: "stop", icon: "", backgroundColor: "#53A7C0"
}
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("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("set", "device.setStatus", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "set", label: "preset", action: "set", icon: "", backgroundColor: "#FFFFFF"
state "add", label: "set mode", action: "set", icon: "", backgroundColor: "#53A7C0"
}
standardTile("refresh", "device.alarmStatus", inactiveLabel: false, decoration: "flat") {
state "refresh", action:"polling.poll", icon:"st.secondary.refresh"
}
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("reboot", "device.image", inactiveLabel: false, decoration: "flat") {
state "reboot", label: "reboot", action: "reboot", icon: "st.Health & Wellness.health8"
}
main "foscam"
details(["cameraDetails", "take", "ledStatus", "alarmStatus", "preset1", "preset2", "preset3", "cruisemap1", "up", "cruisemap2", "left", "stop", "right", "blank", "down", ,"blank", "refresh", "set", "reboot"])
}
}
private getPictureName() {
def pictureUuid = java.util.UUID.randomUUID().toString().replaceAll('-', '')
"image" + "_$pictureUuid" + ".jpg"
}
def take() {
log.debug("Take a photo")
api("snapshot", "") {
log.debug("Image captured")
if(it.headers.'Content-Type'.contains("image/jpeg")) {
if(it.data) {
storeImage(getPictureName(), it.data)
}
}
}
}
def toggleAlarm() {
if(device.currentValue("alarmStatus") == "on") {
alarmOff()
}
else {
alarmOn()
}
}
def alarmOn() {
api("set_alarm", "isEnable=1") {
log.debug("Alarm changed to: on")
sendEvent(name: "alarmStatus", value: "on");
}
}
def alarmOff() {
api("set_alarm", "isEnable=0") {
log.debug("Alarm changed to: off")
sendEvent(name: "alarmStatus", value: "off");
}
}
def left() {
api("decoder_control", "cmd=ptzMoveLeft") {
log.debug("Executing 'LEFT'")
}
stop()
}
def right() {
api("decoder_control", "cmd=ptzMoveRight") {
log.debug("Executing 'RIGHT'")
}
stop()
}
def up() {
api("decoder_control", "cmd=ptzMoveUp") {
log.debug("Executing 'UP'")
}
stop()
}
def down() {
api("decoder_control", "cmd=ptzMoveDown") {
log.debug("Executing 'DOWN'")
}
stop()
}
def stop() {
api("decoder_control", "cmd=ptzStopRun") {
log.debug("Executing 'STOP'")
}
}
def preset1() {
log.debug("Preset 1 Selected - ${preset1}")
preset("${preset1}")
}
def preset2() {
log.debug("Preset 2 Selected - ${preset2}")
preset("${preset2}")
}
def preset3() {
log.debug("Preset 3 Selected - ${preset3}")
preset("${preset3}")
}
//Go to a preset location
def preset(def presetname) {
if(presetname == null) return
if(device.currentValue("setStatus") == "add") {
setPreset(presetname)
}
else {
log.debug("Go To Preset Location - " + presetname)
def cmd = "cmd=ptzGotoPresetPoint&name=" + presetname
api("decoder_control", "${cmd}") {}
}
}
//Set the selected preset to the current location
def setPreset(def presetname) {
log.debug("Set Preset - " + presetname)
delPreset(presetname)
addPreset(presetname)
log.debug("Exit Add Preset Mode")
sendEvent(name: "setStatus", value: "set");
}
//Delete currently selected preset
def delPreset(def presetname) {
log.debug("Delete Preset Location - " + presetname)
def cmd = "cmd=ptzDeletePresetPoint&name=" + presetname
api("decoder_control", "${cmd}") {}
}
//Add currently selected preset
def addPreset(def presetname) {
log.debug("Add Preset Location - " + presetname)
def cmd = "cmd=ptzAddPresetPoint&name=" + presetname
api("decoder_control", "${cmd}") {}
}
//Toggle the the mode to set the preset
def set() {
if((device.currentValue("setStatus") == "set").or(device.currentValue("setStatus") == "")) {
log.debug("Entering Add Preset Mode")
sendEvent(name: "setStatus", value: "add");
}
else {
log.debug("Exit Add Preset Mode")
sendEvent(name: "setStatus", value: "set");
}
}
def cruisemap1() {
log.debug("Cruise Map 1 Selected - ${cruisemap1}")
cruise("${cruisemap1}")
}
def cruisemap2() {
log.debug("Cruise Map 2 Selected - ${cruisemap2}")
cruise("${cruisemap2}")
}
//Start cruise
def cruise(def cruisename) {
log.debug("Start Cruise Map - " + cruisename)
def cmd = "cmd=ptzStartCruise&mapName=" + cruisename
api("decoder_control", "${cmd}") {}
}
//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() {
api("decoder_control", "cmd=setInfraLedConfig&mode=1") {}
api("decoder_control", "cmd=openInfraLed") {
log.debug("LED changed to: on")
sendEvent(name: "ledStatus", value: "on");
}
}
def ledOff() {
api("decoder_control", "cmd=setInfraLedConfig&mode=1") {}
api("decoder_control", "cmd=closeInfraLed") {
log.debug("LED changed to: off")
sendEvent(name: "ledStatus", value: "off");
}
}
def ledAuto() {
api("decoder_control", "cmd=setInfraLedConfig&mode=0") {
log.debug("LED changed to: auto")
sendEvent(name: "ledStatus", value: "auto");
}
}
def reboot() {
api("reboot", "") {
log.debug("Rebooting")
}
}
def api(method, args = [], success = {}) {
def methods = [
"decoder_control": [uri: "http://${ip}:${port}/cgi-bin/CGIProxy.fcgi${login()}&${args}", type: "get"],
"snapshot": [uri: "http://${ip}:${port}/cgi-bin/CGIProxy.fcgi${login()}&cmd=snapPicture2", type: "get"],
"set_alarm": [uri: "http://${ip}:${port}/cgi-bin/CGIProxy.fcgi${login()}&cmd=setMotionDetectConfig&${args}", type: "get"],
"reboot": [uri: "http://${ip}:${port}/cgi-bin/CGIProxy.fcgi${login()}&cmd=rebootSystem", type: "get"],
"camera_control": [uri: "http://${ip}:${port}/camera_control.cgi${login()}&${args}", type: "get"],
"get_params": [uri: "http://${ip}:${port}/cgi-bin/CGIProxy.fcgi${login()}&${args}", type: "get"]
]
def request = methods.getAt(method)
doRequest(request.uri, request.type, success)
}
private doRequest(uri, type, success) {
log.debug(uri)
httpGet(uri, success)
}
private login() {
return "?usr=${username}&pwd=${password}"
}
def poll() {
//Poll Motion Alarm Status
api("get_params", "cmd=getMotionDetectConfig") {
def CGI_Result = new XmlParser().parse(it.data)
def motionAlarm = CGI_Result.isEnable.text()
if(motionAlarm == "0") {
log.info("Polled: Alarm Off")
sendEvent(name: "alarmStatus", value: "off");
}
if(motionAlarm == "1") {
log.info("Polled: Alarm On")
sendEvent(name: "alarmStatus", value: "on");
}
}
//Poll IR LED Mode
api("get_params", "cmd=getInfraLedConfig") {
def CGI_Result = new XmlParser().parse(it.data)
def ledMode = CGI_Result.mode.text()
if(ledMode == "0") {
log.info("Polled: LED Mode Auto")
sendEvent(name: "ledStatus", value: "auto");
}
if(ledMode == "1") {
log.info("Polled: LED Mode Manual")
sendEvent(name: "ledStatus", value: "manual");
}
}
//Reset
}