VM暫存
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Carpark.php 34KB

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