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.

802 lines
25KB

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