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.

преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /*
  3. file: Parkingquery_model.php 停車管理系統(提供資策會使用)
  4. */
  5. class Parkingquery_model extends CI_Model
  6. {
  7. function __construct()
  8. {
  9. parent::__construct();
  10. $this->load->database();
  11. }
  12. public function init($vars)
  13. {
  14. // do nothing
  15. }
  16. // 查詢各樓層剩餘車位
  17. // http://203.75.167.89/parkingquery.html/check_space/12345
  18. public function check_space($seqno, $group_type=1)
  19. {
  20. $data = array();
  21. $results = $this->db->select('group_id, availables, tot')
  22. ->from('pks_groups')
  23. ->where('group_type', 1)
  24. ->get()
  25. ->result_array();
  26. foreach($results as $idx => $rows)
  27. {
  28. $data['result']['floor'][$idx] = array
  29. (
  30. 'floor_name' => $rows['group_id'],
  31. 'valid_count' => $rows['availables'],
  32. 'total_count' => $rows['tot']
  33. );
  34. }
  35. return $data;
  36. }
  37. // 停車位置查詢(板橋好停車)
  38. // http://203.75.167.89/parkingquery.html/check_location/ABC1234
  39. public function check_location($lpr)
  40. {
  41. $lpr = strtoupper($lpr); // 一律轉大寫
  42. $data = array();
  43. $rows = $this->db->select('pksno, pic_name')
  44. ->from('pks')
  45. ->where('lpr', $lpr)
  46. ->limit(1)
  47. ->get()
  48. ->row_array();
  49. if (!empty($rows['pksno']))
  50. {
  51. $data['result']['num'] = $lpr;
  52. $data['result']['location_no'] = "{$rows['pksno']}";
  53. $data['result_code'] = 'OK';
  54. $data['result']['pic_name'] = $rows['pic_name'];
  55. }
  56. else // 查無資料, 啟用模糊比對
  57. {
  58. // 讀取最近一筆入場資料
  59. $rows_cario = $this->db
  60. ->select('cario_no')
  61. ->from('cario')
  62. ->where(array('in_out' => 'CI', 'obj_id' => $lpr, 'finished' => 0, 'err' => 0, 'out_time IS NULL' => null))
  63. ->order_by('cario_no', 'desc')
  64. ->limit(1)
  65. ->get()
  66. ->row_array();
  67. // 有入場記錄, 直接猜在頂樓
  68. if (!empty($rows_cario['cario_no']))
  69. {
  70. $data['result']['num'] = $lpr;
  71. $data['result']['location_no'] = "7000";
  72. $data['result_code'] = 'OK';
  73. }
  74. else
  75. {
  76. $data['result']['num'] = $lpr;
  77. $data['result']['location_no'] = '0';
  78. $data['result_code'] = 'FAIL';
  79. }
  80. }
  81. return $data;
  82. }
  83. /*
  84. // 空車位導引
  85. // http://203.75.167.89/parkingquery.html/get_valid_seat
  86. // 註記現在時間, 並保留10分鐘
  87. public function get_valid_seat($pksno)
  88. {
  89. $data = array();
  90. $this->db->trans_start();
  91. if ($pksno > 0) // 限制從某一個車位開始指派車位
  92. {
  93. $sql = "select pksno from pks where status = 'VA' and pksno >= {$pksno} and prioritys != 0 and (book_time is null or book_time <= now()) order by prioritys asc limit 1 for update;";
  94. }
  95. else
  96. {
  97. $sql = "select pksno from pks where status = 'VA' and prioritys != 0 and (book_time is null or book_time <= now()) order by prioritys asc limit 1 for update;";
  98. }
  99. $rows = $this->db->query($sql)->row_array();
  100. if (!empty($rows['pksno']))
  101. {
  102. $data['result']['location_no'] = "{$rows['pksno']}";
  103. $data['result_code'] = 'OK';
  104. $sql = "update pks set book_time = addtime(now(), '00:10:00') where pksno = {$rows['pksno']};";
  105. $this->db->query($sql);
  106. }
  107. else
  108. {
  109. $data['result']['location_no'] = '0';
  110. $data['result_code'] = 'FAIL';
  111. }
  112. $this->db->trans_complete();
  113. return $data;
  114. }
  115. */
  116. // 空車位導引
  117. // http://203.75.167.89/parkingquery.html/get_valid_seat
  118. // 註記現在時間, 並保留10分鐘
  119. public function get_valid_seat($pksno, $group_type=1)
  120. {
  121. $data = array();
  122. $this->db->trans_start();
  123. if ($pksno > 0) // 限制從某一個車位開始指派車位
  124. {
  125. //$sql = "select pksno from pks where status = 'VA' and pksno >= {$pksno} and prioritys != 0 and (book_time is null or book_time <= now()) order by prioritys asc limit 1 for update;";
  126. // 2017/04/12 調整為支援找最近
  127. //$sql_xy = "select posx, posy from pks where pksno = {$pksno}";
  128. $sql_xy = "
  129. select pks.posx, pks.posy, RIGHT(pks_group_member.group_id, 1 ) as group_idx
  130. from pks
  131. left join pks_group_member on (pks_group_member.pksno = pks.pksno)
  132. left join pks_groups on (pks_groups.group_id = pks_group_member.group_id)
  133. where pks.pksno = {$pksno} AND pks_groups.group_type = {$group_type}
  134. ";
  135. $rows_xy = $this->db->query($sql_xy)->row_array();
  136. if(!empty($rows_xy['posx']) && !empty($rows_xy['posy']))
  137. {
  138. // 找最近
  139. $sql = "
  140. select pks.pksno, pks.posx, pks.posy, pks_group_member.group_id,
  141. (
  142. ABS(cast(pks.pksno as signed) - {$pksno}) +
  143. ABS(cast(pks.posx as signed) - {$rows_xy['posx']}) +
  144. ABS(cast(pks.posy as signed) - {$rows_xy['posy']}) +
  145. ABS(RIGHT(pks_group_member.group_id, 1 ) - {$rows_xy['group_idx']}) * 1000
  146. ) AS v
  147. from pks
  148. left join pks_group_member on (pks_group_member.pksno = pks.pksno)
  149. left join pks_groups on (pks_groups.group_id = pks_group_member.group_id)
  150. where
  151. pks.status = 'VA' and prioritys != 0 and (pks.book_time is null or pks.book_time <= now())
  152. and pks_groups.group_type = 1
  153. order by v asc limit 10 for update;
  154. ";
  155. /*
  156. $sql = "select pksno,
  157. ( ABS(cast(pksno as signed) - {$pksno}) + ABS(cast(posx as signed) - {$rows_xy['posx']}) + ABS(cast(posy as signed) - {$rows_xy['posy']}) ) AS v
  158. from pks where status = 'VA' and prioritys != 0 and (book_time is null or book_time <= now())
  159. order by v asc limit 1 for update;";
  160. */
  161. }
  162. else
  163. {
  164. // 依順序
  165. $sql = "select pksno from pks where status = 'VA' and pksno >= {$pksno} and prioritys != 0 and (book_time is null or book_time <= now()) order by prioritys asc limit 1 for update;";
  166. }
  167. }
  168. else
  169. {
  170. $sql = "select pksno from pks where status = 'VA' and prioritys != 0 and (book_time is null or book_time <= now()) order by prioritys asc limit 1 for update;";
  171. }
  172. $rows = $this->db->query($sql)->row_array();
  173. if (!empty($rows['pksno']))
  174. {
  175. $data['result']['location_no'] = "{$rows['pksno']}";
  176. $data['result_code'] = 'OK';
  177. $sql = "update pks set book_time = addtime(now(), '00:10:00') where pksno = {$rows['pksno']};";
  178. $this->db->query($sql);
  179. trigger_error(__FUNCTION__ . "[{$pksno}]:" . print_r($rows, true));
  180. }
  181. else
  182. {
  183. $data['result']['location_no'] = '0';
  184. $data['result_code'] = 'FAIL';
  185. }
  186. $this->db->trans_complete();
  187. return $data;
  188. }
  189. // 緊急求救
  190. // http://203.75.167.89/parkingquery.html/send_sos/B2/111/123
  191. public function send_sos($floor, $x, $y)
  192. {
  193. $data = array
  194. (
  195. 'result' => array('send_from' => array('floor' => $floor, 'x' => $x, 'y' => $y)),
  196. 'result_code' => 'OK'
  197. );
  198. return $data;
  199. }
  200. // 防盜鎖車
  201. // http://203.75.167.89/parkingquery.html/security_action/ABC1234/pswd/2
  202. public function security_action($lpr, $pswd, $action)
  203. {
  204. $data = array();
  205. /*
  206. $rows = $this->db->select('member_no, passwd, locked')
  207. ->from('members')
  208. ->where(array('lpr' => $lpr, 'passwd' => $pswd))
  209. ->limit(1)
  210. ->get()
  211. ->row_array();
  212. trigger_error('防盜鎖車:'.$this->db->last_query());
  213. // 無資料或密碼錯誤
  214. if (empty($rows['member_no']))
  215. {
  216. $data['result_code'] = 'FAIL';
  217. return($data);
  218. }
  219. */
  220. $rows = $this->db->select('member_no, passwd, locked')
  221. ->from('members')
  222. ->where(array('lpr' => $lpr))
  223. ->limit(1)
  224. ->get()
  225. ->row_array();
  226. trigger_error('防盜鎖車:'.$this->db->last_query());
  227. // 無資料或密碼錯誤
  228. if (empty($rows['member_no']) || md5($rows['passwd']) != $pswd)
  229. {
  230. $data['result_code'] = 'FAIL';
  231. return($data);
  232. }
  233. $data['result_code'] = 'OK';
  234. // 查詢防盜狀態
  235. if ($action == 2)
  236. {
  237. $data['result']['action'] = 'CHECK_SECURITY';
  238. $data['result'][0]['num'] = $lpr;
  239. $data['result'][0]['result'] = $rows['locked'] ? 'ON' : 'OFF';
  240. return $data;
  241. }
  242. $this->db
  243. ->where('member_no', $rows['member_no'])
  244. ->update('members', array('locked' => $action));
  245. $data['result']['action'] = $action == 1 ? 'ON' : 'OFF';
  246. return $data;
  247. }
  248. }