Here is some sample code adapted from one of my rule files that demonstrates instructing two different Foscam model cameras to enable or disable motion detection, based on the switch state of the Present
item. When the switch changes to OFF, it sends HTTP commands to each camera (IP addresses ending in .14 and .15) to enable motion detection, and in the case of the model that supports callbacks on motion, tells it to callback to the openHAB server (IP address ending in .10) to flip a Switch
item. When the Present
switch is set to ON, it disables motion detection on all cameras.
import org.openhab.core.library.types.*
import java.util.HashMap
import java.util.LinkedHashMap
val HashMap<String, LinkedHashMap<String, Object>> cameras =
newLinkedHashMap(
"entrance-cam" -> (newLinkedHashMap(
"snap_url" -> "http://192.168.1.14/snapshot.cgi?user=admin&pwd=password",
"enable_motion_url" -> "http://192.168.1.14/set_alarm.cgi?motion_armed=1&motion_sensitivity=5&motion_compensation=1&mail=1&upload_i
nterval=34463&http=1&http_url=http%3A%2F%2F192.168.1.10%3A8080%2FCMD%3FEntranceMotion%3DON&schedule_enable=0&user=admin&pwd=password",
"disable_motion_url" -> "http://192.168.1.14/set_alarm.cgi?motion_armed=0&user=admin&pwd=password")
as LinkedHashMap<String, Object>),
"kitchen-cam" -> (newLinkedHashMap(
"snap_url" -> "http://192.168.1.15:88/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=admin&pwd=password",
"enable_motion_url" -> "http://192.168.1.15:88/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=1&linkage=7&snapInterval=2&
sensitivity=1&triggerInterval=5&schedule0=1023&schedule1=1023&schedule2=1023&schedule3=1023&schedule4=1023&schedule5=1023&schedule6=1023&area0=1023&ar
ea1=1023&area2=1023&area3=1023&area4=1023&area5=1023&area6=1023&area7=1023&area8=1023&area9=1023&usr=admin&pwd=password",
"disable_motion_url" -> "http://192.168.1.15:88/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=0&usr=admin&pwd=password")
as LinkedHashMap<String, Object>)
)
rule PresenceChanged
when
Item Present changed
then
switch Present.state {
case OFF : cameras.values.forEach [ camera | sendHttpGetRequest(camera.get("enable_motion_url")) ]
case ON : cameras.values.forEach [ camera | sendHttpGetRequest(camera.get("disable_motion_url")) ]
}
end