VM暫存
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

Cars.php 10KB

8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
8 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. // ----- 程式開發階段log設定 -----
  34. if (@ENVIRONMENT == 'development')
  35. {
  36. ini_set('display_errors', '1');
  37. //error_reporting(E_ALL ^ E_NOTICE);
  38. error_reporting(E_ALL);
  39. }
  40. set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
  41. $method_name = $this->router->fetch_method();
  42. $request_assoc = $this->uri->uri_to_assoc(3);
  43. trigger_error(__FUNCTION__ . '..' . $method_name. '..request start..' . print_r($request_assoc, true));
  44. ignore_user_abort(); // 接受client斷線, 繼續run
  45. if(in_array($method_name, array(
  46. 'ipcam', 'ipcam_meta',
  47. 'check_lpr_etag',
  48. 'opendoor',
  49. 'temp_opendoors', 'member_opendoors'
  50. )))
  51. {
  52. ob_end_clean();
  53. ignore_user_abort();
  54. ob_start();
  55. echo 'ok';
  56. header('Connection: close');
  57. header('Content-Length: ' . ob_get_length());
  58. ob_end_flush();
  59. flush();
  60. }
  61. $this->vars['date_time'] = date('Y-m-d H:i:s'); // 格式化時間(2015-10-12 14:36:21)
  62. $this->vars['time_num'] = str_replace(array('-', ':', ' '), '', $this->vars['date_time']); //數字化時間(20151012143621)
  63. $this->vars['date_num'] = substr($this->vars['time_num'], 0, 8); // 數字化日期(20151012)
  64. //$this->vars['station_no'] = STATION_NO; // 本站編號
  65. //session_id(ip2long($_SERVER['REMOTE_ADDR'])); // 設定同一device為同一個session
  66. //session_start();
  67. // 共用記憶體
  68. $this->vars['mcache'] = new Memcache;
  69. $this->vars['mcache']->pconnect(MEMCACHE_HOST, MEMCACHE_PORT); // or die ('Could not connect memcache');
  70. /*
  71. // mqtt subscribe
  72. $this->vars['mqtt'] = new phpMQTT(MQ_HOST, MQ_PORT, uniqid());
  73. //if(!$this->vars['mqtt']->connect()){ die ('Could not connect mqtt'); }
  74. if(!$this->vars['mqtt']->connect()){ trigger_error('..Could not connect mqtt..go on..'); }
  75. */
  76. // 資料介接模組
  77. $this->load->model('sync_data_model');
  78. $this->sync_data_model->init($this->vars); // for memcache
  79. // mqtt subscribe
  80. $station_setting = $this->sync_data_model->station_setting_query();
  81. $mqtt_ip = isset($station_setting['mqtt_ip']) ? $station_setting['mqtt_ip'] : MQ_HOST;
  82. $mqtt_port = isset($station_setting['mqtt_port']) ? $station_setting['mqtt_port'] : MQ_PORT;
  83. $this->vars['mqtt'] = new phpMQTT($mqtt_ip, $mqtt_port, uniqid());
  84. $this->vars['mqtt']->connect();
  85. // init again
  86. $this->sync_data_model->init($this->vars); // for mqtt
  87. $this->load->model('cars_model');
  88. $this->cars_model->init($this->vars);
  89. }
  90. // 發生錯誤時集中在此處理
  91. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  92. {
  93. $log_msg = explode('://', $errstr);
  94. if (count($log_msg) > 1)
  95. {
  96. $log_file = $log_msg[0];
  97. $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
  98. }
  99. else
  100. {
  101. $log_file = APP_NAME;
  102. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  103. }
  104. error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  105. }
  106. // 顯示logs
  107. public function show_logs()
  108. {
  109. $lines = $this->uri->segment(3); // 顯示行數
  110. if (empty($lines)) $lines = 140; // 無行數參數, 預設為40行
  111. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  112. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  113. passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 利用linux指令顯示倒數幾行的logs內容
  114. echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';
  115. echo '</pre></body></html>';
  116. }
  117. /*
  118. 出入口
  119. 說明: 與ipcam相同判斷邏輯, 但不做任何資料更改
  120. */
  121. public function opendoor()
  122. {
  123. $parms = $this->uri->uri_to_assoc(3);
  124. $parms['lpr'] = urldecode($parms['lpr']); // 中文車牌
  125. $this->cars_model->opendoor_lprio($parms);
  126. }
  127. // IVS -> 車號, 影像
  128. /*
  129. 鼎高IVS傳送車號及影像檔
  130. 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
  131. sno: 場站編號(光興國小:12119)
  132. ivsno: ivs編號, 每一支都是獨立編號(序號)
  133. io: i:進場, o:出場
  134. type: C:汽車, H:重機, M:機車
  135. lpr: ABC-1234(車號)
  136. color: red(紅色), 若無請用NULL(4個字)
  137. sq: 序號(參考用)
  138. sq2: 暫不用
  139. etag: eTag ID
  140. ant: eTag
  141. http設定說明:
  142. method: POST
  143. 上傳圖檔名英數字, 副檔名為gif/jpg/png均可
  144. 上傳圖檔欄位名稱為cars
  145. */
  146. public function ipcam()
  147. {
  148. $parms = $this->uri->uri_to_assoc(3);
  149. $parms['lpr'] = urldecode($parms['lpr']); // 中文車牌
  150. // 同步並送出一次出入口 888
  151. $this->sync_data_model->sync_888($parms);
  152. $pic_folder = CAR_PIC.$this->vars['date_num'].'/'; // 今日資料夾名(yyyymmdd)
  153. if (!file_exists($pic_folder)) mkdir($pic_folder); // 如果資料夾不存在, 建立日期資料夾
  154. $config['upload_path'] = $pic_folder;
  155. // $config['allowed_types'] = 'gif|jpg|png';
  156. $config['allowed_types'] = '*';
  157. // ex. lpr_1625AB_I_1_152_C_1_2015080526.jpg -> car_交易序號_進出_順序_車號_時間.jpg
  158. $config['file_name'] = "lpr-{$parms['lpr']}-{$parms['io']}-{$parms['ivsno']}-{$parms['sq']}-{$parms['type']}-{$parms['sq2']}-{$this->vars['time_num']}.jpg";
  159. if (!isset($_FILES['cars']))
  160. {
  161. $status = 'error'; // 顯示上傳錯誤
  162. trigger_error('[ERROR] cars not found: ' . print_r($_FILES, true));
  163. }
  164. else
  165. {
  166. $this->load->library('upload', $config);
  167. if(!$this->upload->do_upload('cars')){
  168. $status = 'error'; // 顯示上傳錯誤
  169. trigger_error($this->upload->display_errors());
  170. }
  171. else
  172. {
  173. // 若無錯誤,則上傳檔案
  174. $file = $this->upload->data('cars');
  175. $status = 'ok';
  176. }
  177. }
  178. $parms['obj_type'] = 1; // 車牌類
  179. $parms['curr_time_str'] = $this->vars['date_time']; // 現在時間, 例2015-09-21 15:36:47
  180. $parms['pic_name'] = $config['file_name']; // 圖片檔名
  181. $this->cars_model->lprio($parms); // 測試eTag
  182. }
  183. // 用車牌與eTag, 檢查資料庫
  184. public function check_lpr_etag()
  185. {
  186. $lpr = $this->uri->segment(3);
  187. $etag = $this->uri->segment(4);
  188. $this->cars_model->check_lpr_etag($lpr, $etag);
  189. exit;
  190. }
  191. // 開門 (臨停)
  192. public function temp_opendoors()
  193. {
  194. $parms['ivsno'] = $this->uri->segment(3);
  195. $parms['lpr'] = $this->uri->segment(4);
  196. $parms['ck'] = $this->uri->segment(5);
  197. // 載入
  198. $this->load->model('cars_model');
  199. $this->cars_model->init($this->vars);
  200. $result = $this->cars_model->do_temp_opendoor($parms);
  201. trigger_error(__FUNCTION__ . "..{$result}.." . print_r($parms, true));
  202. exit;
  203. }
  204. // 開門 (臨停)
  205. public function member_opendoors()
  206. {
  207. $parms['ivsno'] = $this->uri->segment(3);
  208. $parms['lpr'] = $this->uri->segment(4);
  209. $parms['ck'] = $this->uri->segment(5);
  210. // 載入
  211. $this->load->model('cars_model');
  212. $this->cars_model->init($this->vars);
  213. $result = $this->cars_model->do_member_opendoor($parms);
  214. trigger_error(__FUNCTION__ . "..{$result}.." . print_r($parms, true));
  215. exit;
  216. }
  217. /*
  218. 出入口
  219. 說明: 特殊方式進出註記 (ex. 悠遊卡)
  220. */
  221. public function ipcam_meta()
  222. {
  223. $parms = $this->uri->uri_to_assoc(3);
  224. $parms['lpr'] = urldecode($parms['lpr']); // 中文車牌
  225. $this->cars_model->ipcam_meta($parms);
  226. }
  227. /*
  228. public function test_now()
  229. {
  230. echo date('Y-m-d H:i:s');
  231. }
  232. public function test_phpinfo()
  233. {
  234. phpinfo();
  235. }
  236. */
  237. }