VM暫存
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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