VM暫存
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Carpark.php 30KB

8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. <?php
  2. /*
  3. file: carpark.php 停車管理
  4. */
  5. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  6. require_once(MQ_CLASS_FILE);
  7. // ----- 定義常數(路徑, cache秒數) -----
  8. define('APP_VERSION', '100'); // 版本號
  9. define('MAX_AGE', 604800); // cache秒數, 此定義1個月
  10. define('APP_NAME', 'carpark'); // 應用系統名稱
  11. define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views
  12. //define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL
  13. define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost') . ($_SERVER['SERVER_PORT'] != 60123 ? ':' . $_SERVER['SERVER_PORT'] : '') .'/'); // URL
  14. define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑
  15. define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑
  16. define('WEB_LIB', SERVER_URL.'libs/'); // 網頁lib
  17. define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib
  18. define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path
  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 subscribe
  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'] = new phpMQTT($mqtt_ip, $mqtt_port, uniqid());
  52. $this->vars['mqtt']->connect();
  53. // init again
  54. $this->sync_data_model->init($this->vars); // for mqtt
  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. // 接收端 (START)
  115. //
  116. // ------------------------------------------------
  117. // [mqtt] 接收端
  118. public function mqtt_service()
  119. {
  120. $topic = $this->input->post('topic', true);
  121. $msg = $this->input->post('msg', true);
  122. $ck = $this->input->post('ck', true);
  123. if(md5($topic.'altob'.$msg) != $ck)
  124. {
  125. echo 'ck_error';
  126. exit;
  127. }
  128. trigger_error(__FUNCTION__ . "|{$topic}|{$msg}");
  129. if($topic == 'altob.888.mqtt')
  130. {
  131. // 第一個場站編號 先不管場站
  132. $station_setting = $this->sync_data_model->station_setting_query();
  133. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_setting['station_no']);
  134. $first_station_no = $station_no_arr[0];
  135. $msg_arr = explode(',', $msg);
  136. if(sizeof($msg_arr) != 4)
  137. {
  138. trigger_error(__FUNCTION__ . "..error_size.." . print_r($msg_arr, true));
  139. echo 'error_size';
  140. exit;
  141. }
  142. if($msg_arr[0] != 'N888' || $msg_arr[3] != 'altob')
  143. {
  144. trigger_error(__FUNCTION__ . "..unknown_msg.." . print_r($msg_arr, true));
  145. echo 'unknown_msg';
  146. exit;
  147. }
  148. $msg_arr = explode(',', $msg);
  149. $group_id = isset($msg_arr[1]) && $msg_arr[1] == 2 ? 'M888' : 'C888';
  150. $value = isset($msg_arr[2]) ? $msg_arr[2] : 0;
  151. $result = $this->sync_data_model->force_sync_888($first_station_no, $group_id, $value);
  152. trigger_error(__FUNCTION__ . "..{$first_station_no}|{$group_id}|{$value}..result..{$result}..");
  153. }
  154. echo 'ok';
  155. exit;
  156. }
  157. // [設定檔] 取得設定
  158. public function station_setting_query()
  159. {
  160. $reload = $this->input->post('reload', true);
  161. if(isset($reload) && $reload > 0)
  162. {
  163. $station_setting = $this->sync_data_model->station_setting_query(true); // 強制重新載入
  164. trigger_error(__FUNCTION__ . '..station_setting: '. print_r($station_setting, true));
  165. if(!$station_setting)
  166. {
  167. echo json_encode('fail', JSON_UNESCAPED_UNICODE);
  168. exit; // 中斷
  169. }
  170. $info = array('station_no_arr' => $station_setting['station_no']);
  171. usleep(300000); // 0.3 sec delay
  172. // 費率資料同步
  173. $result = $this->sync_data_model->sync_price_plan($info);
  174. trigger_error(__FUNCTION__ . '..sync_price_plan: '. $result);
  175. usleep(300000); // 0.3 sec delay
  176. // 會員資料同步
  177. $result = $this->sync_data_model->sync_members($info);
  178. trigger_error(__FUNCTION__ . '..sync_members: '. $result);
  179. usleep(300000); // 0.3 sec delay
  180. // 在席資料同步
  181. $result = $this->sync_data_model->sync_pks_groups_reload($station_setting);
  182. trigger_error(__FUNCTION__ . '..sync_pks_groups_reload: '. $result);
  183. }
  184. else
  185. {
  186. $station_setting = $this->sync_data_model->station_setting_query(false);
  187. if(!$station_setting)
  188. {
  189. echo json_encode('fail', JSON_UNESCAPED_UNICODE);
  190. exit; // 中斷
  191. }
  192. }
  193. echo json_encode($station_setting, JSON_UNESCAPED_UNICODE);
  194. }
  195. // [排程 or 強制] 同步場站資訊
  196. public function sync_station_data()
  197. {
  198. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  199. $switch_lpr_arr = array(); // 換車牌
  200. $meta = $this->input->post('meta', true);
  201. if(!empty($meta))
  202. {
  203. trigger_error( __FUNCTION__ . '..meta_arr..' . $meta);
  204. $meta_arr = explode('@@@', $meta);
  205. foreach($meta_arr as $raw)
  206. {
  207. if(empty($raw))
  208. continue;
  209. $data = json_decode($raw, true);
  210. if($data['key'] == 'switch_lpr')
  211. {
  212. array_push($switch_lpr_arr, $data['value']);
  213. }
  214. }
  215. }
  216. // 0. 取得場站設定
  217. $station_setting = $this->sync_data_model->station_setting_query(false);
  218. trigger_error(__FUNCTION__ . '..station_setting: '. print_r($station_setting, true));
  219. $station_no_arr = array('station_no_arr' => $station_setting['station_no']);
  220. // 1. 月租系統
  221. $result = $this->sync_data_model->sync_members($station_no_arr);
  222. trigger_error(__FUNCTION__ . '..sync_members: '. $result);
  223. // 2. 同步車牌更換
  224. if(!empty($switch_lpr_arr))
  225. {
  226. $this->sync_data_model->sync_switch_lpr($switch_lpr_arr);
  227. }
  228. }
  229. // [API] 取得最新未結清
  230. public function get_last_unbalanced_cario()
  231. {
  232. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  233. $lpr = $this->input->post('lpr', true);
  234. $station_no = $this->input->post('station_no', true);
  235. $c_s = $this->input->post('c_s', true);
  236. // 確認正確性
  237. if(empty($c_s) || $c_s != md5('altob'.$lpr.'botla'.$station_no))
  238. {
  239. trigger_error(__FUNCTION__ . "..{$lpr}|".$station_no."|{$c_s}..check fail..");
  240. echo 'fail';
  241. exit;
  242. }
  243. $data = $this->sync_data_model->get_last_unbalanced_cario($lpr, $station_no);
  244. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  245. }
  246. // [API] 更新未結清 (行動支付)
  247. public function sync_m2payed()
  248. {
  249. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  250. $lpr = $this->input->post('lpr', true);
  251. $amt = $this->input->post('amt', true);
  252. $station_no = $this->input->post('station_no', true);
  253. $cario_no = $this->input->post('cario_no', true);
  254. $c_s = $this->input->post('c_s', true);
  255. // 確認正確性
  256. if(empty($c_s) || $c_s != md5($lpr.$amt.'altob'.$station_no.'botla'.$cario_no))
  257. {
  258. trigger_error(__FUNCTION__ . "..{$lpr}|{$amt}|{$station_no}|{$cario_no}|{$c_s}..check fail..");
  259. echo 'fail';
  260. exit;
  261. }
  262. // 臨停繳費
  263. $this->load->model('carpayment_model');
  264. echo $this->carpayment_model->p2payed(array('seqno' => $cario_no, 'lpr' => $lpr, 'amt' => $amt), true);
  265. exit;
  266. //echo 'ok';
  267. }
  268. // 驗証 IP
  269. function is_ip_valid()
  270. {
  271. $client_ip = $this->my_ip();
  272. if(!in_array($client_ip, array('61.219.172.11', '61.219.172.82')))
  273. {
  274. trigger_error('..block..from:'.$client_ip.'..unknown network..');
  275. return false;
  276. }
  277. return true;
  278. }
  279. // 取得 IP
  280. function my_ip()
  281. {
  282. if (getenv('HTTP_X_FORWARDED_FOR'))
  283. {
  284. $ip = getenv('HTTP_X_FORWARDED_FOR');
  285. }
  286. elseif (getenv('HTTP_X_REAL_IP'))
  287. {
  288. $ip = getenv('HTTP_X_REAL_IP');
  289. }
  290. else {
  291. $ip = $_SERVER['REMOTE_ADDR'];
  292. }
  293. return $ip;
  294. }
  295. // 同步 (由排程呼叫)
  296. public function sync_minutely()
  297. {
  298. $this->sync_data_model->sync_pks_groups(); // 同步在席現況
  299. }
  300. // 20170816 手動新增入場資料
  301. public function gen_carin()
  302. {
  303. trigger_error(__FUNCTION__ . '..from..'. $this->my_ip() .'..network..'); // IP 會浮動?
  304. $lpr = $this->input->post('lpr', true);
  305. $station_no = $this->input->post('station_no', true);
  306. $c_s = $this->input->post('c_s', true);
  307. // 確認正確性
  308. if(empty($c_s) || $c_s != md5($lpr.'altob'.$station_no.'botla'. __FUNCTION__ ))
  309. {
  310. trigger_error(__FUNCTION__ . "..{$lpr}|{$station_no}|{$c_s}..check fail..");
  311. echo 'fail';
  312. exit;
  313. }
  314. $parms = array(
  315. 'sno' => $station_no,
  316. 'lpr' => $lpr,
  317. 'etag' => 'NONE',
  318. 'io' => 'CI',
  319. 'ivsno' => 0
  320. );
  321. $this->carpark_model->gen_carin($parms);
  322. echo 'ok';
  323. }
  324. // ------------------------------------------------
  325. //
  326. // 接收端 (END)
  327. //
  328. // ------------------------------------------------
  329. // [START] 2016/06/03 登入
  330. public function index()
  331. {
  332. if($this->session->userdata('logged_in'))
  333. {
  334. $session_data = $this->session->userdata('logged_in');
  335. $data['username'] = $session_data['username'];
  336. $data['type'] = $session_data['type'];
  337. if($data['type'] == 'admin')
  338. {
  339. $this->show_page('admin_page', $data); // 進階管理者介面
  340. }
  341. else
  342. {
  343. $this->show_page('main_page', $data); // 一般
  344. }
  345. }
  346. else
  347. {
  348. //If no session, redirect to login page
  349. //redirect('login', 'refresh');
  350. $this->show_page('login_page');
  351. }
  352. }
  353. // 登入
  354. public function user_login()
  355. {
  356. // form_validation
  357. $this->form_validation->set_rules('login_name', 'login_name', 'trim|required');
  358. $this->form_validation->set_rules('pswd', 'pswd', 'trim|required');
  359. if($this->form_validation->run() == FALSE)
  360. {
  361. return RESULT_FORM_VALIDATION_FAIL;
  362. }
  363. // go model
  364. $data = array
  365. (
  366. 'login_name' => $this->input->post('login_name', true),
  367. 'pswd' => $this->input->post('pswd', true)
  368. );
  369. $result = $this->user_model->user_login($data);
  370. if($result)
  371. {
  372. $sess_array = array();
  373. foreach($result as $row)
  374. {
  375. $sess_array = array
  376. (
  377. 'username' => $row->login_name ,
  378. 'type' => $row->user_type
  379. );
  380. $this->session->set_userdata('logged_in', $sess_array);
  381. }
  382. echo RESULT_SUCCESS;
  383. }
  384. else
  385. {
  386. return RESULE_FAIL;
  387. }
  388. }
  389. // 登出
  390. public function user_logout()
  391. {
  392. $this->session->unset_userdata('logged_in');
  393. session_destroy();
  394. return RESULT_SUCCESS;
  395. }
  396. // [END] 2016/06/03 登入
  397. // response http
  398. protected function http_return($return_code, $type)
  399. {
  400. if ($type == 'text') echo $return_code;
  401. else echo json_encode($return_code, JSON_UNESCAPED_UNICODE);
  402. }
  403. // 送出html code
  404. public function get_html()
  405. {
  406. /*
  407. $data = array
  408. (
  409. 'company_no' => $this->input->post('company_no', true), // 場站統編
  410. 'hq_company_no' => '80682490'
  411. );
  412. $this->load->view(APP_NAME.'/'.$this->input->post('tag_name', true), $data);
  413. */
  414. $this->load->view(APP_NAME.'/'.$this->input->post('tag_name', true), array());
  415. }
  416. // 讀取cookie內容
  417. protected function get_cookie($cookie_name)
  418. {
  419. if (empty($_COOKIE[$cookie_name])) return array();
  420. return(json_decode($_COOKIE[$cookie_name], true));
  421. }
  422. // 儲存cookie內容
  423. protected function save_cookie($cookie_name, $cookie_info)
  424. {
  425. return setcookie($cookie_name, json_encode($cookie_info, JSON_UNESCAPED_UNICODE), 0, '/');
  426. }
  427. // 月租資料同步
  428. public function rent_sync()
  429. {
  430. $station_no = $this->input->post('station_no', true);
  431. $start_date = $this->input->post('start_date', true);
  432. $end_date = $this->input->post('end_date', true);
  433. // $data = $this->carpark_model->rent_sync($station_no, $start_date, $end_date);
  434. // print_r($data);
  435. }
  436. // 重設剩餘車位數
  437. public function available_set()
  438. {
  439. $data = $this->carpark_model->available_set();
  440. $this->http_return($data, 'json');
  441. }
  442. // 剩餘車位數更新
  443. public function available_update()
  444. {
  445. $station_no = $this->input->get_post('station_no', true);
  446. $data['name'] = $this->input->get_post('st_name', true);
  447. $data['tot_pkg'] = $this->input->get_post('tot_pkg', true);
  448. $data['ava_pkg'] = $this->input->get_post('ava_pkg', true);
  449. $this->http_return($this->carpark_model->available_update($station_no, $data), 'json');
  450. }
  451. // 剩餘車位數查核
  452. public function available_check()
  453. {
  454. $time_point = $this->input->get_post('time_point', true);
  455. $this->http_return($this->carpark_model->available_check($time_point), 'json');
  456. }
  457. // 顯示logs
  458. public function show_logs()
  459. {
  460. $lines = $this->uri->segment(3); // 顯示行數
  461. if (empty($lines)) $lines = 140; // 無行數參數, 預設為40行
  462. if($lines > 1000) $lines = 1000; // 最多 1000行
  463. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  464. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  465. passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 利用linux指令顯示倒數幾行的logs內容
  466. echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';
  467. echo '</pre></body></html>';
  468. }
  469. // 顯示logs (cars grep 888)
  470. public function show_888_logs()
  471. {
  472. $lines = $this->uri->segment(3); // 顯示行數
  473. if (empty($lines)) $lines = 1000; // 無行數參數, 預設為1000行
  474. if($lines > 20000) $lines = 20000; // 最多 20000行
  475. $grep_str = ' |grep 888';
  476. $target_str = LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'. $grep_str;
  477. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  478. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  479. passthru('/usr/bin/tail -n ' . $lines . ' ' . $target_str); // 利用linux指令顯示倒數幾行的logs內容
  480. echo "\n----- " . $target_str . ' -----';
  481. echo '</pre></body></html>';
  482. }
  483. // 新增月租資料
  484. public function member_add()
  485. {
  486. $data = array
  487. (
  488. 'member_no' => $this->input->post('member_no', true),
  489. 'station_no' => $this->input->post('station_no', true),
  490. 'lpr' => strtoupper($this->input->post('lpr', true)),
  491. 'old_lpr' => strtoupper($this->input->post('old_lpr', true)),
  492. 'etag' => strtoupper($this->input->post('etag', true)),
  493. 'start_date' => $this->input->post('start_date', true),
  494. 'end_date' => $this->input->post('end_date', true),
  495. 'member_name' => $this->input->post('member_name', true),
  496. 'member_nick_name' => $this->input->post('member_name', true),
  497. 'mobile_no' => $this->input->post('mobile_no', true),
  498. 'member_id' => $this->input->post('member_id', true),
  499. 'contract_no' => $this->input->post('contract_no', true),
  500. 'amt' => $this->input->post('amt', true),
  501. 'tel_h' => $this->input->post('tel_h', true),
  502. 'tel_o' => $this->input->post('tel_o', true),
  503. 'addr' => $this->input->post('addr', true)
  504. );
  505. trigger_error("add:".print_r($data, true));
  506. if ($data['member_no'] == 0 || $data['old_lpr'] != $data['lpr'])
  507. {
  508. if ($this->carpark_model->check_lpr($data['lpr']) > 0)
  509. {
  510. echo '車牌重複, 請查明再輸入';
  511. exit;
  512. }
  513. }
  514. $this->carpark_model->member_add($data);
  515. echo 'ok';
  516. }
  517. // 查詢月租資料
  518. public function member_query()
  519. {
  520. $data = $this->carpark_model->member_query();
  521. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  522. }
  523. // 刪除月租資料
  524. public function member_delete()
  525. {
  526. $member_no = $this->input->post('member_no', true);
  527. $this->carpark_model->member_delete($member_no);
  528. echo 'ok';
  529. }
  530. // 進出場現況表
  531. public function cario_list()
  532. {
  533. $data = $this->carpark_model->cario_list();
  534. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  535. }
  536. // 進出場事件即時顯示
  537. public function cario_event()
  538. {
  539. set_time_limit(0);
  540. while(true)
  541. {
  542. }
  543. }
  544. // 顯示圖檔(http://url/carpark.html/pics/lpr_ABY8873_O_0_0_C_20150919210022)
  545. public function pics()
  546. {
  547. // ???
  548. readfile(CAR_PIC.$this->uri->segment(3).'/'.str_replace('/', '', $this->uri->segment(4)).'.jpg');
  549. }
  550. // 汽車開門
  551. public function opendoors()
  552. {
  553. $ivsno = $this->uri->segment(3);
  554. $lanes = array
  555. (
  556. 0 => array ('devno' => 0, 'temp' => 0, 'member' => 1), // 1號入口, temp:臨停, member:月租
  557. // 1 => array ('devno' => 0, 'temp' => 2, 'member' => 3), // 2號調撥入口
  558. 1 => array ('devno' => 1, 'temp' => 0, 'member' => 1), // 3號調撥出口
  559. 2 => array ('devno' => 1, 'temp' => 0, 'member' => 1), // 3號調撥出口
  560. 3 => array ('devno' => 1, 'temp' => 2, 'member' => 3) // 4號出口
  561. );
  562. $url = 'http://admin:99999999@192.168.10.53/cgi-bin/basic_setting.cgi?ID=1&';
  563. $member_tag = 'member'; // member:月租會員
  564. // 短路開柵欄
  565. @get_headers("{$url}OUTDEV={$lanes[$ivsno]['devno']}&OUTCH={$lanes[$ivsno][$member_tag]}&OUTSTATUS=1");
  566. usleep(400000); // 暫停0.4秒
  567. // 斷路, 車過關柵欄
  568. @get_headers("{$url}OUTDEV={$lanes[$ivsno]['devno']}&OUTCH={$lanes[$ivsno][$member_tag]}&OUTSTATUS=0");
  569. }
  570. // 調撥車道查詢
  571. public function reversible_lane_query()
  572. {
  573. $max_lane = 4; // 共幾個車道數
  574. for ($idx = 0; $idx < $max_lane; ++$idx)
  575. {
  576. $data[$idx] = file_get_contents("http://192.168.10.51:8090/cgi-bin/switcher.cgi?id={$idx}&action=9");
  577. }
  578. // $data = array(1, 1, 0, 1);
  579. echo json_encode($data);
  580. }
  581. // 車號入場查詢
  582. public function carin_lpr_query()
  583. {
  584. $lpr = $this->uri->segment(3);
  585. $data = $this->carpark_model->carin_lpr_query($lpr);
  586. echo json_encode($data);
  587. }
  588. // 以時間查詢入場資訊
  589. public function carin_time_query()
  590. {
  591. $time_query = $this->input->post('time_query', true);
  592. $minutes_range = $this->input->post('minutes_range', true);
  593. $data = $this->carpark_model->carin_time_query($time_query, $minutes_range);
  594. echo json_encode($data);
  595. }
  596. // 調撥車道設定
  597. public function reversible_lane_set()
  598. {
  599. $lane_no = $this->input->post('lane_no', true);
  600. $actions = $this->input->post('actions', true);
  601. $data = file_get_contents("http://192.168.10.51:8090/cgi-bin/switcher.cgi?id={$lane_no}&action={$actions}");
  602. echo "{$lane_no}|{$actions}|{$data}";
  603. }
  604. // 在席車位檢查未有入場資料清單
  605. public function pks_check_list()
  606. {
  607. $max_rows = $this->uri->segment(3, 100); // 一次讀取筆數, 預設為100筆
  608. $data = $this->carpark_model->pks_check_list($max_rows);
  609. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  610. }
  611. // 重設在席查核
  612. public function reset_pks_check()
  613. {
  614. $data = $this->carpark_model->reset_pks_check();
  615. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  616. }
  617. // 更正在席車號
  618. public function correct_pks_lpr()
  619. {
  620. $pksno = $this->uri->segment(3, 0); // 車格號碼
  621. $lpr = $this->uri->segment(4, 'NONE'); // 車號
  622. if ($pksno == 0 || $lpr == 'NONE')
  623. {
  624. echo json_encode(array('err' => 1, 'cario_no' => 0), JSON_UNESCAPED_UNICODE);
  625. }
  626. else
  627. {
  628. $data = $this->carpark_model->correct_pks_lpr($pksno, $lpr);
  629. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  630. }
  631. }
  632. // 入場車號查核在席無資料清單
  633. public function carin_check_list()
  634. {
  635. $max_rows = $this->uri->segment(3, 20); // 一次讀取筆數, 預設為100筆
  636. $data = $this->carpark_model->carin_check_list($max_rows);
  637. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  638. }
  639. // 更正入場車號
  640. public function correct_carin_lpr()
  641. {
  642. $cario_no = $this->uri->segment(3, 0); // 車格號碼
  643. $lpr = $this->uri->segment(4, 'NONE'); // 車號
  644. $in_time = urldecode($this->uri->segment(5, '')); // 入場時間
  645. $data = $this->carpark_model->correct_carin_lpr($cario_no, $lpr, $in_time);
  646. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  647. }
  648. // 查詢行動支付記錄
  649. public function tx_bill_query()
  650. {
  651. $data = $this->carpark_model->tx_bill_query();
  652. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  653. }
  654. // 查詢月租繳款機記錄
  655. public function tx_bill_ats_query()
  656. {
  657. $data = $this->carpark_model->tx_bill_ats_query();
  658. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  659. }
  660. // 查詢樓層在席群組
  661. public function pks_group_query()
  662. {
  663. $data = $this->carpark_model->pks_group_query();
  664. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  665. }
  666. // 微調剩餘車位數
  667. public function pks_availables_update()
  668. {
  669. $group_id = $this->uri->segment(3); // id
  670. $value = $this->uri->segment(4, 0); // value
  671. $station_no = $this->uri->segment(5); // station_no
  672. $data = $this->sync_data_model->pks_availables_update($group_id, $value, true, $station_no);
  673. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  674. }
  675. // 進出場觸發 888 呼叫
  676. /*
  677. public function sync_888()
  678. {
  679. $io = $this->uri->segment(3); // CI, CO, MI, MO
  680. if(empty($io))
  681. {
  682. echo 'io_not_found';
  683. exit;
  684. }
  685. $parms = array('io' => $io, 'etag' => __FUNCTION__, 'lpr' => __FUNCTION__);
  686. echo $this->sync_data_model->sync_888($parms);
  687. exit;
  688. }
  689. */
  690. public function test()
  691. {
  692. //echo `whoami`;
  693. //echo phpinfo();
  694. }
  695. // 博辰測試用
  696. // http://localhost/carpark.html/test_8068_002/APK7310
  697. public function test_8068_002()
  698. {
  699. $lpr_in = $this->uri->segment(3);
  700. $seqno = '00001';
  701. $cmd = '002';
  702. $token = '000000000';
  703. $lpr = str_pad($lpr_in, 7, '%', STR_PAD_LEFT);
  704. $in_time = '2000/01/01 00:00:00';
  705. $error_str = '';
  706. $service_port = 8068;
  707. $address = "192.168.10.201";
  708. /* Create a TCP/IP socket. */
  709. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  710. if ($socket === false) {
  711. echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  712. $error_str .= "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  713. } else {
  714. echo "OK.\n";
  715. }
  716. echo "Attempting to connect to '{$address}' on port '{$service_port}'...";
  717. $result = socket_connect($socket, $address, $service_port);
  718. if ($result === false) {
  719. echo "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($socket)) . "\n";
  720. $error_str .= "socket_connect() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  721. } else {
  722. echo "OK.\n";
  723. }
  724. $in = pack('Ca5Ca3Ca9Ca7Ca19', 0x1c, $seqno, 0x1c, $cmd, 0x1c,
  725. $token, 0x1f, $lpr, 0x1f, $in_time
  726. );
  727. $out = '';
  728. echo "socket_write:";
  729. echo "{$in}<br/><br/>";
  730. if(!socket_write($socket, $in, strlen($in)))
  731. {
  732. echo('<p>Write failed</p>');
  733. $error_str .= "socket_write() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  734. }
  735. echo "socket_write..OK..";
  736. echo "<br/><br/>socket_read:";
  737. $out = socket_read($socket, 2048) or die("Could not read server responsen");
  738. //while ($out = socket_read($socket, 2048)) {
  739. // echo $out;
  740. //}
  741. echo "{$out}<br/><br/>";
  742. echo "Closing socket...";
  743. socket_close($socket);
  744. echo "OK.\n\n";
  745. trigger_error($error_str);
  746. exit;
  747. }
  748. // test
  749. public function test_phpexcel()
  750. {
  751. $query_year = 2017;
  752. $query_month = 3;
  753. //$this->excel_model->export_cario_data($query_year, $query_month);
  754. $this->excel_model->export_members();
  755. }
  756. }