VM暫存
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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