VM暫存
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

1032 Zeilen
33KB

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