VM暫存
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
9.7KB

  1. <?php
  2. /**
  3. * Altob 資料同步
  4. *
  5. * @version 1.0
  6. * @author mip
  7. */
  8. class AltobSyncAgent
  9. {
  10. const SYNC_PKS_URL = 'http://61.219.172.12/parkings/cars.html/'; // 在席記錄
  11. const SYNC_CARS_URL = 'http://61.219.172.12/parkings/cars.html/'; // 進出記錄
  12. const SYNC_ST_URL = 'http://61.219.172.11:60123/admins_station.html/'; // 場站記錄
  13. const API_URL = 'http://parks.altob.com.tw:60123/parkingquery.html/'; // 場站 API
  14. const ALLPA_URL = 'http://61.219.172.11:60123/allpa_service.html/'; // 歐pa卡
  15. public $io = '';
  16. public $etag = '';
  17. public $pic_name = '';
  18. public $ivsno = 0;
  19. public $member_no = 0;
  20. public $finished = 0;
  21. public $in_time = '';
  22. public $cario_no = 0;
  23. private $post_parms;
  24. /**
  25. * 初始化
  26. */
  27. public function init($station_no, $time='')
  28. {
  29. $this->post_parms = array();
  30. $this->post_parms['station_no'] = $station_no;
  31. $this->post_parms['io_time'] = $time;
  32. }
  33. // 檢查基本欄位
  34. function check_init_parms()
  35. {
  36. if(empty($this->post_parms['station_no']))
  37. return 'station_no not set'; // 場站編號
  38. if(empty($this->post_parms['io_time']))
  39. return 'io_time not set'; // 進出時間 (若為繳費帶進場時間)
  40. return false;
  41. }
  42. // ===============================================
  43. // st_io
  44. // ===============================================
  45. // 傳送進場記錄
  46. public function sync_st_in($parms)
  47. {
  48. $error_parms_msg = $this->check_init_parms();
  49. if(!empty($error_parms_msg)) { return $error_parms_msg; }
  50. if(empty($this->cario_no))
  51. return 'cario_no not found';
  52. if(empty($parms['lpr']))
  53. return 'lpr not found';
  54. if(empty($parms['io']))
  55. return 'io not found';
  56. $this->post_parms['cario_no'] = $this->cario_no; // 需設定
  57. $this->post_parms['member_no'] = $this->member_no; // 預設為 0
  58. $this->post_parms['lpr'] = $parms['lpr'];
  59. $this->post_parms['io'] = $parms['io'];
  60. $this->post_parms['etag'] = $parms['etag'] == 'NONE' ? '' : $parms['etag'];
  61. $this->post_parms['pic_name'] = empty($parms['pic_name']) ? '' : $parms['pic_name'];
  62. $this->post_parms['ivsno'] = $parms['ivsno'];
  63. $this->post_parms['out_before_time'] = $this->post_parms['io_time'];
  64. // 初始化網路服務物件。
  65. $oService = new AltobSyncService();
  66. $oService->ServiceURL = AltobSyncAgent::SYNC_CARS_URL;
  67. $oService->ServiceCMD = 'st_in';
  68. // 傳遞參數至遠端。
  69. return $oService->ServerPost($this->post_parms);
  70. }
  71. // 傳送離場記錄
  72. public function sync_st_out($parms)
  73. {
  74. $error_parms_msg = $this->check_init_parms();
  75. if(!empty($error_parms_msg)) { return $error_parms_msg; }
  76. // 有入場記錄時
  77. if($this->cario_no > 0)
  78. {
  79. $this->post_parms['cario_no'] = $this->cario_no;
  80. if(empty($this->in_time))
  81. return 'in_time not found'; // 需設定
  82. $this->post_parms['in_time'] = $this->in_time;
  83. $this->post_parms['minutes'] = floor((strtotime($this->post_parms['io_time']) - strtotime($this->in_time)) / 60); // 停車時數 (分鐘)
  84. }
  85. if(empty($parms['lpr']))
  86. return 'lpr not found';
  87. if(empty($parms['io']))
  88. return 'io not found';
  89. $this->post_parms['member_no'] = $this->member_no; // 預設為 0
  90. $this->post_parms['finished'] = $this->finished; // 預設為 0
  91. $this->post_parms['lpr'] = $parms['lpr'];
  92. $this->post_parms['io'] = $parms['io'];
  93. $this->post_parms['etag'] = $parms['etag'] == 'NONE' ? '' : $parms['etag'];
  94. $this->post_parms['pic_name'] = empty($parms['pic_name']) ? '' : $parms['pic_name'];
  95. $this->post_parms['ivsno'] = $parms['ivsno'];
  96. // 初始化網路服務物件。
  97. $oService = new AltobSyncService();
  98. $oService->ServiceURL = AltobSyncAgent::SYNC_CARS_URL;
  99. $oService->ServiceCMD = 'st_out';
  100. // 傳遞參數至遠端。
  101. return $oService->ServerPost($this->post_parms);
  102. }
  103. // 傳送付費更新記錄
  104. public function sync_st_pay($lpr, $pay_time, $pay_type=0, $out_before_time='')
  105. {
  106. $error_parms_msg = $this->check_init_parms();
  107. if(!empty($error_parms_msg)) { return $error_parms_msg; }
  108. if(empty($this->cario_no))
  109. return 'cario_no not found';
  110. if(empty($lpr))
  111. return 'lpr not found';
  112. if(empty($pay_time))
  113. return 'pay_time not found';
  114. $this->post_parms['cario_no'] = $this->cario_no; // 需設定
  115. $this->post_parms['lpr'] = $lpr;
  116. $this->post_parms['pay_time'] = $pay_time;
  117. $this->post_parms['pay_type'] = $pay_type;
  118. $this->post_parms['out_before_time'] = empty($out_before_time) ? date('Y-m-d H:i:s', strtotime("{$pay_time} + 15 minutes")) : $out_before_time;
  119. // 初始化網路服務物件。
  120. $oService = new AltobSyncService();
  121. $oService->ServiceURL = AltobSyncAgent::SYNC_CARS_URL;
  122. $oService->ServiceCMD = 'st_pay';
  123. // 傳遞參數至遠端。
  124. return $oService->ServerPost($this->post_parms);
  125. }
  126. // ===============================================
  127. // members
  128. // ===============================================
  129. // 取得場站會員
  130. public function get_st_members()
  131. {
  132. if(empty($this->post_parms['station_no']))
  133. return 'station_no not set'; // 場站編號
  134. // 初始化網路服務物件。
  135. $oService = new AltobSyncService();
  136. $oService->ServiceURL = AltobSyncAgent::SYNC_ST_URL;
  137. $oService->ServiceCMD = 'member_query_all';
  138. // 傳遞參數至遠端。
  139. return $oService->ServerPost($this->post_parms);
  140. }
  141. // 同步會員鎖車 (0: 解鎖, 1: 上鎖, 2: 查詢)
  142. public function sync_security_action($lpr, $pswd, $action)
  143. {
  144. if(empty($this->post_parms['station_no']))
  145. return 'station_no not set'; // 場站編號
  146. $station_no = $this->post_parms['station_no'];
  147. $ck = md5($lpr.'i'.$pswd.'iii'.$action);
  148. // 初始化網路服務物件。
  149. $oService = new AltobSyncService();
  150. $oService->ServiceURL = AltobSyncAgent::API_URL;
  151. $oService->ServiceCMD = __FUNCTION__ . "/{$lpr}/{$pswd}/{$action}/{$ck}/{$station_no}";
  152. // 傳遞參數至遠端。
  153. return $oService->ServerPost($this->post_parms);
  154. }
  155. // 同步會員改密碼
  156. public function sync_change_pwd($lpr, $pswd, $new_pwd)
  157. {
  158. if(empty($this->post_parms['station_no']))
  159. return 'station_no not set'; // 場站編號
  160. $station_no = $this->post_parms['station_no'];
  161. $ck = md5($lpr.'i'.$pswd.'iii'.$new_pwd);
  162. // 初始化網路服務物件。
  163. $oService = new AltobSyncService();
  164. $oService->ServiceURL = AltobSyncAgent::API_URL;
  165. $oService->ServiceCMD = __FUNCTION__ . "/{$lpr}/{$pswd}/{$new_pwd}/{$ck}/{$station_no}";
  166. // 傳遞參數至遠端。
  167. return $oService->ServerPost($this->post_parms);
  168. }
  169. // ===============================================
  170. // pks
  171. // ===============================================
  172. // 在席
  173. public function upd_pks_groups($data)
  174. {
  175. if(empty($this->post_parms['station_no']))
  176. return 'station_no not set'; // 場站編號
  177. $this->post_parms['data'] = $data;
  178. // 初始化網路服務物件。
  179. $oService = new AltobSyncService();
  180. $oService->ServiceURL = AltobSyncAgent::SYNC_PKS_URL;
  181. $oService->ServiceCMD = __FUNCTION__;
  182. // 傳遞參數至遠端。
  183. return $oService->ServerPost($this->post_parms);
  184. }
  185. // ===============================================
  186. // 歐Pa卡
  187. // ===============================================
  188. // 歐Pa卡 - 開門
  189. public function allpa_go($lpr)
  190. {
  191. $error_parms_msg = $this->check_init_parms();
  192. if(!empty($error_parms_msg)) { return $error_parms_msg; }
  193. if(empty($this->in_time))
  194. return 'in_time not found'; // 需設定
  195. if(empty($lpr))
  196. return 'lpr not found'; // 需設定
  197. $this->post_parms['in_time'] = $this->in_time;
  198. $this->post_parms['lpr'] = $lpr;
  199. $this->post_parms['ck'] = md5(
  200. $this->post_parms['lpr']. 'a' . date('dmh') . 'lt' .
  201. $this->post_parms['in_time'] . 'o' . // timestamp
  202. $this->post_parms['station_no'] . 'b' . __FUNCTION__ .
  203. $this->post_parms['io_time'] // timestamp
  204. );
  205. // 初始化網路服務物件。
  206. $oService = new AltobSyncService();
  207. $oService->ServiceURL = AltobSyncAgent::ALLPA_URL;
  208. $oService->ServiceCMD = __FUNCTION__;
  209. // 傳遞參數至遠端。
  210. return $oService->ServerPost($this->post_parms);
  211. }
  212. // 歐Pa卡 - 取得場站會員
  213. public function query_allpa_users()
  214. {
  215. $this->post_parms['ck'] = md5('a' . date('dmh') . 'lt' . __FUNCTION__ . 'ob');
  216. // 初始化網路服務物件。
  217. $oService = new AltobSyncService();
  218. $oService->ServiceURL = AltobSyncAgent::ALLPA_URL;
  219. $oService->ServiceCMD = __FUNCTION__;
  220. // 傳遞參數至遠端。
  221. return $oService->ServerPost($this->post_parms);
  222. }
  223. }
  224. /**
  225. * 呼叫網路服務的類別。
  226. */
  227. class AltobSyncService {
  228. /**
  229. * 網路服務類別呼叫的位址。
  230. */
  231. public $ServiceURL = 'ServiceURL';
  232. public $ServiceCMD = 'ServiceCMD';
  233. /**
  234. * 網路服務類別的建構式。
  235. */
  236. function __construct() {$this->AltobSyncService();}
  237. /**
  238. * 網路服務類別的實體。
  239. */
  240. function AltobSyncService() {}
  241. /**
  242. * 提供伺服器端呼叫遠端伺服器 Web API 的方法。
  243. */
  244. function ServerPost($parameters) {
  245. $ch = curl_init();
  246. curl_setopt($ch, CURLOPT_URL, $this->ServiceURL . $this->ServiceCMD);
  247. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  248. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  249. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  250. curl_setopt($ch, CURLOPT_POST, TRUE);
  251. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5);
  252. curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds
  253. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
  254. $rs = curl_exec($ch);
  255. curl_close($ch);
  256. return $rs;
  257. }
  258. }