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.

272 line
9.0KB

  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. class Parkingquery extends CI_Controller
  8. {
  9. var $vars = array(); // 共用變數
  10. function __construct()
  11. {
  12. parent::__construct();
  13. // ----- 程式開發階段log設定 -----
  14. if (@ENVIRONMENT == 'development')
  15. {
  16. ini_set('display_errors', '1');
  17. //error_reporting(E_ALL ^ E_NOTICE);
  18. error_reporting(E_ALL);
  19. }
  20. set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
  21. ignore_user_abort(); // 接受client斷線, 繼續run
  22. $method_name = $this->router->fetch_method();
  23. if ($method_name == 'security_action')
  24. {
  25. ob_end_clean();
  26. ignore_user_abort();
  27. ob_start();
  28. echo 'ok';
  29. header('Connection: close');
  30. header('Content-Length: ' . ob_get_length());
  31. ob_end_flush();
  32. flush();
  33. }
  34. /*
  35. // 共用記憶體
  36. $this->vars['mcache'] = new Memcache;
  37. $this->vars['mcache']->connect(MEMCACHE_HOST, MEMCACHE_PORT) or die ('Could not connect memcache');
  38. // mqtt subscribe
  39. $this->vars['mqtt'] = new phpMQTT(MQ_HOST, MQ_PORT, uniqid());
  40. if(!$this->vars['mqtt']->connect()){ die ('Could not connect mqtt'); }
  41. */
  42. // ----- 定義常數(路徑, cache秒數) -----
  43. define('APP_VERSION', '100'); // 版本號
  44. define('MAX_AGE', 604800); // cache秒數, 此定義1個月
  45. define('APP_NAME', 'parkingquery'); // 應用系統名稱
  46. define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views
  47. define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL
  48. define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑
  49. define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑
  50. define('WEB_LIB', SERVER_URL.'libs/'); // 網頁lib
  51. define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib
  52. define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path
  53. $this->load->model('parkingquery_model');
  54. $this->load->model('security_model'); // 鎖車
  55. // $this->parkingquery_model->init($this->vars);
  56. }
  57. // 發生錯誤時集中在此處理
  58. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  59. {
  60. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  61. //error_log($str, 3, $log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  62. error_log($str, 3, LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  63. }
  64. // 顯示靜態網頁(html檔)
  65. protected function show_page($page_name, &$data = null)
  66. {
  67. $page_file = PAGE_PATH.$page_name.'.php';
  68. $last_modified_time = filemtime($page_file);
  69. // 若檔案修改時間沒有異動, 或版本無異動, 通知瀏覽器使用cache, 不再下傳網頁
  70. header('Cache-Control:max-age='.MAX_AGE); // cache 1個月
  71. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified_time).' GMT');
  72. header('Etag: '. APP_VERSION);
  73. header('Cache-Control: public');
  74. if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == APP_VERSION && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time)
  75. {
  76. header('HTTP/1.1 304 Not Modified');
  77. }
  78. else
  79. {
  80. $this->load->view(APP_NAME.'/'.$page_name, $data);
  81. }
  82. }
  83. // response http
  84. protected function http_return($return_code, $type)
  85. {
  86. if ($type == 'text') echo $return_code;
  87. else echo json_encode($return_code, JSON_UNESCAPED_UNICODE);
  88. }
  89. // 查詢各樓層剩餘車位
  90. public function check_space_all()
  91. {
  92. $seqno = $this->uri->segment(3);
  93. $data = $this->parkingquery_model->check_space_all($seqno);
  94. $data['result']['num'] = $seqno;
  95. $data['result_code'] = 'OK';
  96. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  97. }
  98. // 查詢各樓層剩餘車位
  99. public function check_space()
  100. {
  101. $seqno = $this->uri->segment(3);
  102. $data = $this->parkingquery_model->check_space($seqno);
  103. $data['result']['num'] = $seqno;
  104. $data['result_code'] = 'OK';
  105. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  106. }
  107. // 查詢各樓層剩餘車位 (身障)
  108. public function check_space2()
  109. {
  110. $seqno = $this->uri->segment(3);
  111. $data = $this->parkingquery_model->check_space($seqno, 3);
  112. $data['result']['num'] = $seqno;
  113. $data['result_code'] = 'OK';
  114. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  115. }
  116. // 查詢各樓層剩餘車位 (婦友)
  117. public function check_space3()
  118. {
  119. $seqno = $this->uri->segment(3);
  120. $data = $this->parkingquery_model->check_space($seqno, 4);
  121. $data['result']['num'] = $seqno;
  122. $data['result_code'] = 'OK';
  123. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  124. }
  125. // 停車位置查詢
  126. public function check_location()
  127. {
  128. $lpr = $this->uri->segment(3);
  129. $data = $this->parkingquery_model->check_location($lpr);
  130. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  131. }
  132. // 空車位導引
  133. public function get_valid_seat()
  134. {
  135. $pksno = $this->uri->segment(3, 0); // 從某一個車位開始, 若無則設0
  136. $data = $this->parkingquery_model->get_valid_seat($pksno);
  137. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  138. }
  139. // 空車位導引 (身障)
  140. public function get_valid_seat2()
  141. {
  142. $pksno = $this->uri->segment(3, 0); // 從某一個車位開始, 若無則設0
  143. $data = $this->parkingquery_model->get_valid_seat($pksno, 3);
  144. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  145. }
  146. // 空車位導引 (婦友)
  147. public function get_valid_seat3()
  148. {
  149. $pksno = $this->uri->segment(3, 0); // 從某一個車位開始, 若無則設0
  150. $data = $this->parkingquery_model->get_valid_seat($pksno, 4);
  151. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  152. }
  153. // 防盜鎖車
  154. // http://xxxxxxxx/parkingquery.html/security_action/ABC1234/pswd/2
  155. public function security_action()
  156. {
  157. $lpr = $this->uri->segment(3);
  158. $pswd = $this->uri->segment(4);
  159. $action = $this->uri->segment(5);
  160. $data = $this->security_model->security_action($lpr, $pswd, $action);
  161. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  162. }
  163. // 查詢樓層總覽
  164. public function q_local_pks()
  165. {
  166. $seqno = $this->uri->segment(3);
  167. if(empty($seqno))
  168. $seqno = 'B1';
  169. $data = $this->parkingquery_model->q_local_pks($seqno);
  170. $data['result']['num'] = $seqno;
  171. $data['result_code'] = 'OK';
  172. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  173. }
  174. // [警急求救] 警急求救地圖
  175. public function floor_map()
  176. {
  177. $this->show_page("floor_map");
  178. }
  179. // [警急求救] 警急求救地圖, 讀取緊急求救檔
  180. public function floor_map_read_sos()
  181. {
  182. if($this->my_ip() != '192.168.10.202') // 限制車辨主機
  183. {
  184. trigger_error(__FUNCTION__ . '..unknown host..' . $this->my_ip());
  185. exit;
  186. }
  187. if (file_exists(SOS_MSG))
  188. {
  189. $str = file_get_contents(SOS_MSG);
  190. unlink(SOS_MSG);
  191. echo $str;
  192. }
  193. else
  194. {
  195. echo 'NONE';
  196. }
  197. }
  198. // [警急求救] 緊急求救 API
  199. // http://XXXXXXXXXXXXXXXX/parkingquery.html/send_sos/B2/111/123
  200. public function send_sos()
  201. {
  202. $floor = $this->uri->segment(3);
  203. $x = $this->uri->segment(4);
  204. $y = $this->uri->segment(5);
  205. file_put_contents(SOS_MSG, "{$floor},{$x},{$y}");
  206. $data = $this->parkingquery_model->send_sos($floor, $x, $y);
  207. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  208. }
  209. // 取得 IP
  210. function my_ip()
  211. {
  212. if (getenv('HTTP_X_FORWARDED_FOR'))
  213. {
  214. $ip = getenv('HTTP_X_FORWARDED_FOR');
  215. }
  216. elseif (getenv('HTTP_X_REAL_IP'))
  217. {
  218. $ip = getenv('HTTP_X_REAL_IP');
  219. }
  220. else {
  221. $ip = $_SERVER['REMOTE_ADDR'];
  222. }
  223. return $ip;
  224. }
  225. }