VM暫存
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

1120 řádky
35KB

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