VM暫存
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Carpark.php 34KB

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