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.

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