Code: Select all
#!/usr/bin/php
<?php
require_once("./mqtt/iot.php");
// These are the IDXs for the Motion Sensors - note multiple cameras can trigger one sensor. The Array keys - are the camera IDs (we have a 16ch system)
$trigger[1] = 785;
$trigger[2] = 785;
$trigger[3] = 786;
$trigger[4] = 786;
$trigger[5] = 787;
$trigger[6] = 788;
$trigger[9] = 789;
$trigger[15] = 790;
$trigger[16] = 790;
// This is the table of previous states and timestamps
$sens['cafe']['state'] = 0;
$sens['foyer']['state'] = 0;
$sens['auditorium']['state'] = 0;
$sens['balcony']['state'] = 0;
$sens['houseRear']['state'] = 0;
$sens['theatreExt']['state'] = 0;
$sens['cafe']['time'] = 0;
$sens['foyer']['time'] = 0;
$sens['auditorium']['time'] = 0;
$sens['balcony']['time'] = 0;
$sens['houseRear']['time'] = 0;
$sens['theatreExt']['time'] = 0;
// This links the virtual IDXs to the sensors in the above table
$idx[785] = "cafe";
$idx[786] = "foyer";
$idx[787] = "auditorium";
$idx[788] = "balcony";
$idx[789] = "houseRear";
$idx[790] = "theatreExt";
$cctv = loadCCTVState();
$data = processMotionData();
$time = time() - 35;
echo "Timenow is {$time}\n";
foreach($cctv['sensors'] as $s => $v) {
echo "Sensor $s has time of {$cctv['sensors'][$s]['time']}\n";
if ($cctv['sensors'][$s]['time'] < $time && $cctv['sensors'][$s]['state'] == 1) {
$cctv['sensors'][$s]['state'] = 0;
}
}
storeCCTVState();
function processMotionData() {
global $cctv, $trigger, $idx;
$cmds = array();
$data = getData();
print_r($data);
if (!$data['count']) { die('Empty Motion Data List'); }
foreach ($data['events'] as $k => $v) {
$cam = $v['id'];
$ids = (isset($trigger[$cam]) ? $trigger[$cam] : 0);
echo "ID: {$k} - Trigger: {$cam} == ";
echo ($ids ? $ids : "NULL") . " :: " .($ids ? $idx[$ids] : "") ."\n";
if ($ids) {
$tstamp = strtotime($v['date'] ." " .$v['time']);
$s = $idx[$ids];
if (!$cctv['sensors'][$s]['state']) {
$cctv['sensors'][$s]['state'] = 1;
$cctv['sensors'][$s]['time'] = $tstamp;
if (!isset($cmds['time'][$s])) {
$cmds['trigger'][$s] = 1;
$cmds['time'][$s] = $tstamp;
}
}
if ($cctv['sensors'][$s]['state'] == 1 && ($cctv['sensors'][$s]['time'] + 65) < $tstamp) {
if (!isset($cmds['time'][$s])) {
$cmds['trigger'][$s] = 1;
$cmds['time'][$s] = $tstamp;
$cctv['sensors'][$s]['time'] = $tstamp;
}
}
}
}
if (count($cmds['trigger'])) {
$arr = array();
$arr['command'] = "switchlight";
$arr['switchcmd'] = "On";
$ids = array_flip($idx);
foreach($cmds['trigger'] as $k => $v) {
$arr['idx'] = $ids[$k];
pmqttPublishMessage("domoticz/in",strMakeCMDSafe($arr));
}
}
}
function getData() {
$motion = array();
$motion['count'] = 0;
$url = "http://<IP OF CCTV BOX>/cgi-bin/supervisor/NetworkBk.cgi";
$u = "admin";
$p = "<PASSWORD OF CCTV BOX>";
$cmd['action'] = "query";
$cmd['type'] = "search_list";
//$cmd['list_mode'] = "calendar";
$cmd['command'] = "latest";
$cmd['hdd_num'] = 1;
$cmd['list_num'] = 50;
$cmd['list_type'] = "MOTION";
$post = http_build_query($cmd);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url ."?{$post}");
curl_setopt($ch, CURLOPT_USERPWD, "$u:$p");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$ret = curl_exec($ch);
curl_close($ch);
if (!empty($ret)) {
$lines = explode("\n",$ret);
if (isset($lines[3])) {
$count = 2;
while ($count < count($lines)) {
if (!empty(trim($lines[$count]))) {
$cols = explode(" ",$lines[$count]);
$cam = array();
$cam['id'] = $cols[1];
$cam['date'] = $cols[2];
$cam['time'] = $cols[3];
$motion['events'][] = $cam;
$motion['count']++;
}
$count++;
}
return $motion;
}
}
return $motion;
}
function loadCCTVState() {
global $sens;
$cache['time'] = 0;
$cache['sensors'] = $sens;
$f = "./cache/cctv-state.json";
if (file_exists($f)) {
$cache = json_decode(file_get_contents($f),true);
return $cache;
}
return $cache;
}
function storeCCTVState() {
global $cctv;
$t = array();
$t['time'] = $cctv['time'];
$t['sensors'] = $cctv['sensors'];
$f = "./cache/cctv-state.json";
file_put_contents($f,json_encode($t));
}
?>
Code: Select all
<?php
$mqttsecure['user'] = "<USER>";
$mqttsecure['pass'] = "<PASSWORD>";
$mqttsecure['host'] = "<MQTTHOST>";
$mqttsecure['topic'] = "domoticz/in";
function strMakeCMDSafe($arr) {
return str_replace("\"","\\\"",json_encode($arr));
}
function pmqttPublishMessage($topic,$msg) {
global $mqttsecure;
global $debug;
$time = time();
$cmd = "/usr/bin/mosquitto_pub -h \"{$mqttsecure['host']}\" -t \"{$topic}\" -u \"{$mqttsecure['user']}\" -P \"{$mqttsecure['pass']}\" -m \"{$msg}\"";
if (!$debug || stripos($topic,"/exec")) {
exec($cmd,$out);
} else {
echo $cmd;
}
//echo date('g:i:sa',$time) .":: ". $cmd ."\n";
}
?>