VM暫存
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

929 lines
30KB

  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. $data_member_arr = array();
  356. $data_car_arr = array();
  357. try{
  358. // 查現況
  359. $ch = curl_init();
  360. curl_setopt($ch, CURLOPT_URL, SYNC_API_URL . 'member_query_all_in_one');
  361. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  362. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  363. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  364. curl_setopt($ch, CURLOPT_POST, TRUE);
  365. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,10);
  366. curl_setopt($ch, CURLOPT_TIMEOUT, 10); //timeout in seconds
  367. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($info_arr));
  368. $data = curl_exec($ch);
  369. curl_close($ch);
  370. }catch (Exception $e){
  371. trigger_error('error msg:'.$e->getMessage());
  372. trigger_error(SYNC_DATA_LOG_TITLE . $e->getMessage());
  373. }
  374. $data_member_arr = json_decode($data, true);
  375. if (sizeof($data_member_arr) <= 0)
  376. {
  377. trigger_error(SYNC_DATA_LOG_TITLE . '.. empty ..'); // 忽略完全沒會員的情況
  378. return 'empty';
  379. }
  380. else
  381. {
  382. foreach($data_member_arr as $data)
  383. {
  384. // create member_car
  385. $data_car = array
  386. (
  387. 'station_no' => $data['station_no'],
  388. 'member_no' => $data['member_no'],
  389. 'lpr' => $data['lpr'],
  390. 'lpr_correct' => $data['lpr'],
  391. 'etag' => $data['etag'],
  392. 'start_time' => $data['start_date'],
  393. 'end_time' => $data['end_date']
  394. );
  395. array_push($data_car_arr, $data_car);
  396. }
  397. }
  398. //trigger_error(SYNC_DATA_LOG_TITLE . '.. test ..' . print_r($data_member_arr, true));
  399. $this->db->trans_start();
  400. // 清空
  401. $this->db->empty_table('members');
  402. $this->db->empty_table('member_car');
  403. // 建立 members
  404. $this->db->insert_batch('members', $data_member_arr);
  405. // 建立 member_car
  406. $this->db->insert_batch('member_car', $data_car_arr);
  407. $this->db->trans_complete();
  408. if ($this->db->trans_status() === FALSE)
  409. {
  410. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync fail ..'. '| last_query: ' . $this->db->last_query());
  411. return 'fail';
  412. }
  413. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync completed ..');
  414. return 'ok';
  415. }
  416. // 同步車牌更換 (功能: 換車牌同步)
  417. public function sync_switch_lpr($switch_lpr_arr)
  418. {
  419. trigger_error( __FUNCTION__ . '..' . print_r($switch_lpr_arr, true));
  420. $this->db->trans_start();
  421. foreach($switch_lpr_arr as $data)
  422. {
  423. $station_no = $data['station_no'];
  424. $member_no = $data['member_no'];
  425. $old_lpr = $data['old_lpr'];
  426. $new_lpr = $data['new_lpr'];
  427. $new_data = array('lpr' => $new_lpr, 'lpr_correct' => $new_lpr, 'member_no' => $member_no);
  428. $this->db->update('etag_lpr', $new_data, array('lpr_correct' => $old_lpr));
  429. $affect_rows = $this->db->affected_rows();
  430. trigger_error(SYNC_DATA_LOG_TITLE . "換車牌更新 etag_lpr 共[{$affect_rows}]筆..".print_r($data, true));
  431. /*
  432. if($station_no == STATION_NO)
  433. {
  434. $new_data = array('lpr' => $new_lpr, 'lpr_correct' => $new_lpr, 'member_no' => $member_no);
  435. $this->db->update('etag_lpr', $new_data, array('lpr_correct' => $old_lpr));
  436. $affect_rows = $this->db->affected_rows();
  437. trigger_error(SYNC_DATA_LOG_TITLE . "換車牌更新 etag_lpr 共[{$affect_rows}]筆..".print_r($data, true));
  438. }
  439. else
  440. {
  441. trigger_error(SYNC_DATA_LOG_TITLE . __FUNCTION__ . "..資料異常..".print_r($data, true));
  442. }
  443. */
  444. }
  445. $this->db->trans_complete();
  446. if ($this->db->trans_status() === FALSE)
  447. {
  448. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync fail ..'. '| last_query: ' . $this->db->last_query());
  449. return 'fail';
  450. }
  451. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync completed ..');
  452. return 'ok';
  453. }
  454. // 同步場站費率
  455. public function sync_price_plan($info_arr=array('station_no_arr' => STATION_NO))
  456. {
  457. try{
  458. // 查另一台主機
  459. $ch = curl_init();
  460. curl_setopt($ch, CURLOPT_URL, SYNC_API_URL . 'price_plan_query_all_in_one');
  461. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  462. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  463. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  464. curl_setopt($ch, CURLOPT_POST, TRUE);
  465. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5);
  466. curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds
  467. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($info_arr));
  468. $data = curl_exec($ch);
  469. curl_close($ch);
  470. }catch (Exception $e){
  471. trigger_error('error msg:'.$e->getMessage());
  472. }
  473. $decode_result = json_decode($data, true);
  474. if (sizeof($decode_result) <= 0) return "empty";
  475. $this->db->trans_start();
  476. foreach ($decode_result as $key => $value)
  477. {
  478. $station_no = $value["station_no"];
  479. $tx_price_plan_id = $value["txid"];
  480. $tx_type = $value["tx_type"];
  481. $price_plan_data = array
  482. (
  483. 'station_no' => $station_no,
  484. 'tx_type' => $tx_type,
  485. 'remarks' => $value['remarks'],
  486. 'price_plan' => $value['price_plan'],
  487. 'start_time' => $value['start_time'],
  488. 'valid_time' => $value['valid_time']
  489. );
  490. // 刪除
  491. $this->db->delete('tx_price_plan', array('station_no' => $station_no, 'tx_type' => $tx_type));
  492. // 新增
  493. $this->db->insert('tx_price_plan', $price_plan_data);
  494. }
  495. $this->db->trans_complete();
  496. return "ok";
  497. }
  498. // 取得最新未結清 (功能: 行動支付)
  499. public function get_last_unbalanced_cario($lpr, $station_no)
  500. {
  501. $sql = "SELECT station_no, cario_no, in_time, pay_time, out_time, out_before_time, member_no
  502. FROM cario
  503. WHERE
  504. station_no = '{$station_no}' AND obj_id = '{$lpr}' AND finished = 0 AND err = 0
  505. ORDER BY cario_no DESC
  506. LIMIT 1
  507. ";
  508. $results = $this->db->query($sql)->result_array();
  509. if(isset($results[0]))
  510. return $results[0];
  511. return false;
  512. }
  513. // 同步車位現況 (功能: 888 同步, 在席同步)
  514. public function sync_pks_groups_reload($station_setting)
  515. {
  516. $info = array();
  517. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_setting['station_no']);
  518. $station_name_arr = explode(SYNC_DELIMITER_ST_NAME, $station_setting['station_name']);
  519. $station_888_arr = explode(SYNC_DELIMITER_ST_INFO, $station_setting['station_888']);
  520. foreach($station_no_arr as $key => $station_no)
  521. {
  522. if($station_888_arr[$key] == 1) // 啟用
  523. array_push($info, array('station_no' => $station_no_arr[$key], 'station_name' => $station_name_arr[$key]));
  524. else if($station_888_arr[$key] == 4) // 關閉
  525. {
  526. // 清除 888
  527. /*
  528. $clean_888_arr = array(
  529. 'station_no' => $station_no_arr[$key],
  530. 'group_id in ' => "('". SYNC_PKS_GROUP_ID_CI . "', '". SYNC_PKS_GROUP_ID_MI . "')"
  531. );
  532. */
  533. $this->db
  534. ->where('station_no', $station_no_arr[$key])
  535. ->where_in('group_id', array(SYNC_PKS_GROUP_ID_CI, SYNC_PKS_GROUP_ID_MI))
  536. ->delete('pks_groups');
  537. trigger_error(__FUNCTION__ . '..clean pks_groups 888..');
  538. }
  539. else
  540. {
  541. trigger_error(__FUNCTION__ . '..unknown station_888:' . $station_888_arr[$key]);
  542. }
  543. }
  544. if(empty($info))
  545. return 'none';
  546. return $this->sync_pks_groups($info, true);
  547. }
  548. // 同步車位現況 (功能: 888 同步, 在席同步)
  549. public function sync_pks_groups($info_arr=array(array('station_no' => STATION_NO, 'station_name' => STATION_NAME)), $reload=false)
  550. {
  551. if($reload)
  552. {
  553. // 確認應該要有的 pks_groups
  554. $pks_groups_arr = array();
  555. $pks_groups_name_arr = array();
  556. foreach($info_arr as $data)
  557. {
  558. $pks_key = $data['station_no'] . SYNC_DELIMITER_ST_INFO . SYNC_PKS_GROUP_ID_CI;
  559. array_push($pks_groups_arr, $pks_key); // 汽車 888
  560. $pks_key = $data['station_no'] . SYNC_DELIMITER_ST_INFO . SYNC_PKS_GROUP_ID_MI;
  561. array_push($pks_groups_arr, $pks_key); // 機車 888
  562. $pks_groups_name_arr[$data['station_no']] = $data['station_name']. '(888)'; // 群組名稱
  563. }
  564. // 過濾已存在的部份
  565. $sql = "SELECT station_no, group_id FROM pks_groups";
  566. $current_pks_group = $this->db->query($sql)->result_array();
  567. foreach($current_pks_group as $data)
  568. {
  569. $pks_key = $data['station_no'] . SYNC_DELIMITER_ST_INFO . $data['group_id'];
  570. $key = array_search($pks_key, $pks_groups_arr);
  571. if($key !== false)
  572. unset($pks_groups_arr[$key]);
  573. }
  574. // 建立缺少的部份
  575. if(!empty($pks_groups_arr))
  576. {
  577. // [A.開始]
  578. $this->db->trans_start();
  579. foreach($pks_groups_arr as $new_data)
  580. {
  581. $pks_info = explode(SYNC_DELIMITER_ST_INFO, $new_data);
  582. $new_pks_groups_data = array(
  583. 'station_no' => $pks_info[0],
  584. 'group_id' => $pks_info[1],
  585. 'tot' => 100, // 預設車位數
  586. 'availables' => 100, // 預設車位數
  587. 'floors' => 'TOT',
  588. 'group_name' => $pks_groups_name_arr[$pks_info[0]]
  589. );
  590. $this->db->insert('pks_groups', $new_pks_groups_data);
  591. trigger_error(__FUNCTION__ . '..insert pks_groups..'. print_r($new_pks_groups_data, true));
  592. }
  593. // [C.完成]
  594. $this->db->trans_complete();
  595. if ($this->db->trans_status() === FALSE)
  596. {
  597. trigger_error(__FUNCTION__ . '..trans_error..' . '| last_query: ' . $this->db->last_query());
  598. return 'fail'; // 中斷
  599. }
  600. }
  601. }
  602. $sql = "SELECT pks_groups.station_no,
  603. 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
  604. FROM pks_groups
  605. ORDER BY pks_groups.group_id DESC";
  606. $pks_group_query_data = $this->db->query($sql)->result_array();
  607. //trigger_error(__FUNCTION__ . '..sync..' . print_r($pks_group_query_data, true));
  608. // 同步
  609. require_once(ALTOB_SYNC_FILE);
  610. $sync_agent = new AltobSyncAgent();
  611. $sync_agent->init(STATION_NO); // 已帶上的資料場站編號為主
  612. $sync_result = $sync_agent->upd_pks_groups(json_encode($pks_group_query_data, JSON_UNESCAPED_UNICODE));
  613. trigger_error( SYNC_DATA_LOG_TITLE . '..'. __FUNCTION__ . "..upd_pks_groups.." . $sync_result);
  614. return $sync_result;
  615. }
  616. // 重新載入場站設定
  617. public function reload_station_setting()
  618. {
  619. $port_info = (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 60123) ? '/' . $_SERVER['SERVER_PORT'] : '';
  620. $reload_url = SYNC_API_URL . 'station_setting_query' . $port_info;
  621. try{
  622. // 查現況
  623. $ch = curl_init();
  624. curl_setopt($ch, CURLOPT_URL, $reload_url);
  625. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  626. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  627. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  628. curl_setopt($ch, CURLOPT_POST, TRUE);
  629. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,10);
  630. curl_setopt($ch, CURLOPT_TIMEOUT, 10); //timeout in seconds
  631. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array()));
  632. $return_result = curl_exec($ch);
  633. curl_close($ch);
  634. trigger_error(__FUNCTION__ . "..curl {$reload_url}.." . print_r($return_result, true));
  635. }catch (Exception $e){
  636. trigger_error('error msg:'.$e->getMessage());
  637. }
  638. $station_setting_result = json_decode($return_result, true);
  639. if(!isset($station_setting_result['results']) || sizeof($station_setting_result['results']) <= 0)
  640. {
  641. trigger_error(__FUNCTION__ . '..fail..' . print_r($return_result, true));
  642. return 'fail';
  643. }
  644. $station_ip_str = $station_setting_result['station_ip']; // 場站目前對外IP
  645. if(empty($port_info))
  646. $station_port_str = '80';
  647. else
  648. $station_port_str = substr($port_info, 1); // 場站目前對外PORT
  649. $station_setting_arr = $station_setting_result['results'];
  650. $station_no_arr = array();
  651. $station_no_list_arr = array();
  652. $station_name_arr = array();
  653. $station_888_arr = array();
  654. $settings = array();
  655. foreach($station_setting_arr as $data)
  656. {
  657. $station_no = $data['station_no'];
  658. array_push($station_no_arr, $station_no);
  659. array_push($station_name_arr, $data['short_name']);
  660. array_push($station_888_arr, $data['station_888']);
  661. // 若有設定多個場站, 已多場站設定為主
  662. $tmp_station_no_list_arr = explode(SYNC_DELIMITER_ST_NO, $data['station_no_list']);
  663. foreach($tmp_station_no_list_arr as $key => $station_no_list_value)
  664. {
  665. if(intval($station_no_list_value) > 0)
  666. {
  667. if(empty($station_no_list_arr))
  668. array_push($station_no_list_arr, $station_no_list_value);
  669. else if(!array_search($station_no_list_value, $station_no_list_arr))
  670. array_push($station_no_list_arr, $station_no_list_value);
  671. }
  672. }
  673. // 若無設定直接放目前場站
  674. if(empty($station_no_list_arr))
  675. array_push($station_no_list_arr, $station_no);
  676. if(!isset($settings[$station_no]))
  677. {
  678. $settings[$station_no] = array();
  679. }
  680. $settings[$station_no]['station_888'] = empty($data['station_888']) ? 1 : $data['station_888'];
  681. $settings[$station_no]['mqtt_ip'] = empty($data['mqtt_ip']) ? MQ_HOST : $data['mqtt_ip'];
  682. $settings[$station_no]['mqtt_port'] = empty($data['mqtt_port']) ? MQ_PORT : $data['mqtt_port'];
  683. $settings[$station_no]['local_ip'] = empty($data['local_ip']) ? STATION_LOCAL_IP : $data['local_ip'];
  684. }
  685. $station_no_str = implode(SYNC_DELIMITER_ST_NO, $station_no_arr); // 取值時會用到
  686. $station_no_list_str = implode(SYNC_DELIMITER_ST_NO, $station_no_list_arr); // 會員資料同步相關
  687. $station_name_str = implode(SYNC_DELIMITER_ST_NAME, $station_name_arr); // 純顯示
  688. $station_888_str = implode(SYNC_DELIMITER_ST_INFO, $station_888_arr); // 場站 888 設定
  689. // 設定到 mcache
  690. $this->vars['mcache']->set(MCACHE_STATION_NO_STR, $station_no_str);
  691. $this->vars['mcache']->set(MCACHE_STATION_NO_LIST_STR, $station_no_list_str);
  692. $this->vars['mcache']->set(MCACHE_STATION_NAME_STR, $station_name_str);
  693. $this->vars['mcache']->set(MCACHE_STATION_IP_STR, $station_ip_str);
  694. $this->vars['mcache']->set(MCACHE_STATION_PORT_STR, $station_port_str);
  695. $this->vars['mcache']->set(MCACHE_STATION_888_STR, $station_888_str);
  696. $this->vars['mcache']->set(MCACHE_STATION_SETTINGS, $settings);
  697. return 'ok';
  698. }
  699. // 取得目前場站設定
  700. public function station_setting_query($reload=false)
  701. {
  702. $station_no_str = $this->vars['mcache']->get(MCACHE_STATION_NO_STR);
  703. $station_no_list_str = $this->vars['mcache']->get(MCACHE_STATION_NO_LIST_STR);
  704. $station_name_str = $this->vars['mcache']->get(MCACHE_STATION_NAME_STR);
  705. $station_ip_str = $this->vars['mcache']->get(MCACHE_STATION_IP_STR);
  706. $station_port_str = $this->vars['mcache']->get(MCACHE_STATION_PORT_STR);
  707. $station_888_str = $this->vars['mcache']->get(MCACHE_STATION_888_STR);
  708. $settings = $this->vars['mcache']->get(MCACHE_STATION_SETTINGS);
  709. if( $reload ||
  710. empty($station_no_list_str) ||
  711. empty($station_no_str) || empty($station_name_str) ||
  712. empty($station_ip_str) || empty($station_port_str) ||
  713. empty($station_888_str) ||
  714. empty($settings)
  715. )
  716. {
  717. $result = $this->reload_station_setting();
  718. if($result == 'ok')
  719. {
  720. $station_no_str = $this->vars['mcache']->get(MCACHE_STATION_NO_STR);
  721. $station_no_list_str = $this->vars['mcache']->get(MCACHE_STATION_NO_LIST_STR);
  722. $station_name_str = $this->vars['mcache']->get(MCACHE_STATION_NAME_STR);
  723. $station_ip_str = $this->vars['mcache']->get(MCACHE_STATION_IP_STR);
  724. $station_port_str = $this->vars['mcache']->get(MCACHE_STATION_PORT_STR);
  725. $station_888_str = $this->vars['mcache']->get(MCACHE_STATION_888_STR);
  726. $settings = $this->vars['mcache']->get(MCACHE_STATION_SETTINGS);
  727. }
  728. else
  729. {
  730. /*
  731. $station_setting = array();
  732. $station_setting['station_no'] = STATION_NO;
  733. $station_setting['station_name'] = STATION_NAME;
  734. $station_setting['station_ip'] = STATION_IP;
  735. return $station_setting;
  736. */
  737. return false;
  738. }
  739. }
  740. // 第一個場站編號
  741. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_no_str);
  742. $first_station_no = $station_no_arr[0];
  743. $station_setting = array();
  744. $station_setting['station_no'] = $station_no_str;
  745. $station_setting['station_no_list'] = $station_no_list_str;
  746. $station_setting['station_name'] = $station_name_str;
  747. $station_setting['station_ip'] = $station_ip_str;
  748. $station_setting['station_port'] = $station_port_str;
  749. $station_setting['station_888'] = $station_888_str;
  750. $station_setting['settings'] = $settings;
  751. $station_setting['mqtt_ip'] = $settings[$first_station_no]['mqtt_ip'];
  752. $station_setting['mqtt_port'] = $settings[$first_station_no]['mqtt_port'];
  753. return $station_setting;
  754. }
  755. // ------------------------------------------------
  756. //
  757. // 中控接收端 (END)
  758. //
  759. // ------------------------------------------------
  760. // ------------------------------------------------
  761. //
  762. // 其它 (START)
  763. //
  764. // ------------------------------------------------
  765. // 手動新增入場資料
  766. public function gen_carin($parms)
  767. {
  768. $in_time = date('Y-m-d H:i:s');
  769. $data = array(
  770. 'station_no' => $parms['sno'],
  771. 'obj_type' => 1,
  772. 'obj_id' => $parms['lpr'],
  773. 'etag' => '',
  774. 'in_out' => $parms['io'],
  775. 'finished' => 0,
  776. 'in_time' => $in_time,
  777. 'in_lane' => $parms['ivsno'],
  778. 'out_before_time' => $in_time
  779. );
  780. $this->db->insert('cario', $data);
  781. trigger_error("新增入場資料:".print_r($parms, true));
  782. require_once(ALTOB_SYNC_FILE);
  783. // 傳送進場記錄
  784. $sync_agent = new AltobSyncAgent();
  785. $sync_agent->init($parms['sno'], $in_time);
  786. $sync_agent->cario_no = $this->db->insert_id(); // 進出編號
  787. $sync_result = $sync_agent->sync_st_in($parms);
  788. trigger_error( SYNC_DATA_LOG_TITLE . '..'. __FUNCTION__ . "..sync_st_in.." . $sync_result);
  789. }
  790. // ------------------------------------------------
  791. //
  792. // 其它 (END)
  793. //
  794. // ------------------------------------------------
  795. }