You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

324 lines
12 KiB

<?php
error_reporting( E_ALL );
require_once('../settings.php');
require_once('../defines.php');
require_once('../php/argus/server.php');
$DATAPATH = $SYSPath . 'lib/controller/data/reports/';
function generateUUIDv4(): string
{
// Generate 16 random bytes
$data = random_bytes(16);
// Set the version to 4 (0100 in binary)
// The 6th byte (index 5 in a 0-indexed array) contains the version information.
// We clear the top 4 bits and then set the version bits to 0100 (4).
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// Set the variant to 2 (10 in binary, RFC 4122)
// The 8th byte (index 7 in a 0-indexed array) contains the variant information.
// We clear the top 2 bits and then set the variant bits to 10 (2).
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
// Format the byte string into a UUID string with hyphens
// vsprintf is used with a format string to insert the hexadecimal representation
// of the byte chunks into the correct positions.
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
function createSession( $TID, $STS, $ETS ){
global $DATABASE;
$STSS = intval( $STS / 1000 );
$STSM = $STS % 1000;
$ETSS = intval( $ETS / 1000 );
$UUID = generateUUIDv4();
$QS = "INSERT INTO `sessions` SET `ts`={$STSS}, `ts_ms`={$STSM}, `lts`={$ETSS}, `uuid`='{$UUID}', `track_id`={$TID}, `status`=5, `final`='Y'";
$DATABASE->Query( $QS );
return $DATABASE->getLastInsertID();
};
function makeSureSessionDirIsOk( $TID, $TS ){
global $DATAPATH;
$status = true;
$Y = date( 'Y', $TS );
$M = date( 'm', $TS );
$D = date( 'd', $TS );
$CP = $DATAPATH . $TID;
if( !file_exists( $CP ) ) {
$status = mkdir( $CP );
chown( $CP, 'www-data' );
chgrp( $CP, 'www-data' );
chmod( $CP, 0765 );
};
$CP .= '/'.$Y;
if( $status ) if( !file_exists( $CP ) ) {
$status = mkdir( $CP );
chown( $CP, 'www-data' );
chgrp( $CP, 'www-data' );
chmod( $CP, 0765 );
};
$CP .= '/'.$M;
if( $status ) if( !file_exists( $CP ) ) {
$status = mkdir( $CP );
chown( $CP, 'www-data' );
chgrp( $CP, 'www-data' );
chmod( $CP, 0765 );
};
$CP .= '/'.$D;
if( $status ) if( !file_exists( $CP ) ) {
$status = mkdir( $CP );
chown( $CP, 'www-data' );
chgrp( $CP, 'www-data' );
chmod( $CP, 0765 );
};
if( $status ) return $CP . '/';
return false;
};
function getSessionBasePath( $SID ){
global $DATABASE, $DATAPATH;
$OO = intval( $SID );
if( $OO ){
$QS = "SELECT `ts`,`track_id` FROM `sessions` WHERE `id`={$OO}";
$RR = $DATABASE->Query( $QS );
if( count( $RR ) ){
$CTRC = intval( $RR[0]['track_id'] );
$CSTS = intval( $RR[0]['ts'] );
return makeSureSessionDirIsOk( $CTRC, $CSTS );
};
};
return false;
};
function updateSessionStatEvt( $SID ){
global $DATABASE;
$QS = "UPDATE `sessions` SET `evt_cnt`=`evt_cnt`+1 WHERE `id`={$SID}";
$DATABASE->Query( $QS );
};
function updateSessionStatVid( $SID ){
global $DATABASE;
$QS = "UPDATE `sessions` SET `vid_cnt`=`vid_cnt`+1 WHERE `id`={$SID}";
$DATABASE->Query( $QS );
};
function addVideoFileToMediaConvertion( $FN, $W, $H, $FPS, $FMT, $STS, $MID, $SID ){
global $DATABASE;
if( !intval( $SID ) ) return false;
if( !file_exists( $FN ) ) return false;
if( ( $BD = getSessionBasePath( $SID ) ) === false ) return false;
$UUID = generateUUIDv4();
$DST = $BD . $UUID . '.'. $FMT;
if( copy( $FN, $DST ) ){
//unlink( $FN );
$STSS = intval( $STS / 1000 );
$STSM = $STS % 1000;
$QS = "INSERT INTO `media_queue` SET `module_id`={$MID}, `session_id`={$SID}, `ts`={$STSS}, `tsms`={$STSM}, `uuid`='{$UUID}', `width`={$W}, `height`={$H}, `fps`={$FPS}, `fmt`='$FMT', `status`=1";
echo $QS . "\n";
$DATABASE->Query( $QS );
return true;
};
return false;
};
function copyEventsToReport( $SID, $MID, $STS, $ETS, $RecTypes = array(10,11,12,13,14,36)){
global $RBASE;
$STSS = intval( $STS / 1000 );
$STSM = $STS % 1000;
$ETSS = intval( $STS / 1000 );
$ETSM = $ETS % 1000;
if( ( $BD = getSessionBasePath( $SID ) ) === false ) return false;
if( !file_exists( $BD.'report.db' ) ){
$LBASE = new SQLite_DB( $BD.'report.db' );
chown( $BD.'report.db', 'www-data' );
chgrp( $BD.'report.db', 'www-data' );
chmod( $BD.'report.db', 0670 );
$LBASE->Query("CREATE TABLE IF NOT EXISTS \"events\" ( \"id\" INTEGER NOT NULL, \"session_id\" INTEGER NOT NULL, \"module_id\" INTEGER NOT NULL, \"ts\" INTEGER NOT NULL, \"ts_ms\" INTEGER NOT NULL, \"evt_type\" INTEGER NOT NULL, \"azimuth\" REAL, \"elevation\" REAL, \"range\" REAL, \"area\" REAL, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
$LBASE->Query("CREATE TABLE IF NOT EXISTS \"videos\" ( \"id\" INTEGER NOT NULL, \"session_id\" INTEGER NOT NULL, \"module_id\" INTEGER NOT NULL, \"uuid\" TEXT NOT NULL UNIQUE, \"ts\" INTEGER NOT NULL, \"ts_ms\" INTEGER NOT NULL, \"ext\" TEXT NOT NULL, \"fps\" INTEGER NOT NULL, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
} else {
$LBASE = new SQLite_DB( $BD.'report.db' );
};
$QS = "SELECT * FROM `events` WHERE `evt_type` IN( 10,11,12,13,14,36) AND (`ts`*1000+`ts_ms`)<={$ETS} AND (`ts`*1000+`ts_ms`)>={$STS} ORDER BY (`ts`*1000+`ts_ms`)";
$Data = $RBASE->Query( $QS );
foreach( $Data as $Datum ){
$QS="INSERT INTO `events`( `session_id`,`module_id`,`ts`,`ts_ms`,`evt_type`,`azimuth`,`elevation`,`range`,`area` ) VALUES({$SID},{$MID},{$Datum['ts']},{$Datum['ts_ms']},{$Datum['evt_type']},{$Datum['azimuth']},{$Datum['elevation']},{$Datum['range']},{$Datum['area']})";
echo $QS . "\n";
$LBASE->Query( $QS );
updateSessionStatEvt($SID);
};
unset( $LBASE );
};
$MODE = 1; // targets
$MODE = 2; // ROI
$MODE = 3; // ALL
// SET DATA CONNECTION AND GLOBAL USER VARIABLES
$TT_123 = explode( '.', PHP_VERSION );
if( $TT_123[0] == '4' ){
require_once( '../php/db/MySQL_class.php' );
$DATABASE = new MySQL_DB($DBHost, $DBName, $DBUser, $DBPassword);
} elseif( $TT_123[0] > '4' ){
require_once( '../php/db/MySQLi_class.php' );
$DATABASE = new MySQLi_DB($DBHost, $DBName, $DBUser, $DBPassword);
} else {
exit();
};
require_once( '../php/db/internal.php' );
require_once( '../php/db/SQLite_Class.php');
if( count( $argv ) < 2 ){
echo "No Parameters are set!\n\tThe Right Usage is:\n\n\t{$argv[0]} <module_id> <report_folder>\n\n";
$QS = "SELECT `mm`.`id`, `mm`.`name`, `mt`.`platform_id`, `mt`.`short` FROM `metro_modules` `mm` LEFT JOIN `metro_tracks` `mt` ON ( `mm`.`track_id` = `mt`.`id`) ORDER BY `mm`.`id`";
$Res = $DATABASE->Query( $QS );
if( count( $Res ) ){
echo "\tAvealable Modules are:\n";
foreach( $Res as $R ){
$MID = intval( $R['id'] );
$PID = intval( $R['platform_id'] );
$QS = "SELECT `st`.`name`, `pl`.`name` as `pl_name` FROM `metro_platforms` `pl` LEFT JOIN `metro_stations` `st` ON( `pl`.`station_id`=`st`.`id`) WHERE `pl`.`id`={$PID}";
$JJ = $DATABASE->Query( $QS );
echo "\t\t{$MID}\t[{$JJ[0]['name']}]:[{$JJ[0]['pl_name']}]:[{$R['short']}]:[{$R['name']}]\n";
};
} else {
echo "However No Modules are existed!!!\n";
};
} elseif( count( $argv ) < 3 ) {
$SMID = intval( $argv[1] );
if($SMID){
echo "Insuffision Parameters are set!\n\tThe Right Usage is:\n\n\t{$argv[0]} {$argv[1]} <report_folder>\n\n";
} else {
echo "Wrong Parameter is set!\n\tThe Right Usage is:\n\n\t{$argv[0]} <module_id> <report_folder>\n\n";
exit();
};
$QS = "SELECT `mm`.`id`, `mm`.`name`, `mt`.`platform_id`, `mt`.`short` FROM `metro_modules` `mm` LEFT JOIN `metro_tracks` `mt` ON ( `mm`.`track_id` = `mt`.`id`) WHERE `mm`.`id`={$SMID}";
$Res = $DATABASE->Query( $QS );
if( count( $Res ) ){
echo "\tSelected Module is:\n\n";
$R = $Res[0];
$MID = intval( $R['id'] );
$PID = intval( $R['platform_id'] );
$QS = "SELECT `st`.`name`, `pl`.`name` as `pl_name` FROM `metro_platforms` `pl` LEFT JOIN `metro_stations` `st` ON( `pl`.`station_id`=`st`.`id`) WHERE `pl`.`id`={$PID}";
$JJ = $DATABASE->Query( $QS );
echo "\t\t{$MID}\t[{$JJ[0]['name']}]:[{$JJ[0]['pl_name']}]:[{$R['short']}]:[{$R['name']}]\n\n";
} else {
echo "\tThe Module ID is also WRONG!!!\n";
};
} else {
$SMID = intval( $argv[1] );
$FID = $argv[2];
$EX = 0;
if( !file_exists( $FID ) ){
echo "Insuffision Parameters are set!\n\tThe Right Usage is:\n\n\t{$argv[0]} {$argv[1]} <report_folder>\n\n";
} else {
if( !file_exists( $FID . '/report.db' ) ){
} else {
$EX = 1;
}
};
$QS = "SELECT `mm`.`id`, `mm`.`track_id`, `mm`.`name`, `mt`.`platform_id`, `mt`.`short` FROM `metro_modules` `mm` LEFT JOIN `metro_tracks` `mt` ON ( `mm`.`track_id` = `mt`.`id`) WHERE `mm`.`id`={$SMID}";
$Res = $DATABASE->Query( $QS );
if( count( $Res ) ){
echo "\tSelected Module is:\n\n";
$R = $Res[0];
$MID = intval( $R['id'] );
$TID = intval( $R['track_id'] );
$PID = intval( $R['platform_id'] );
$QS = "SELECT `st`.`name`, `pl`.`name` as `pl_name` FROM `metro_platforms` `pl` LEFT JOIN `metro_stations` `st` ON( `pl`.`station_id`=`st`.`id`) WHERE `pl`.`id`={$PID}";
$JJ = $DATABASE->Query( $QS );
echo "\t\t{$MID}\t[{$JJ[0]['name']}]:[{$JJ[0]['pl_name']}]:[{$R['short']}]:[{$R['name']}]\n\n";
} else {
$EX = 0;
echo "\tThe Module ID is also WRONG!!!\n";
};
if( $EX ){
$RBASE = new SQLite_DB( $FID . '/report.db' );
$QS = "SELECT COUNT(`id`) AS `cnt` FROM `events`";
$R = $RBASE->Query( $QS );
if( count( $R ) ){
echo "\t{$R[0]['cnt']} Tottal Events in Database!\n";
$QS = "SELECT COUNT(`id`) AS `cnt` FROM `events` WHERE `evt_type` IN( 10,11,12,13,14 )";
$R = $RBASE->Query( $QS );
$RTC = intval( $R[0]['cnt'] );
echo "\t{$RTC} Real Target Events in Database!\n";
$QS = "SELECT COUNT(`id`) AS `cnt` FROM `events` WHERE `evt_type`=36";
$R = $RBASE->Query( $QS );
$ITC = intval( $R[0]['cnt'] );
echo "\t{$ITC} ROI Events in Database!\n";
$QS = "SELECT COUNT(`id`) AS `cnt` FROM `videos`";
$R = $RBASE->Query( $QS );
$VC = intval( $R[0]['cnt'] );
echo "\t{$VC} Videos in Database!\n";
///////////////////////////////////////////////
echo "\tWORKING .... \n";
$QS = "SELECT `vv`.`id`, `vv`.`md5`, `vv`.`uts`, `vv`.`uts_ms`,`vv`.`ext`,`vv`.`fps` FROM `videos` `vv`";
$Data = $RBASE->Query( $QS );
$SES_START = 0;
$SES_END = 0;
$SESS_ID = 0;
$SESS_VC = 0;
$CVIDEOS = array();
if( count( $Data ) ){
foreach( $Data as $Datum ){
$VNAM = $FID . '/' . $Datum['md5'] . '.'. $Datum['ext'];
$Datum['FN'] = $VNAM;
if( file_exists( $VNAM ) ){
$FSIZ = filesize( $VNAM );
$VID = intval( $Datum['id'] );
$VTS = intval( $Datum['uts'] ) * 1000 + intval( $Datum['uts_ms'] );
$Datum['STS'] = $VTS;
$CVIDEOS[] = $Datum;
if( $SES_START == 0 ){
$SES_START = $VTS;
$SES_END = $VTS+60000;
$SESS_VC = 1;
} else {
if( $VTS < $SES_END ){
if( $VTS < $SES_START ){
$SES_START = $VTS;
} else {
if( ( $VTS+60000 ) > $SES_END ) $SES_END = $VTS+60000;
};
$SESS_VC++;
} else {
// Close Old Session and Start New One
//Add session
$CSID = createSession( $TID, $SES_START, $SES_END );
if( $CSID ){
echo "Session [{$SESS_ID}] Created as [{$CSID}]\n";
foreach( $CVIDEOS as $V ){
addVideoFileToMediaConvertion( $V['FN'], 1280, 720, $V['fps'], $V['ext'], $V['STS'], $MID, $CSID );
updateSessionStatVid( $CSID );
};
copyEventsToReport( $CSID, $MID, $SES_START, $SES_END );
$CVIDEOS = array();
};
$SESS_ID++;
$SES_START = $VTS;
$SES_END = $VTS+60000;
$SESS_VC = 1;
};
};
};
};
// Last Session
$CSID = createSession( $TID, $SES_START, $SES_END );
if( $CSID ){
echo "Session [{$SESS_ID}] Created as [{$CSID}]\n";
foreach( $CVIDEOS as $V ){
addVideoFileToMediaConvertion( $V['FN'], 1280, 720, $V['fps'], $V['ext'], $V['STS'], $MID, $CSID );
updateSessionStatVid( $CSID );
};
copyEventsToReport( $CSID, $MID, $SES_START, $SES_END );
$CVIDEOS = array();
};
};
} else {
echo "\tNo Events in Database!!!\n";
}
};
};
//$REPORT = new SQLite_DB( '/srv/www/web/lib/controller/data/mysqlitedb.db');
function analyseReportDB( $DBF ){
};
?>