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