VM暫存
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1044 line
32KB

  1. <?php
  2. /*
  3. file: carpark.php 停車管理
  4. */
  5. class Carpark extends CC_Controller
  6. {
  7. function __construct()
  8. {
  9. parent::__construct('carpark');
  10. // load library
  11. $this->load->library(array('form_validation','session'));
  12. // load helpers
  13. $this->load->helper(array('form'));
  14. // ajax code
  15. define('RESULT_SUCCESS', 'ok');
  16. define('RESULT_FORM_VALIDATION_FAIL', '-1');
  17. define('RESULE_FAIL', 'gg');
  18. }
  19. // ------------------------------------------------
  20. //
  21. // 報表
  22. //
  23. // ------------------------------------------------
  24. // 進出記錄表 (VIP)
  25. public function export_vip_cario_report()
  26. {
  27. // 次月算上月
  28. $last_day_of_previous_month = date("Y-n-j", strtotime("last day of previous month"));
  29. $d = date_parse_from_format("Y-m-d", $last_day_of_previous_month);
  30. $station_name = empty($this->input->post('station_name', true)) ? '場站名稱' : $this->input->post('station_name', true);
  31. $year = empty($this->input->post('year', true)) ? $d['year'] : $this->input->post('year', true);
  32. $month = empty($this->input->post('month', true)) ? $d['month'] : $this->input->post('month', true);
  33. $addr = empty($this->input->post('addr', true)) ? '地址' : $this->input->post('addr', true);
  34. $phone_no = empty($this->input->post('phone_no', true)) ? '電話' : $this->input->post('phone_no', true);
  35. $result = $this->app_model('excel')->export_cario_report($station_name . '(VIP)', $year, $month, $addr, $phone_no, 250);
  36. if(empty($result))
  37. {
  38. echo '無記錄';
  39. exit;
  40. }
  41. }
  42. // 進出記錄表
  43. public function export_cario_report()
  44. {
  45. // 次月算上月
  46. $last_day_of_previous_month = date("Y-n-j", strtotime("last day of previous month"));
  47. $d = date_parse_from_format("Y-m-d", $last_day_of_previous_month);
  48. $station_name = empty($this->input->post('station_name', true)) ? '場站名稱' : $this->input->post('station_name', true);
  49. $year = empty($this->input->post('year', true)) ? $d['year'] : $this->input->post('year', true);
  50. $month = empty($this->input->post('month', true)) ? $d['month'] : $this->input->post('month', true);
  51. $addr = empty($this->input->post('addr', true)) ? '地址' : $this->input->post('addr', true);
  52. $phone_no = empty($this->input->post('phone_no', true)) ? '電話' : $this->input->post('phone_no', true);
  53. $result = $this->app_model('excel')->export_cario_report($station_name, $year, $month, $addr, $phone_no);
  54. if(empty($result))
  55. {
  56. echo '無記錄';
  57. exit;
  58. }
  59. }
  60. // ------------------------------------------------
  61. //
  62. // 博辰 (START)
  63. //
  64. // ------------------------------------------------
  65. // 同步 博辰 888
  66. function sync_parktron_888()
  67. {
  68. try{
  69. $ch = curl_init();
  70. curl_setopt($ch, CURLOPT_URL, 'http://192.168.10.80:5477/parktron/ipms/services/areaCount/findAll');
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  72. curl_setopt($ch, CURLOPT_POST, TRUE);
  73. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5);
  74. curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds
  75. curl_setopt($ch, CURLOPT_POSTFIELDS, '{}');
  76. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  77. $result = curl_exec($ch);
  78. $parktron_result = json_decode($result);
  79. trigger_error(PARKTRON_LOG_TITLE . '..' . __FUNCTION__ . '..' . print_r($parktron_result, true));
  80. $this->data_model()->sync_parktron_888($parktron_result);
  81. if(curl_errno($ch))
  82. {
  83. trigger_error(PARKTRON_LOG_TITLE . '..' . __FUNCTION__ . '..' . ', curl error: '. curl_error($ch));
  84. }
  85. curl_close($ch);
  86. }catch (Exception $e){
  87. trigger_error(PARKTRON_LOG_TITLE . '..' . __FUNCTION__ . '..' . 'error:'.$e->getMessage());
  88. }
  89. }
  90. // ------------------------------------------------
  91. //
  92. // CRM (START)
  93. //
  94. // ------------------------------------------------
  95. // [test] zzz
  96. public function gen_test_case()
  97. {
  98. $sno = '12302';
  99. $ivsno = 0;
  100. $io = 'CI';
  101. $lpr = 'TEST1109A';
  102. $ts = date('YmdHis');
  103. $parms = array();
  104. $parms['sno'] = $sno;
  105. $parms['ivsno'] = $ivsno;
  106. $parms['io'] = $io;
  107. $parms['lpr'] = $lpr;
  108. $parms['ts'] = $ts;
  109. $function_name = 'remote_lprio';
  110. $ck = $this->gen_cms_ck($parms, $function_name);
  111. echo "http://localhost/carpark.html/{$function_name}/sno/{$sno}/ivsno/{$ivsno}/io/{$io}/type/C/lpr/{$lpr}/color/NONE/sq/0/ts/{$ts}/sq2/0/etag/NONE/ant/1/ck/{$ck}";
  112. echo "\n\n";
  113. $function_name = 'remote_opendoor_lprio';
  114. $ck = $this->gen_cms_ck($parms, $function_name);
  115. echo "http://localhost/carpark.html/{$function_name}/sno/{$sno}/ivsno/{$ivsno}/io/{$io}/type/C/lpr/{$lpr}/color/NONE/sq/0/ts/{$ts}/sq2/0/etag/NONE/ant/1/ck/{$ck}";
  116. echo "\n\n";
  117. $function_name = 'remote_opendoor_anyway';
  118. $ck = $this->gen_cms_ck($parms, $function_name);
  119. echo "http://localhost/carpark.html/{$function_name}/sno/{$sno}/ivsno/{$ivsno}/io/{$io}/lpr/{$lpr}/ts/{$ts}/ck/{$ck}";
  120. echo "\n\n";
  121. exit;
  122. }
  123. // 產生 CK
  124. function gen_cms_ck($parms, $function_name)
  125. {
  126. return md5($parms['sno']. 'a' . date('dmh') . 'l' . $parms['ts'] . 't'. $parms['lpr']. 'o'. $parms['ivsno'] . 'b'. $parms['io'] . $function_name);
  127. }
  128. // [local] 新增車辨記錄
  129. public function local_lprio()
  130. {
  131. $LOG_FLAG = 'cms://';
  132. $sno = $this->input->post('station_no', true);
  133. $ivsno = $this->input->post('ivsno', true);
  134. $io = $this->input->post('io', true);
  135. $ctype = $this->input->post('ctype', true);
  136. $lpr = $this->input->post('lpr', true);
  137. $cmd = $this->input->post('cmd', true);
  138. // 判斷 cmd 正確性
  139. if($cmd == 1)
  140. {
  141. // 新增車辨記錄
  142. }
  143. else
  144. {
  145. echo 'unknown_cmd';
  146. exit;
  147. }
  148. // 摸擬連結參數
  149. $parms = array();
  150. $parms['sno'] = $sno;
  151. $parms['ivsno'] = $ivsno;
  152. $parms['io'] = $ctype.$io;
  153. $parms['type'] = 'C';
  154. $parms['lpr'] = preg_replace('/[^0-9A-Z]/', '', strtoupper(urldecode($lpr)));
  155. $parms['color'] = 'NONE';
  156. $parms['sq'] = 0;
  157. $parms['ts'] = date('YmdHis');
  158. $parms['sq2'] = 0;
  159. $parms['etag'] = 'NONE';
  160. $parms['ant'] = 1;
  161. // 補充
  162. $parms['obj_type'] = 1;
  163. $parms['curr_time_str'] = date('Y-m-d H:i:s');
  164. $parms['pic_name'] = '';
  165. trigger_error($LOG_FLAG . __FUNCTION__ . '..' . print_r($parms, true));
  166. // 判斷 io 正確性
  167. if(!in_array($parms['io'], array('CI', 'CO', 'MI', 'MO')))
  168. {
  169. echo 'unknown_io';
  170. exit;
  171. }
  172. // 執行
  173. $this->app_model('cars')->lprio($parms);
  174. echo 'ok';
  175. exit;
  176. }
  177. // [remote] 新增車辨記錄
  178. public function remote_lprio()
  179. {
  180. $LOG_FLAG = 'cms://';
  181. $parms = $this->uri->uri_to_assoc(3);
  182. // ck
  183. if($parms['ck'] != $this->gen_cms_ck($parms, __FUNCTION__))
  184. {
  185. echo 'ck_error'; // 中斷
  186. exit;
  187. }
  188. $parms['lpr'] = urldecode($parms['lpr']);
  189. $parms['obj_type'] = 1;
  190. $parms['curr_time_str'] = date('Y-m-d H:i:s');
  191. $parms['pic_name'] = '';
  192. trigger_error($LOG_FLAG . __FUNCTION__ . '..' . print_r($parms, true));
  193. // 執行
  194. $this->app_model('cars')->lprio($parms);
  195. echo 'ok';
  196. exit;
  197. }
  198. // [remote] 車辨開門
  199. public function remote_opendoor_lprio()
  200. {
  201. $LOG_FLAG = 'cms://';
  202. $parms = $this->uri->uri_to_assoc(3);
  203. // ck
  204. if($parms['ck'] != $this->gen_cms_ck($parms, __FUNCTION__))
  205. {
  206. echo 'ck_error'; // 中斷
  207. exit;
  208. }
  209. $parms['lpr'] = urldecode($parms['lpr']);
  210. trigger_error($LOG_FLAG . __FUNCTION__ . '..' . print_r($parms, true));
  211. // 初始 mqtt
  212. $this->init_mqtt();
  213. // 執行
  214. $this->app_model('cars')->opendoor_lprio($parms);
  215. echo 'ok';
  216. exit;
  217. }
  218. // [remote] 直接開門
  219. public function remote_opendoor_anyway()
  220. {
  221. $LOG_FLAG = 'cms://';
  222. $parms = $this->uri->uri_to_assoc(3);
  223. // ck
  224. if($parms['ck'] != $this->gen_cms_ck($parms, __FUNCTION__))
  225. {
  226. echo 'ck_error'; // 中斷
  227. exit;
  228. }
  229. $parms['lpr'] = urldecode($parms['lpr']);
  230. trigger_error($LOG_FLAG . __FUNCTION__ . '..' . print_r($parms, true));
  231. // 初始 mqtt
  232. $this->init_mqtt();
  233. // 判斷會員身份
  234. $cars_model = $this->app_model('cars');
  235. $rows = $cars_model->get_member($lpr);
  236. if ($rows['member_no'] == 0)
  237. {
  238. $parms['ck'] = $cars_model->gen_opendoor_ck($parms, 'temp_opendoors'); // 臨停訊號
  239. $cars_model->do_temp_opendoor($parms);
  240. }
  241. else
  242. {
  243. $parms['ck'] = $cars_model->gen_opendoor_ck($parms, 'member_opendoors'); // 月租訊號
  244. $cars_model->do_member_opendoor($parms);
  245. }
  246. echo 'ok';
  247. exit;
  248. }
  249. // ------------------------------------------------
  250. //
  251. // 接收端 (START)
  252. //
  253. // ------------------------------------------------
  254. // [mqtt] 接收端
  255. public function mqtt_service()
  256. {
  257. $LOG_FLAG = 'mqtt://';
  258. $topic = $this->input->post('topic', true);
  259. $msg = $this->input->post('msg', true);
  260. $ck = $this->input->post('ck', true);
  261. if(md5($topic.'altob'.$msg) != $ck)
  262. {
  263. echo 'ck_error';
  264. exit;
  265. }
  266. trigger_error($LOG_FLAG . __FUNCTION__ . "|{$topic}|{$msg}");
  267. if($topic == 'altob.888.mqtt')
  268. {
  269. $data_model = $this->data_model();
  270. // 第一個場站編號 先不管場站
  271. $station_setting = $data_model->station_setting_query();
  272. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_setting['station_no']);
  273. $first_station_no = $station_no_arr[0];
  274. $msg_arr = explode(',', $msg);
  275. if(sizeof($msg_arr) != 4)
  276. {
  277. trigger_error($LOG_FLAG . __FUNCTION__ . "..error_size.." . print_r($msg_arr, true));
  278. echo 'error_size';
  279. exit;
  280. }
  281. if($msg_arr[0] != 'N888' || $msg_arr[3] != 'altob')
  282. {
  283. trigger_error($LOG_FLAG . __FUNCTION__ . "..unknown_msg.." . print_r($msg_arr, true));
  284. echo 'unknown_msg';
  285. exit;
  286. }
  287. $msg_arr = explode(',', $msg);
  288. $group_id = isset($msg_arr[1]) && $msg_arr[1] == 2 ? 'M888' : 'C888';
  289. $value = isset($msg_arr[2]) ? $msg_arr[2] : 0;
  290. $result = $data_model->force_sync_888($first_station_no, $group_id, $value);
  291. trigger_error($LOG_FLAG . __FUNCTION__ . "..{$first_station_no}|{$group_id}|{$value}..result..{$result}..");
  292. }
  293. echo 'ok';
  294. exit;
  295. }
  296. // [設定檔] 取得設定
  297. public function station_setting_query()
  298. {
  299. $reload = $this->input->post('reload', true);
  300. $data_model = $this->data_model();
  301. if(isset($reload) && $reload > 0)
  302. {
  303. $station_setting = $data_model->station_setting_query(true); // 強制重新載入
  304. trigger_error(__FUNCTION__ . '..station_setting: '. print_r($station_setting, true));
  305. if(!$station_setting)
  306. {
  307. echo json_encode('fail', JSON_UNESCAPED_UNICODE);
  308. exit; // 中斷
  309. }
  310. usleep(300000); // 0.3 sec delay
  311. // 費率資料同步
  312. $result = $data_model->sync_price_plan(array('station_no_arr' => $station_setting['station_no']));
  313. trigger_error(__FUNCTION__ . '..sync_price_plan: '. $result);
  314. usleep(300000); // 0.3 sec delay
  315. // 會員資料同步
  316. $result = $data_model->sync_members(array('station_no_arr' => $station_setting['station_no_list'],
  317. 'current_station_no_arr' => $station_setting['station_no'])); // 20171211 upd
  318. trigger_error(__FUNCTION__ . '..sync_members: '. $result);
  319. usleep(300000); // 0.3 sec delay
  320. // 歐pa卡同步
  321. $result = $data_model->sync_allpa_user(array('station_no_arr' => $station_setting['station_no']));
  322. trigger_error(__FUNCTION__ . '..sync_allpa_user: '. $result);
  323. usleep(300000); // 0.3 sec delay
  324. // 在席資料同步
  325. $result = $data_model->sync_pks_groups_reload($station_setting);
  326. trigger_error(__FUNCTION__ . '..sync_pks_groups_reload: '. $result);
  327. }
  328. else
  329. {
  330. $station_setting = $data_model->station_setting_query(false);
  331. if(!$station_setting)
  332. {
  333. echo json_encode('fail', JSON_UNESCAPED_UNICODE);
  334. exit; // 中斷
  335. }
  336. }
  337. echo json_encode($station_setting, JSON_UNESCAPED_UNICODE);
  338. }
  339. // [排程 or 強制] 同步場站資訊
  340. public function sync_station_data()
  341. {
  342. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  343. $switch_lpr_arr = array(); // 換車牌
  344. $meta = $this->input->post('meta', true);
  345. if(!empty($meta))
  346. {
  347. trigger_error( __FUNCTION__ . '..meta_arr..' . $meta);
  348. $meta_arr = explode('@@@', $meta);
  349. foreach($meta_arr as $raw)
  350. {
  351. if(empty($raw))
  352. continue;
  353. $data = json_decode($raw, true);
  354. if($data['key'] == 'switch_lpr')
  355. {
  356. array_push($switch_lpr_arr, $data['value']);
  357. }
  358. }
  359. }
  360. $data_model = $this->data_model();
  361. // 0. 取得場站設定
  362. $station_setting = $data_model->station_setting_query(false);
  363. trigger_error(__FUNCTION__ . '..station_setting: '. print_r($station_setting, true));
  364. $station_no_arr = array('station_no_arr' => $station_setting['station_no']);
  365. // 1. 月租系統
  366. $result = $data_model->sync_members($station_no_arr);
  367. trigger_error(__FUNCTION__ . '..sync_members: '. $result);
  368. // 2. 同步車牌更換
  369. if(!empty($switch_lpr_arr))
  370. {
  371. $data_model->sync_switch_lpr($switch_lpr_arr);
  372. }
  373. // 3. 歐pa卡同步 (TODO: 暫時放在這)
  374. $result = $data_model->sync_allpa_user($station_no_arr);
  375. trigger_error(__FUNCTION__ . '..sync_allpa_user: '. $result);
  376. }
  377. // [API] 取得最新未結清
  378. public function get_last_unbalanced_cario()
  379. {
  380. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  381. $lpr = $this->input->post('lpr', true);
  382. $station_no = $this->input->post('station_no', true);
  383. $c_s = $this->input->post('c_s', true);
  384. // 確認正確性
  385. if(empty($c_s) || $c_s != md5('altob'.$lpr.'botla'.$station_no))
  386. {
  387. trigger_error(__FUNCTION__ . "..{$lpr}|".$station_no."|{$c_s}..check fail..");
  388. echo 'fail';
  389. exit;
  390. }
  391. $data = $this->data_model()->get_last_unbalanced_cario($lpr, $station_no);
  392. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  393. }
  394. // [API] 更新未結清 (行動支付)
  395. public function sync_m2payed()
  396. {
  397. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  398. $lpr = $this->input->post('lpr', true);
  399. $amt = $this->input->post('amt', true);
  400. $station_no = $this->input->post('station_no', true);
  401. $cario_no = $this->input->post('cario_no', true);
  402. $c_s = $this->input->post('c_s', true);
  403. // 確認正確性
  404. if(empty($c_s) || $c_s != md5($lpr.$amt.'altob'.$station_no.'botla'.$cario_no))
  405. {
  406. trigger_error(__FUNCTION__ . "..{$lpr}|{$amt}|{$station_no}|{$cario_no}|{$c_s}..check fail..");
  407. echo 'fail';
  408. exit;
  409. }
  410. // 臨停繳費
  411. echo $this->app_model('carpayment')->p2payed(array('seqno' => $cario_no, 'lpr' => $lpr, 'amt' => $amt), true);
  412. exit;
  413. }
  414. // 同步 (由排程呼叫)
  415. public function sync_minutely()
  416. {
  417. $this->sync_parktron_888(); // 同步博辰 888
  418. $this->data_model()->sync_pks_groups(); // 同步在席現況
  419. }
  420. /*
  421. // 20170816 手動新增入場資料
  422. public function gen_carin()
  423. {
  424. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  425. $lpr = $this->input->post('lpr', true);
  426. $station_no = $this->input->post('station_no', true);
  427. $c_s = $this->input->post('c_s', true);
  428. // 確認正確性
  429. if(empty($c_s) || $c_s != md5($lpr.'altob'.$station_no.'botla'. __FUNCTION__ ))
  430. {
  431. trigger_error(__FUNCTION__ . "..{$lpr}|{$station_no}|{$c_s}..check fail..");
  432. echo 'fail';
  433. exit;
  434. }
  435. $parms = array(
  436. 'sno' => $station_no,
  437. 'lpr' => $lpr,
  438. 'etag' => 'NONE',
  439. 'io' => 'CI',
  440. 'ivsno' => 0
  441. );
  442. $this->carpark_model->gen_carin($parms);
  443. echo 'ok';
  444. }
  445. */
  446. // ------------------------------------------------
  447. //
  448. // 接收端 (END)
  449. //
  450. // ------------------------------------------------
  451. // [START] 2016/06/03 登入
  452. public function index()
  453. {
  454. if($this->session->userdata('logged_in'))
  455. {
  456. $session_data = $this->session->userdata('logged_in');
  457. $data['username'] = $session_data['username'];
  458. $data['type'] = $session_data['type'];
  459. // 取得場站設定
  460. $station_setting = $this->data_model()->station_setting_query();
  461. if(isset($station_setting['station_no']))
  462. {
  463. $data['station_no'] = $station_setting['station_no'];
  464. $data['station_name'] = $station_setting['station_name'];
  465. }
  466. if($data['type'] == 'admin')
  467. {
  468. $this->show_page('admin_page', $data); // 進階管理者介面
  469. }
  470. else
  471. {
  472. $this->show_page('main_page', $data); // 一般
  473. }
  474. }
  475. else
  476. {
  477. //If no session, redirect to login page
  478. //redirect('login', 'refresh');
  479. $this->show_page('login_page');
  480. }
  481. }
  482. // 登入
  483. public function user_login()
  484. {
  485. // form_validation
  486. $this->form_validation->set_rules('login_name', 'login_name', 'trim|required');
  487. $this->form_validation->set_rules('pswd', 'pswd', 'trim|required');
  488. if($this->form_validation->run() == FALSE)
  489. {
  490. return RESULT_FORM_VALIDATION_FAIL;
  491. }
  492. // go model
  493. $data = array
  494. (
  495. 'login_name' => $this->input->post('login_name', true),
  496. 'pswd' => $this->input->post('pswd', true)
  497. );
  498. $result = $this->app_model('user')->user_login($data);
  499. if($result)
  500. {
  501. $sess_array = array();
  502. foreach($result as $row)
  503. {
  504. $sess_array = array
  505. (
  506. 'username' => $row->login_name ,
  507. 'type' => $row->user_type
  508. );
  509. $this->session->set_userdata('logged_in', $sess_array);
  510. }
  511. echo RESULT_SUCCESS;
  512. }
  513. else
  514. {
  515. return RESULE_FAIL;
  516. }
  517. }
  518. // 登出
  519. public function user_logout()
  520. {
  521. $this->session->unset_userdata('logged_in');
  522. session_destroy();
  523. return RESULT_SUCCESS;
  524. }
  525. // [END] 2016/06/03 登入
  526. /*
  527. // response http
  528. protected function http_return($return_code, $type)
  529. {
  530. if ($type == 'text') echo $return_code;
  531. else echo json_encode($return_code, JSON_UNESCAPED_UNICODE);
  532. }
  533. // 讀取cookie內容
  534. protected function get_cookie($cookie_name)
  535. {
  536. if (empty($_COOKIE[$cookie_name])) return array();
  537. return(json_decode($_COOKIE[$cookie_name], true));
  538. }
  539. // 儲存cookie內容
  540. protected function save_cookie($cookie_name, $cookie_info)
  541. {
  542. return setcookie($cookie_name, json_encode($cookie_info, JSON_UNESCAPED_UNICODE), 0, '/');
  543. }
  544. // 月租資料同步
  545. public function rent_sync()
  546. {
  547. $station_no = $this->input->post('station_no', true);
  548. $start_date = $this->input->post('start_date', true);
  549. $end_date = $this->input->post('end_date', true);
  550. // $data = $this->carpark_model->rent_sync($station_no, $start_date, $end_date);
  551. // print_r($data);
  552. }
  553. // 重設剩餘車位數
  554. public function available_set()
  555. {
  556. $data = $this->carpark_model->available_set();
  557. $this->http_return($data, 'json');
  558. }
  559. // 剩餘車位數更新
  560. public function available_update()
  561. {
  562. $station_no = $this->input->get_post('station_no', true);
  563. $data['name'] = $this->input->get_post('st_name', true);
  564. $data['tot_pkg'] = $this->input->get_post('tot_pkg', true);
  565. $data['ava_pkg'] = $this->input->get_post('ava_pkg', true);
  566. $this->http_return($this->carpark_model->available_update($station_no, $data), 'json');
  567. }
  568. // 剩餘車位數查核
  569. public function available_check()
  570. {
  571. $time_point = $this->input->get_post('time_point', true);
  572. $this->http_return($this->carpark_model->available_check($time_point), 'json');
  573. }
  574. // 顯示logs (cars grep 888)
  575. public function show_888_logs()
  576. {
  577. $lines = $this->uri->segment(3); // 顯示行數
  578. if (empty($lines)) $lines = 1000; // 無行數參數, 預設為1000行
  579. if($lines > 20000) $lines = 20000; // 最多 20000行
  580. $grep_str = ' |grep 888';
  581. $target_str = LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'. $grep_str;
  582. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  583. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  584. passthru('/usr/bin/tail -n ' . $lines . ' ' . $target_str); // 利用linux指令顯示倒數幾行的logs內容
  585. echo "\n----- " . $target_str . ' -----';
  586. echo '</pre></body></html>';
  587. }
  588. // 新增月租資料
  589. public function member_add()
  590. {
  591. $data = array
  592. (
  593. 'member_no' => $this->input->post('member_no', true),
  594. 'station_no' => $this->input->post('station_no', true),
  595. 'lpr' => strtoupper($this->input->post('lpr', true)),
  596. 'old_lpr' => strtoupper($this->input->post('old_lpr', true)),
  597. 'etag' => strtoupper($this->input->post('etag', true)),
  598. 'start_date' => $this->input->post('start_date', true),
  599. 'end_date' => $this->input->post('end_date', true),
  600. 'member_name' => $this->input->post('member_name', true),
  601. 'member_nick_name' => $this->input->post('member_name', true),
  602. 'mobile_no' => $this->input->post('mobile_no', true),
  603. 'member_id' => $this->input->post('member_id', true),
  604. 'contract_no' => $this->input->post('contract_no', true),
  605. 'amt' => $this->input->post('amt', true),
  606. 'tel_h' => $this->input->post('tel_h', true),
  607. 'tel_o' => $this->input->post('tel_o', true),
  608. 'addr' => $this->input->post('addr', true)
  609. );
  610. trigger_error("add:".print_r($data, true));
  611. if ($data['member_no'] == 0 || $data['old_lpr'] != $data['lpr'])
  612. {
  613. if ($this->carpark_model->check_lpr($data['lpr']) > 0)
  614. {
  615. echo '車牌重複, 請查明再輸入';
  616. exit;
  617. }
  618. }
  619. $this->carpark_model->member_add($data);
  620. echo 'ok';
  621. }
  622. // 刪除月租資料
  623. public function member_delete()
  624. {
  625. $member_no = $this->input->post('member_no', true);
  626. $this->carpark_model->member_delete($member_no);
  627. echo 'ok';
  628. }
  629. // 進出場事件即時顯示
  630. public function cario_event()
  631. {
  632. set_time_limit(0);
  633. while(true)
  634. {
  635. }
  636. }
  637. // 汽車開門
  638. public function opendoors()
  639. {
  640. $ivsno = $this->uri->segment(3);
  641. $lanes = array
  642. (
  643. 0 => array ('devno' => 0, 'temp' => 0, 'member' => 1), // 1號入口, temp:臨停, member:月租
  644. // 1 => array ('devno' => 0, 'temp' => 2, 'member' => 3), // 2號調撥入口
  645. 1 => array ('devno' => 1, 'temp' => 0, 'member' => 1), // 3號調撥出口
  646. 2 => array ('devno' => 1, 'temp' => 0, 'member' => 1), // 3號調撥出口
  647. 3 => array ('devno' => 1, 'temp' => 2, 'member' => 3) // 4號出口
  648. );
  649. $url = 'http://admin:99999999@192.168.10.53/cgi-bin/basic_setting.cgi?ID=1&';
  650. $member_tag = 'member'; // member:月租會員
  651. // 短路開柵欄
  652. @get_headers("{$url}OUTDEV={$lanes[$ivsno]['devno']}&OUTCH={$lanes[$ivsno][$member_tag]}&OUTSTATUS=1");
  653. usleep(400000); // 暫停0.4秒
  654. // 斷路, 車過關柵欄
  655. @get_headers("{$url}OUTDEV={$lanes[$ivsno]['devno']}&OUTCH={$lanes[$ivsno][$member_tag]}&OUTSTATUS=0");
  656. }
  657. // 調撥車道查詢
  658. public function reversible_lane_query()
  659. {
  660. $max_lane = 4; // 共幾個車道數
  661. for ($idx = 0; $idx < $max_lane; ++$idx)
  662. {
  663. $data[$idx] = file_get_contents("http://192.168.10.51:8090/cgi-bin/switcher.cgi?id={$idx}&action=9");
  664. }
  665. // $data = array(1, 1, 0, 1);
  666. echo json_encode($data);
  667. }
  668. // 調撥車道設定
  669. public function reversible_lane_set()
  670. {
  671. $lane_no = $this->input->post('lane_no', true);
  672. $actions = $this->input->post('actions', true);
  673. $data = file_get_contents("http://192.168.10.51:8090/cgi-bin/switcher.cgi?id={$lane_no}&action={$actions}");
  674. echo "{$lane_no}|{$actions}|{$data}";
  675. }
  676. // 在席車位檢查未有入場資料清單
  677. public function pks_check_list()
  678. {
  679. $max_rows = $this->uri->segment(3, 100); // 一次讀取筆數, 預設為100筆
  680. $data = $this->carpark_model->pks_check_list($max_rows);
  681. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  682. }
  683. // 重設在席查核
  684. public function reset_pks_check()
  685. {
  686. $data = $this->carpark_model->reset_pks_check();
  687. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  688. }
  689. // 更正在席車號
  690. public function correct_pks_lpr()
  691. {
  692. $pksno = $this->uri->segment(3, 0); // 車格號碼
  693. $lpr = $this->uri->segment(4, 'NONE'); // 車號
  694. if ($pksno == 0 || $lpr == 'NONE')
  695. {
  696. echo json_encode(array('err' => 1, 'cario_no' => 0), JSON_UNESCAPED_UNICODE);
  697. }
  698. else
  699. {
  700. $data = $this->carpark_model->correct_pks_lpr($pksno, $lpr);
  701. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  702. }
  703. }
  704. // 入場車號查核在席無資料清單
  705. public function carin_check_list()
  706. {
  707. $max_rows = $this->uri->segment(3, 20); // 一次讀取筆數, 預設為100筆
  708. $data = $this->carpark_model->carin_check_list($max_rows);
  709. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  710. }
  711. // 更正入場車號
  712. public function correct_carin_lpr()
  713. {
  714. $cario_no = $this->uri->segment(3, 0); // 車格號碼
  715. $lpr = $this->uri->segment(4, 'NONE'); // 車號
  716. $in_time = urldecode($this->uri->segment(5, '')); // 入場時間
  717. $data = $this->carpark_model->correct_carin_lpr($cario_no, $lpr, $in_time);
  718. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  719. }
  720. */
  721. // 查詢月租資料
  722. public function member_query()
  723. {
  724. $data = $this->app_model()->member_query();
  725. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  726. }
  727. // 進出場現況表
  728. public function cario_list()
  729. {
  730. $data = $this->app_model()->cario_list();
  731. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  732. }
  733. // 顯示圖檔(http://url/carpark.html/pics/lpr_ABY8873_O_0_0_C_20150919210022)
  734. public function pics()
  735. {
  736. readfile(CAR_PIC.$this->uri->segment(3).'/'.str_replace('/', '', $this->uri->segment(4)).'.jpg');
  737. }
  738. // 車號入場查詢
  739. public function carin_lpr_query()
  740. {
  741. $lpr = $this->uri->segment(3);
  742. $data = $this->app_model()->carin_lpr_query($lpr);
  743. echo json_encode($data);
  744. }
  745. // 以時間查詢入場資訊
  746. public function carin_time_query()
  747. {
  748. $time_query = $this->input->post('time_query', true);
  749. $minutes_range = $this->input->post('minutes_range', true);
  750. $data = $this->app_model()->carin_time_query($time_query, $minutes_range);
  751. echo json_encode($data);
  752. }
  753. // 查詢行動支付記錄
  754. public function tx_bill_query()
  755. {
  756. $data = $this->app_model()->tx_bill_query();
  757. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  758. }
  759. // 查詢月租繳款機記錄
  760. public function tx_bill_ats_query()
  761. {
  762. $data = $this->app_model()->tx_bill_ats_query();
  763. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  764. }
  765. // 查詢樓層在席群組
  766. public function pks_group_query()
  767. {
  768. $data = $this->app_model()->pks_group_query();
  769. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  770. }
  771. // 微調剩餘車位數
  772. public function pks_availables_update()
  773. {
  774. $group_id = $this->uri->segment(3); // id
  775. $value = $this->uri->segment(4, 0); // value
  776. $station_no = $this->uri->segment(5); // station_no
  777. // 初始 mqtt
  778. $this->init_mqtt();
  779. $data = $this->data_model()->pks_availables_update($group_id, $value, true, $station_no);
  780. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  781. }
  782. // 進出場觸發 888 呼叫
  783. /*
  784. public function sync_888()
  785. {
  786. $io = $this->uri->segment(3); // CI, CO, MI, MO
  787. if(empty($io))
  788. {
  789. echo 'io_not_found';
  790. exit;
  791. }
  792. $parms = array('io' => $io, 'etag' => __FUNCTION__, 'lpr' => __FUNCTION__);
  793. echo $this->sync_data_model->sync_888($parms);
  794. exit;
  795. }
  796. */
  797. // 博辰測試用
  798. // http://localhost/carpark.html/test_8068_002/APK7310
  799. public function test_8068_002()
  800. {
  801. $lpr_in = $this->uri->segment(3);
  802. $seqno = '00001';
  803. $cmd = '002';
  804. $token = '000000000';
  805. $lpr = str_pad($lpr_in, 7, '%', STR_PAD_LEFT);
  806. $in_time = '2000/01/01 00:00:00';
  807. $error_str = '';
  808. $service_port = 8068;
  809. $address = empty($this->uri->segment(4)) ? "192.168.10.201" : "192.168.10." . $this->uri->segment(4);
  810. /* Create a TCP/IP socket. */
  811. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  812. if ($socket === false) {
  813. echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  814. $error_str .= "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  815. } else {
  816. echo "OK.\n";
  817. }
  818. echo "Attempting to connect to '{$address}' on port '{$service_port}'...";
  819. $result = socket_connect($socket, $address, $service_port);
  820. if ($result === false) {
  821. echo "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($socket)) . "\n";
  822. $error_str .= "socket_connect() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  823. } else {
  824. echo "OK.\n";
  825. }
  826. $in = pack('Ca5Ca3Ca9Ca7Ca19', 0x1c, $seqno, 0x1c, $cmd, 0x1c,
  827. $token, 0x1f, $lpr, 0x1f, $in_time
  828. );
  829. $out = '';
  830. echo "socket_write:";
  831. echo "{$in}<br/><br/>";
  832. if(!socket_write($socket, $in, strlen($in)))
  833. {
  834. echo('<p>Write failed</p>');
  835. $error_str .= "socket_write() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  836. }
  837. echo "socket_write..OK..";
  838. echo "<br/><br/>socket_read:";
  839. $out = socket_read($socket, 2048) or die("Could not read server responsen");
  840. //while ($out = socket_read($socket, 2048)) {
  841. // echo $out;
  842. //}
  843. echo "{$out}<br/><br/>";
  844. echo "Closing socket...";
  845. socket_close($socket);
  846. echo "OK.\n\n";
  847. trigger_error($error_str);
  848. exit;
  849. }
  850. }