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.

909 lines
29KB

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