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.

Sync_data_model.php 27KB

vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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_no_list_arr = array();
  564. $station_name_arr = array();
  565. $station_888_arr = array();
  566. $settings = array();
  567. foreach($station_setting_arr as $data)
  568. {
  569. $station_no = $data['station_no'];
  570. array_push($station_no_arr, $station_no);
  571. array_push($station_name_arr, $data['short_name']);
  572. array_push($station_888_arr, $data['station_888']);
  573. // 若有設定多個場站, 已多場站設定為主
  574. $tmp_station_no_list_arr = explode(SYNC_DELIMITER_ST_NO, $data['station_no_list']);
  575. if(empty($tmp_station_no_list_arr))
  576. {
  577. array_push($station_no_list_arr, $station_no); // 若無設定直接放目前場站
  578. }
  579. else
  580. {
  581. foreach($tmp_station_no_list_arr as $key => $station_no_list_value)
  582. {
  583. if(intval($station_no_list_value) > 0)
  584. {
  585. if(empty($station_no_list_arr))
  586. array_push($station_no_list_arr, $station_no_list_value);
  587. else if(!array_search($station_no_list_value, $station_no_list_arr))
  588. array_push($station_no_list_arr, $station_no_list_value);
  589. }
  590. }
  591. }
  592. if(!isset($settings[$station_no]))
  593. {
  594. $settings[$station_no] = array();
  595. }
  596. $settings[$station_no]['station_888'] = empty($data['station_888']) ? 1 : $data['station_888'];
  597. $settings[$station_no]['mqtt_ip'] = empty($data['mqtt_ip']) ? MQ_HOST : $data['mqtt_ip'];
  598. $settings[$station_no]['mqtt_port'] = empty($data['mqtt_port']) ? MQ_PORT : $data['mqtt_port'];
  599. $settings[$station_no]['local_ip'] = empty($data['local_ip']) ? STATION_LOCAL_IP : $data['local_ip'];
  600. }
  601. $station_no_str = implode(SYNC_DELIMITER_ST_NO, $station_no_arr); // 取值時會用到
  602. $station_no_list_str = implode(SYNC_DELIMITER_ST_NO, $station_no_list_arr); // 會員資料同步相關
  603. $station_name_str = implode(SYNC_DELIMITER_ST_NAME, $station_name_arr); // 純顯示
  604. $station_888_str = implode(SYNC_DELIMITER_ST_INFO, $station_888_arr); // 場站 888 設定
  605. // 設定到 mcache
  606. $this->vars['mcache']->set(MCACHE_STATION_NO_STR, $station_no_str);
  607. $this->vars['mcache']->set(MCACHE_STATION_NO_LIST_STR, $station_no_list_str);
  608. $this->vars['mcache']->set(MCACHE_STATION_NAME_STR, $station_name_str);
  609. $this->vars['mcache']->set(MCACHE_STATION_IP_STR, $station_ip_str);
  610. $this->vars['mcache']->set(MCACHE_STATION_PORT_STR, $station_port_str);
  611. $this->vars['mcache']->set(MCACHE_STATION_888_STR, $station_888_str);
  612. $this->vars['mcache']->set(MCACHE_STATION_SETTINGS, $settings);
  613. return 'ok';
  614. }
  615. // 取得目前場站設定
  616. public function station_setting_query($reload=false)
  617. {
  618. $station_no_str = $this->vars['mcache']->get(MCACHE_STATION_NO_STR);
  619. $station_no_list_str = $this->vars['mcache']->get(MCACHE_STATION_NO_LIST_STR);
  620. $station_name_str = $this->vars['mcache']->get(MCACHE_STATION_NAME_STR);
  621. $station_ip_str = $this->vars['mcache']->get(MCACHE_STATION_IP_STR);
  622. $station_port_str = $this->vars['mcache']->get(MCACHE_STATION_PORT_STR);
  623. $station_888_str = $this->vars['mcache']->get(MCACHE_STATION_888_STR);
  624. $settings = $this->vars['mcache']->get(MCACHE_STATION_SETTINGS);
  625. if( $reload ||
  626. empty($station_no_list_str) ||
  627. empty($station_no_str) || empty($station_name_str) ||
  628. empty($station_ip_str) || empty($station_port_str) ||
  629. empty($station_888_str) ||
  630. empty($settings)
  631. )
  632. {
  633. $result = $this->reload_station_setting();
  634. if($result == 'ok')
  635. {
  636. $station_no_str = $this->vars['mcache']->get(MCACHE_STATION_NO_STR);
  637. $station_no_list_str = $this->vars['mcache']->get(MCACHE_STATION_NO_LIST_STR);
  638. $station_name_str = $this->vars['mcache']->get(MCACHE_STATION_NAME_STR);
  639. $station_ip_str = $this->vars['mcache']->get(MCACHE_STATION_IP_STR);
  640. $station_port_str = $this->vars['mcache']->get(MCACHE_STATION_PORT_STR);
  641. $station_888_str = $this->vars['mcache']->get(MCACHE_STATION_888_STR);
  642. $settings = $this->vars['mcache']->get(MCACHE_STATION_SETTINGS);
  643. }
  644. else
  645. {
  646. /*
  647. $station_setting = array();
  648. $station_setting['station_no'] = STATION_NO;
  649. $station_setting['station_name'] = STATION_NAME;
  650. $station_setting['station_ip'] = STATION_IP;
  651. return $station_setting;
  652. */
  653. return false;
  654. }
  655. }
  656. // 第一個場站編號
  657. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_no_str);
  658. $first_station_no = $station_no_arr[0];
  659. $station_setting = array();
  660. $station_setting['station_no'] = $station_no_str;
  661. $station_setting['station_no_list'] = $station_no_list_str;
  662. $station_setting['station_name'] = $station_name_str;
  663. $station_setting['station_ip'] = $station_ip_str;
  664. $station_setting['station_port'] = $station_port_str;
  665. $station_setting['station_888'] = $station_888_str;
  666. $station_setting['settings'] = $settings;
  667. $station_setting['mqtt_ip'] = $settings[$first_station_no]['mqtt_ip'];
  668. $station_setting['mqtt_port'] = $settings[$first_station_no]['mqtt_port'];
  669. return $station_setting;
  670. }
  671. // ------------------------------------------------
  672. //
  673. // 中控接收端 (END)
  674. //
  675. // ------------------------------------------------
  676. // ------------------------------------------------
  677. //
  678. // 其它 (START)
  679. //
  680. // ------------------------------------------------
  681. // 手動新增入場資料
  682. public function gen_carin($parms)
  683. {
  684. $in_time = date('Y-m-d H:i:s');
  685. $data = array(
  686. 'station_no' => $parms['sno'],
  687. 'obj_type' => 1,
  688. 'obj_id' => $parms['lpr'],
  689. 'etag' => '',
  690. 'in_out' => $parms['io'],
  691. 'finished' => 0,
  692. 'in_time' => $in_time,
  693. 'in_lane' => $parms['ivsno'],
  694. 'out_before_time' => $in_time
  695. );
  696. $this->db->insert('cario', $data);
  697. trigger_error("新增入場資料:".print_r($parms, true));
  698. require_once(ALTOB_SYNC_FILE);
  699. // 傳送進場記錄
  700. $sync_agent = new AltobSyncAgent();
  701. $sync_agent->init($parms['sno'], $in_time);
  702. $sync_agent->cario_no = $this->db->insert_id(); // 進出編號
  703. $sync_result = $sync_agent->sync_st_in($parms);
  704. trigger_error( SYNC_DATA_LOG_TITLE . '..'. __FUNCTION__ . "..sync_st_in.." . $sync_result);
  705. }
  706. // ------------------------------------------------
  707. //
  708. // 其它 (END)
  709. //
  710. // ------------------------------------------------
  711. }