The original PHP version of the Model Railroad RFID interface written by Richard Cagle in 2017 for the KCS 3rd Sub
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

false_read.php 3.4KB

  1. <?php
  2. ini_set('display_errors', 1);
  3. error_reporting(-1);
  4. define('RFID_ROOT', __DIR__);
  5. define("DS", DIRECTORY_SEPARATOR); // Portability
  6. require RFID_ROOT . DS . "vendor" . DS . "autoload.php";
  7. /**
  8. * DB Connection Class
  9. */
  10. class DB
  11. {
  12. public static $db = null;
  13. function __construct(){
  14. $connection_config = array(
  15. 'driver' => 'mysql',
  16. 'host' => 'localhost',
  17. 'database' => 'rfid',
  18. 'username' => 'root',
  19. 'password' => 'root'
  20. );
  21. $connection = new \Pixie\Connection('mysql', $connection_config);
  22. self::$db = new \Pixie\QueryBuilder\QueryBuilderHandler($connection);
  23. self::$db->raw("SET time_zone = 'America/Chicago'"); // Set our timezone from the config
  24. return self::$db;
  25. }
  26. public static function getDB(){
  27. return self::$db;
  28. }
  29. }
  30. $DBCLASS = new DB();
  31. $db = $DBCLASS::getDB();
  32. if(!$db) die('Database Connection Failure');
  33. $config_data = $db->table('config')->where('profile_title', 'default')->first();
  34. function hasActiveSession($mac){
  35. global $db;
  36. $session = $db->table('sessions')->where('mac', $mac)->orderBy('id', 'DESC')->first();
  37. if(!$session) return false;
  38. if(!is_null($session->ended)) return false;
  39. return $session->id;
  40. }
  41. function save($mac, $rfid){
  42. global $db, $config_data;
  43. $reader = $db->table('readers')->where('mac', $mac)->first();
  44. // if(!$reader->record) return false;
  45. $current_time = date('Y-m-d H:i:s');
  46. $current_timestamp = time();
  47. $session_id = 0;
  48. $activeSession = hasActiveSession($mac);
  49. $last_read = $db->table('reads')->where('mac', $reader->mac)->where('session_id', $activeSession)->orderBy('date', 'DESC')->first();
  50. if($activeSession){
  51. // if(!isset($last_read->date) OR !$last_read->date){
  52. // $session = $db->table('sessions')->where('id', $activeSession)->first();
  53. // $last_read_date = $session->started;
  54. // } else {
  55. // $last_read_date = $last_read->date;
  56. // }
  57. // if(($current_timestamp - strtotime($last_read_date)) > $config_data->read_wait_time){
  58. // // Need to go ahead and end the session.
  59. // $db->table('sessions')->where("id", $activeSession)->update(array('ended' => $current_time));
  60. // $activeSession = 0;
  61. // } else {
  62. // // If there is an active session, we need to set the session id.
  63. // $session_id = $activeSession;
  64. // }
  65. $session_id = $activeSession;
  66. } else {
  67. if($reader->autoread == 1){
  68. // This is an autoreading reader that has no active session. So let's start one.
  69. $session_id = $db->table('sessions')->insert(array('mac' => $mac, 'started' => $current_time));
  70. }
  71. }
  72. $insert_array = array(
  73. 'mac' => $mac,
  74. 'rfid' => $rfid,
  75. 'date' => date('Y-m-d H:i:s'),
  76. 'session_id' => $session_id
  77. );
  78. $db->table('reads')->insert($insert_array);
  79. }
  80. if(isset($_GET['rfid'])){
  81. $rfid = $_GET['rfid'];
  82. } else {
  83. // Get a random RFID
  84. $allRFIDS = $db->table('cars')->where('rfid', '<>', '')->get();
  85. $result_count = count($allRFIDS);
  86. $key = rand(0, $result_count - 1);
  87. $rfid = $allRFIDS[$key]->rfid;
  88. }
  89. if(isset($_GET['mac'])){
  90. $mac = $_GET['mac'];
  91. } else {
  92. // Get a random reader
  93. $allReaders = $db->table('readers')->get();
  94. $result_count = count($allReaders);
  95. $key = rand(0, $result_count - 1);
  96. $mac = $allReaders[$key]->mac;
  97. }
  98. // Insert this bad boy, ya'll
  99. save($mac, $rfid);
  100. echo "done";
  101. ?>