VM暫存
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1002 行
32KB

  1. <?php
  2. /*
  3. file: Sync_data_model.php 資料同步相關
  4. */
  5. define('SYNC_DATA_LOG_TITLE', 'sync://'); // LOG (sync)
  6. define('SYNC_PKS_GROUP_ID_CI', 'C888'); // 汽車 888
  7. define('SYNC_PKS_GROUP_ID_MI', 'M888'); // 機車 888
  8. define('SYNC_API_URL', 'http://61.219.172.11:60123/admins_station.html/');
  9. class Sync_data_model extends CI_Model
  10. {
  11. var $vars = array();
  12. function __construct()
  13. {
  14. parent::__construct();
  15. $this->load->database();
  16. }
  17. public function init($vars)
  18. {
  19. $this->vars = $vars;
  20. }
  21. // 送出至message queue(目前用mqtt)
  22. public function mq_send($topic, $msg)
  23. {
  24. $this->vars['mqtt']->publish($topic, $msg, 0);
  25. trigger_error("mqtt:{$topic}|{$msg}");
  26. usleep(100000); // delay 0.1 sec (避免漏訊號)
  27. }
  28. // ------------------------------------------------
  29. //
  30. // 博辰 (START)
  31. //
  32. // ------------------------------------------------
  33. // 博辰 888 同步
  34. public function sync_parktron_888($parktron_result)
  35. {
  36. if(empty($parktron_result))
  37. return 'empty';
  38. // 解析資料
  39. $pks_groups_arr = array();
  40. foreach($parktron_result as $content_result_list)
  41. {
  42. foreach ($content_result_list as $item_result_list)
  43. {
  44. $area_id = 0;
  45. $space_count = 0;
  46. $parking_count = 0;
  47. $blanking_count = 0;
  48. foreach ($item_result_list as $key => $value)
  49. {
  50. switch($key)
  51. {
  52. case 'areaId': $area_id = $value; break;
  53. case 'spaceCount': $space_count = $value; break;
  54. case 'parkingCount': $parking_count = $value; break;
  55. case 'blankingCount': $blanking_count = $value; break;
  56. default: trigger_error(__FUNCTION__ . "..unknown..{$key}|{$value}..");
  57. }
  58. }
  59. if($area_id > 0)
  60. {
  61. //trigger_error(__FUNCTION__ . "..$area_id, $space_count, $parking_count, $blanking_count..");
  62. $pks_groups_arr["P{$area_id}"] = array('tot' => $space_count, 'parked' => $parking_count, 'availables' => $blanking_count);
  63. }
  64. }
  65. }
  66. if(empty($pks_groups_arr))
  67. {
  68. trigger_error(__FUNCTION__ . "..empty pks_groups_arr..");
  69. return 'empty';
  70. }
  71. // 取得場站編號
  72. $station_setting = $this->station_setting_query();
  73. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_setting['station_no']);
  74. $station_no = $station_no_arr[0];
  75. trigger_error(__FUNCTION__ . "..$station_no.." . print_r($pks_groups_arr, true));
  76. // 更新博辰資料
  77. $this->db->trans_start();
  78. foreach($pks_groups_arr as $key => $data)
  79. {
  80. $where_group_arr = array('group_id' => $key, 'station_no' => $station_no);
  81. $rows = $this->db->select('group_id')->from('pks_groups')
  82. ->where($where_group_arr)
  83. ->limit(1)
  84. ->get()
  85. ->row_array();
  86. if(!isset($rows['group_id']))
  87. {
  88. // 不存在就新增
  89. $data['station_no'] = $station_no;
  90. $data['group_id'] = $key;
  91. $data['floors'] = $key;
  92. $data['group_name'] = '博辰 ' . $key;
  93. $this->db->insert('pks_groups', $data);
  94. trigger_error(__FUNCTION__ . '..insert pks_groups..'. print_r($data, true));
  95. }
  96. else
  97. {
  98. $data['renum'] = 0;
  99. $this->db->where($where_group_arr)->update('pks_groups', $data);
  100. }
  101. }
  102. $this->db->trans_complete();
  103. return 'ok';
  104. }
  105. // ------------------------------------------------
  106. //
  107. // 在席系統同步 (START)
  108. //
  109. // ------------------------------------------------
  110. // 強制更新顯示
  111. public function force_sync_888($station_no, $group_id, $value)
  112. {
  113. $where_group_arr = array('group_id' => $group_id, 'station_no' => $station_no);
  114. $rows = $this->db->select('renum, parked, tot, availables')
  115. ->from('pks_groups')
  116. ->where($where_group_arr)
  117. ->limit(1)
  118. ->get()
  119. ->row_array();
  120. // force upd
  121. $new_renum = 0;
  122. $new_ava = $value;
  123. $new_parked = $rows['tot'] - $new_ava;
  124. trigger_error(__FUNCTION__ . "..{$rows['renum']}|{$rows['parked']}|{$rows['availables']}|{$rows['tot']}..to..{$new_renum}|{$new_parked}|{$new_ava}|{$rows['tot']}");
  125. $this->db->where($where_group_arr)->update('pks_groups', array('renum' => $new_renum, 'parked' => $new_parked, 'availables' => $new_ava));
  126. return $this->db->affected_rows();
  127. }
  128. // 同步 888
  129. public function sync_888($parms)
  130. {
  131. $result = -888;
  132. if(!isset($parms['lpr']) || !isset($parms['etag']) || !isset($parms['io']) ||
  133. ($parms['lpr'] == 'NONE' && $parms['etag'] == 'NONE')
  134. )
  135. {
  136. trigger_error(__FUNCTION__ . '..NONE..'. print_r($parms, true));
  137. return $result;
  138. }
  139. trigger_error(__FUNCTION__ . "..{$parms['sno']}|{$parms['io']}|{$parms['lpr']}|{$parms['etag']}..");
  140. // [START] 擋相同車號進出
  141. $skip_or_not = false;
  142. $new_cars_tmp = array
  143. (
  144. 'timestamp' => time(),
  145. 'sno_io' => $parms['sno'] . $parms['io'],
  146. 'lpr' => $parms['lpr'],
  147. 'etag' => $parms['etag']
  148. );
  149. $cars_tmp_log_arr = $this->vars['mcache']->get(MCACHE_SYNC_888_TMP_LOG);
  150. if(empty($cars_tmp_log_arr))
  151. {
  152. $cars_tmp_log_arr = array();
  153. }
  154. if(isset($cars_tmp_log_arr[$new_cars_tmp['sno_io']]))
  155. {
  156. $last_cars_tmp = $cars_tmp_log_arr[$new_cars_tmp['sno_io']];
  157. // 判斷是否跳過 (記錄於一小時內, 相同場站進出 lpr 或 etag)
  158. if( ( ($last_cars_tmp['lpr'] == $new_cars_tmp['lpr'] && $last_cars_tmp['lpr'] != 'NONE') ||
  159. ($last_cars_tmp['etag'] == $new_cars_tmp['etag'] && $last_cars_tmp['etag'] != 'NONE') ) && $last_cars_tmp['timestamp'] > $new_cars_tmp['timestamp'] - 3600
  160. )
  161. $skip_or_not = true;
  162. }
  163. // 更新
  164. $cars_tmp_log_arr[$new_cars_tmp['sno_io']] = $new_cars_tmp;
  165. $this->vars['mcache']->set(MCACHE_SYNC_888_TMP_LOG, $cars_tmp_log_arr);
  166. trigger_error(__FUNCTION__ . '..upd ' . MCACHE_SYNC_888_TMP_LOG . " |s:{$skip_or_not}|" . print_r($cars_tmp_log_arr, true));
  167. // 跳過
  168. if($skip_or_not)
  169. {
  170. trigger_error(__FUNCTION__ . '..skip..');
  171. return false;
  172. }
  173. // [END] 擋相同車號進出
  174. switch($parms['io'])
  175. {
  176. // 入場
  177. case 'CI':
  178. $result = $this->pks_availables_update(SYNC_PKS_GROUP_ID_CI, -1, false, $parms['sno']);
  179. break;
  180. case 'MI':
  181. $result = $this->pks_availables_update(SYNC_PKS_GROUP_ID_MI, -1, false, $parms['sno']);
  182. break;
  183. // 出場
  184. case 'CO':
  185. $result = $this->pks_availables_update(SYNC_PKS_GROUP_ID_CI, 1, false, $parms['sno']);
  186. break;
  187. case 'MO':
  188. $result = $this->pks_availables_update(SYNC_PKS_GROUP_ID_MI, 1, false, $parms['sno']);
  189. break;
  190. }
  191. return $result;
  192. }
  193. // 微調剩餘車位數
  194. public function pks_availables_update($group_id, $value, $is_renum=true, $station_no=STATION_NO)
  195. {
  196. $where_group_arr = array('group_id' => $group_id, 'station_no' => $station_no);
  197. $rows = $this->db->select('renum, parked, tot')
  198. ->from('pks_groups')
  199. ->where($where_group_arr)
  200. ->limit(1)
  201. ->get()
  202. ->row_array();
  203. $renum = $rows['renum'];
  204. $parked = $rows['parked'];
  205. $tot = $rows['tot'];
  206. trigger_error("更新車位數|{$group_id}|{$value}|{$is_renum}|".print_r($rows, true));
  207. if($is_renum)
  208. {
  209. // 一般微調
  210. if($value == 0)
  211. {
  212. $this->db->where($where_group_arr)
  213. ->update('pks_groups', array('renum' => 0, 'parked' => 0, 'availables' => $tot));
  214. trigger_error(__FUNCTION__ . '..reset everything and exit..');
  215. return true; // 中斷
  216. }
  217. else if($value >= 1)
  218. {
  219. // 增加
  220. $renum = $renum + 1;
  221. }
  222. else
  223. {
  224. // 減少
  225. $renum = $renum - 1;
  226. }
  227. $availables = $tot - $parked + $renum;
  228. // 防止負值
  229. if($availables <= 0)
  230. {
  231. $availables = 0;
  232. $parked = $tot;
  233. $renum = 0;
  234. trigger_error(__FUNCTION__ . '..ava < 0..auto set (ava = 0, parked = tot, renum = 0)..');
  235. }
  236. else if($availables >= $tot)
  237. {
  238. $availables = $tot;
  239. $parked = 0;
  240. $renum = 0;
  241. trigger_error(__FUNCTION__ . '..ava > tot..auto set (ava = tot, parked = 0, renum = 0)..');
  242. }
  243. // 更新 db
  244. $this->db->where($where_group_arr)
  245. ->update('pks_groups', array('parked' => $parked, 'availables' => $availables, 'renum' => $renum));
  246. }
  247. else
  248. {
  249. // 進出場
  250. if($value == 0)
  251. {
  252. trigger_error(__FUNCTION__ . '..??? exit..');
  253. return true; // 中斷
  254. }
  255. else if($value >= 1)
  256. {
  257. // 已停車位數減少, 空車位數增加
  258. $parked = $parked - 1;
  259. }
  260. else
  261. {
  262. // 已停車位數增加, 空車位數減少
  263. $parked = $parked + 1;
  264. }
  265. /*
  266. // 防止負值
  267. if($parked < 0)
  268. {
  269. $parked = 0;
  270. $renum = 0; // 自動重設 renum
  271. trigger_error(__FUNCTION__ . '..parked < 0..set (parked = 0, renum = 0)..');
  272. }
  273. else if($parked >= $tot)
  274. {
  275. $parked = $tot;
  276. $renum = 0; // 自動重設 renum
  277. trigger_error(__FUNCTION__ . '..parked > tot.. = tot..set (parked = tot, renum = 0)..');
  278. }
  279. */
  280. $availables = $tot - $parked + $renum;
  281. // 防止負值
  282. if($availables <= 0)
  283. {
  284. $availables = 0;
  285. $parked = $tot;
  286. $renum = 0;
  287. trigger_error(__FUNCTION__ . '..ava < 0..auto set (ava = 0, parked = tot, renum = 0)..');
  288. }
  289. else if($availables >= $tot)
  290. {
  291. $availables = $tot;
  292. $parked = 0;
  293. $renum = 0;
  294. trigger_error(__FUNCTION__ . '..ava > tot..auto set (ava = tot, parked = 0, renum = 0)..');
  295. }
  296. // 更新 db
  297. $this->db->where($where_group_arr)
  298. ->update('pks_groups', array('parked' => $parked, 'availables' => $availables, 'renum' => $renum));
  299. }
  300. // 送出即時訊號
  301. if($group_id == SYNC_PKS_GROUP_ID_CI)
  302. {
  303. //$this->mq_send(MQ_TOPIC_ALTOB, MQ_ALTOB_888 . ",1,{$availables}" . MQ_ALTOB_888_END_TAG); // 送出 888 (汽車)
  304. }
  305. else if($group_id == SYNC_PKS_GROUP_ID_MI)
  306. {
  307. //$this->mq_send(MQ_TOPIC_ALTOB, MQ_ALTOB_888 . ",2,{$availables}" . MQ_ALTOB_888_END_TAG); // 送出 888 (機車)
  308. }
  309. else
  310. {
  311. $availables_pad = str_pad($availables, 3, '0', STR_PAD_LEFT); // 補零
  312. $this->mq_send(MQ_TOPIC_SUBLEVEL, "{$group_id},{$availables_pad}"); // 送出剩餘車位數給字幕機
  313. }
  314. return $this->db->affected_rows();
  315. }
  316. // ------------------------------------------------
  317. //
  318. // 在席系統同步 (END)
  319. //
  320. // ------------------------------------------------
  321. // ------------------------------------------------
  322. //
  323. // 中控接收端 (START)
  324. //
  325. // ------------------------------------------------
  326. // 同步歐pa卡 (功能: 歐pa卡同步)
  327. public function sync_allpa_user($info_arr=array('station_no_arr' => STATION_NO))
  328. {
  329. require_once(ALTOB_SYNC_FILE);
  330. $sync_agent = new AltobSyncAgent();
  331. $data = $sync_agent->query_allpa_users();
  332. $data_allpa_user_arr = json_decode($data, true);
  333. if (sizeof($data_allpa_user_arr) <= 0)
  334. {
  335. trigger_error(SYNC_DATA_LOG_TITLE . '.. allpa_user empty ..'); // 忽略完全沒會員的情況
  336. return 'empty';
  337. }
  338. $this->db->trans_start();
  339. // 清空
  340. $this->db->empty_table('allpa_user');
  341. // 建立 members
  342. $this->db->insert_batch('allpa_user', $data_allpa_user_arr);
  343. $this->db->trans_complete();
  344. if ($this->db->trans_status() === FALSE)
  345. {
  346. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync allpa_user fail ..'. '| last_query: ' . $this->db->last_query());
  347. return 'fail';
  348. }
  349. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync allpa_user completed ..');
  350. return 'ok';
  351. }
  352. // 同步場站會員 (功能: 會員同步)
  353. public function sync_members($info_arr=array('station_no_arr' => STATION_NO))
  354. {
  355. // 查現況
  356. $parms['station_no_arr'] = $info_arr['station_no_arr'];
  357. try{
  358. $ch = curl_init();
  359. curl_setopt($ch, CURLOPT_URL, SYNC_API_URL . 'member_query_all_in_one');
  360. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  361. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  362. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  363. curl_setopt($ch, CURLOPT_POST, TRUE);
  364. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,10);
  365. curl_setopt($ch, CURLOPT_TIMEOUT, 10); //timeout in seconds
  366. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parms));
  367. $result = curl_exec($ch);
  368. curl_close($ch);
  369. }catch (Exception $e){
  370. trigger_error('error msg:'.$e->getMessage());
  371. trigger_error(SYNC_DATA_LOG_TITLE . $e->getMessage());
  372. }
  373. // 取得資料
  374. $result_arr = json_decode($result, true);
  375. $data_member_arr = array();
  376. $data_car_arr = array();
  377. if (sizeof($result_arr) <= 0)
  378. {
  379. trigger_error(SYNC_DATA_LOG_TITLE . '.. empty ..'); // 忽略完全沒會員的情況
  380. return 'empty';
  381. }
  382. /*
  383. // 每一目標場站, 都建立一份會員清單 20171211 upd
  384. if(isset($info_arr['current_station_no_arr']))
  385. {
  386. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $info_arr['current_station_no_arr']);
  387. foreach($station_no_arr as $station_no)
  388. {
  389. foreach($result_arr as $data)
  390. {
  391. // create member
  392. $data_member = array
  393. (
  394. 'member_no' => $data['member_no'],
  395. 'station_no' => $station_no,
  396. 'member_name' => $data['member_name'],
  397. 'member_attr' => $data['member_attr'],
  398. 'member_type' => $data['member_type'],
  399. 'contract_no' => $data['contract_no'],
  400. 'mobile_no' => $data['mobile_no'],
  401. 'lpr' => $data['lpr'],
  402. 'locked' => $data['locked'],
  403. 'etag' => $data['etag'],
  404. 'fee_period' => $data['fee_period'],
  405. 'start_date' => $data['start_date'],
  406. 'end_date' => $data['end_date'],
  407. 'remarks' => $data['remarks'],
  408. 'park_time' => $data['park_time'],
  409. 'valid_time' => $data['valid_time']
  410. );
  411. array_push($data_member_arr, $data_member);
  412. // create member_car
  413. $data_car = array
  414. (
  415. 'station_no' => $station_no,
  416. 'member_no' => $data['member_no'],
  417. 'lpr' => $data['lpr'],
  418. 'lpr_correct' => $data['lpr'],
  419. 'etag' => $data['etag'],
  420. 'start_time' => $data['start_date'],
  421. 'end_time' => $data['end_date']
  422. );
  423. array_push($data_car_arr, $data_car);
  424. }
  425. }
  426. }
  427. */
  428. // 暫時版
  429. foreach($result_arr as $data)
  430. {
  431. // create member
  432. $data_member = array
  433. (
  434. 'member_no' => $data['member_no'],
  435. 'station_no' => $data['station_no'],
  436. 'member_name' => $data['member_name'],
  437. 'member_attr' => $data['member_attr'],
  438. 'member_type' => $data['member_type'],
  439. 'contract_no' => $data['contract_no'],
  440. 'mobile_no' => $data['mobile_no'],
  441. 'lpr' => $data['lpr'],
  442. 'locked' => $data['locked'],
  443. 'etag' => $data['etag'],
  444. 'fee_period' => $data['fee_period'],
  445. 'start_date' => $data['start_date'],
  446. 'end_date' => $data['end_date'],
  447. 'remarks' => $data['remarks'],
  448. 'park_time' => $data['park_time'],
  449. 'valid_time' => $data['valid_time']
  450. );
  451. array_push($data_member_arr, $data_member);
  452. // create member_car
  453. $data_car = array
  454. (
  455. 'station_no' => $data['station_no'],
  456. 'member_no' => $data['member_no'],
  457. 'lpr' => $data['lpr'],
  458. 'lpr_correct' => $data['lpr'],
  459. 'etag' => $data['etag'],
  460. 'start_time' => $data['start_date'],
  461. 'end_time' => $data['end_date']
  462. );
  463. array_push($data_car_arr, $data_car);
  464. }
  465. //trigger_error(SYNC_DATA_LOG_TITLE . '.. test ..' . print_r($data_member_arr, true));
  466. $this->db->trans_start();
  467. // 清空
  468. $this->db->empty_table('members');
  469. $this->db->empty_table('member_car');
  470. // 建立 members
  471. $this->db->insert_batch('members', $data_member_arr);
  472. // 建立 member_car
  473. $this->db->insert_batch('member_car', $data_car_arr);
  474. $this->db->trans_complete();
  475. if ($this->db->trans_status() === FALSE)
  476. {
  477. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync fail ..'. '| last_query: ' . $this->db->last_query());
  478. return 'fail';
  479. }
  480. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync completed ..');
  481. return 'ok';
  482. }
  483. // 同步車牌更換 (功能: 換車牌同步)
  484. public function sync_switch_lpr($switch_lpr_arr)
  485. {
  486. trigger_error( __FUNCTION__ . '..' . print_r($switch_lpr_arr, true));
  487. $this->db->trans_start();
  488. foreach($switch_lpr_arr as $data)
  489. {
  490. $station_no = $data['station_no'];
  491. $member_no = $data['member_no'];
  492. $old_lpr = $data['old_lpr'];
  493. $new_lpr = $data['new_lpr'];
  494. $new_data = array('lpr' => $new_lpr, 'lpr_correct' => $new_lpr, 'member_no' => $member_no);
  495. $this->db->update('etag_lpr', $new_data, array('lpr_correct' => $old_lpr));
  496. $affect_rows = $this->db->affected_rows();
  497. trigger_error(SYNC_DATA_LOG_TITLE . "換車牌更新 etag_lpr 共[{$affect_rows}]筆..".print_r($data, true));
  498. /*
  499. if($station_no == STATION_NO)
  500. {
  501. $new_data = array('lpr' => $new_lpr, 'lpr_correct' => $new_lpr, 'member_no' => $member_no);
  502. $this->db->update('etag_lpr', $new_data, array('lpr_correct' => $old_lpr));
  503. $affect_rows = $this->db->affected_rows();
  504. trigger_error(SYNC_DATA_LOG_TITLE . "換車牌更新 etag_lpr 共[{$affect_rows}]筆..".print_r($data, true));
  505. }
  506. else
  507. {
  508. trigger_error(SYNC_DATA_LOG_TITLE . __FUNCTION__ . "..資料異常..".print_r($data, true));
  509. }
  510. */
  511. }
  512. $this->db->trans_complete();
  513. if ($this->db->trans_status() === FALSE)
  514. {
  515. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync fail ..'. '| last_query: ' . $this->db->last_query());
  516. return 'fail';
  517. }
  518. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync completed ..');
  519. return 'ok';
  520. }
  521. // 同步場站費率
  522. public function sync_price_plan($info_arr=array('station_no_arr' => STATION_NO))
  523. {
  524. try{
  525. // 查另一台主機
  526. $ch = curl_init();
  527. curl_setopt($ch, CURLOPT_URL, SYNC_API_URL . 'price_plan_query_all_in_one');
  528. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  529. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  530. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  531. curl_setopt($ch, CURLOPT_POST, TRUE);
  532. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5);
  533. curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds
  534. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($info_arr));
  535. $data = curl_exec($ch);
  536. curl_close($ch);
  537. }catch (Exception $e){
  538. trigger_error('error msg:'.$e->getMessage());
  539. }
  540. $decode_result = json_decode($data, true);
  541. if (sizeof($decode_result) <= 0) return "empty";
  542. $this->db->trans_start();
  543. foreach ($decode_result as $key => $value)
  544. {
  545. $station_no = $value["station_no"];
  546. $tx_price_plan_id = $value["txid"];
  547. $tx_type = $value["tx_type"];
  548. $price_plan_data = array
  549. (
  550. 'station_no' => $station_no,
  551. 'tx_type' => $tx_type,
  552. 'remarks' => $value['remarks'],
  553. 'price_plan' => $value['price_plan'],
  554. 'start_time' => $value['start_time'],
  555. 'valid_time' => $value['valid_time']
  556. );
  557. // 刪除
  558. $this->db->delete('tx_price_plan', array('station_no' => $station_no, 'tx_type' => $tx_type));
  559. // 新增
  560. $this->db->insert('tx_price_plan', $price_plan_data);
  561. }
  562. $this->db->trans_complete();
  563. return "ok";
  564. }
  565. // 取得最新未結清 (功能: 行動支付)
  566. public function get_last_unbalanced_cario($lpr, $station_no)
  567. {
  568. $sql = "SELECT station_no, cario_no, in_time, pay_time, out_time, out_before_time, member_no
  569. FROM cario
  570. WHERE
  571. station_no = '{$station_no}' AND obj_id = '{$lpr}' AND finished = 0 AND err = 0
  572. ORDER BY cario_no DESC
  573. LIMIT 1
  574. ";
  575. $results = $this->db->query($sql)->result_array();
  576. if(isset($results[0]))
  577. return $results[0];
  578. return false;
  579. }
  580. // 同步車位現況 (功能: 888 同步, 在席同步)
  581. public function sync_pks_groups_reload($station_setting)
  582. {
  583. $info = array();
  584. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_setting['station_no']);
  585. $station_name_arr = explode(SYNC_DELIMITER_ST_NAME, $station_setting['station_name']);
  586. $station_888_arr = explode(SYNC_DELIMITER_ST_INFO, $station_setting['station_888']);
  587. foreach($station_no_arr as $key => $station_no)
  588. {
  589. if($station_888_arr[$key] == 1) // 啟用
  590. array_push($info, array('station_no' => $station_no_arr[$key], 'station_name' => $station_name_arr[$key]));
  591. else if($station_888_arr[$key] == 4) // 關閉
  592. {
  593. // 清除 888
  594. /*
  595. $clean_888_arr = array(
  596. 'station_no' => $station_no_arr[$key],
  597. 'group_id in ' => "('". SYNC_PKS_GROUP_ID_CI . "', '". SYNC_PKS_GROUP_ID_MI . "')"
  598. );
  599. */
  600. $this->db
  601. ->where('station_no', $station_no_arr[$key])
  602. ->where_in('group_id', array(SYNC_PKS_GROUP_ID_CI, SYNC_PKS_GROUP_ID_MI))
  603. ->delete('pks_groups');
  604. trigger_error(__FUNCTION__ . '..clean pks_groups 888..');
  605. }
  606. else
  607. {
  608. trigger_error(__FUNCTION__ . '..unknown station_888:' . $station_888_arr[$key]);
  609. }
  610. }
  611. if(empty($info))
  612. return 'none';
  613. return $this->sync_pks_groups($info, true);
  614. }
  615. // 同步車位現況 (功能: 888 同步, 在席同步)
  616. public function sync_pks_groups($info_arr=array(array('station_no' => STATION_NO, 'station_name' => STATION_NAME)), $reload=false)
  617. {
  618. if($reload)
  619. {
  620. // 確認應該要有的 pks_groups
  621. $pks_groups_arr = array();
  622. $pks_groups_name_arr = array();
  623. foreach($info_arr as $data)
  624. {
  625. $pks_key = $data['station_no'] . SYNC_DELIMITER_ST_INFO . SYNC_PKS_GROUP_ID_CI;
  626. array_push($pks_groups_arr, $pks_key); // 汽車 888
  627. $pks_key = $data['station_no'] . SYNC_DELIMITER_ST_INFO . SYNC_PKS_GROUP_ID_MI;
  628. array_push($pks_groups_arr, $pks_key); // 機車 888
  629. $pks_groups_name_arr[$data['station_no']] = $data['station_name']. '(888)'; // 群組名稱
  630. }
  631. // 過濾已存在的部份
  632. $sql = "SELECT station_no, group_id FROM pks_groups";
  633. $current_pks_group = $this->db->query($sql)->result_array();
  634. foreach($current_pks_group as $data)
  635. {
  636. $pks_key = $data['station_no'] . SYNC_DELIMITER_ST_INFO . $data['group_id'];
  637. $key = array_search($pks_key, $pks_groups_arr);
  638. if($key !== false)
  639. unset($pks_groups_arr[$key]);
  640. }
  641. // 建立缺少的部份
  642. if(!empty($pks_groups_arr))
  643. {
  644. // [A.開始]
  645. $this->db->trans_start();
  646. foreach($pks_groups_arr as $new_data)
  647. {
  648. $pks_info = explode(SYNC_DELIMITER_ST_INFO, $new_data);
  649. $new_pks_groups_data = array(
  650. 'station_no' => $pks_info[0],
  651. 'group_id' => $pks_info[1],
  652. 'tot' => 100, // 預設車位數
  653. 'availables' => 100, // 預設車位數
  654. 'floors' => 'TOT',
  655. 'group_name' => $pks_groups_name_arr[$pks_info[0]]
  656. );
  657. $this->db->insert('pks_groups', $new_pks_groups_data);
  658. trigger_error(__FUNCTION__ . '..insert pks_groups..'. print_r($new_pks_groups_data, true));
  659. }
  660. // [C.完成]
  661. $this->db->trans_complete();
  662. if ($this->db->trans_status() === FALSE)
  663. {
  664. trigger_error(__FUNCTION__ . '..trans_error..' . '| last_query: ' . $this->db->last_query());
  665. return 'fail'; // 中斷
  666. }
  667. }
  668. }
  669. $sql = "SELECT pks_groups.station_no,
  670. pks_groups.group_name as group_name, pks_groups.tot as tot, pks_groups.parked as parked, pks_groups.availables as availables, pks_groups.group_id as group_id, pks_groups.renum as renum
  671. FROM pks_groups
  672. ORDER BY pks_groups.group_id DESC";
  673. $pks_group_query_data = $this->db->query($sql)->result_array();
  674. //trigger_error(__FUNCTION__ . '..sync..' . print_r($pks_group_query_data, true));
  675. // 同步
  676. require_once(ALTOB_SYNC_FILE);
  677. $sync_agent = new AltobSyncAgent();
  678. $sync_agent->init(STATION_NO); // 已帶上的資料場站編號為主
  679. $sync_result = $sync_agent->upd_pks_groups(json_encode($pks_group_query_data, JSON_UNESCAPED_UNICODE));
  680. trigger_error( SYNC_DATA_LOG_TITLE . '..'. __FUNCTION__ . "..upd_pks_groups.." . $sync_result);
  681. return $sync_result;
  682. }
  683. // 重新載入場站設定
  684. public function reload_station_setting()
  685. {
  686. $port_info = (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 60123) ? '/' . $_SERVER['SERVER_PORT'] : '';
  687. $reload_url = SYNC_API_URL . 'station_setting_query' . $port_info;
  688. try{
  689. // 查現況
  690. $ch = curl_init();
  691. curl_setopt($ch, CURLOPT_URL, $reload_url);
  692. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  693. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  694. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  695. curl_setopt($ch, CURLOPT_POST, TRUE);
  696. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,10);
  697. curl_setopt($ch, CURLOPT_TIMEOUT, 10); //timeout in seconds
  698. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array()));
  699. $return_result = curl_exec($ch);
  700. curl_close($ch);
  701. trigger_error(__FUNCTION__ . "..curl {$reload_url}.." . print_r($return_result, true));
  702. }catch (Exception $e){
  703. trigger_error('error msg:'.$e->getMessage());
  704. }
  705. $station_setting_result = json_decode($return_result, true);
  706. if(!isset($station_setting_result['results']) || sizeof($station_setting_result['results']) <= 0)
  707. {
  708. trigger_error(__FUNCTION__ . '..fail..' . print_r($return_result, true));
  709. return 'fail';
  710. }
  711. $station_ip_str = $station_setting_result['station_ip']; // 場站目前對外IP
  712. if(empty($port_info))
  713. $station_port_str = '80';
  714. else
  715. $station_port_str = substr($port_info, 1); // 場站目前對外PORT
  716. $station_setting_arr = $station_setting_result['results'];
  717. $station_no_arr = array();
  718. $station_no_list_arr = array();
  719. $station_name_arr = array();
  720. $station_888_arr = array();
  721. $settings = array();
  722. foreach($station_setting_arr as $data)
  723. {
  724. $station_no = $data['station_no'];
  725. array_push($station_no_arr, $station_no);
  726. array_push($station_name_arr, $data['short_name']);
  727. array_push($station_888_arr, $data['station_888']);
  728. // 若有設定多個場站, 已多場站設定為主
  729. $tmp_station_no_list_arr = explode(SYNC_DELIMITER_ST_NO, $data['station_no_list']);
  730. foreach($tmp_station_no_list_arr as $key => $station_no_list_value)
  731. {
  732. if(intval($station_no_list_value) > 0)
  733. {
  734. if(empty($station_no_list_arr))
  735. array_push($station_no_list_arr, $station_no_list_value);
  736. else if(!array_search($station_no_list_value, $station_no_list_arr))
  737. array_push($station_no_list_arr, $station_no_list_value);
  738. }
  739. }
  740. // 若無設定直接放目前場站
  741. if(empty($station_no_list_arr))
  742. array_push($station_no_list_arr, $station_no);
  743. if(!isset($settings[$station_no]))
  744. {
  745. $settings[$station_no] = array();
  746. }
  747. $settings[$station_no]['free_time'] = empty($data['free_time']) ? 0 : $data['free_time']; // 20180205 免費入場 (minutes)
  748. $settings[$station_no]['station_888'] = empty($data['station_888']) ? 1 : $data['station_888'];
  749. $settings[$station_no]['mqtt_ip'] = empty($data['mqtt_ip']) ? MQ_HOST : $data['mqtt_ip'];
  750. $settings[$station_no]['mqtt_port'] = empty($data['mqtt_port']) ? MQ_PORT : $data['mqtt_port'];
  751. $settings[$station_no]['local_ip'] = empty($data['local_ip']) ? STATION_LOCAL_IP : $data['local_ip'];
  752. }
  753. $station_no_str = implode(SYNC_DELIMITER_ST_NO, $station_no_arr); // 取值時會用到
  754. $station_no_list_str = implode(SYNC_DELIMITER_ST_NO, $station_no_list_arr); // 會員資料同步相關
  755. $station_name_str = implode(SYNC_DELIMITER_ST_NAME, $station_name_arr); // 純顯示
  756. $station_888_str = implode(SYNC_DELIMITER_ST_INFO, $station_888_arr); // 場站 888 設定
  757. // 設定到 mcache
  758. $this->vars['mcache']->set(MCACHE_STATION_NO_STR, $station_no_str);
  759. $this->vars['mcache']->set(MCACHE_STATION_NO_LIST_STR, $station_no_list_str);
  760. $this->vars['mcache']->set(MCACHE_STATION_NAME_STR, $station_name_str);
  761. $this->vars['mcache']->set(MCACHE_STATION_IP_STR, $station_ip_str);
  762. $this->vars['mcache']->set(MCACHE_STATION_PORT_STR, $station_port_str);
  763. $this->vars['mcache']->set(MCACHE_STATION_888_STR, $station_888_str);
  764. $this->vars['mcache']->set(MCACHE_STATION_SETTINGS, $settings);
  765. return 'ok';
  766. }
  767. // 取得目前場站設定
  768. public function station_setting_query($reload=false)
  769. {
  770. $station_no_str = $this->vars['mcache']->get(MCACHE_STATION_NO_STR);
  771. $station_no_list_str = $this->vars['mcache']->get(MCACHE_STATION_NO_LIST_STR);
  772. $station_name_str = $this->vars['mcache']->get(MCACHE_STATION_NAME_STR);
  773. $station_ip_str = $this->vars['mcache']->get(MCACHE_STATION_IP_STR);
  774. $station_port_str = $this->vars['mcache']->get(MCACHE_STATION_PORT_STR);
  775. $station_888_str = $this->vars['mcache']->get(MCACHE_STATION_888_STR);
  776. $settings = $this->vars['mcache']->get(MCACHE_STATION_SETTINGS);
  777. if( $reload ||
  778. empty($station_no_list_str) ||
  779. empty($station_no_str) || empty($station_name_str) ||
  780. empty($station_ip_str) || empty($station_port_str) ||
  781. empty($station_888_str) ||
  782. empty($settings)
  783. )
  784. {
  785. $result = $this->reload_station_setting();
  786. if($result == 'ok')
  787. {
  788. $station_no_str = $this->vars['mcache']->get(MCACHE_STATION_NO_STR);
  789. $station_no_list_str = $this->vars['mcache']->get(MCACHE_STATION_NO_LIST_STR);
  790. $station_name_str = $this->vars['mcache']->get(MCACHE_STATION_NAME_STR);
  791. $station_ip_str = $this->vars['mcache']->get(MCACHE_STATION_IP_STR);
  792. $station_port_str = $this->vars['mcache']->get(MCACHE_STATION_PORT_STR);
  793. $station_888_str = $this->vars['mcache']->get(MCACHE_STATION_888_STR);
  794. $settings = $this->vars['mcache']->get(MCACHE_STATION_SETTINGS);
  795. }
  796. else
  797. {
  798. /*
  799. $station_setting = array();
  800. $station_setting['station_no'] = STATION_NO;
  801. $station_setting['station_name'] = STATION_NAME;
  802. $station_setting['station_ip'] = STATION_IP;
  803. return $station_setting;
  804. */
  805. return false;
  806. }
  807. }
  808. // 第一個場站編號
  809. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_no_str);
  810. $first_station_no = $station_no_arr[0];
  811. $station_setting = array();
  812. $station_setting['station_no'] = $station_no_str;
  813. $station_setting['station_no_list'] = $station_no_list_str;
  814. $station_setting['station_name'] = $station_name_str;
  815. $station_setting['station_ip'] = $station_ip_str;
  816. $station_setting['station_port'] = $station_port_str;
  817. $station_setting['station_888'] = $station_888_str;
  818. $station_setting['settings'] = $settings;
  819. $station_setting['mqtt_ip'] = $settings[$first_station_no]['mqtt_ip'];
  820. $station_setting['mqtt_port'] = $settings[$first_station_no]['mqtt_port'];
  821. return $station_setting;
  822. }
  823. // ------------------------------------------------
  824. //
  825. // 中控接收端 (END)
  826. //
  827. // ------------------------------------------------
  828. // ------------------------------------------------
  829. //
  830. // 其它 (START)
  831. //
  832. // ------------------------------------------------
  833. // 手動新增入場資料
  834. public function gen_carin($parms)
  835. {
  836. $in_time = date('Y-m-d H:i:s');
  837. $data = array(
  838. 'station_no' => $parms['sno'],
  839. 'obj_type' => 1,
  840. 'obj_id' => $parms['lpr'],
  841. 'etag' => '',
  842. 'in_out' => $parms['io'],
  843. 'finished' => 0,
  844. 'in_time' => $in_time,
  845. 'in_lane' => $parms['ivsno'],
  846. 'out_before_time' => $in_time
  847. );
  848. $this->db->insert('cario', $data);
  849. trigger_error("新增入場資料:".print_r($parms, true));
  850. require_once(ALTOB_SYNC_FILE);
  851. // 傳送進場記錄
  852. $sync_agent = new AltobSyncAgent();
  853. $sync_agent->init($parms['sno'], $in_time);
  854. $sync_agent->cario_no = $this->db->insert_id(); // 進出編號
  855. $sync_result = $sync_agent->sync_st_in($parms);
  856. trigger_error( SYNC_DATA_LOG_TITLE . '..'. __FUNCTION__ . "..sync_st_in.." . $sync_result);
  857. }
  858. // ------------------------------------------------
  859. //
  860. // 其它 (END)
  861. //
  862. // ------------------------------------------------
  863. }