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

1112 рядки
34KB

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