Loop new implementation test@1

This commit is contained in:
xdrm-brackets 2017-09-28 10:33:50 +02:00
parent f20b69f084
commit 4798afbd76
1 changed files with 23 additions and 34 deletions

View File

@ -15,6 +15,7 @@
$FEATURE = basename(dirname(__DIR__)); $FEATURE = basename(dirname(__DIR__));
$LOOP_FREQ = 100; // reading frequency in ms
@ -121,14 +122,16 @@
return false; return false;
$cur_timeout = floor(microtime(true) - $timeout);
/* [2] Manage timeout /* [2] Manage timeout
=========================================================*/ =========================================================*/
/* (1) If no action for this @timeout -> reset to 0 */ /* (1) If no action for this @cur_timeout -> reset to 0 */
if( !isset($actions[$timeout]) || !is_array($actions[$timeout]) ) if( !isset($actions[$cur_timeout]) || !is_array($actions[$cur_timeout]) )
$timeout = 0; return false;
/* (2) fetch actions for the current @timeout */ /* (2) fetch actions for the current @cur_timeout */
$actionlist = $actions[$timeout]; $actionlist = $actions[floor($cur_timeout)];
/* [3] Manage permissions (action that can be performed) /* [3] Manage permissions (action that can be performed)
@ -221,7 +224,6 @@
slog('cannot update STATE file', 'rfid-read:loop'); slog('cannot update STATE file', 'rfid-read:loop');
/* [6] Log action /* [6] Log action
=========================================================*/ =========================================================*/
/* (1) Log action to default log file */ /* (1) Log action to default log file */
@ -270,6 +272,7 @@
/* (2) Append accesses (logs) */ /* (2) Append accesses (logs) */
if( !file_exists(DATA_DIR."/$FEATURE") ) if( !file_exists(DATA_DIR."/$FEATURE") )
file_put_contents(DATA_DIR."/$FEATURE", ''); file_put_contents(DATA_DIR."/$FEATURE", '');
$f_accesslog = new SplFileObject(DATA_DIR."/$FEATURE", 'a'); $f_accesslog = new SplFileObject(DATA_DIR."/$FEATURE", 'a');
@ -351,13 +354,10 @@
/* [1] Wait for rfid card /* [1] Wait for rfid card
=========================================================*/ =========================================================*/
/* (1) Start timer */ /* (1) Read card */
$start_ts = microtime(true);
/* (2) Read card */
$code = syscall(SOURCE_DIR."/feature/$FEATURE/read"); $code = syscall(SOURCE_DIR."/feature/$FEATURE/read");
/* (3) If no card read -> reset @last_user / @timeout + abort */ /* (2) If no card read -> reset @last_user / @timeout + abort */
if( $code === false ){ if( $code === false ){
$last_user = null; $last_user = null;
@ -366,34 +366,21 @@
} }
/* (4) If timeout exceeded -> reset @timeout and @last_user */ /* (3) If code -> format it */
if( microtime(true) - $start_ts >= 0.5 ){
$timeout = 0;
$last_user = null;
}
/* (5) Wait for 1/2 second */
while( microtime(true) - $start_ts < 0.5 );
/* (6) If code -> format it */
$code = strtoupper($code); $code = strtoupper($code);
slog("card '$code' read", 'rfid-read:read'); slog("card '$code' read", 'rfid-read:read');
/* [2] Check for user in auth list /* [2] Check for user in auth list
=========================================================*/ =========================================================*/
/* (1) Check user for this code */ /* (1) Check user for this code */
$user = auth($code); $user = auth($code);
/* (2) If not found -> reset @last_user / @timeout + abort */ /* (2) If not found -> reset @last_user / @timeout + abort */
if( is_null($user) ){ if( is_null($user) ){
slog("Unknown user (not authenticated)", "rfid-read:loop"); slog("Unknown user", "rfid-read:loop");
$last_user = null; $last_user = null;
$timeout = 0; $timeout = 0;
return false; return false;
@ -403,13 +390,10 @@
/* [3] Manage @timeout incrementation + @last_user /* [3] Manage @timeout incrementation + @last_user
=========================================================*/ =========================================================*/
/* (1) If same as last -> increment @timeout */ /* (1) If new user or different one -> Set timeout to current time */
if( $last_user == $user['id'] ) if( $last_user != $user['id'] )
$timeout++; $timeout = microtime(true);
/* (2) If different -> reset @timeout to 0 */
else
$timeout = 0;
/* [4] Manage action /* [4] Manage action
@ -469,10 +453,15 @@
=========================================================*/ =========================================================*/
while( true ){ while( true ){
$start_ts = microtime(true); /* (1) Store time */
$loop_ts = microtime(true);
/* (2) Execute loop code */
mfrc522_loop(); mfrc522_loop();
/* (3) Wait for @LOOP_FREQ seconds */
while( microtime(true) - $loop_ts < ($LOOP_FREQ/1000) );
} }
?> ?>