VM暫存
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.

Cars.php 9.2KB

8 år sedan
8 år sedan
8 år sedan
8 år sedan
8 år sedan
8 år sedan
8 år sedan
8 år sedan
8 år sedan
8 år sedan
8 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. file: cars.php 車輛進出場處理(板車版含感應線圈)
  4. URL:
  5. http://192.168.10.201/cars.html/ipcam/sno/12119/ivsno/0/io/O/type/C/lpr/4750YC/color/NULL/sq/0/ts/1441051995/sq2/0/etag/ABCD123456789/ant/1
  6. javascript/html從server讀取key/value方式
  7. http://61.220.179.128/jsid.i/ip=ip&uniqid=clientid&mqtt_ip=mqtt_ip
  8. 例如:
  9. <script src="http://61.220.179.128/jsid.i/ip=ip&uniqid=clientid&mqtt_ip=mqtt_ip"></script>
  10. response:
  11. var ip='66.249.82.183';var clientid='565162cb67dfb';var mqtt_ip='192.168.51.11';
  12. */
  13. // ----- 定義常數(路徑, cache秒數) -----
  14. define('APP_VERSION', '100'); // 版本號
  15. define('MAX_AGE', 604800); // cache秒數, 此定義1個月
  16. define('APP_NAME', 'cars'); // 應用系統名稱
  17. define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views
  18. define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL
  19. define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑
  20. define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑
  21. define('WEB_LIB', SERVER_URL.'/libs/'); // 網頁lib
  22. define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib
  23. define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path name
  24. define('LOG_FILE', FILE_BASE.APP_NAME.'/logs/cario.'); // log file name
  25. require_once(MQ_CLASS_FILE);
  26. class Cars extends CI_Controller
  27. {
  28. var $vars = array();
  29. function __construct()
  30. {
  31. // $this->time_start = microtime(true);
  32. parent::__construct();
  33. ignore_user_abort(); // 接受client斷線, 繼續run
  34. $method_name = $this->router->fetch_method();
  35. if ($method_name == 'ipcam' || $method_name == 'check_lpr_etag')
  36. {
  37. ob_end_clean();
  38. ignore_user_abort();
  39. ob_start();
  40. echo 'ok';
  41. header('Connection: close');
  42. header('Content-Length: ' . ob_get_length());
  43. ob_end_flush();
  44. flush();
  45. }
  46. else if($method_name == 'opendoor')
  47. {
  48. ob_end_clean();
  49. ignore_user_abort();
  50. ob_start();
  51. echo 'ok';
  52. header('Connection: close');
  53. header('Content-Length: ' . ob_get_length());
  54. ob_end_flush();
  55. flush();
  56. }
  57. $this->vars['date_time'] = date('Y-m-d H:i:s'); // 格式化時間(2015-10-12 14:36:21)
  58. $this->vars['time_num'] = str_replace(array('-', ':', ' '), '', $this->vars['date_time']); //數字化時間(20151012143621)
  59. $this->vars['date_num'] = substr($this->vars['time_num'], 0, 8); // 數字化日期(20151012)
  60. //$this->vars['station_no'] = STATION_NO; // 本站編號
  61. session_id(ip2long($_SERVER['REMOTE_ADDR'])); // 設定同一device為同一個session
  62. session_start();
  63. // ----- 程式開發階段log設定 -----
  64. if (@ENVIRONMENT == 'development')
  65. {
  66. ini_set('display_errors', '1');
  67. //error_reporting(E_ALL ^ E_NOTICE);
  68. error_reporting(E_ALL);
  69. }
  70. set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
  71. // 共用記憶體
  72. $this->vars['mcache'] = new Memcache;
  73. $this->vars['mcache']->pconnect(MEMCACHE_HOST, MEMCACHE_PORT); // or die ('Could not connect memcache');
  74. /*
  75. // mqtt subscribe
  76. $this->vars['mqtt'] = new phpMQTT(MQ_HOST, MQ_PORT, uniqid());
  77. //if(!$this->vars['mqtt']->connect()){ die ('Could not connect mqtt'); }
  78. if(!$this->vars['mqtt']->connect()){ trigger_error('..Could not connect mqtt..go on..'); }
  79. */
  80. // 資料介接模組
  81. $this->load->model('sync_data_model');
  82. $this->sync_data_model->init($this->vars); // for memcache
  83. // mqtt subscribe
  84. $station_setting = $this->sync_data_model->station_setting_query();
  85. $mqtt_ip = isset($station_setting['mqtt_ip']) ? $station_setting['mqtt_ip'] : MQ_HOST;
  86. $mqtt_port = isset($station_setting['mqtt_port']) ? $station_setting['mqtt_port'] : MQ_PORT;
  87. $this->vars['mqtt'] = new phpMQTT($mqtt_ip, $mqtt_port, uniqid());
  88. $this->vars['mqtt']->connect();
  89. // init again
  90. $this->sync_data_model->init($this->vars); // for mqtt
  91. $this->load->model('cars_model');
  92. $this->cars_model->init($this->vars);
  93. }
  94. // 發生錯誤時集中在此處理
  95. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  96. {
  97. $log_msg = explode('://', $errstr);
  98. if (count($log_msg) > 1)
  99. {
  100. $log_file = $log_msg[0];
  101. $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
  102. }
  103. else
  104. {
  105. $log_file = APP_NAME;
  106. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  107. }
  108. error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  109. }
  110. // 顯示logs
  111. public function show_logs()
  112. {
  113. $lines = $this->uri->segment(3); // 顯示行數
  114. if (empty($lines)) $lines = 140; // 無行數參數, 預設為40行
  115. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  116. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  117. passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 利用linux指令顯示倒數幾行的logs內容
  118. echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';
  119. echo '</pre></body></html>';
  120. }
  121. /*
  122. 出入口
  123. 說明: 與ipcam相同判斷邏輯, 但不做任何資料更改
  124. */
  125. public function opendoor()
  126. {
  127. $parms = $this->uri->uri_to_assoc(3);
  128. $parms['lpr'] = urldecode($parms['lpr']); // 中文車牌
  129. $this->cars_model->opendoor_lprio($parms);
  130. }
  131. // IVS -> 車號, 影像
  132. /*
  133. 鼎高IVS傳送車號及影像檔
  134. http://192.168.10.201/cars.html/ipcam/sno/12119/ivsno/0/io/O/type/C/lpr/4750YC/color/NULL/sq/0/ts/1441051995/sq2/0/etag/ABCD123456789/ant/1
  135. sno: 場站編號(光興國小:12119)
  136. ivsno: ivs編號, 每一支都是獨立編號(序號)
  137. io: i:進場, o:出場
  138. type: C:汽車, H:重機, M:機車
  139. lpr: ABC-1234(車號)
  140. color: red(紅色), 若無請用NULL(4個字)
  141. sq: 序號(參考用)
  142. sq2: 暫不用
  143. etag: eTag ID
  144. ant: eTag
  145. http設定說明:
  146. method: POST
  147. 上傳圖檔名英數字, 副檔名為gif/jpg/png均可
  148. 上傳圖檔欄位名稱為cars
  149. */
  150. public function ipcam()
  151. {
  152. $parms = $this->uri->uri_to_assoc(3);
  153. $parms['lpr'] = urldecode($parms['lpr']); // 中文車牌
  154. // 同步並送出一次出入口 888
  155. $this->sync_data_model->sync_888($parms);
  156. $pic_folder = CAR_PIC.$this->vars['date_num'].'/'; // 今日資料夾名(yyyymmdd)
  157. if (!file_exists($pic_folder)) mkdir($pic_folder); // 如果資料夾不存在, 建立日期資料夾
  158. $config['upload_path'] = $pic_folder;
  159. // $config['allowed_types'] = 'gif|jpg|png';
  160. $config['allowed_types'] = '*';
  161. // ex. lpr_1625AB_I_1_152_C_1_2015080526.jpg -> car_交易序號_進出_順序_車號_時間.jpg
  162. $config['file_name'] = "lpr-{$parms['lpr']}-{$parms['io']}-{$parms['ivsno']}-{$parms['sq']}-{$parms['type']}-{$parms['sq2']}-{$this->vars['time_num']}.jpg";
  163. if (!isset($_FILES['cars']))
  164. {
  165. $status = 'error'; // 顯示上傳錯誤
  166. trigger_error('[ERROR] cars not found: ' . print_r($_FILES, true));
  167. }
  168. else
  169. {
  170. $this->load->library('upload', $config);
  171. if(!$this->upload->do_upload('cars')){
  172. $status = 'error'; // 顯示上傳錯誤
  173. trigger_error($this->upload->display_errors());
  174. }
  175. else
  176. {
  177. // 若無錯誤,則上傳檔案
  178. $file = $this->upload->data('cars');
  179. $status = 'ok';
  180. }
  181. }
  182. $parms['obj_type'] = 1; // 車牌類
  183. $parms['curr_time_str'] = $this->vars['date_time']; // 現在時間, 例2015-09-21 15:36:47
  184. $parms['pic_name'] = $config['file_name']; // 圖片檔名
  185. $this->cars_model->lprio($parms); // 測試eTag
  186. }
  187. // 用車牌與eTag, 檢查資料庫
  188. public function check_lpr_etag()
  189. {
  190. $lpr = $this->uri->segment(3);
  191. $etag = $this->uri->segment(4);
  192. $this->cars_model->check_lpr_etag($lpr, $etag);
  193. exit;
  194. }
  195. public function test_now()
  196. {
  197. echo date('Y-m-d H:i:s');
  198. }
  199. public function test_phpinfo()
  200. {
  201. phpinfo();
  202. }
  203. }