VM暫存
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1089 rindas
34KB

  1. <?php
  2. /*
  3. file: carpark.php 停車管理
  4. */
  5. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  6. // ----- 定義常數(路徑, cache秒數) -----
  7. define('APP_VERSION', '100'); // 版本號
  8. define('MAX_AGE', 604800); // cache秒數, 此定義1個月
  9. define('APP_NAME', 'carpark'); // 應用系統名稱
  10. define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views
  11. //define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL
  12. define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost') . ($_SERVER['SERVER_PORT'] != 60123 ? ':' . $_SERVER['SERVER_PORT'] : '') .'/'); // URL
  13. define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑
  14. define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑
  15. define('WEB_LIB', SERVER_URL.'libs/'); // 網頁lib
  16. define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib
  17. define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path
  18. require_once(MQ_CLASS_FILE);
  19. class Carpark extends CI_Controller
  20. {
  21. var $vars = array(); // 共用變數
  22. function __construct()
  23. {
  24. parent::__construct();
  25. // ----- 程式開發階段log設定 -----
  26. if (@ENVIRONMENT == 'development')
  27. {
  28. ini_set('display_errors', '1');
  29. //error_reporting(E_ALL ^ E_NOTICE);
  30. error_reporting(E_ALL);
  31. }
  32. set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
  33. // 共用記憶體
  34. $this->vars['mcache'] = new Memcache;
  35. $this->vars['mcache']->connect(MEMCACHE_HOST, MEMCACHE_PORT) or die ('Could not connect memcache');
  36. /*
  37. // mqtt subscribe
  38. $this->vars['mqtt'] = new phpMQTT(MQ_HOST, MQ_PORT, uniqid());
  39. //if(!$this->vars['mqtt']->connect()){ die ('Could not connect mqtt'); }
  40. $this->vars['mqtt']->connect();
  41. */
  42. $this->load->model('carpark_model');
  43. $this->carpark_model->init($this->vars);
  44. // 資料介接模組
  45. $this->load->model('sync_data_model');
  46. $this->sync_data_model->init($this->vars); // for memcache
  47. // mqtt
  48. $station_setting = $this->sync_data_model->station_setting_query();
  49. $mqtt_ip = isset($station_setting['mqtt_ip']) ? $station_setting['mqtt_ip'] : MQ_HOST;
  50. $mqtt_port = isset($station_setting['mqtt_port']) ? $station_setting['mqtt_port'] : MQ_PORT;
  51. $this->vars['mqtt_ip'] = $mqtt_ip;
  52. $this->vars['mqtt_port'] = $mqtt_port;
  53. // init sync model
  54. $this->sync_data_model->init($this->vars);
  55. // 產生 excel 報表
  56. $this->load->model('excel_model');
  57. $this->excel_model->init($this->vars);
  58. // [START] 2016/06/03 登入
  59. $this->load->model('user_model');
  60. // load library
  61. $this->load->library(array('form_validation','session'));
  62. // load helpers
  63. $this->load->helper(array('form'));
  64. // ajax code
  65. define('RESULT_SUCCESS', 'ok');
  66. define('RESULT_FORM_VALIDATION_FAIL', '-1');
  67. define('RESULE_FAIL', 'gg');
  68. // [START] 2016/06/03 登入
  69. }
  70. // 發生錯誤時集中在此處理
  71. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  72. {
  73. // ex: car_err://message....
  74. $log_msg = explode('://', $errstr);
  75. if (count($log_msg) > 1)
  76. {
  77. $log_file = LOG_PATH.$log_msg[0];
  78. $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
  79. }
  80. else
  81. {
  82. $log_file = LOG_PATH.APP_NAME;
  83. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  84. }
  85. //$str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  86. error_log($str, 3, $log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  87. }
  88. // 顯示靜態網頁(html檔)
  89. protected function show_page($page_name, &$data = null)
  90. {
  91. $page_file = PAGE_PATH.$page_name.'.php';
  92. $last_modified_time = filemtime($page_file);
  93. // 若檔案修改時間沒有異動, 或版本無異動, 通知瀏覽器使用cache, 不再下傳網頁
  94. // header('Cache-Control:max-age='.MAX_AGE); // cache 1個月
  95. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified_time).' GMT');
  96. header('Etag: '. APP_VERSION);
  97. header('Cache-Control: public');
  98. // 20170921
  99. header("cache-Control: no-store, no-cache, must-revalidate");
  100. header("cache-Control: post-check=0, pre-check=0", false);
  101. header("Pragma: no-cache");
  102. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  103. if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == APP_VERSION && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time)
  104. {
  105. header('HTTP/1.1 304 Not Modified');
  106. }
  107. else
  108. {
  109. $this->load->view(APP_NAME.'/'.$page_name, $data);
  110. }
  111. }
  112. // ------------------------------------------------
  113. //
  114. // CRM (START)
  115. //
  116. // ------------------------------------------------
  117. // [test] zzz
  118. public function gen_test_case()
  119. {
  120. $sno = '12302';
  121. $ivsno = 0;
  122. $io = 'CI';
  123. $lpr = 'TEST1109A';
  124. $ts = date('YmdHis');
  125. $parms = array();
  126. $parms['sno'] = $sno;
  127. $parms['ivsno'] = $ivsno;
  128. $parms['io'] = $io;
  129. $parms['lpr'] = $lpr;
  130. $parms['ts'] = $ts;
  131. $function_name = 'remote_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_lprio';
  136. $ck = $this->gen_cms_ck($parms, $function_name);
  137. 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}";
  138. echo "\n\n";
  139. $function_name = 'remote_opendoor_anyway';
  140. $ck = $this->gen_cms_ck($parms, $function_name);
  141. echo "http://localhost/carpark.html/{$function_name}/sno/{$sno}/ivsno/{$ivsno}/io/{$io}/lpr/{$lpr}/ts/{$ts}/ck/{$ck}";
  142. echo "\n\n";
  143. exit;
  144. }
  145. // 產生 CK
  146. function gen_cms_ck($parms, $function_name)
  147. {
  148. return md5($parms['sno']. 'a' . date('dmh') . 'l' . $parms['ts'] . 't'. $parms['lpr']. 'o'. $parms['ivsno'] . 'b'. $parms['io'] . $function_name);
  149. }
  150. // [remote] 新增車辨記錄
  151. public function remote_lprio()
  152. {
  153. $LOG_FLAG = 'cms://';
  154. $parms = $this->uri->uri_to_assoc(3);
  155. // ck
  156. if($parms['ck'] != $this->gen_cms_ck($parms, __FUNCTION__))
  157. {
  158. echo 'ck_error'; // 中斷
  159. exit;
  160. }
  161. $parms['lpr'] = urldecode($parms['lpr']);
  162. $parms['obj_type'] = 1;
  163. $parms['curr_time_str'] = date('Y-m-d H:i:s');
  164. $parms['pic_name'] = '';
  165. trigger_error($LOG_FLAG . __FUNCTION__ . '..' . print_r($parms, true));
  166. // 載入模組
  167. $this->load->model('cars_model');
  168. $this->cars_model->init($this->vars);
  169. $this->cars_model->lprio($parms);
  170. $this->cars_model->stop();
  171. echo 'ok';
  172. exit;
  173. }
  174. // [remote] 車辨開門
  175. public function remote_opendoor_lprio()
  176. {
  177. $LOG_FLAG = 'cms://';
  178. $parms = $this->uri->uri_to_assoc(3);
  179. // ck
  180. if($parms['ck'] != $this->gen_cms_ck($parms, __FUNCTION__))
  181. {
  182. echo 'ck_error'; // 中斷
  183. exit;
  184. }
  185. $parms['lpr'] = urldecode($parms['lpr']);
  186. trigger_error($LOG_FLAG . __FUNCTION__ . '..' . print_r($parms, true));
  187. // 載入模組
  188. $this->load->model('cars_model');
  189. $this->cars_model->init($this->vars);
  190. $this->cars_model->opendoor_lprio($parms);
  191. $this->cars_model->stop();
  192. echo 'ok';
  193. exit;
  194. }
  195. // [remote] 直接開門
  196. public function remote_opendoor_anyway()
  197. {
  198. $LOG_FLAG = 'cms://';
  199. $parms = $this->uri->uri_to_assoc(3);
  200. // ck
  201. if($parms['ck'] != $this->gen_cms_ck($parms, __FUNCTION__))
  202. {
  203. echo 'ck_error'; // 中斷
  204. exit;
  205. }
  206. $parms['lpr'] = urldecode($parms['lpr']);
  207. trigger_error($LOG_FLAG . __FUNCTION__ . '..' . print_r($parms, true));
  208. // 載入模組
  209. $this->load->model('cars_model');
  210. $this->cars_model->init($this->vars);
  211. // 判斷會員身份
  212. $rows = $this->cars_model->get_member($lpr);
  213. // 取得 ck
  214. $parms['ck'] = $this->cars_model->gen_opendoor_ck($parms);
  215. if ($rows['member_no'] == 0)
  216. {
  217. //$this->cars_model->mq_send_opendoor(MQ_TOPIC_OPEN_DOOR, "DO{$parms['ivsno']},TICKET,{$parms['lpr']}");
  218. $this->cars_model->temp_opendoors($parms); // 臨停訊號
  219. }
  220. else
  221. {
  222. //$this->cars_model->mq_send_opendoor(MQ_TOPIC_OPEN_DOOR, "DO{$parms['ivsno']},OPEN,{$parms['lpr']}");
  223. $this->cars_model->member_opendoors($parms); // 月租訊號
  224. }
  225. $this->cars_model->stop();
  226. echo 'ok';
  227. exit;
  228. }
  229. // ------------------------------------------------
  230. //
  231. // 接收端 (START)
  232. //
  233. // ------------------------------------------------
  234. // [mqtt] 接收端
  235. public function mqtt_service()
  236. {
  237. $LOG_FLAG = 'mqtt://';
  238. $topic = $this->input->post('topic', true);
  239. $msg = $this->input->post('msg', true);
  240. $ck = $this->input->post('ck', true);
  241. if(md5($topic.'altob'.$msg) != $ck)
  242. {
  243. echo 'ck_error';
  244. exit;
  245. }
  246. trigger_error($LOG_FLAG . __FUNCTION__ . "|{$topic}|{$msg}");
  247. if($topic == 'altob.888.mqtt')
  248. {
  249. // 第一個場站編號 先不管場站
  250. $station_setting = $this->sync_data_model->station_setting_query();
  251. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_setting['station_no']);
  252. $first_station_no = $station_no_arr[0];
  253. $msg_arr = explode(',', $msg);
  254. if(sizeof($msg_arr) != 4)
  255. {
  256. $this->sync_data_model->stop();
  257. trigger_error($LOG_FLAG . __FUNCTION__ . "..error_size.." . print_r($msg_arr, true));
  258. echo 'error_size';
  259. exit;
  260. }
  261. if($msg_arr[0] != 'N888' || $msg_arr[3] != 'altob')
  262. {
  263. $this->sync_data_model->stop();
  264. trigger_error($LOG_FLAG . __FUNCTION__ . "..unknown_msg.." . print_r($msg_arr, true));
  265. echo 'unknown_msg';
  266. exit;
  267. }
  268. $msg_arr = explode(',', $msg);
  269. $group_id = isset($msg_arr[1]) && $msg_arr[1] == 2 ? 'M888' : 'C888';
  270. $value = isset($msg_arr[2]) ? $msg_arr[2] : 0;
  271. $result = $this->sync_data_model->force_sync_888($first_station_no, $group_id, $value);
  272. trigger_error($LOG_FLAG . __FUNCTION__ . "..{$first_station_no}|{$group_id}|{$value}..result..{$result}..");
  273. }
  274. $this->sync_data_model->stop();
  275. echo 'ok';
  276. exit;
  277. }
  278. // [設定檔] 取得設定
  279. public function station_setting_query()
  280. {
  281. $reload = $this->input->post('reload', true);
  282. if(isset($reload) && $reload > 0)
  283. {
  284. $station_setting = $this->sync_data_model->station_setting_query(true); // 強制重新載入
  285. trigger_error(__FUNCTION__ . '..station_setting: '. print_r($station_setting, true));
  286. if(!$station_setting)
  287. {
  288. $this->sync_data_model->stop();
  289. echo json_encode('fail', JSON_UNESCAPED_UNICODE);
  290. exit; // 中斷
  291. }
  292. $info = array('station_no_arr' => $station_setting['station_no']);
  293. usleep(300000); // 0.3 sec delay
  294. // 費率資料同步
  295. $result = $this->sync_data_model->sync_price_plan($info);
  296. trigger_error(__FUNCTION__ . '..sync_price_plan: '. $result);
  297. usleep(300000); // 0.3 sec delay
  298. // 會員資料同步
  299. $result = $this->sync_data_model->sync_members($info);
  300. trigger_error(__FUNCTION__ . '..sync_members: '. $result);
  301. usleep(300000); // 0.3 sec delay
  302. // 在席資料同步
  303. $result = $this->sync_data_model->sync_pks_groups_reload($station_setting);
  304. trigger_error(__FUNCTION__ . '..sync_pks_groups_reload: '. $result);
  305. }
  306. else
  307. {
  308. $station_setting = $this->sync_data_model->station_setting_query(false);
  309. if(!$station_setting)
  310. {
  311. $this->sync_data_model->stop();
  312. echo json_encode('fail', JSON_UNESCAPED_UNICODE);
  313. exit; // 中斷
  314. }
  315. }
  316. $this->sync_data_model->stop();
  317. echo json_encode($station_setting, JSON_UNESCAPED_UNICODE);
  318. exit;
  319. }
  320. // [排程 or 強制] 同步場站資訊
  321. public function sync_station_data()
  322. {
  323. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  324. $switch_lpr_arr = array(); // 換車牌
  325. $meta = $this->input->post('meta', true);
  326. if(!empty($meta))
  327. {
  328. trigger_error( __FUNCTION__ . '..meta_arr..' . $meta);
  329. $meta_arr = explode('@@@', $meta);
  330. foreach($meta_arr as $raw)
  331. {
  332. if(empty($raw))
  333. continue;
  334. $data = json_decode($raw, true);
  335. if($data['key'] == 'switch_lpr')
  336. {
  337. array_push($switch_lpr_arr, $data['value']);
  338. }
  339. }
  340. }
  341. // 0. 取得場站設定
  342. $station_setting = $this->sync_data_model->station_setting_query(false);
  343. trigger_error(__FUNCTION__ . '..station_setting: '. print_r($station_setting, true));
  344. $station_no_arr = array('station_no_arr' => $station_setting['station_no']);
  345. // 1. 月租系統
  346. $result = $this->sync_data_model->sync_members($station_no_arr);
  347. trigger_error(__FUNCTION__ . '..sync_members: '. $result);
  348. // 2. 同步車牌更換
  349. if(!empty($switch_lpr_arr))
  350. {
  351. $this->sync_data_model->sync_switch_lpr($switch_lpr_arr);
  352. }
  353. $this->sync_data_model->stop();
  354. }
  355. // [API] 取得最新未結清
  356. public function get_last_unbalanced_cario()
  357. {
  358. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  359. $lpr = $this->input->post('lpr', true);
  360. $station_no = $this->input->post('station_no', true);
  361. $c_s = $this->input->post('c_s', true);
  362. // 確認正確性
  363. if(empty($c_s) || $c_s != md5('altob'.$lpr.'botla'.$station_no))
  364. {
  365. trigger_error(__FUNCTION__ . "..{$lpr}|".$station_no."|{$c_s}..check fail..");
  366. echo 'fail';
  367. exit;
  368. }
  369. $data = $this->sync_data_model->get_last_unbalanced_cario($lpr, $station_no);
  370. $this->sync_data_model->stop();
  371. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  372. }
  373. // [API] 更新未結清 (行動支付)
  374. public function sync_m2payed()
  375. {
  376. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  377. $lpr = $this->input->post('lpr', true);
  378. $amt = $this->input->post('amt', true);
  379. $station_no = $this->input->post('station_no', true);
  380. $cario_no = $this->input->post('cario_no', true);
  381. $c_s = $this->input->post('c_s', true);
  382. // 確認正確性
  383. if(empty($c_s) || $c_s != md5($lpr.$amt.'altob'.$station_no.'botla'.$cario_no))
  384. {
  385. trigger_error(__FUNCTION__ . "..{$lpr}|{$amt}|{$station_no}|{$cario_no}|{$c_s}..check fail..");
  386. echo 'fail';
  387. exit;
  388. }
  389. // 臨停繳費
  390. $this->load->model('carpayment_model');
  391. echo $this->carpayment_model->p2payed(array('seqno' => $cario_no, 'lpr' => $lpr, 'amt' => $amt), true);
  392. exit;
  393. //echo 'ok';
  394. }
  395. // 驗証 IP
  396. function is_ip_valid()
  397. {
  398. $client_ip = $this->my_ip();
  399. if(!in_array($client_ip, array('61.219.172.11', '61.219.172.82')))
  400. {
  401. trigger_error('..block..from:'.$client_ip.'..unknown network..');
  402. return false;
  403. }
  404. return true;
  405. }
  406. // 取得 IP
  407. function my_ip()
  408. {
  409. if (getenv('HTTP_X_FORWARDED_FOR'))
  410. {
  411. $ip = getenv('HTTP_X_FORWARDED_FOR');
  412. }
  413. elseif (getenv('HTTP_X_REAL_IP'))
  414. {
  415. $ip = getenv('HTTP_X_REAL_IP');
  416. }
  417. else {
  418. $ip = $_SERVER['REMOTE_ADDR'];
  419. }
  420. return $ip;
  421. }
  422. // 同步 (由排程呼叫)
  423. public function sync_minutely()
  424. {
  425. $this->sync_data_model->sync_pks_groups(); // 同步在席現況
  426. $this->sync_data_model->stop();
  427. }
  428. // 20170816 手動新增入場資料
  429. public function gen_carin()
  430. {
  431. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  432. $lpr = $this->input->post('lpr', true);
  433. $station_no = $this->input->post('station_no', true);
  434. $c_s = $this->input->post('c_s', true);
  435. // 確認正確性
  436. if(empty($c_s) || $c_s != md5($lpr.'altob'.$station_no.'botla'. __FUNCTION__ ))
  437. {
  438. trigger_error(__FUNCTION__ . "..{$lpr}|{$station_no}|{$c_s}..check fail..");
  439. echo 'fail';
  440. exit;
  441. }
  442. $parms = array(
  443. 'sno' => $station_no,
  444. 'lpr' => $lpr,
  445. 'etag' => 'NONE',
  446. 'io' => 'CI',
  447. 'ivsno' => 0
  448. );
  449. $this->carpark_model->gen_carin($parms);
  450. echo 'ok';
  451. }
  452. // ------------------------------------------------
  453. //
  454. // 接收端 (END)
  455. //
  456. // ------------------------------------------------
  457. // [START] 2016/06/03 登入
  458. public function index()
  459. {
  460. if($this->session->userdata('logged_in'))
  461. {
  462. $session_data = $this->session->userdata('logged_in');
  463. $data['username'] = $session_data['username'];
  464. $data['type'] = $session_data['type'];
  465. if($data['type'] == 'admin')
  466. {
  467. $this->show_page('admin_page', $data); // 進階管理者介面
  468. }
  469. else
  470. {
  471. $this->show_page('main_page', $data); // 一般
  472. }
  473. }
  474. else
  475. {
  476. //If no session, redirect to login page
  477. //redirect('login', 'refresh');
  478. $this->show_page('login_page');
  479. }
  480. }
  481. // 登入
  482. public function user_login()
  483. {
  484. // form_validation
  485. $this->form_validation->set_rules('login_name', 'login_name', 'trim|required');
  486. $this->form_validation->set_rules('pswd', 'pswd', 'trim|required');
  487. if($this->form_validation->run() == FALSE)
  488. {
  489. return RESULT_FORM_VALIDATION_FAIL;
  490. }
  491. // go model
  492. $data = array
  493. (
  494. 'login_name' => $this->input->post('login_name', true),
  495. 'pswd' => $this->input->post('pswd', true)
  496. );
  497. $result = $this->user_model->user_login($data);
  498. if($result)
  499. {
  500. $sess_array = array();
  501. foreach($result as $row)
  502. {
  503. $sess_array = array
  504. (
  505. 'username' => $row->login_name ,
  506. 'type' => $row->user_type
  507. );
  508. $this->session->set_userdata('logged_in', $sess_array);
  509. }
  510. echo RESULT_SUCCESS;
  511. }
  512. else
  513. {
  514. return RESULE_FAIL;
  515. }
  516. }
  517. // 登出
  518. public function user_logout()
  519. {
  520. $this->session->unset_userdata('logged_in');
  521. session_destroy();
  522. return RESULT_SUCCESS;
  523. }
  524. // [END] 2016/06/03 登入
  525. // response http
  526. protected function http_return($return_code, $type)
  527. {
  528. if ($type == 'text') echo $return_code;
  529. else echo json_encode($return_code, JSON_UNESCAPED_UNICODE);
  530. }
  531. // 送出html code
  532. public function get_html()
  533. {
  534. /*
  535. $data = array
  536. (
  537. 'company_no' => $this->input->post('company_no', true), // 場站統編
  538. 'hq_company_no' => '80682490'
  539. );
  540. $this->load->view(APP_NAME.'/'.$this->input->post('tag_name', true), $data);
  541. */
  542. $this->load->view(APP_NAME.'/'.$this->input->post('tag_name', true), array());
  543. }
  544. // 讀取cookie內容
  545. protected function get_cookie($cookie_name)
  546. {
  547. if (empty($_COOKIE[$cookie_name])) return array();
  548. return(json_decode($_COOKIE[$cookie_name], true));
  549. }
  550. // 儲存cookie內容
  551. protected function save_cookie($cookie_name, $cookie_info)
  552. {
  553. return setcookie($cookie_name, json_encode($cookie_info, JSON_UNESCAPED_UNICODE), 0, '/');
  554. }
  555. // 月租資料同步
  556. public function rent_sync()
  557. {
  558. $station_no = $this->input->post('station_no', true);
  559. $start_date = $this->input->post('start_date', true);
  560. $end_date = $this->input->post('end_date', true);
  561. // $data = $this->carpark_model->rent_sync($station_no, $start_date, $end_date);
  562. // print_r($data);
  563. }
  564. // 重設剩餘車位數
  565. public function available_set()
  566. {
  567. $data = $this->carpark_model->available_set();
  568. $this->http_return($data, 'json');
  569. }
  570. // 剩餘車位數更新
  571. public function available_update()
  572. {
  573. $station_no = $this->input->get_post('station_no', true);
  574. $data['name'] = $this->input->get_post('st_name', true);
  575. $data['tot_pkg'] = $this->input->get_post('tot_pkg', true);
  576. $data['ava_pkg'] = $this->input->get_post('ava_pkg', true);
  577. $this->http_return($this->carpark_model->available_update($station_no, $data), 'json');
  578. }
  579. // 剩餘車位數查核
  580. public function available_check()
  581. {
  582. $time_point = $this->input->get_post('time_point', true);
  583. $this->http_return($this->carpark_model->available_check($time_point), 'json');
  584. }
  585. // 顯示logs
  586. public function show_logs()
  587. {
  588. $lines = $this->uri->segment(3); // 顯示行數
  589. if (empty($lines)) $lines = 140; // 無行數參數, 預設為40行
  590. if($lines > 1000) $lines = 1000; // 最多 1000行
  591. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  592. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  593. passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 利用linux指令顯示倒數幾行的logs內容
  594. echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';
  595. echo '</pre></body></html>';
  596. }
  597. // 顯示logs (cars grep 888)
  598. public function show_888_logs()
  599. {
  600. $lines = $this->uri->segment(3); // 顯示行數
  601. if (empty($lines)) $lines = 1000; // 無行數參數, 預設為1000行
  602. if($lines > 20000) $lines = 20000; // 最多 20000行
  603. $grep_str = ' |grep 888';
  604. $target_str = LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'. $grep_str;
  605. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  606. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  607. passthru('/usr/bin/tail -n ' . $lines . ' ' . $target_str); // 利用linux指令顯示倒數幾行的logs內容
  608. echo "\n----- " . $target_str . ' -----';
  609. echo '</pre></body></html>';
  610. }
  611. // 新增月租資料
  612. public function member_add()
  613. {
  614. $data = array
  615. (
  616. 'member_no' => $this->input->post('member_no', true),
  617. 'station_no' => $this->input->post('station_no', true),
  618. 'lpr' => strtoupper($this->input->post('lpr', true)),
  619. 'old_lpr' => strtoupper($this->input->post('old_lpr', true)),
  620. 'etag' => strtoupper($this->input->post('etag', true)),
  621. 'start_date' => $this->input->post('start_date', true),
  622. 'end_date' => $this->input->post('end_date', true),
  623. 'member_name' => $this->input->post('member_name', true),
  624. 'member_nick_name' => $this->input->post('member_name', true),
  625. 'mobile_no' => $this->input->post('mobile_no', true),
  626. 'member_id' => $this->input->post('member_id', true),
  627. 'contract_no' => $this->input->post('contract_no', true),
  628. 'amt' => $this->input->post('amt', true),
  629. 'tel_h' => $this->input->post('tel_h', true),
  630. 'tel_o' => $this->input->post('tel_o', true),
  631. 'addr' => $this->input->post('addr', true)
  632. );
  633. trigger_error("add:".print_r($data, true));
  634. if ($data['member_no'] == 0 || $data['old_lpr'] != $data['lpr'])
  635. {
  636. if ($this->carpark_model->check_lpr($data['lpr']) > 0)
  637. {
  638. echo '車牌重複, 請查明再輸入';
  639. exit;
  640. }
  641. }
  642. $this->carpark_model->member_add($data);
  643. echo 'ok';
  644. }
  645. // 查詢月租資料
  646. public function member_query()
  647. {
  648. $data = $this->carpark_model->member_query();
  649. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  650. }
  651. // 刪除月租資料
  652. public function member_delete()
  653. {
  654. $member_no = $this->input->post('member_no', true);
  655. $this->carpark_model->member_delete($member_no);
  656. echo 'ok';
  657. }
  658. // 進出場現況表
  659. public function cario_list()
  660. {
  661. $data = $this->carpark_model->cario_list();
  662. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  663. }
  664. // 進出場事件即時顯示
  665. public function cario_event()
  666. {
  667. set_time_limit(0);
  668. while(true)
  669. {
  670. }
  671. }
  672. // 顯示圖檔(http://url/carpark.html/pics/lpr_ABY8873_O_0_0_C_20150919210022)
  673. public function pics()
  674. {
  675. // ???
  676. readfile(CAR_PIC.$this->uri->segment(3).'/'.str_replace('/', '', $this->uri->segment(4)).'.jpg');
  677. }
  678. // 汽車開門
  679. public function opendoors()
  680. {
  681. $ivsno = $this->uri->segment(3);
  682. $lanes = array
  683. (
  684. 0 => array ('devno' => 0, 'temp' => 0, 'member' => 1), // 1號入口, temp:臨停, member:月租
  685. // 1 => array ('devno' => 0, 'temp' => 2, 'member' => 3), // 2號調撥入口
  686. 1 => array ('devno' => 1, 'temp' => 0, 'member' => 1), // 3號調撥出口
  687. 2 => array ('devno' => 1, 'temp' => 0, 'member' => 1), // 3號調撥出口
  688. 3 => array ('devno' => 1, 'temp' => 2, 'member' => 3) // 4號出口
  689. );
  690. $url = 'http://admin:99999999@192.168.10.53/cgi-bin/basic_setting.cgi?ID=1&';
  691. $member_tag = 'member'; // member:月租會員
  692. // 短路開柵欄
  693. @get_headers("{$url}OUTDEV={$lanes[$ivsno]['devno']}&OUTCH={$lanes[$ivsno][$member_tag]}&OUTSTATUS=1");
  694. usleep(400000); // 暫停0.4秒
  695. // 斷路, 車過關柵欄
  696. @get_headers("{$url}OUTDEV={$lanes[$ivsno]['devno']}&OUTCH={$lanes[$ivsno][$member_tag]}&OUTSTATUS=0");
  697. }
  698. // 調撥車道查詢
  699. public function reversible_lane_query()
  700. {
  701. $max_lane = 4; // 共幾個車道數
  702. for ($idx = 0; $idx < $max_lane; ++$idx)
  703. {
  704. $data[$idx] = file_get_contents("http://192.168.10.51:8090/cgi-bin/switcher.cgi?id={$idx}&action=9");
  705. }
  706. // $data = array(1, 1, 0, 1);
  707. echo json_encode($data);
  708. }
  709. // 車號入場查詢
  710. public function carin_lpr_query()
  711. {
  712. $lpr = $this->uri->segment(3);
  713. $data = $this->carpark_model->carin_lpr_query($lpr);
  714. echo json_encode($data);
  715. }
  716. // 以時間查詢入場資訊
  717. public function carin_time_query()
  718. {
  719. $time_query = $this->input->post('time_query', true);
  720. $minutes_range = $this->input->post('minutes_range', true);
  721. $data = $this->carpark_model->carin_time_query($time_query, $minutes_range);
  722. echo json_encode($data);
  723. }
  724. // 調撥車道設定
  725. public function reversible_lane_set()
  726. {
  727. $lane_no = $this->input->post('lane_no', true);
  728. $actions = $this->input->post('actions', true);
  729. $data = file_get_contents("http://192.168.10.51:8090/cgi-bin/switcher.cgi?id={$lane_no}&action={$actions}");
  730. echo "{$lane_no}|{$actions}|{$data}";
  731. }
  732. // 在席車位檢查未有入場資料清單
  733. public function pks_check_list()
  734. {
  735. $max_rows = $this->uri->segment(3, 100); // 一次讀取筆數, 預設為100筆
  736. $data = $this->carpark_model->pks_check_list($max_rows);
  737. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  738. }
  739. // 重設在席查核
  740. public function reset_pks_check()
  741. {
  742. $data = $this->carpark_model->reset_pks_check();
  743. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  744. }
  745. // 更正在席車號
  746. public function correct_pks_lpr()
  747. {
  748. $pksno = $this->uri->segment(3, 0); // 車格號碼
  749. $lpr = $this->uri->segment(4, 'NONE'); // 車號
  750. if ($pksno == 0 || $lpr == 'NONE')
  751. {
  752. echo json_encode(array('err' => 1, 'cario_no' => 0), JSON_UNESCAPED_UNICODE);
  753. }
  754. else
  755. {
  756. $data = $this->carpark_model->correct_pks_lpr($pksno, $lpr);
  757. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  758. }
  759. }
  760. // 入場車號查核在席無資料清單
  761. public function carin_check_list()
  762. {
  763. $max_rows = $this->uri->segment(3, 20); // 一次讀取筆數, 預設為100筆
  764. $data = $this->carpark_model->carin_check_list($max_rows);
  765. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  766. }
  767. // 更正入場車號
  768. public function correct_carin_lpr()
  769. {
  770. $cario_no = $this->uri->segment(3, 0); // 車格號碼
  771. $lpr = $this->uri->segment(4, 'NONE'); // 車號
  772. $in_time = urldecode($this->uri->segment(5, '')); // 入場時間
  773. $data = $this->carpark_model->correct_carin_lpr($cario_no, $lpr, $in_time);
  774. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  775. }
  776. // 查詢行動支付記錄
  777. public function tx_bill_query()
  778. {
  779. $data = $this->carpark_model->tx_bill_query();
  780. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  781. }
  782. // 查詢月租繳款機記錄
  783. public function tx_bill_ats_query()
  784. {
  785. $data = $this->carpark_model->tx_bill_ats_query();
  786. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  787. }
  788. // 查詢樓層在席群組
  789. public function pks_group_query()
  790. {
  791. $data = $this->carpark_model->pks_group_query();
  792. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  793. }
  794. // 微調剩餘車位數
  795. public function pks_availables_update()
  796. {
  797. $group_id = $this->uri->segment(3); // id
  798. $value = $this->uri->segment(4, 0); // value
  799. $station_no = $this->uri->segment(5); // station_no
  800. // 重新載入
  801. $data = $this->sync_data_model->pks_availables_update($group_id, $value, true, $station_no);
  802. $this->sync_data_model->stop();
  803. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  804. }
  805. // 進出場觸發 888 呼叫
  806. /*
  807. public function sync_888()
  808. {
  809. $io = $this->uri->segment(3); // CI, CO, MI, MO
  810. if(empty($io))
  811. {
  812. echo 'io_not_found';
  813. exit;
  814. }
  815. $parms = array('io' => $io, 'etag' => __FUNCTION__, 'lpr' => __FUNCTION__);
  816. echo $this->sync_data_model->sync_888($parms);
  817. exit;
  818. }
  819. */
  820. public function test()
  821. {
  822. //echo `whoami`;
  823. //echo phpinfo();
  824. }
  825. // 博辰測試用
  826. // http://localhost/carpark.html/test_8068_002/APK7310
  827. public function test_8068_002()
  828. {
  829. $lpr_in = $this->uri->segment(3);
  830. $seqno = '00001';
  831. $cmd = '002';
  832. $token = '000000000';
  833. $lpr = str_pad($lpr_in, 7, '%', STR_PAD_LEFT);
  834. $in_time = '2000/01/01 00:00:00';
  835. $error_str = '';
  836. $service_port = 8068;
  837. $address = "192.168.10.201";
  838. /* Create a TCP/IP socket. */
  839. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  840. if ($socket === false) {
  841. echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  842. $error_str .= "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  843. } else {
  844. echo "OK.\n";
  845. }
  846. echo "Attempting to connect to '{$address}' on port '{$service_port}'...";
  847. $result = socket_connect($socket, $address, $service_port);
  848. if ($result === false) {
  849. echo "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($socket)) . "\n";
  850. $error_str .= "socket_connect() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  851. } else {
  852. echo "OK.\n";
  853. }
  854. $in = pack('Ca5Ca3Ca9Ca7Ca19', 0x1c, $seqno, 0x1c, $cmd, 0x1c,
  855. $token, 0x1f, $lpr, 0x1f, $in_time
  856. );
  857. $out = '';
  858. echo "socket_write:";
  859. echo "{$in}<br/><br/>";
  860. if(!socket_write($socket, $in, strlen($in)))
  861. {
  862. echo('<p>Write failed</p>');
  863. $error_str .= "socket_write() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  864. }
  865. echo "socket_write..OK..";
  866. echo "<br/><br/>socket_read:";
  867. $out = socket_read($socket, 2048) or die("Could not read server responsen");
  868. //while ($out = socket_read($socket, 2048)) {
  869. // echo $out;
  870. //}
  871. echo "{$out}<br/><br/>";
  872. echo "Closing socket...";
  873. socket_close($socket);
  874. echo "OK.\n\n";
  875. trigger_error($error_str);
  876. exit;
  877. }
  878. // test
  879. public function test_phpexcel()
  880. {
  881. $query_year = 2017;
  882. $query_month = 3;
  883. //$this->excel_model->export_cario_data($query_year, $query_month);
  884. $this->excel_model->export_members();
  885. }
  886. }