VM暫存
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

306 Zeilen
9.9KB

  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. // 停車位置查詢 (2)
  133. public function check_location2()
  134. {
  135. $lpr = $this->uri->segment(3);
  136. $data = $this->parkingquery_model->check_location2($lpr);
  137. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  138. }
  139. // 空車位導引
  140. public function get_valid_seat()
  141. {
  142. $pksno = $this->uri->segment(3, 0); // 從某一個車位開始, 若無則設0
  143. $group_id = $this->uri->segment(4, 0); // 指定樓層群組
  144. if(empty($group_id))
  145. $group_id = '';
  146. $data = $this->parkingquery_model->get_valid_seat($pksno, 1, $group_id);
  147. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  148. }
  149. // 空車位導引 (身障)
  150. public function get_valid_seat2()
  151. {
  152. $pksno = $this->uri->segment(3, 0); // 從某一個車位開始, 若無則設0
  153. $group_id = $this->uri->segment(4, 0); // 指定樓層群組
  154. if(empty($group_id))
  155. $group_id = '';
  156. $data = $this->parkingquery_model->get_valid_seat($pksno, 3, $group_id);
  157. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  158. }
  159. // 空車位導引 (婦友)
  160. public function get_valid_seat3()
  161. {
  162. $pksno = $this->uri->segment(3, 0); // 從某一個車位開始, 若無則設0
  163. $group_id = $this->uri->segment(4, 0); // 指定樓層群組
  164. if(empty($group_id))
  165. $group_id = '';
  166. $data = $this->parkingquery_model->get_valid_seat($pksno, 4, $group_id);
  167. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  168. }
  169. // 防盜鎖車
  170. // http://xxxxxxxx/parkingquery.html/security_action/ABC1234/pswd/2
  171. public function security_action()
  172. {
  173. $lpr = $this->uri->segment(3);
  174. $pswd = $this->uri->segment(4);
  175. $action = $this->uri->segment(5);
  176. $data = $this->security_model->security_action($lpr, $pswd, $action);
  177. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  178. }
  179. // 查詢樓層總覽
  180. public function q_local_pks()
  181. {
  182. $seqno = $this->uri->segment(3);
  183. if(empty($seqno))
  184. $seqno = 'B1';
  185. $data = $this->parkingquery_model->q_local_pks($seqno);
  186. $data['result']['num'] = $seqno;
  187. $data['result_code'] = 'OK';
  188. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  189. }
  190. // [警急求救] 警急求救地圖
  191. public function floor_map()
  192. {
  193. $data = $this->parkingquery_model->check_space(0);
  194. if(isset($data['result']['floor']))
  195. $page_data['floor_info'] = json_encode($data['result']['floor'], JSON_UNESCAPED_UNICODE);
  196. $this->show_page("floor_map", $page_data);
  197. }
  198. // [警急求救] 警急求救地圖, 讀取緊急求救檔
  199. public function floor_map_read_sos()
  200. {
  201. if($this->my_ip() != '192.168.10.202') // 限制車辨主機
  202. {
  203. trigger_error(__FUNCTION__ . '..unknown host..' . $this->my_ip());
  204. exit;
  205. }
  206. if (file_exists(SOS_MSG))
  207. {
  208. $str = file_get_contents(SOS_MSG);
  209. unlink(SOS_MSG);
  210. echo $str;
  211. }
  212. else
  213. {
  214. echo 'NONE';
  215. }
  216. }
  217. // [警急求救] 緊急求救 API
  218. // http://XXXXXXXXXXXXXXXX/parkingquery.html/send_sos/B2/111/123
  219. public function send_sos()
  220. {
  221. $floor = $this->uri->segment(3);
  222. $x = $this->uri->segment(4);
  223. $y = $this->uri->segment(5);
  224. file_put_contents(SOS_MSG, "{$floor},{$x},{$y}");
  225. $data = $this->parkingquery_model->send_sos($floor, $x, $y);
  226. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  227. }
  228. // [第三方] 展示頁
  229. public function any_map()
  230. {
  231. $this->show_page("any_map");
  232. }
  233. // 取得 IP
  234. function my_ip()
  235. {
  236. if (getenv('HTTP_X_FORWARDED_FOR'))
  237. {
  238. $ip = getenv('HTTP_X_FORWARDED_FOR');
  239. }
  240. elseif (getenv('HTTP_X_REAL_IP'))
  241. {
  242. $ip = getenv('HTTP_X_REAL_IP');
  243. }
  244. else {
  245. $ip = $_SERVER['REMOTE_ADDR'];
  246. }
  247. return $ip;
  248. }
  249. }