VM暫存
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

Sync_data_model.php 25KB

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