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.

286 lines
9.5KB

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. file: Mitac_service.php 與 mitac 介接相關都放這支
  4. */
  5. // ----- 定義常數(路徑, cache秒數) -----
  6. define('APP_VERSION', '100'); // 版本號
  7. define('MAX_AGE', 604800); // cache秒數, 此定義1個月
  8. define('APP_NAME', 'mitac_service'); // 應用系統名稱
  9. define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views
  10. define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL
  11. define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑
  12. define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑
  13. define('WEB_LIB', SERVER_URL.'/libs/'); // 網頁lib
  14. define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib
  15. define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path name
  16. define('LOG_FILE', FILE_BASE.APP_NAME.'/logs/mitac_service.'); // log file name
  17. class Mitac_service extends CI_Controller
  18. {
  19. var $vars = array();
  20. function __construct()
  21. {
  22. parent::__construct();
  23. // ----- 程式開發階段log設定 -----
  24. if (@ENVIRONMENT == 'development')
  25. {
  26. ini_set('display_errors', '1');
  27. //error_reporting(E_ALL ^ E_NOTICE);
  28. error_reporting(E_ALL);
  29. }
  30. set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
  31. ignore_user_abort(); // 接受client斷線, 繼續run
  32. $method_name = $this->router->fetch_method();
  33. $request_assoc = $this->uri->uri_to_assoc(3);
  34. trigger_error(__FUNCTION__ . '..' . $method_name. '..request start..' . print_r($request_assoc, true));
  35. if (in_array($method_name, array('parking_fee_altob', 'deduct_result')))
  36. {
  37. ob_end_clean();
  38. ignore_user_abort();
  39. ob_start();
  40. echo 'ok';
  41. header('Connection: close');
  42. header('Content-Length: ' . ob_get_length());
  43. ob_end_flush();
  44. flush();
  45. }
  46. $this->vars['date_time'] = date('Y-m-d H:i:s'); // 格式化時間(2015-10-12 14:36:21)
  47. $this->vars['time_num'] = str_replace(array('-', ':', ' '), '', $this->vars['date_time']); //數字化時間(20151012143621)
  48. $this->vars['date_num'] = substr($this->vars['time_num'], 0, 8); // 數字化日期(20151012)
  49. $this->vars['station_no'] = STATION_NO; // 本站編號
  50. // session_id(ip2long($_SERVER['REMOTE_ADDR'])); // 設定同一device為同一個session
  51. //session_start();
  52. // 阻檔未知的 IP
  53. if(!in_array($_SERVER['HTTP_X_REAL_IP'], array('127.0.0.1')))
  54. {
  55. trigger_error('refused://from:'.$_SERVER['HTTP_X_REAL_IP'].'..refused..');
  56. exit;
  57. }
  58. // MITAC 模組
  59. $this->load->model('mitac_service_model');
  60. $this->mitac_service_model->init($this->vars);
  61. }
  62. // 發生錯誤時集中在此處理
  63. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  64. {
  65. $log_msg = explode('://', $errstr);
  66. if (count($log_msg) > 1)
  67. {
  68. $log_file = $log_msg[0];
  69. $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
  70. }
  71. else
  72. {
  73. $log_file = APP_NAME;
  74. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  75. }
  76. error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  77. }
  78. // 顯示logs
  79. public function show_logs()
  80. {
  81. $lines = $this->uri->segment(3); // 顯示行數
  82. if (empty($lines)) $lines = 100; // 無行數參數, 預設為40行
  83. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  84. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  85. passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 利用linux指令顯示倒數幾行的logs內容
  86. echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';
  87. echo '</pre></body></html>';
  88. }
  89. // [排程] 詢問是否存活
  90. public function echo_mitac_alive()
  91. {
  92. echo $this->mitac_service_model->echo_mitac_alive();
  93. exit;
  94. }
  95. // 要求扣款 (ALTOB to MITAC)
  96. public function parking_fee_altob()
  97. {
  98. $seqno = $this->input->post('seqno', true); // 通訊序號
  99. $lpr = $this->input->post('lpr', true); // 車牌
  100. $in_time = $this->input->post('in_time', true); // 進場時間
  101. $out_time = $this->input->post('out_time', true); // 出場時間
  102. $gate_id = $this->input->post('gate_id', true); // 驗票機編號
  103. $ck = $this->input->post('ck', true); // 驗証碼
  104. // 通訊內容
  105. $parms = array(
  106. 'seqno' => $seqno,
  107. 'lpr' => $lpr,
  108. 'in_time' => $in_time,
  109. 'out_time' => $out_time,
  110. 'gate_id' => $gate_id);
  111. if($ck != md5($parms['seqno']. 'a' . date('dmh') . 'l' . $parms['lpr'] . 't'. $parms['in_time']. 'o'. $parms['out_time'] . 'b'. $parms['gate_id'] . __FUNCTION__))
  112. {
  113. trigger_error(__FUNCTION__ . '..ck_error..' . print_r($parms, true));
  114. exit; // 中斷
  115. }
  116. trigger_error(__FUNCTION__ . '..' . print_r($parms, true));
  117. $this->mitac_service_model->parking_fee_altob($parms);
  118. exit;
  119. }
  120. // 回應扣款成功 (MITAC to ALTOB)
  121. public function deduct_result()
  122. {
  123. $seqno = $this->input->post('seqno', true);
  124. $lpr = $this->input->post('lpr', true);
  125. $in_time = $this->input->post('in_time', true);
  126. $out_time = $this->input->post('out_time', true);
  127. $gate_id = $this->input->post('gate_id', true);
  128. $amt = $this->input->post('amt', true);
  129. $amt_discount = $this->input->post('amt_discount', true);
  130. $amt_real = $this->input->post('amt_real', true);
  131. $ck = $this->input->post('ck', true);
  132. // 通訊內容
  133. $parms = array(
  134. 'seqno' => $seqno,
  135. 'lpr' => $lpr,
  136. 'in_time' => $in_time,
  137. 'out_time' => $out_time,
  138. 'gate_id' => $gate_id,
  139. 'amt' => $amt,
  140. 'amt_discount' => $amt_discount,
  141. 'amt_real' => $amt_real);
  142. if($ck != md5($parms['seqno']. 'a' . date('dmh') . 'l' . $parms['lpr'] . 't'. $parms['amt']. 'o'. $parms['amt_discount'] . 'b'. $parms['amt_real'] . __FUNCTION__))
  143. {
  144. trigger_error(__FUNCTION__ . '..ck_error..' . print_r($parms, true));
  145. exit; // 中斷
  146. }
  147. trigger_error(__FUNCTION__ . '..' . print_r($parms, true));
  148. $this->mitac_service_model->deduct_result($parms);
  149. exit;
  150. }
  151. // http://localhost/mitac_service.html/test_parking_fee_altob
  152. public function test_parking_fee_altob()
  153. {
  154. $function_name = 'parking_fee_altob';
  155. $seqno = '20161004_101010';
  156. $lpr = 'AA1234';
  157. $in_time = '2017-11-11 16:40:02';
  158. $out_time = '2017-11-11 16:58:36';
  159. $gate_id = 1;
  160. // 通訊內容
  161. $parms = array(
  162. 'seqno' => $seqno,
  163. 'lpr' => $lpr,
  164. 'in_time' => $in_time,
  165. 'out_time' => $out_time,
  166. 'gate_id' => $gate_id);
  167. // 驗証碼
  168. $parms['ck'] = md5($parms['seqno']. 'a' . date('dmh') . 'l' . $parms['lpr'] . 't'. $parms['in_time']. 'o'. $parms['out_time'] . 'b'. $parms['gate_id'] . $function_name);
  169. // 測試呼叫
  170. $ch = curl_init();
  171. curl_setopt($ch, CURLOPT_URL, "http://localhost/mitac_service.html/{$function_name}");
  172. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  173. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  174. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  175. curl_setopt($ch, CURLOPT_POST, TRUE);
  176. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parms));
  177. $rs = curl_exec($ch);
  178. curl_close($ch);
  179. echo $rs;
  180. exit;
  181. }
  182. // http://localhost/mitac_service.html/test_49993/
  183. public function test_49993()
  184. {
  185. $seqno = '201711111_027771';
  186. $lpr = 'TEST1111B';
  187. $in_time = '20171111_190048';
  188. $out_time = '20171111_190048';
  189. $gate_id = 0;
  190. $amt = 66;
  191. $amt_discount = 10;
  192. $amt_real = 56;
  193. $msg = implode(',', ['Altob', 'DeductResult', $seqno, $lpr, $in_time, $out_time, $gate_id, $amt, $amt_discount, $amt_real]);
  194. $error_str = '';
  195. $service_port = 49993;
  196. $address = "192.168.10.201";
  197. /* Create a TCP/IP socket. */
  198. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  199. if ($socket === false) {
  200. echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  201. $error_str .= "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  202. } else {
  203. echo "OK.\n";
  204. }
  205. echo "Attempting to connect to '{$address}' on port '{$service_port}'...";
  206. $result = socket_connect($socket, $address, $service_port);
  207. if ($result === false) {
  208. echo "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($socket)) . "\n";
  209. $error_str .= "socket_connect() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  210. } else {
  211. echo "OK.\n";
  212. }
  213. /*
  214. $in = pack('Ca5Ca3Ca9Ca7Ca19', 0x1c, $seqno, 0x1c, $cmd, 0x1c,
  215. $token, 0x1f, $lpr, 0x1f, $in_time
  216. );
  217. */
  218. $in = $msg;
  219. $out = '';
  220. echo "socket_write:";
  221. echo "{$in}<br/><br/>";
  222. if(!socket_write($socket, $in, strlen($in)))
  223. {
  224. echo('<p>Write failed</p>');
  225. $error_str .= "socket_write() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  226. }
  227. echo "socket_write..OK..";
  228. echo "<br/><br/>socket_read:";
  229. $out = socket_read($socket, 2048) or die("Could not read server responsen");
  230. //while ($out = socket_read($socket, 2048)) {
  231. // echo $out;
  232. //}
  233. echo "{$out}<br/><br/>";
  234. echo "Closing socket...";
  235. socket_close($socket);
  236. echo "OK.\n\n";
  237. trigger_error($error_str);
  238. exit;
  239. }
  240. }