VM暫存
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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