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.

907 lines
29KB

  1. <?php
  2. /*
  3. file: carpark.php 停車管理
  4. */
  5. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  6. require_once(MQ_CLASS_FILE);
  7. // ----- 定義常數(路徑, cache秒數) -----
  8. define('APP_VERSION', '100'); // 版本號
  9. define('MAX_AGE', 604800); // cache秒數, 此定義1個月
  10. define('APP_NAME', 'carpark'); // 應用系統名稱
  11. define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views
  12. //define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL
  13. define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost') . ($_SERVER['SERVER_PORT'] != 60123 ? ':' . $_SERVER['SERVER_PORT'] : '') .'/'); // URL
  14. define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑
  15. define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑
  16. define('WEB_LIB', SERVER_URL.'libs/'); // 網頁lib
  17. define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib
  18. define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path
  19. class Carpark extends CI_Controller
  20. {
  21. var $vars = array(); // 共用變數
  22. function __construct()
  23. {
  24. parent::__construct();
  25. // ----- 程式開發階段log設定 -----
  26. if (@ENVIRONMENT == 'development')
  27. {
  28. ini_set('display_errors', '1');
  29. //error_reporting(E_ALL ^ E_NOTICE);
  30. error_reporting(E_ALL);
  31. }
  32. set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
  33. // 共用記憶體
  34. $this->vars['mcache'] = new Memcache;
  35. $this->vars['mcache']->connect(MEMCACHE_HOST, MEMCACHE_PORT) or die ('Could not connect memcache');
  36. /*
  37. // mqtt subscribe
  38. $this->vars['mqtt'] = new phpMQTT(MQ_HOST, MQ_PORT, uniqid());
  39. //if(!$this->vars['mqtt']->connect()){ die ('Could not connect mqtt'); }
  40. $this->vars['mqtt']->connect();
  41. */
  42. $this->load->model('carpark_model');
  43. $this->carpark_model->init($this->vars);
  44. // 資料介接模組
  45. $this->load->model('sync_data_model');
  46. $this->sync_data_model->init($this->vars); // for memcache
  47. // mqtt subscribe
  48. $station_setting = $this->sync_data_model->station_setting_query();
  49. $mqtt_ip = isset($station_setting['mqtt_ip']) ? $station_setting['mqtt_ip'] : MQ_HOST;
  50. $mqtt_port = isset($station_setting['mqtt_port']) ? $station_setting['mqtt_port'] : MQ_PORT;
  51. $this->vars['mqtt'] = new phpMQTT($mqtt_ip, $mqtt_port, uniqid());
  52. $this->vars['mqtt']->connect();
  53. // init again
  54. $this->sync_data_model->init($this->vars); // for mqtt
  55. // 產生 excel 報表
  56. $this->load->model('excel_model');
  57. $this->excel_model->init($this->vars);
  58. // [START] 2016/06/03 登入
  59. $this->load->model('user_model');
  60. // load library
  61. $this->load->library(array('form_validation','session'));
  62. // load helpers
  63. $this->load->helper(array('form'));
  64. // ajax code
  65. define('RESULT_SUCCESS', 'ok');
  66. define('RESULT_FORM_VALIDATION_FAIL', '-1');
  67. define('RESULE_FAIL', 'gg');
  68. // [START] 2016/06/03 登入
  69. }
  70. // 發生錯誤時集中在此處理
  71. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  72. {
  73. // ex: car_err://message....
  74. $log_msg = explode('://', $errstr);
  75. if (count($log_msg) > 1)
  76. {
  77. $log_file = LOG_PATH.$log_msg[0];
  78. $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
  79. }
  80. else
  81. {
  82. $log_file = LOG_PATH.APP_NAME;
  83. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  84. }
  85. //$str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  86. error_log($str, 3, $log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  87. }
  88. // 顯示靜態網頁(html檔)
  89. protected function show_page($page_name, &$data = null)
  90. {
  91. $page_file = PAGE_PATH.$page_name.'.php';
  92. $last_modified_time = filemtime($page_file);
  93. // 若檔案修改時間沒有異動, 或版本無異動, 通知瀏覽器使用cache, 不再下傳網頁
  94. // header('Cache-Control:max-age='.MAX_AGE); // cache 1個月
  95. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified_time).' GMT');
  96. header('Etag: '. APP_VERSION);
  97. header('Cache-Control: public');
  98. // 20170921
  99. header("cache-Control: no-store, no-cache, must-revalidate");
  100. header("cache-Control: post-check=0, pre-check=0", false);
  101. header("Pragma: no-cache");
  102. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  103. if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == APP_VERSION && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time)
  104. {
  105. header('HTTP/1.1 304 Not Modified');
  106. }
  107. else
  108. {
  109. $this->load->view(APP_NAME.'/'.$page_name, $data);
  110. }
  111. }
  112. // ------------------------------------------------
  113. //
  114. // 接收端 (START)
  115. //
  116. // ------------------------------------------------
  117. // [mqtt] 接收端
  118. public function mqtt_service()
  119. {
  120. $topic = $this->input->post('topic', true);
  121. $msg = $this->input->post('msg', true);
  122. $ck = $this->input->post('ck', true);
  123. if(md5($topic.'altob'.$msg) != $ck)
  124. {
  125. echo 'ck_error';
  126. exit;
  127. }
  128. trigger_error(__FUNCTION__ . "|{$topic}|{$msg}");
  129. echo 'ok';
  130. exit;
  131. }
  132. // [設定檔] 取得設定
  133. public function station_setting_query()
  134. {
  135. $reload = $this->input->post('reload', true);
  136. if(isset($reload) && $reload > 0)
  137. {
  138. $station_setting = $this->sync_data_model->station_setting_query(true); // 強制重新載入
  139. trigger_error(__FUNCTION__ . '..station_setting: '. print_r($station_setting, true));
  140. if(!$station_setting)
  141. {
  142. echo json_encode('fail', JSON_UNESCAPED_UNICODE);
  143. exit; // 中斷
  144. }
  145. $info = array('station_no_arr' => $station_setting['station_no']);
  146. usleep(300000); // 0.3 sec delay
  147. // 費率資料同步
  148. $result = $this->sync_data_model->sync_price_plan($info);
  149. trigger_error(__FUNCTION__ . '..sync_price_plan: '. $result);
  150. usleep(300000); // 0.3 sec delay
  151. // 會員資料同步
  152. $result = $this->sync_data_model->sync_members($info);
  153. trigger_error(__FUNCTION__ . '..sync_members: '. $result);
  154. usleep(300000); // 0.3 sec delay
  155. // 在席資料同步
  156. $result = $this->sync_data_model->sync_pks_groups_reload($station_setting);
  157. trigger_error(__FUNCTION__ . '..sync_pks_groups_reload: '. $result);
  158. }
  159. else
  160. {
  161. $station_setting = $this->sync_data_model->station_setting_query(false);
  162. if(!$station_setting)
  163. {
  164. echo json_encode('fail', JSON_UNESCAPED_UNICODE);
  165. exit; // 中斷
  166. }
  167. }
  168. echo json_encode($station_setting, JSON_UNESCAPED_UNICODE);
  169. }
  170. // [排程 or 強制] 同步場站資訊
  171. public function sync_station_data()
  172. {
  173. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  174. $switch_lpr_arr = array(); // 換車牌
  175. $meta = $this->input->post('meta', true);
  176. if(!empty($meta))
  177. {
  178. trigger_error( __FUNCTION__ . '..meta_arr..' . $meta);
  179. $meta_arr = explode('@@@', $meta);
  180. foreach($meta_arr as $raw)
  181. {
  182. if(empty($raw))
  183. continue;
  184. $data = json_decode($raw, true);
  185. if($data['key'] == 'switch_lpr')
  186. {
  187. array_push($switch_lpr_arr, $data['value']);
  188. }
  189. }
  190. }
  191. // 0. 取得場站設定
  192. $station_setting = $this->sync_data_model->station_setting_query(false);
  193. trigger_error(__FUNCTION__ . '..station_setting: '. print_r($station_setting, true));
  194. $station_no_arr = array('station_no_arr' => $station_setting['station_no']);
  195. // 1. 月租系統
  196. $result = $this->sync_data_model->sync_members($station_no_arr);
  197. trigger_error(__FUNCTION__ . '..sync_members: '. $result);
  198. // 2. 同步車牌更換
  199. if(!empty($switch_lpr_arr))
  200. {
  201. $this->sync_data_model->sync_switch_lpr($switch_lpr_arr);
  202. }
  203. }
  204. // [API] 取得最新未結清
  205. public function get_last_unbalanced_cario()
  206. {
  207. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  208. $lpr = $this->input->post('lpr', true);
  209. $station_no = $this->input->post('station_no', true);
  210. $c_s = $this->input->post('c_s', true);
  211. // 確認正確性
  212. if(empty($c_s) || $c_s != md5('altob'.$lpr.'botla'.$station_no))
  213. {
  214. trigger_error(__FUNCTION__ . "..{$lpr}|".$station_no."|{$c_s}..check fail..");
  215. echo 'fail';
  216. exit;
  217. }
  218. $data = $this->sync_data_model->get_last_unbalanced_cario($lpr, $station_no);
  219. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  220. }
  221. // [API] 更新未結清 (行動支付)
  222. public function sync_m2payed()
  223. {
  224. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  225. $lpr = $this->input->post('lpr', true);
  226. $amt = $this->input->post('amt', true);
  227. $station_no = $this->input->post('station_no', true);
  228. $cario_no = $this->input->post('cario_no', true);
  229. $c_s = $this->input->post('c_s', true);
  230. // 確認正確性
  231. if(empty($c_s) || $c_s != md5($lpr.$amt.'altob'.$station_no.'botla'.$cario_no))
  232. {
  233. trigger_error(__FUNCTION__ . "..{$lpr}|{$amt}|{$station_no}|{$cario_no}|{$c_s}..check fail..");
  234. echo 'fail';
  235. exit;
  236. }
  237. // 臨停繳費
  238. $this->load->model('carpayment_model');
  239. echo $this->carpayment_model->p2payed(array('seqno' => $cario_no, 'lpr' => $lpr, 'amt' => $amt), true);
  240. exit;
  241. //echo 'ok';
  242. }
  243. // 驗証 IP
  244. function is_ip_valid()
  245. {
  246. $client_ip = $this->my_ip();
  247. if(!in_array($client_ip, array('61.219.172.11', '61.219.172.82')))
  248. {
  249. trigger_error('..block..from:'.$client_ip.'..unknown network..');
  250. return false;
  251. }
  252. return true;
  253. }
  254. // 取得 IP
  255. function my_ip()
  256. {
  257. if (getenv('HTTP_X_FORWARDED_FOR'))
  258. {
  259. $ip = getenv('HTTP_X_FORWARDED_FOR');
  260. }
  261. elseif (getenv('HTTP_X_REAL_IP'))
  262. {
  263. $ip = getenv('HTTP_X_REAL_IP');
  264. }
  265. else {
  266. $ip = $_SERVER['REMOTE_ADDR'];
  267. }
  268. return $ip;
  269. }
  270. // 同步 (由排程呼叫)
  271. public function sync_minutely()
  272. {
  273. $this->sync_data_model->sync_pks_groups(); // 同步在席現況
  274. }
  275. // 20170816 手動新增入場資料
  276. public function gen_carin()
  277. {
  278. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  279. $lpr = $this->input->post('lpr', true);
  280. $station_no = $this->input->post('station_no', true);
  281. $c_s = $this->input->post('c_s', true);
  282. // 確認正確性
  283. if(empty($c_s) || $c_s != md5($lpr.'altob'.$station_no.'botla'. __FUNCTION__ ))
  284. {
  285. trigger_error(__FUNCTION__ . "..{$lpr}|{$station_no}|{$c_s}..check fail..");
  286. echo 'fail';
  287. exit;
  288. }
  289. $parms = array(
  290. 'sno' => $station_no,
  291. 'lpr' => $lpr,
  292. 'etag' => 'NONE',
  293. 'io' => 'CI',
  294. 'ivsno' => 0
  295. );
  296. $this->carpark_model->gen_carin($parms);
  297. echo 'ok';
  298. }
  299. // ------------------------------------------------
  300. //
  301. // 接收端 (END)
  302. //
  303. // ------------------------------------------------
  304. // [START] 2016/06/03 登入
  305. public function index()
  306. {
  307. if($this->session->userdata('logged_in'))
  308. {
  309. $session_data = $this->session->userdata('logged_in');
  310. $data['username'] = $session_data['username'];
  311. $data['type'] = $session_data['type'];
  312. if($data['type'] == 'admin')
  313. {
  314. $this->show_page('admin_page', $data); // 進階管理者介面
  315. }
  316. else
  317. {
  318. $this->show_page('main_page', $data); // 一般
  319. }
  320. }
  321. else
  322. {
  323. //If no session, redirect to login page
  324. //redirect('login', 'refresh');
  325. $this->show_page('login_page');
  326. }
  327. }
  328. // 登入
  329. public function user_login()
  330. {
  331. // form_validation
  332. $this->form_validation->set_rules('login_name', 'login_name', 'trim|required');
  333. $this->form_validation->set_rules('pswd', 'pswd', 'trim|required');
  334. if($this->form_validation->run() == FALSE)
  335. {
  336. return RESULT_FORM_VALIDATION_FAIL;
  337. }
  338. // go model
  339. $data = array
  340. (
  341. 'login_name' => $this->input->post('login_name', true),
  342. 'pswd' => $this->input->post('pswd', true)
  343. );
  344. $result = $this->user_model->user_login($data);
  345. if($result)
  346. {
  347. $sess_array = array();
  348. foreach($result as $row)
  349. {
  350. $sess_array = array
  351. (
  352. 'username' => $row->login_name ,
  353. 'type' => $row->user_type
  354. );
  355. $this->session->set_userdata('logged_in', $sess_array);
  356. }
  357. echo RESULT_SUCCESS;
  358. }
  359. else
  360. {
  361. return RESULE_FAIL;
  362. }
  363. }
  364. // 登出
  365. public function user_logout()
  366. {
  367. $this->session->unset_userdata('logged_in');
  368. session_destroy();
  369. return RESULT_SUCCESS;
  370. }
  371. // [END] 2016/06/03 登入
  372. // response http
  373. protected function http_return($return_code, $type)
  374. {
  375. if ($type == 'text') echo $return_code;
  376. else echo json_encode($return_code, JSON_UNESCAPED_UNICODE);
  377. }
  378. // 送出html code
  379. public function get_html()
  380. {
  381. /*
  382. $data = array
  383. (
  384. 'company_no' => $this->input->post('company_no', true), // 場站統編
  385. 'hq_company_no' => '80682490'
  386. );
  387. $this->load->view(APP_NAME.'/'.$this->input->post('tag_name', true), $data);
  388. */
  389. $this->load->view(APP_NAME.'/'.$this->input->post('tag_name', true), array());
  390. }
  391. // 讀取cookie內容
  392. protected function get_cookie($cookie_name)
  393. {
  394. if (empty($_COOKIE[$cookie_name])) return array();
  395. return(json_decode($_COOKIE[$cookie_name], true));
  396. }
  397. // 儲存cookie內容
  398. protected function save_cookie($cookie_name, $cookie_info)
  399. {
  400. return setcookie($cookie_name, json_encode($cookie_info, JSON_UNESCAPED_UNICODE), 0, '/');
  401. }
  402. // 月租資料同步
  403. public function rent_sync()
  404. {
  405. $station_no = $this->input->post('station_no', true);
  406. $start_date = $this->input->post('start_date', true);
  407. $end_date = $this->input->post('end_date', true);
  408. // $data = $this->carpark_model->rent_sync($station_no, $start_date, $end_date);
  409. // print_r($data);
  410. }
  411. // 重設剩餘車位數
  412. public function available_set()
  413. {
  414. $data = $this->carpark_model->available_set();
  415. $this->http_return($data, 'json');
  416. }
  417. // 剩餘車位數更新
  418. public function available_update()
  419. {
  420. $station_no = $this->input->get_post('station_no', true);
  421. $data['name'] = $this->input->get_post('st_name', true);
  422. $data['tot_pkg'] = $this->input->get_post('tot_pkg', true);
  423. $data['ava_pkg'] = $this->input->get_post('ava_pkg', true);
  424. $this->http_return($this->carpark_model->available_update($station_no, $data), 'json');
  425. }
  426. // 剩餘車位數查核
  427. public function available_check()
  428. {
  429. $time_point = $this->input->get_post('time_point', true);
  430. $this->http_return($this->carpark_model->available_check($time_point), 'json');
  431. }
  432. // 顯示logs
  433. public function show_logs()
  434. {
  435. $lines = $this->uri->segment(3); // 顯示行數
  436. if (empty($lines)) $lines = 140; // 無行數參數, 預設為40行
  437. if($lines > 1000) $lines = 1000; // 最多 1000行
  438. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  439. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  440. passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 利用linux指令顯示倒數幾行的logs內容
  441. echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';
  442. echo '</pre></body></html>';
  443. }
  444. // 顯示logs (cars grep 888)
  445. public function show_888_logs()
  446. {
  447. $lines = $this->uri->segment(3); // 顯示行數
  448. if (empty($lines)) $lines = 1000; // 無行數參數, 預設為1000行
  449. if($lines > 20000) $lines = 20000; // 最多 20000行
  450. $grep_str = ' |grep 888';
  451. $target_str = LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'. $grep_str;
  452. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  453. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  454. passthru('/usr/bin/tail -n ' . $lines . ' ' . $target_str); // 利用linux指令顯示倒數幾行的logs內容
  455. echo "\n----- " . $target_str . ' -----';
  456. echo '</pre></body></html>';
  457. }
  458. // 新增月租資料
  459. public function member_add()
  460. {
  461. $data = array
  462. (
  463. 'member_no' => $this->input->post('member_no', true),
  464. 'station_no' => $this->input->post('station_no', true),
  465. 'lpr' => strtoupper($this->input->post('lpr', true)),
  466. 'old_lpr' => strtoupper($this->input->post('old_lpr', true)),
  467. 'etag' => strtoupper($this->input->post('etag', true)),
  468. 'start_date' => $this->input->post('start_date', true),
  469. 'end_date' => $this->input->post('end_date', true),
  470. 'member_name' => $this->input->post('member_name', true),
  471. 'member_nick_name' => $this->input->post('member_name', true),
  472. 'mobile_no' => $this->input->post('mobile_no', true),
  473. 'member_id' => $this->input->post('member_id', true),
  474. 'contract_no' => $this->input->post('contract_no', true),
  475. 'amt' => $this->input->post('amt', true),
  476. 'tel_h' => $this->input->post('tel_h', true),
  477. 'tel_o' => $this->input->post('tel_o', true),
  478. 'addr' => $this->input->post('addr', true)
  479. );
  480. trigger_error("add:".print_r($data, true));
  481. if ($data['member_no'] == 0 || $data['old_lpr'] != $data['lpr'])
  482. {
  483. if ($this->carpark_model->check_lpr($data['lpr']) > 0)
  484. {
  485. echo '車牌重複, 請查明再輸入';
  486. exit;
  487. }
  488. }
  489. $this->carpark_model->member_add($data);
  490. echo 'ok';
  491. }
  492. // 查詢月租資料
  493. public function member_query()
  494. {
  495. $data = $this->carpark_model->member_query();
  496. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  497. }
  498. // 刪除月租資料
  499. public function member_delete()
  500. {
  501. $member_no = $this->input->post('member_no', true);
  502. $this->carpark_model->member_delete($member_no);
  503. echo 'ok';
  504. }
  505. // 進出場現況表
  506. public function cario_list()
  507. {
  508. $data = $this->carpark_model->cario_list();
  509. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  510. }
  511. // 進出場事件即時顯示
  512. public function cario_event()
  513. {
  514. set_time_limit(0);
  515. while(true)
  516. {
  517. }
  518. }
  519. // 顯示圖檔(http://url/carpark.html/pics/lpr_ABY8873_O_0_0_C_20150919210022)
  520. public function pics()
  521. {
  522. // ???
  523. readfile(CAR_PIC.$this->uri->segment(3).'/'.str_replace('/', '', $this->uri->segment(4)).'.jpg');
  524. }
  525. // 汽車開門
  526. public function opendoors()
  527. {
  528. $ivsno = $this->uri->segment(3);
  529. $lanes = array
  530. (
  531. 0 => array ('devno' => 0, 'temp' => 0, 'member' => 1), // 1號入口, temp:臨停, member:月租
  532. // 1 => array ('devno' => 0, 'temp' => 2, 'member' => 3), // 2號調撥入口
  533. 1 => array ('devno' => 1, 'temp' => 0, 'member' => 1), // 3號調撥出口
  534. 2 => array ('devno' => 1, 'temp' => 0, 'member' => 1), // 3號調撥出口
  535. 3 => array ('devno' => 1, 'temp' => 2, 'member' => 3) // 4號出口
  536. );
  537. $url = 'http://admin:99999999@192.168.10.53/cgi-bin/basic_setting.cgi?ID=1&';
  538. $member_tag = 'member'; // member:月租會員
  539. // 短路開柵欄
  540. @get_headers("{$url}OUTDEV={$lanes[$ivsno]['devno']}&OUTCH={$lanes[$ivsno][$member_tag]}&OUTSTATUS=1");
  541. usleep(400000); // 暫停0.4秒
  542. // 斷路, 車過關柵欄
  543. @get_headers("{$url}OUTDEV={$lanes[$ivsno]['devno']}&OUTCH={$lanes[$ivsno][$member_tag]}&OUTSTATUS=0");
  544. }
  545. // 調撥車道查詢
  546. public function reversible_lane_query()
  547. {
  548. $max_lane = 4; // 共幾個車道數
  549. for ($idx = 0; $idx < $max_lane; ++$idx)
  550. {
  551. $data[$idx] = file_get_contents("http://192.168.10.51:8090/cgi-bin/switcher.cgi?id={$idx}&action=9");
  552. }
  553. // $data = array(1, 1, 0, 1);
  554. echo json_encode($data);
  555. }
  556. // 車號入場查詢
  557. public function carin_lpr_query()
  558. {
  559. $lpr = $this->uri->segment(3);
  560. $data = $this->carpark_model->carin_lpr_query($lpr);
  561. echo json_encode($data);
  562. }
  563. // 以時間查詢入場資訊
  564. public function carin_time_query()
  565. {
  566. $time_query = $this->input->post('time_query', true);
  567. $minutes_range = $this->input->post('minutes_range', true);
  568. $data = $this->carpark_model->carin_time_query($time_query, $minutes_range);
  569. echo json_encode($data);
  570. }
  571. // 調撥車道設定
  572. public function reversible_lane_set()
  573. {
  574. $lane_no = $this->input->post('lane_no', true);
  575. $actions = $this->input->post('actions', true);
  576. $data = file_get_contents("http://192.168.10.51:8090/cgi-bin/switcher.cgi?id={$lane_no}&action={$actions}");
  577. echo "{$lane_no}|{$actions}|{$data}";
  578. }
  579. // 在席車位檢查未有入場資料清單
  580. public function pks_check_list()
  581. {
  582. $max_rows = $this->uri->segment(3, 100); // 一次讀取筆數, 預設為100筆
  583. $data = $this->carpark_model->pks_check_list($max_rows);
  584. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  585. }
  586. // 重設在席查核
  587. public function reset_pks_check()
  588. {
  589. $data = $this->carpark_model->reset_pks_check();
  590. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  591. }
  592. // 更正在席車號
  593. public function correct_pks_lpr()
  594. {
  595. $pksno = $this->uri->segment(3, 0); // 車格號碼
  596. $lpr = $this->uri->segment(4, 'NONE'); // 車號
  597. if ($pksno == 0 || $lpr == 'NONE')
  598. {
  599. echo json_encode(array('err' => 1, 'cario_no' => 0), JSON_UNESCAPED_UNICODE);
  600. }
  601. else
  602. {
  603. $data = $this->carpark_model->correct_pks_lpr($pksno, $lpr);
  604. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  605. }
  606. }
  607. // 入場車號查核在席無資料清單
  608. public function carin_check_list()
  609. {
  610. $max_rows = $this->uri->segment(3, 20); // 一次讀取筆數, 預設為100筆
  611. $data = $this->carpark_model->carin_check_list($max_rows);
  612. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  613. }
  614. // 更正入場車號
  615. public function correct_carin_lpr()
  616. {
  617. $cario_no = $this->uri->segment(3, 0); // 車格號碼
  618. $lpr = $this->uri->segment(4, 'NONE'); // 車號
  619. $in_time = urldecode($this->uri->segment(5, '')); // 入場時間
  620. $data = $this->carpark_model->correct_carin_lpr($cario_no, $lpr, $in_time);
  621. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  622. }
  623. // 查詢行動支付記錄
  624. public function tx_bill_query()
  625. {
  626. $data = $this->carpark_model->tx_bill_query();
  627. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  628. }
  629. // 查詢月租繳款機記錄
  630. public function tx_bill_ats_query()
  631. {
  632. $data = $this->carpark_model->tx_bill_ats_query();
  633. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  634. }
  635. // 查詢樓層在席群組
  636. public function pks_group_query()
  637. {
  638. $data = $this->carpark_model->pks_group_query();
  639. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  640. }
  641. // 微調剩餘車位數
  642. public function pks_availables_update()
  643. {
  644. $group_id = $this->uri->segment(3); // id
  645. $value = $this->uri->segment(4, 0); // value
  646. $station_no = $this->uri->segment(5); // station_no
  647. $data = $this->sync_data_model->pks_availables_update($group_id, $value, true, $station_no);
  648. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  649. }
  650. // 進出場觸發 888 呼叫
  651. /*
  652. public function sync_888()
  653. {
  654. $io = $this->uri->segment(3); // CI, CO, MI, MO
  655. if(empty($io))
  656. {
  657. echo 'io_not_found';
  658. exit;
  659. }
  660. $parms = array('io' => $io, 'etag' => __FUNCTION__, 'lpr' => __FUNCTION__);
  661. echo $this->sync_data_model->sync_888($parms);
  662. exit;
  663. }
  664. */
  665. public function test()
  666. {
  667. //echo `whoami`;
  668. //echo phpinfo();
  669. }
  670. // 博辰測試用
  671. // http://localhost/carpark.html/test_8068_002/APK7310
  672. public function test_8068_002()
  673. {
  674. $lpr_in = $this->uri->segment(3);
  675. $seqno = '00001';
  676. $cmd = '002';
  677. $token = '000000000';
  678. $lpr = str_pad($lpr_in, 7, '%', STR_PAD_LEFT);
  679. $in_time = '2000/01/01 00:00:00';
  680. $error_str = '';
  681. $service_port = 8068;
  682. $address = "192.168.10.201";
  683. /* Create a TCP/IP socket. */
  684. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  685. if ($socket === false) {
  686. echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  687. $error_str .= "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  688. } else {
  689. echo "OK.\n";
  690. }
  691. echo "Attempting to connect to '{$address}' on port '{$service_port}'...";
  692. $result = socket_connect($socket, $address, $service_port);
  693. if ($result === false) {
  694. echo "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($socket)) . "\n";
  695. $error_str .= "socket_connect() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  696. } else {
  697. echo "OK.\n";
  698. }
  699. $in = pack('Ca5Ca3Ca9Ca7Ca19', 0x1c, $seqno, 0x1c, $cmd, 0x1c,
  700. $token, 0x1f, $lpr, 0x1f, $in_time
  701. );
  702. $out = '';
  703. echo "socket_write:";
  704. echo "{$in}<br/><br/>";
  705. if(!socket_write($socket, $in, strlen($in)))
  706. {
  707. echo('<p>Write failed</p>');
  708. $error_str .= "socket_write() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  709. }
  710. echo "socket_write..OK..";
  711. echo "<br/><br/>socket_read:";
  712. $out = socket_read($socket, 2048) or die("Could not read server responsen");
  713. //while ($out = socket_read($socket, 2048)) {
  714. // echo $out;
  715. //}
  716. echo "{$out}<br/><br/>";
  717. echo "Closing socket...";
  718. socket_close($socket);
  719. echo "OK.\n\n";
  720. trigger_error($error_str);
  721. exit;
  722. }
  723. // test
  724. public function test_phpexcel()
  725. {
  726. $query_year = 2017;
  727. $query_month = 3;
  728. //$this->excel_model->export_cario_data($query_year, $query_month);
  729. $this->excel_model->export_members();
  730. }
  731. }