VM暫存
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Sync_data_model.php 24KB

před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. // 同步場站會員 (功能: 會員同步)
  249. public function sync_members($info_arr=array('station_no_arr' => STATION_NO))
  250. {
  251. $data_member_arr = array();
  252. $data_car_arr = array();
  253. try{
  254. // 查現況
  255. $ch = curl_init();
  256. curl_setopt($ch, CURLOPT_URL, SYNC_API_URL . 'member_query_all_in_one');
  257. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  258. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  259. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  260. curl_setopt($ch, CURLOPT_POST, TRUE);
  261. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,10);
  262. curl_setopt($ch, CURLOPT_TIMEOUT, 10); //timeout in seconds
  263. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($info_arr));
  264. $data = curl_exec($ch);
  265. curl_close($ch);
  266. }catch (Exception $e){
  267. trigger_error('error msg:'.$e->getMessage());
  268. trigger_error(SYNC_DATA_LOG_TITLE . $e->getMessage());
  269. }
  270. $data_member_arr = json_decode($data, true);
  271. if (sizeof($data_member_arr) <= 0)
  272. {
  273. trigger_error(SYNC_DATA_LOG_TITLE . '.. empty ..'); // 忽略完全沒會員的情況
  274. return 'empty';
  275. }
  276. else
  277. {
  278. foreach($data_member_arr as $data)
  279. {
  280. // create member_car
  281. $data_car = array
  282. (
  283. 'station_no' => $data['station_no'],
  284. 'member_no' => $data['member_no'],
  285. 'lpr' => $data['lpr'],
  286. 'lpr_correct' => $data['lpr'],
  287. 'etag' => $data['etag'],
  288. 'start_time' => $data['start_date'],
  289. 'end_time' => $data['end_date']
  290. );
  291. array_push($data_car_arr, $data_car);
  292. }
  293. }
  294. //trigger_error(SYNC_DATA_LOG_TITLE . '.. test ..' . print_r($data_member_arr, true));
  295. $this->db->trans_start();
  296. // 清空
  297. $this->db->empty_table('members');
  298. $this->db->empty_table('member_car');
  299. // 建立 members
  300. $this->db->insert_batch('members', $data_member_arr);
  301. // 建立 member_car
  302. $this->db->insert_batch('member_car', $data_car_arr);
  303. $this->db->trans_complete();
  304. if ($this->db->trans_status() === FALSE)
  305. {
  306. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync fail ..'. '| last_query: ' . $this->db->last_query());
  307. return 'fail';
  308. }
  309. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync completed ..');
  310. return 'ok';
  311. }
  312. // 同步車牌更換 (功能: 換車牌同步)
  313. public function sync_switch_lpr($switch_lpr_arr)
  314. {
  315. trigger_error( __FUNCTION__ . '..' . print_r($switch_lpr_arr, true));
  316. $this->db->trans_start();
  317. foreach($switch_lpr_arr as $data)
  318. {
  319. $station_no = $data['station_no'];
  320. $member_no = $data['member_no'];
  321. $old_lpr = $data['old_lpr'];
  322. $new_lpr = $data['new_lpr'];
  323. $new_data = array('lpr' => $new_lpr, 'lpr_correct' => $new_lpr, 'member_no' => $member_no);
  324. $this->db->update('etag_lpr', $new_data, array('lpr_correct' => $old_lpr));
  325. $affect_rows = $this->db->affected_rows();
  326. trigger_error(SYNC_DATA_LOG_TITLE . "換車牌更新 etag_lpr 共[{$affect_rows}]筆..".print_r($data, true));
  327. /*
  328. if($station_no == STATION_NO)
  329. {
  330. $new_data = array('lpr' => $new_lpr, 'lpr_correct' => $new_lpr, 'member_no' => $member_no);
  331. $this->db->update('etag_lpr', $new_data, array('lpr_correct' => $old_lpr));
  332. $affect_rows = $this->db->affected_rows();
  333. trigger_error(SYNC_DATA_LOG_TITLE . "換車牌更新 etag_lpr 共[{$affect_rows}]筆..".print_r($data, true));
  334. }
  335. else
  336. {
  337. trigger_error(SYNC_DATA_LOG_TITLE . __FUNCTION__ . "..資料異常..".print_r($data, true));
  338. }
  339. */
  340. }
  341. $this->db->trans_complete();
  342. if ($this->db->trans_status() === FALSE)
  343. {
  344. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync fail ..'. '| last_query: ' . $this->db->last_query());
  345. return 'fail';
  346. }
  347. trigger_error(SYNC_DATA_LOG_TITLE . '.. sync completed ..');
  348. return 'ok';
  349. }
  350. // 同步場站費率
  351. public function sync_price_plan($info_arr=array('station_no_arr' => STATION_NO))
  352. {
  353. try{
  354. // 查另一台主機
  355. $ch = curl_init();
  356. curl_setopt($ch, CURLOPT_URL, SYNC_API_URL . 'price_plan_query_all_in_one');
  357. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  358. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  359. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  360. curl_setopt($ch, CURLOPT_POST, TRUE);
  361. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5);
  362. curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds
  363. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($info_arr));
  364. $data = curl_exec($ch);
  365. curl_close($ch);
  366. }catch (Exception $e){
  367. trigger_error('error msg:'.$e->getMessage());
  368. }
  369. $decode_result = json_decode($data, true);
  370. if (sizeof($decode_result) <= 0) return "empty";
  371. $this->db->trans_start();
  372. foreach ($decode_result as $key => $value)
  373. {
  374. $station_no = $value["station_no"];
  375. $tx_price_plan_id = $value["txid"];
  376. $tx_type = $value["tx_type"];
  377. $price_plan_data = array
  378. (
  379. 'station_no' => $station_no,
  380. 'tx_type' => $tx_type,
  381. 'remarks' => $value['remarks'],
  382. 'price_plan' => $value['price_plan'],
  383. 'start_time' => $value['start_time'],
  384. 'valid_time' => $value['valid_time']
  385. );
  386. // 刪除
  387. $this->db->delete('tx_price_plan', array('station_no' => $station_no, 'tx_type' => $tx_type));
  388. // 新增
  389. $this->db->insert('tx_price_plan', $price_plan_data);
  390. }
  391. $this->db->trans_complete();
  392. return "ok";
  393. }
  394. // 取得最新未結清 (功能: 行動支付)
  395. public function get_last_unbalanced_cario($lpr, $station_no)
  396. {
  397. $sql = "SELECT station_no, cario_no, in_time, pay_time, out_time, out_before_time, member_no
  398. FROM cario
  399. WHERE
  400. station_no = '{$station_no}' AND obj_id = '{$lpr}' AND finished = 0 AND err = 0
  401. ORDER BY cario_no DESC
  402. LIMIT 1
  403. ";
  404. $results = $this->db->query($sql)->result_array();
  405. if(isset($results[0]))
  406. return $results[0];
  407. return false;
  408. }
  409. // 同步車位現況 (功能: 888 同步, 在席同步)
  410. public function sync_pks_groups_reload($station_setting)
  411. {
  412. $info = array();
  413. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_setting['station_no']);
  414. $station_name_arr = explode(SYNC_DELIMITER_ST_NAME, $station_setting['station_name']);
  415. $station_888_arr = explode(SYNC_DELIMITER_ST_INFO, $station_setting['station_888']);
  416. foreach($station_no_arr as $key => $station_no)
  417. {
  418. if($station_888_arr[$key] == 1) // 啟用
  419. array_push($info, array('station_no' => $station_no_arr[$key], 'station_name' => $station_name_arr[$key]));
  420. else if($station_888_arr[$key] == 4) // 關閉
  421. {
  422. // 清除 888
  423. $this->db->delete('pks_groups', array('station_no' => $station_no_arr[$key]));
  424. }
  425. else
  426. {
  427. trigger_error(__FUNCTION__ . '..unknown station_888:' . $station_888_arr[$key]);
  428. }
  429. }
  430. if(empty($info))
  431. return 'none';
  432. return $this->sync_pks_groups($info, true);
  433. }
  434. // 同步車位現況 (功能: 888 同步, 在席同步)
  435. public function sync_pks_groups($info_arr=array(array('station_no' => STATION_NO, 'station_name' => STATION_NAME)), $reload=false)
  436. {
  437. if($reload)
  438. {
  439. // 確認應該要有的 pks_groups
  440. $pks_groups_arr = array();
  441. $pks_groups_name_arr = array();
  442. foreach($info_arr as $data)
  443. {
  444. $pks_key = $data['station_no'] . SYNC_DELIMITER_ST_INFO . SYNC_PKS_GROUP_ID_CI;
  445. array_push($pks_groups_arr, $pks_key); // 汽車 888
  446. $pks_key = $data['station_no'] . SYNC_DELIMITER_ST_INFO . SYNC_PKS_GROUP_ID_MI;
  447. array_push($pks_groups_arr, $pks_key); // 機車 888
  448. $pks_groups_name_arr[$data['station_no']] = $data['station_name']. '(888)'; // 群組名稱
  449. }
  450. // 過濾已存在的部份
  451. $sql = "SELECT station_no, group_id FROM pks_groups";
  452. $current_pks_group = $this->db->query($sql)->result_array();
  453. foreach($current_pks_group as $data)
  454. {
  455. $pks_key = $data['station_no'] . SYNC_DELIMITER_ST_INFO . $data['group_id'];
  456. $key = array_search($pks_key, $pks_groups_arr);
  457. if($key !== false)
  458. unset($pks_groups_arr[$key]);
  459. }
  460. // 建立缺少的部份
  461. if(!empty($pks_groups_arr))
  462. {
  463. // [A.開始]
  464. $this->db->trans_start();
  465. foreach($pks_groups_arr as $new_data)
  466. {
  467. $pks_info = explode(SYNC_DELIMITER_ST_INFO, $new_data);
  468. $new_pks_groups_data = array(
  469. 'station_no' => $pks_info[0],
  470. 'group_id' => $pks_info[1],
  471. 'tot' => 100, // 預設車位數
  472. 'availables' => 100, // 預設車位數
  473. 'floors' => 'TOT',
  474. 'group_name' => $pks_groups_name_arr[$pks_info[0]]
  475. );
  476. $this->db->insert('pks_groups', $new_pks_groups_data);
  477. trigger_error(__FUNCTION__ . '..insert pks_groups..'. print_r($new_pks_groups_data, true));
  478. }
  479. // [C.完成]
  480. $this->db->trans_complete();
  481. if ($this->db->trans_status() === FALSE)
  482. {
  483. trigger_error(__FUNCTION__ . '..trans_error..' . '| last_query: ' . $this->db->last_query());
  484. return 'fail'; // 中斷
  485. }
  486. }
  487. }
  488. $sql = "SELECT pks_groups.station_no,
  489. 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
  490. FROM pks_groups
  491. ORDER BY pks_groups.group_id DESC";
  492. $pks_group_query_data = $this->db->query($sql)->result_array();
  493. //trigger_error(__FUNCTION__ . '..sync..' . print_r($pks_group_query_data, true));
  494. // 同步
  495. require_once(ALTOB_SYNC_FILE);
  496. $sync_agent = new AltobSyncAgent();
  497. $sync_agent->init(STATION_NO); // 已帶上的資料場站編號為主
  498. $sync_result = $sync_agent->upd_pks_groups(json_encode($pks_group_query_data, JSON_UNESCAPED_UNICODE));
  499. trigger_error( SYNC_DATA_LOG_TITLE . '..'. __FUNCTION__ . "..upd_pks_groups.." . $sync_result);
  500. }
  501. // 重新載入場站設定
  502. public function reload_station_setting()
  503. {
  504. $port_info = (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 60123) ? '/' . $_SERVER['SERVER_PORT'] : '';
  505. $reload_url = SYNC_API_URL . 'station_setting_query' . $port_info;
  506. try{
  507. // 查現況
  508. $ch = curl_init();
  509. curl_setopt($ch, CURLOPT_URL, $reload_url);
  510. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  511. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  512. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  513. curl_setopt($ch, CURLOPT_POST, TRUE);
  514. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,10);
  515. curl_setopt($ch, CURLOPT_TIMEOUT, 10); //timeout in seconds
  516. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array()));
  517. $return_result = curl_exec($ch);
  518. curl_close($ch);
  519. trigger_error(__FUNCTION__ . "..curl {$reload_url}.." . print_r($return_result, true));
  520. }catch (Exception $e){
  521. trigger_error('error msg:'.$e->getMessage());
  522. }
  523. $station_setting_result = json_decode($return_result, true);
  524. if(!isset($station_setting_result['results']) || sizeof($station_setting_result['results']) <= 0)
  525. {
  526. trigger_error(__FUNCTION__ . '..fail..' . print_r($return_result, true));
  527. return 'fail';
  528. }
  529. $station_ip_str = $station_setting_result['station_ip']; // 場站目前對外IP
  530. if(empty($port_info))
  531. $station_port_str = '80';
  532. else
  533. $station_port_str = substr($port_info, 1); // 場站目前對外PORT
  534. $station_setting_arr = $station_setting_result['results'];
  535. $station_no_arr = array();
  536. $station_name_arr = array();
  537. $station_888_arr = array();
  538. $settings = array();
  539. foreach($station_setting_arr as $data)
  540. {
  541. $station_no = $data['station_no'];
  542. array_push($station_no_arr, $station_no);
  543. array_push($station_name_arr, $data['short_name']);
  544. array_push($station_888_arr, $data['station_888']);
  545. if(!isset($settings[$station_no]))
  546. {
  547. $settings[$station_no] = array();
  548. }
  549. $settings[$station_no]['station_888'] = empty($data['station_888']) ? 1 : $data['station_888'];
  550. $settings[$station_no]['mqtt_ip'] = empty($data['mqtt_ip']) ? MQ_HOST : $data['mqtt_ip'];
  551. $settings[$station_no]['mqtt_port'] = empty($data['mqtt_port']) ? MQ_PORT : $data['mqtt_port'];
  552. }
  553. $station_no_str = implode(SYNC_DELIMITER_ST_NO, $station_no_arr); // 取值時會用到
  554. $station_name_str = implode(SYNC_DELIMITER_ST_NAME, $station_name_arr); // 純顯示
  555. $station_888_str = implode(SYNC_DELIMITER_ST_INFO, $station_888_arr); // 場站 888 設定
  556. // 設定到 mcache
  557. $this->vars['mcache']->set(MCACHE_STATION_NO_STR, $station_no_str);
  558. $this->vars['mcache']->set(MCACHE_STATION_NAME_STR, $station_name_str);
  559. $this->vars['mcache']->set(MCACHE_STATION_IP_STR, $station_ip_str);
  560. $this->vars['mcache']->set(MCACHE_STATION_PORT_STR, $station_port_str);
  561. $this->vars['mcache']->set(MCACHE_STATION_888_STR, $station_888_str);
  562. $this->vars['mcache']->set(MCACHE_STATION_SETTINGS, $settings);
  563. return 'ok';
  564. }
  565. // 取得目前場站設定
  566. public function station_setting_query($reload=false)
  567. {
  568. $station_no_str = $this->vars['mcache']->get(MCACHE_STATION_NO_STR);
  569. $station_name_str = $this->vars['mcache']->get(MCACHE_STATION_NAME_STR);
  570. $station_ip_str = $this->vars['mcache']->get(MCACHE_STATION_IP_STR);
  571. $station_port_str = $this->vars['mcache']->get(MCACHE_STATION_PORT_STR);
  572. $station_888_str = $this->vars['mcache']->get(MCACHE_STATION_888_STR);
  573. $settings = $this->vars['mcache']->get(MCACHE_STATION_SETTINGS);
  574. if( $reload ||
  575. empty($station_no_str) || empty($station_name_str) ||
  576. empty($station_ip_str) || empty($station_port_str) ||
  577. empty($station_888_str) ||
  578. empty($settings)
  579. )
  580. {
  581. $result = $this->reload_station_setting();
  582. if($result == 'ok')
  583. {
  584. $station_no_str = $this->vars['mcache']->get(MCACHE_STATION_NO_STR);
  585. $station_name_str = $this->vars['mcache']->get(MCACHE_STATION_NAME_STR);
  586. $station_ip_str = $this->vars['mcache']->get(MCACHE_STATION_IP_STR);
  587. $station_port_str = $this->vars['mcache']->get(MCACHE_STATION_PORT_STR);
  588. $station_888_str = $this->vars['mcache']->get(MCACHE_STATION_888_STR);
  589. $settings = $this->vars['mcache']->get(MCACHE_STATION_SETTINGS);
  590. }
  591. else
  592. {
  593. /*
  594. $station_setting = array();
  595. $station_setting['station_no'] = STATION_NO;
  596. $station_setting['station_name'] = STATION_NAME;
  597. $station_setting['station_ip'] = STATION_IP;
  598. return $station_setting;
  599. */
  600. return false;
  601. }
  602. }
  603. // 第一個場站編號
  604. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_no_str);
  605. $first_station_no = $station_no_arr[0];
  606. $station_setting = array();
  607. $station_setting['station_no'] = $station_no_str;
  608. $station_setting['station_name'] = $station_name_str;
  609. $station_setting['station_ip'] = $station_ip_str;
  610. $station_setting['station_port'] = $station_port_str;
  611. $station_setting['station_888'] = $station_888_str;
  612. $station_setting['settings'] = $settings;
  613. $station_setting['mqtt_ip'] = $settings[$first_station_no]['mqtt_ip'];
  614. $station_setting['mqtt_port'] = $settings[$first_station_no]['mqtt_port'];
  615. return $station_setting;
  616. }
  617. // ------------------------------------------------
  618. //
  619. // 中控接收端 (END)
  620. //
  621. // ------------------------------------------------
  622. // ------------------------------------------------
  623. //
  624. // 其它 (START)
  625. //
  626. // ------------------------------------------------
  627. // 手動新增入場資料
  628. public function gen_carin($parms)
  629. {
  630. $in_time = date('Y-m-d H:i:s');
  631. $data = array(
  632. 'station_no' => $parms['sno'],
  633. 'obj_type' => 1,
  634. 'obj_id' => $parms['lpr'],
  635. 'etag' => '',
  636. 'in_out' => $parms['io'],
  637. 'finished' => 0,
  638. 'in_time' => $in_time,
  639. 'in_lane' => $parms['ivsno'],
  640. 'out_before_time' => $in_time
  641. );
  642. $this->db->insert('cario', $data);
  643. trigger_error("新增入場資料:".print_r($parms, true));
  644. require_once(ALTOB_SYNC_FILE);
  645. // 傳送進場記錄
  646. $sync_agent = new AltobSyncAgent();
  647. $sync_agent->init($parms['sno'], $in_time);
  648. $sync_agent->cario_no = $this->db->insert_id(); // 進出編號
  649. $sync_result = $sync_agent->sync_st_in($parms);
  650. trigger_error( SYNC_DATA_LOG_TITLE . '..'. __FUNCTION__ . "..sync_st_in.." . $sync_result);
  651. }
  652. // ------------------------------------------------
  653. //
  654. // 其它 (END)
  655. //
  656. // ------------------------------------------------
  657. }