VM暫存
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sync_data_model.php 27KB

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