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.

323 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. error_reporting( E_ALL );
  3. require_once('../settings.php');
  4. require_once('../defines.php');
  5. require_once('../php/argus/server.php');
  6. $DATAPATH = $SYSPath . 'lib/controller/data/reports/';
  7. function generateUUIDv4(): string
  8. {
  9. // Generate 16 random bytes
  10. $data = random_bytes(16);
  11. // Set the version to 4 (0100 in binary)
  12. // The 6th byte (index 5 in a 0-indexed array) contains the version information.
  13. // We clear the top 4 bits and then set the version bits to 0100 (4).
  14. $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
  15. // Set the variant to 2 (10 in binary, RFC 4122)
  16. // The 8th byte (index 7 in a 0-indexed array) contains the variant information.
  17. // We clear the top 2 bits and then set the variant bits to 10 (2).
  18. $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
  19. // Format the byte string into a UUID string with hyphens
  20. // vsprintf is used with a format string to insert the hexadecimal representation
  21. // of the byte chunks into the correct positions.
  22. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  23. }
  24. function createSession( $TID, $STS, $ETS ){
  25. global $DATABASE;
  26. $STSS = intval( $STS / 1000 );
  27. $STSM = $STS % 1000;
  28. $ETSS = intval( $ETS / 1000 );
  29. $UUID = generateUUIDv4();
  30. $QS = "INSERT INTO `sessions` SET `ts`={$STSS}, `ts_ms`={$STSM}, `lts`={$ETSS}, `uuid`='{$UUID}', `track_id`={$TID}, `status`=5, `final`='Y'";
  31. $DATABASE->Query( $QS );
  32. return $DATABASE->getLastInsertID();
  33. };
  34. function makeSureSessionDirIsOk( $TID, $TS ){
  35. global $DATAPATH;
  36. $status = true;
  37. $Y = date( 'Y', $TS );
  38. $M = date( 'm', $TS );
  39. $D = date( 'd', $TS );
  40. $CP = $DATAPATH . $TID;
  41. if( !file_exists( $CP ) ) {
  42. $status = mkdir( $CP );
  43. chown( $CP, 'www-data' );
  44. chgrp( $CP, 'www-data' );
  45. chmod( $CP, 0765 );
  46. };
  47. $CP .= '/'.$Y;
  48. if( $status ) if( !file_exists( $CP ) ) {
  49. $status = mkdir( $CP );
  50. chown( $CP, 'www-data' );
  51. chgrp( $CP, 'www-data' );
  52. chmod( $CP, 0765 );
  53. };
  54. $CP .= '/'.$M;
  55. if( $status ) if( !file_exists( $CP ) ) {
  56. $status = mkdir( $CP );
  57. chown( $CP, 'www-data' );
  58. chgrp( $CP, 'www-data' );
  59. chmod( $CP, 0765 );
  60. };
  61. $CP .= '/'.$D;
  62. if( $status ) if( !file_exists( $CP ) ) {
  63. $status = mkdir( $CP );
  64. chown( $CP, 'www-data' );
  65. chgrp( $CP, 'www-data' );
  66. chmod( $CP, 0765 );
  67. };
  68. if( $status ) return $CP . '/';
  69. return false;
  70. };
  71. function getSessionBasePath( $SID ){
  72. global $DATABASE, $DATAPATH;
  73. $OO = intval( $SID );
  74. if( $OO ){
  75. $QS = "SELECT `ts`,`track_id` FROM `sessions` WHERE `id`={$OO}";
  76. $RR = $DATABASE->Query( $QS );
  77. if( count( $RR ) ){
  78. $CTRC = intval( $RR[0]['track_id'] );
  79. $CSTS = intval( $RR[0]['ts'] );
  80. return makeSureSessionDirIsOk( $CTRC, $CSTS );
  81. };
  82. };
  83. return false;
  84. };
  85. function updateSessionStatEvt( $SID ){
  86. global $DATABASE;
  87. $QS = "UPDATE `sessions` SET `evt_cnt`=`evt_cnt`+1 WHERE `id`={$SID}";
  88. $DATABASE->Query( $QS );
  89. };
  90. function updateSessionStatVid( $SID ){
  91. global $DATABASE;
  92. $QS = "UPDATE `sessions` SET `vid_cnt`=`vid_cnt`+1 WHERE `id`={$SID}";
  93. $DATABASE->Query( $QS );
  94. };
  95. function addVideoFileToMediaConvertion( $FN, $W, $H, $FPS, $FMT, $STS, $MID, $SID ){
  96. global $DATABASE;
  97. if( !intval( $SID ) ) return false;
  98. if( !file_exists( $FN ) ) return false;
  99. if( ( $BD = getSessionBasePath( $SID ) ) === false ) return false;
  100. $UUID = generateUUIDv4();
  101. $DST = $BD . $UUID . '.'. $FMT;
  102. if( copy( $FN, $DST ) ){
  103. //unlink( $FN );
  104. $STSS = intval( $STS / 1000 );
  105. $STSM = $STS % 1000;
  106. $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";
  107. echo $QS . "\n";
  108. $DATABASE->Query( $QS );
  109. return true;
  110. };
  111. return false;
  112. };
  113. function copyEventsToReport( $SID, $MID, $STS, $ETS, $RecTypes = array(10,11,12,13,14,36)){
  114. global $RBASE;
  115. $STSS = intval( $STS / 1000 );
  116. $STSM = $STS % 1000;
  117. $ETSS = intval( $STS / 1000 );
  118. $ETSM = $ETS % 1000;
  119. if( ( $BD = getSessionBasePath( $SID ) ) === false ) return false;
  120. if( !file_exists( $BD.'report.db' ) ){
  121. $LBASE = new SQLite_DB( $BD.'report.db' );
  122. chown( $BD.'report.db', 'www-data' );
  123. chgrp( $BD.'report.db', 'www-data' );
  124. chmod( $BD.'report.db', 0670 );
  125. $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) )");
  126. $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) )");
  127. } else {
  128. $LBASE = new SQLite_DB( $BD.'report.db' );
  129. };
  130. $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`)";
  131. $Data = $RBASE->Query( $QS );
  132. foreach( $Data as $Datum ){
  133. $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']})";
  134. echo $QS . "\n";
  135. $LBASE->Query( $QS );
  136. updateSessionStatEvt($SID);
  137. };
  138. unset( $LBASE );
  139. };
  140. $MODE = 1; // targets
  141. $MODE = 2; // ROI
  142. $MODE = 3; // ALL
  143. // SET DATA CONNECTION AND GLOBAL USER VARIABLES
  144. $TT_123 = explode( '.', PHP_VERSION );
  145. if( $TT_123[0] == '4' ){
  146. require_once( '../php/db/MySQL_class.php' );
  147. $DATABASE = new MySQL_DB($DBHost, $DBName, $DBUser, $DBPassword);
  148. } elseif( $TT_123[0] > '4' ){
  149. require_once( '../php/db/MySQLi_class.php' );
  150. $DATABASE = new MySQLi_DB($DBHost, $DBName, $DBUser, $DBPassword);
  151. } else {
  152. exit();
  153. };
  154. require_once( '../php/db/internal.php' );
  155. require_once( '../php/db/SQLite_Class.php');
  156. if( count( $argv ) < 2 ){
  157. echo "No Parameters are set!\n\tThe Right Usage is:\n\n\t{$argv[0]} <module_id> <report_folder>\n\n";
  158. $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`";
  159. $Res = $DATABASE->Query( $QS );
  160. if( count( $Res ) ){
  161. echo "\tAvealable Modules are:\n";
  162. foreach( $Res as $R ){
  163. $MID = intval( $R['id'] );
  164. $PID = intval( $R['platform_id'] );
  165. $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}";
  166. $JJ = $DATABASE->Query( $QS );
  167. echo "\t\t{$MID}\t[{$JJ[0]['name']}]:[{$JJ[0]['pl_name']}]:[{$R['short']}]:[{$R['name']}]\n";
  168. };
  169. } else {
  170. echo "However No Modules are existed!!!\n";
  171. };
  172. } elseif( count( $argv ) < 3 ) {
  173. $SMID = intval( $argv[1] );
  174. if($SMID){
  175. echo "Insuffision Parameters are set!\n\tThe Right Usage is:\n\n\t{$argv[0]} {$argv[1]} <report_folder>\n\n";
  176. } else {
  177. echo "Wrong Parameter is set!\n\tThe Right Usage is:\n\n\t{$argv[0]} <module_id> <report_folder>\n\n";
  178. exit();
  179. };
  180. $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}";
  181. $Res = $DATABASE->Query( $QS );
  182. if( count( $Res ) ){
  183. echo "\tSelected Module is:\n\n";
  184. $R = $Res[0];
  185. $MID = intval( $R['id'] );
  186. $PID = intval( $R['platform_id'] );
  187. $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}";
  188. $JJ = $DATABASE->Query( $QS );
  189. echo "\t\t{$MID}\t[{$JJ[0]['name']}]:[{$JJ[0]['pl_name']}]:[{$R['short']}]:[{$R['name']}]\n\n";
  190. } else {
  191. echo "\tThe Module ID is also WRONG!!!\n";
  192. };
  193. } else {
  194. $SMID = intval( $argv[1] );
  195. $FID = $argv[2];
  196. $EX = 0;
  197. if( !file_exists( $FID ) ){
  198. echo "Insuffision Parameters are set!\n\tThe Right Usage is:\n\n\t{$argv[0]} {$argv[1]} <report_folder>\n\n";
  199. } else {
  200. if( !file_exists( $FID . '/report.db' ) ){
  201. } else {
  202. $EX = 1;
  203. }
  204. };
  205. $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}";
  206. $Res = $DATABASE->Query( $QS );
  207. if( count( $Res ) ){
  208. echo "\tSelected Module is:\n\n";
  209. $R = $Res[0];
  210. $MID = intval( $R['id'] );
  211. $TID = intval( $R['track_id'] );
  212. $PID = intval( $R['platform_id'] );
  213. $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}";
  214. $JJ = $DATABASE->Query( $QS );
  215. echo "\t\t{$MID}\t[{$JJ[0]['name']}]:[{$JJ[0]['pl_name']}]:[{$R['short']}]:[{$R['name']}]\n\n";
  216. } else {
  217. $EX = 0;
  218. echo "\tThe Module ID is also WRONG!!!\n";
  219. };
  220. if( $EX ){
  221. $RBASE = new SQLite_DB( $FID . '/report.db' );
  222. $QS = "SELECT COUNT(`id`) AS `cnt` FROM `events`";
  223. $R = $RBASE->Query( $QS );
  224. if( count( $R ) ){
  225. echo "\t{$R[0]['cnt']} Tottal Events in Database!\n";
  226. $QS = "SELECT COUNT(`id`) AS `cnt` FROM `events` WHERE `evt_type` IN( 10,11,12,13,14 )";
  227. $R = $RBASE->Query( $QS );
  228. $RTC = intval( $R[0]['cnt'] );
  229. echo "\t{$RTC} Real Target Events in Database!\n";
  230. $QS = "SELECT COUNT(`id`) AS `cnt` FROM `events` WHERE `evt_type`=36";
  231. $R = $RBASE->Query( $QS );
  232. $ITC = intval( $R[0]['cnt'] );
  233. echo "\t{$ITC} ROI Events in Database!\n";
  234. $QS = "SELECT COUNT(`id`) AS `cnt` FROM `videos`";
  235. $R = $RBASE->Query( $QS );
  236. $VC = intval( $R[0]['cnt'] );
  237. echo "\t{$VC} Videos in Database!\n";
  238. ///////////////////////////////////////////////
  239. echo "\tWORKING .... \n";
  240. $QS = "SELECT `vv`.`id`, `vv`.`md5`, `vv`.`uts`, `vv`.`uts_ms`,`vv`.`ext`,`vv`.`fps` FROM `videos` `vv`";
  241. $Data = $RBASE->Query( $QS );
  242. $SES_START = 0;
  243. $SES_END = 0;
  244. $SESS_ID = 0;
  245. $SESS_VC = 0;
  246. $CVIDEOS = array();
  247. if( count( $Data ) ){
  248. foreach( $Data as $Datum ){
  249. $VNAM = $FID . '/' . $Datum['md5'] . '.'. $Datum['ext'];
  250. $Datum['FN'] = $VNAM;
  251. if( file_exists( $VNAM ) ){
  252. $FSIZ = filesize( $VNAM );
  253. $VID = intval( $Datum['id'] );
  254. $VTS = intval( $Datum['uts'] ) * 1000 + intval( $Datum['uts_ms'] );
  255. $Datum['STS'] = $VTS;
  256. $CVIDEOS[] = $Datum;
  257. if( $SES_START == 0 ){
  258. $SES_START = $VTS;
  259. $SES_END = $VTS+60000;
  260. $SESS_VC = 1;
  261. } else {
  262. if( $VTS < $SES_END ){
  263. if( $VTS < $SES_START ){
  264. $SES_START = $VTS;
  265. } else {
  266. if( ( $VTS+60000 ) > $SES_END ) $SES_END = $VTS+60000;
  267. };
  268. $SESS_VC++;
  269. } else {
  270. // Close Old Session and Start New One
  271. //Add session
  272. $CSID = createSession( $TID, $SES_START, $SES_END );
  273. if( $CSID ){
  274. echo "Session [{$SESS_ID}] Created as [{$CSID}]\n";
  275. foreach( $CVIDEOS as $V ){
  276. addVideoFileToMediaConvertion( $V['FN'], 1280, 720, $V['fps'], $V['ext'], $V['STS'], $MID, $CSID );
  277. updateSessionStatVid( $CSID );
  278. };
  279. copyEventsToReport( $CSID, $MID, $SES_START, $SES_END );
  280. $CVIDEOS = array();
  281. };
  282. $SESS_ID++;
  283. $SES_START = $VTS;
  284. $SES_END = $VTS+60000;
  285. $SESS_VC = 1;
  286. };
  287. };
  288. };
  289. };
  290. // Last Session
  291. $CSID = createSession( $TID, $SES_START, $SES_END );
  292. if( $CSID ){
  293. echo "Session [{$SESS_ID}] Created as [{$CSID}]\n";
  294. foreach( $CVIDEOS as $V ){
  295. addVideoFileToMediaConvertion( $V['FN'], 1280, 720, $V['fps'], $V['ext'], $V['STS'], $MID, $CSID );
  296. updateSessionStatVid( $CSID );
  297. };
  298. copyEventsToReport( $CSID, $MID, $SES_START, $SES_END );
  299. $CVIDEOS = array();
  300. };
  301. };
  302. } else {
  303. echo "\tNo Events in Database!!!\n";
  304. }
  305. };
  306. };
  307. //$REPORT = new SQLite_DB( '/srv/www/web/lib/controller/data/mysqlitedb.db');
  308. function analyseReportDB( $DBF ){
  309. };
  310. ?>