VM暫存
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

978 lines
30KB

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