kq_funclib.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. <?php
  2. function queyGpsRouteListByimei( $imei, $start_time, $end_time ){
  3. $dsnV2 = C('DB_DSN_V2');
  4. //地图无分页,占用内存大
  5. if(!$start_time){
  6. $start_time=date('Y-m-d').' 00:00:00';
  7. }
  8. if(!$end_time){
  9. $end_time=date('Y-m-d').' 23:59:59';
  10. }
  11. ini_set('memory_limit', '512M');
  12. $startTime = strtotime($start_time);
  13. $endTime = strtotime($end_time);
  14. $device_number = $imei;
  15. $where = array(
  16. 'DeviceNumber' => trim($device_number),
  17. 'Timestamp' => array('between', array($startTime, $endTime)),
  18. );
  19. $option['table_name'] = 'wxt_route_gps';
  20. $model = new \OtsModel($option['table_name']);
  21. if(!$model){
  22. $this->errmsg = 'failed to init otsmodel ' . $tableName;
  23. return false;
  24. }
  25. $option['pagesize'] = 4000;
  26. $option['sort'] = 'desc';
  27. $data = $model->limit($option['pagesize'])->order($option['sort'])->where($where)->select();
  28. foreach($data as &$val){
  29. $val['OnlineTime']=date('Y-m-d H:i:s',$val['Timestamp']);
  30. }
  31. return $data;
  32. }
  33. function analyzeRouteInfoWayzBatch( $list, $route_type ){
  34. if(!$list){
  35. return array();
  36. }
  37. $macLength=0;
  38. foreach($list as $item){
  39. if(isset($item['WifiMacs']) && $item['WifiMacs'] !=''){
  40. $macLength++;
  41. }
  42. }
  43. $lngLatAlter = new \Jms\Algo\Geometry();
  44. $arr= [];
  45. $preTime=0;
  46. $filter=0;//默认不过滤
  47. $wifiPreTime=0;
  48. $wifiFilter=0;//默认不过滤
  49. $batch_wifi=array();
  50. foreach($list as $k=> $v){
  51. if($filter>0){//gps点过多时间也过滤部分
  52. $interval=abs($v['Timestamp']-$preTime);
  53. if( $interval < $filter){
  54. continue;
  55. }
  56. }
  57. if(isset($v['WifiMacs']) && $v['WifiMacs'] !=''){
  58. $wifiInterval=abs($v['Timestamp']-$wifiPreTime);
  59. if( $wifiInterval < $wifiFilter){
  60. continue;
  61. }
  62. if($route_type=='GPS'){
  63. continue;
  64. }
  65. $batch_wifi[$k]=$v;
  66. if(count($batch_wifi)==16 || $k==($count-1)){
  67. $wifiRes =requestWifiLBS_wayz_batch($batch_wifi);
  68. foreach($wifiRes as $key=>$value){
  69. if($value['success']){
  70. if($value['data']['lon'] == '' || is_null($value['data']['lat'])){
  71. continue;
  72. }
  73. $list[$key]['Latitude'] = $value['data']['lat'];
  74. $list[$key]['Longitude'] = $value['data']['lon'];
  75. $list[$key]['SignalType'] = 'WiFi';
  76. // $list[$key]['WifiAddress'] = $value['data']['address'];
  77. if(isset($value['data']['address'])){
  78. $list[$key]['WifiAddress'] = $value['data']['address'];
  79. }
  80. $arr[$key]=$list[$key];
  81. //array_push($arr, $list[$k]);
  82. }else{
  83. continue;
  84. }
  85. }
  86. $batch_wifi=array();
  87. }
  88. $wifiPreTime=$v['Timestamp'];
  89. }else{
  90. if($route_type=='WiFi'){
  91. continue;
  92. }
  93. if($v['Altitude']=='0.003' || $v['Altitude']=='3'){
  94. continue;
  95. }
  96. if( !isset($v['Latitude']) || !isset($v['Longitude']) || $v['Latitude']<0.065 || $v['Longitude']<0.065){
  97. continue;
  98. }
  99. $latLng = $lngLatAlter->convertBd09ToGcj02($v['Latitude'], $v['Longitude']);
  100. $list[$k]['Latitude'] = $latLng['lat'];
  101. $list[$k]['Longitude'] = $latLng['lng'];
  102. $list[$k]['SignalType'] = 'GPS';
  103. $arr[$k]=$list[$k];
  104. //array_push($arr, $list[$k]);
  105. }
  106. $preTime=$v['Timestamp'];
  107. }
  108. //防止最后剩余wifi点未更新
  109. if($batch_wifi){
  110. $wifiRes =requestWifiLBS_wayz_batch($batch_wifi);
  111. foreach($wifiRes as $key=>$value){
  112. if($value['success']){
  113. if($value['data']['lon'] == '' || is_null($value['data']['lat'])){
  114. continue;
  115. }
  116. $list[$key]['Latitude'] = $value['data']['lat'];
  117. $list[$key]['Longitude'] = $value['data']['lon'];
  118. $list[$key]['SignalType'] = 'WiFi';
  119. // $list[$key]['WifiAddress'] = $value['data']['address'];
  120. if(isset($value['data']['address'])){
  121. $list[$key]['WifiAddress'] = $value['data']['address'];
  122. }
  123. $arr[$key]=$list[$key];
  124. //array_push($arr, $list[$k]);
  125. }else{
  126. continue;
  127. }
  128. }
  129. $batch_wifi=array();
  130. }
  131. ksort($arr);
  132. $response_data=[];
  133. foreach($arr as $item){
  134. array_push($response_data, $item);
  135. }
  136. return $response_data;
  137. }
  138. function bmapFilterOutliers( $rows ){
  139. if(!$rows){
  140. return array();
  141. }
  142. $response_data=[];
  143. $point1=[];
  144. $speedArr=[];
  145. $timestamp=0;
  146. $geometry = new \Jms\Algo\Geometry();
  147. for($i=0;$i<count($rows);$i++){
  148. if($i==0){
  149. //当前最新的一个定位保留
  150. $timestamp=$rows[$i]['Timestamp'];
  151. array_push($response_data, $rows[$i]);
  152. $point1=['lng'=>$rows[$i]['Longitude'],'lat'=>$rows[$i]['Latitude']];
  153. continue;
  154. }
  155. $point2=['lng'=>$rows[$i]['Longitude'],'lat'=>$rows[$i]['Latitude']];
  156. $distance = $geometry->distanceBetween2BdPoints($point1,$point2);
  157. $time=($timestamp-$rows[$i]['Timestamp'])/3600;//单位h
  158. $speed=$distance/$time;//计算速度 超过130的默认为异常点
  159. $speedArr[]=$speed;
  160. if($rows[$i]['SignalType']=='WiFi'){
  161. $length=count($speedArr);
  162. if($length>=5){
  163. $filterSpeed=($speedArr[$length-1]+$speedArr[$length-2]+$speedArr[$length-3]+$speedArr[$length-4]+$speedArr[$length-5])/5*1.5;//超过平均速度 1.5倍
  164. }else{
  165. $filterSpeed=50;
  166. }
  167. if($filterSpeed<10){
  168. $filterSpeed=10;
  169. }
  170. if($speed<$filterSpeed){
  171. $timestamp=$rows[$i]['Timestamp'];
  172. $point1=$point2;
  173. array_push($response_data, $rows[$i]);
  174. }else{
  175. array_pop($speedArr);//去除无效速度
  176. }
  177. }else{
  178. $timestamp=$rows[$i]['Timestamp'];
  179. $point1=$point2;
  180. //只有wifi点需要过滤 gps点不过滤
  181. array_push($response_data, $rows[$i]);
  182. }
  183. }
  184. return bmapReverseFilterOutliers($response_data);
  185. }
  186. function bmapReverseFilterOutliers( $rows ){
  187. if(!$rows){
  188. return array();
  189. }
  190. $response_data=[];
  191. $point1=[];
  192. $speedArr=[];
  193. $timestamp=0;
  194. $geometry = new \Jms\Algo\Geometry();
  195. for($i=count($rows)-1;$i>=0;$i--){
  196. if($i==count($rows)-1){
  197. //当前最新的一个定位保留
  198. array_push($response_data, $rows[$i]);
  199. $timestamp=$rows[$i]['Timestamp'];
  200. $point1=['lng'=>$rows[$i]['Longitude'],'lat'=>$rows[$i]['Latitude']];
  201. continue;
  202. }
  203. $point2=['lng'=>$rows[$i]['Longitude'],'lat'=>$rows[$i]['Latitude']];
  204. $distance = $geometry->distanceBetween2BdPoints($point1,$point2);
  205. $time=($rows[$i]['Timestamp']-$timestamp)/3600;//单位h
  206. $speed=$distance/$time;//计算速度 超过130的默认为异常点
  207. $speedArr[]=$speed;
  208. if($rows[$i]['SignalType']=='WiFi'){
  209. $length=count($speedArr);
  210. if($length>=5){
  211. $filterSpeed=($speedArr[$length-1]+$speedArr[$length-2]+$speedArr[$length-3]+$speedArr[$length-4]+$speedArr[$length-5])/5*1.5;//超过平均速度 1.5倍
  212. }else{
  213. $filterSpeed=50;
  214. }
  215. // test_log('filt_map','['.$rows[$i]['PassTime'].'] filterSpeed: '. $filterSpeed.'speed: '. $speed.' time: '. $time);
  216. if($filterSpeed<10){
  217. $filterSpeed=10;
  218. }
  219. if($speed<$filterSpeed){
  220. $point1=$point2;
  221. $timestamp=$rows[$i]['Timestamp'];
  222. array_push($response_data, $rows[$i]);
  223. }else{
  224. array_pop($speedArr);//去除无效速度
  225. }
  226. }else{
  227. $point1=$point2;
  228. $timestamp=$rows[$i]['Timestamp'];
  229. //只有wifi点需要过滤 gps点不过滤
  230. array_push($response_data, $rows[$i]);
  231. }
  232. }
  233. $res_data=[];
  234. for($i=0;$i<count($response_data)-1;$i++){
  235. array_push($res_data, $response_data[$i]);
  236. }
  237. return $res_data;
  238. }
  239. function requestWifiLBS_wayz_batch( $routeArr ){
  240. if(!$routeArr){
  241. return array(
  242. 'success' => false,
  243. 'msg' => '空数据'
  244. );
  245. }
  246. //当前毫秒时间戳
  247. list($msec, $sec) = explode(' ', microtime());
  248. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  249. //鉴权方式 采用 access_key 的方式,在 URL 的 Query Param 中添加 access_key。
  250. $access_key = 'VOiuxRnGhlJsAzVzhaU2Hen24zvwuWck';
  251. $post_data=array();
  252. $batch_keys=array();
  253. $isFilter=true;//
  254. $filterSignal=85;//过滤信号强度
  255. $filterTotal=5;//wifi个数大于次数才开始过滤
  256. foreach($routeArr as $k=>$routeInfo){
  257. $asset = array(
  258. "id"=>date('YmdHis'),
  259. "manufacturer"=>"hzrl",
  260. "model"=>"校园卡",
  261. "imeiMd5"=> md5($routeInfo['DeviceNumber']),
  262. "macAddress"=>$routeInfo['DeviceNumber'],
  263. "serialNumber"=> "",
  264. "uniqueId"=> $routeInfo['DeviceNumber'],
  265. /*"os"=>array(
  266. "type"=> "",
  267. "version"=> ""
  268. )*/
  269. );
  270. //处理WiFi信息
  271. $WifiMacs = $routeInfo['WifiMacs'];
  272. $WifiMacs = explode('|',$WifiMacs);
  273. $wifis = [];
  274. $countWifi=count($WifiMacs);
  275. $total=0;//wifimacs上报 默认按强度排序 可直接计算不过滤前$filterTotal个
  276. foreach($WifiMacs as $v){
  277. $total++;
  278. $w = explode(',',$v);
  279. $wifi = array(
  280. "timestamp"=> intval($msectime),
  281. "signalStrength"=> abs($w[1]),
  282. "macAddress"=> $w[0],
  283. );
  284. //判断是否需要过滤
  285. if( $isFilter && ($total>$filterTotal) && ($wifi['signalStrength'] > $filterSignal )){
  286. continue;
  287. }
  288. array_push($wifis,$wifi);
  289. }
  290. if(count($wifis)<=3){
  291. continue;
  292. }
  293. $data = array(
  294. 'timestamp'=>intval($msectime),
  295. 'id'=>date('YmdHis'),
  296. 'asset'=>$asset,
  297. 'location'=>array(
  298. "wifis"=>$wifis
  299. )
  300. );
  301. //$post_data[$k]=$data;
  302. array_push($post_data,$data);
  303. array_push($batch_keys,$k);
  304. }
  305. $url = "https://api.newayz.com/location/hub/v1/track_points?batch=true&access_key=".$access_key."&field_masks=location.position,location.address,location.place";
  306. //var_dump($url);
  307. //发送数据data
  308. $post_data = json_encode( $post_data );
  309. $curl = curl_init();
  310. curl_setopt($curl, CURLOPT_URL, $url);
  311. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  312. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  313. curl_setopt($curl, CURLOPT_POST, 1);
  314. curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
  315. curl_setopt($curl, CURLOPT_HEADER, 0);
  316. curl_setopt($curl, CURLOPT_HTTPHEADER,
  317. array(
  318. 'Content-Type: application/json; charset=utf-8',
  319. 'Content-Length:' . strlen($post_data)
  320. )
  321. );
  322. // curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate'));
  323. // curl_setopt($curl, CURLOPT_ENCODING, "gzip");
  324. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  325. $response = curl_exec($curl);
  326. $errorno = curl_errno($curl);
  327. if ($errorno) {
  328. return array('success' => false, 'data' => $errorno);
  329. }
  330. curl_close($curl);
  331. if(count($batch_keys)==1){
  332. $response = [json_decode($response, true)];
  333. }else{
  334. $response = json_decode($response, true);
  335. }
  336. $response_data=array();
  337. foreach($response as $key=>$value){
  338. $response_key=$batch_keys[$key];
  339. $item=[];
  340. // var_dump($value);
  341. if(!isset($value['location']) ){
  342. $item=array(
  343. 'success' => false,
  344. 'msg' => '定位信息解析失败'
  345. );
  346. }else{
  347. $location = $value['location'];
  348. if(!isset($location['position']) ){
  349. $item=array(
  350. 'success' => false,
  351. 'msg' =>'定位信息解析失败'
  352. );
  353. }else{
  354. $position = $location['position'];
  355. $point = $position['point'];
  356. $address = array(
  357. 'lon' => $point['longitude'],
  358. 'lat' => $point['latitude'],
  359. // 'address' => $location['address']['name']
  360. );
  361. if(isset($location['address']['name'])){
  362. $address['address']=$location['address']['name'];
  363. }
  364. $item=array(
  365. 'success' => true,
  366. 'data' => $address
  367. );
  368. }
  369. }
  370. $response_data[$response_key]=$item;
  371. }
  372. return $response_data;
  373. }
  374. function requestWifiLBS_wayz( $routeInfo ){
  375. if(!$routeInfo){
  376. return array(
  377. 'success' => false,
  378. 'msg' => '空数据'
  379. );
  380. }
  381. if(!isset( $routeInfo['WifiMacs']) ){
  382. return array(
  383. 'success' => false,
  384. 'msg' => '无wifi信息'
  385. );
  386. }
  387. //当前毫秒时间戳
  388. list($msec, $sec) = explode(' ', microtime());
  389. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  390. //鉴权方式 采用 access_key 的方式,在 URL 的 Query Param 中添加 access_key。
  391. $access_key = 'VOiuxRnGhlJsAzVzhaU2Hen24zvwuWck';
  392. //处理WiFi信息
  393. $WifiMacs = $routeInfo['WifiMacs'];
  394. $WifiMacs = explode('|',$WifiMacs);
  395. $wifis = [];
  396. foreach($WifiMacs as $v){
  397. $w = explode(',',$v);
  398. $wifi = array(
  399. "timestamp"=> intval($msectime),
  400. "signalStrength"=> abs($w[1]),
  401. "macAddress"=> $w[0],
  402. //"ssid"=> "AiMap",
  403. //"frequency"=> 0,
  404. //"channel"=> 0,
  405. //"connected"=> true
  406. );
  407. array_push($wifis,$wifi);
  408. }
  409. $asset = array(
  410. "id"=>date('YmdHis'),
  411. "manufacturer"=>"hzrl",
  412. "model"=>"校园卡",
  413. "imeiMd5"=> md5($routeInfo['DeviceNumber']),
  414. "macAddress"=>$routeInfo['DeviceNumber'],
  415. "serialNumber"=> "",
  416. "uniqueId"=> $routeInfo['DeviceNumber'],
  417. /*"os"=>array(
  418. "type"=> "",
  419. "version"=> ""
  420. )*/
  421. );
  422. $url = "https://api.newayz.com/location/hub/v1/track_points?access_key=".$access_key."&field_masks=location.position,location.address,location.place";
  423. //var_dump($url);
  424. //发送数据data
  425. $data = array(
  426. 'timestamp'=>intval($msectime),
  427. 'id'=>date('YmdHis'),
  428. 'asset'=>$asset,
  429. 'location'=>array(
  430. "wifis"=>$wifis
  431. )
  432. );
  433. $data = json_encode( $data );
  434. debug_log('wayz_wifi_data',$routeInfo['DeviceNumber']."发送数据:".$data);
  435. $curl = curl_init();
  436. curl_setopt($curl, CURLOPT_URL, $url);
  437. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  438. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  439. curl_setopt($curl, CURLOPT_POST, 1);
  440. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  441. curl_setopt($curl, CURLOPT_HEADER, 0);
  442. curl_setopt($curl, CURLOPT_HTTPHEADER,
  443. array(
  444. 'Content-Type: application/json; charset=utf-8',
  445. 'Content-Length:' . strlen($data)
  446. )
  447. );
  448. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  449. $response = curl_exec($curl);
  450. $errorno = curl_errno($curl);
  451. if ($errorno) {
  452. return array('success' => false, 'data' => $errorno);
  453. }
  454. debug_log('wayz_wifi_res',$routeInfo['DeviceNumber']."返回结果".$response);
  455. curl_close($curl);
  456. $response = json_decode($response, true);
  457. //echo '-------way ---------';
  458. //var_dump($response);
  459. //var_dump($response);
  460. /*if($response['info'] != 'OK' || count($response['result']) <2 ){
  461. return array(
  462. 'success' => false,
  463. 'msg' => $response['info']
  464. );
  465. }*/
  466. $location = $response['location'];
  467. $position = $location['position'];
  468. $point = $position['point'];
  469. $geometry = new \Jms\Algo\Geometry();
  470. $lngLat = $geometry->convertGcj02ToBd09($point['latitude'],$point['longitude']);
  471. $response = array(
  472. 'lon' => $lngLat['lng'],
  473. 'lat' => $lngLat['lat'],
  474. 'address' => $location['address']['name']
  475. );
  476. debug_log('wayz_wifi_res',$routeInfo['DeviceNumber']."转换坐标后:".json_encode($response,JSON_UNESCAPED_UNICODE));
  477. //var_dump($response);
  478. //echo '--------way end---------';
  479. return array(
  480. 'success' => true,
  481. 'data' => $response
  482. );
  483. }
  484. function requestWifiLBS_gaode( $routeInfo ){
  485. if(!$routeInfo){
  486. return array(
  487. 'success' => false,
  488. 'msg' => '空数据'
  489. );
  490. }
  491. if(!isset( $routeInfo['WifiMacs']) ){
  492. return array(
  493. 'success' => false,
  494. 'msg' => '无wifi信息'
  495. );
  496. }
  497. $WifiMacs = $routeInfo['WifiMacs'];
  498. $userKey = '';
  499. if(!$userKey){
  500. $userKey = 'f107b0b3a513e6e37c6fb0424bed6633';
  501. }
  502. //$url = 'http://api.cellocation.com:81/loc/?wl=' . $WifiMacs .'&output=json&coord=gcj02';
  503. $url = 'http://apilocate.amap.com/position?accesstype=1&imei=' . $routeInfo['DeviceNumber'] . '&macs=' . $WifiMacs . '&key=' . $userKey;
  504. $curl = curl_init($url);
  505. curl_setopt($curl, CURLOPT_TIMEOUT, 3);
  506. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  507. $response = curl_exec($curl);
  508. if (curl_errno($curl)) {
  509. $errmsg = curl_error($curl);
  510. curl_close($curl);
  511. return array(
  512. 'success' => false,
  513. 'msg' => $errmsg
  514. );
  515. }
  516. curl_close($curl);
  517. $response = json_decode($response, true);
  518. if($response['info'] != 'OK' || count($response['result']) <2 ){
  519. return array(
  520. 'success' => false,
  521. 'msg' => $response['info']
  522. );
  523. }
  524. $result = $response['result'];
  525. $location = $response['result']['location'];
  526. $location = explode(',', $location);
  527. $geometry = new \Jms\Algo\Geometry();
  528. $lngLat = $geometry->convertGcj02ToBd09($location[1],$location[0]);
  529. $response = array(
  530. 'lon' => $lngLat['lng'],
  531. 'lat' => $lngLat['lat'],
  532. 'address' => $result['desc']
  533. );
  534. return array(
  535. 'success' => true,
  536. 'data' => $response
  537. );
  538. }
  539. function requestWifiLBS( $routeInfo ){
  540. if(C('IS_USE_WIFI_WAYZ_API')){
  541. return requestWifiLBS_wayz($routeInfo);
  542. }
  543. if(!$routeInfo){
  544. return array(
  545. 'success' => false,
  546. 'msg' => '空数据'
  547. );
  548. }
  549. if(!isset( $routeInfo['WifiMacs']) ){
  550. return array(
  551. 'success' => false,
  552. 'msg' => '无wifi信息'
  553. );
  554. }
  555. $WifiMacs = $routeInfo['WifiMacs'];
  556. $userKey = '';
  557. if(!$userKey){
  558. $userKey = 'f107b0b3a513e6e37c6fb0424bed6633';
  559. }
  560. //$url = 'http://api.cellocation.com:81/loc/?wl=' . $WifiMacs .'&output=json&coord=gcj02';
  561. $url = 'http://apilocate.amap.com/position?accesstype=1&imei=' . $routeInfo['DeviceNumber'] . '&macs=' . $WifiMacs . '&key=' . $userKey;
  562. $curl = curl_init($url);
  563. curl_setopt($curl, CURLOPT_TIMEOUT, 3);
  564. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  565. $response = curl_exec($curl);
  566. if (curl_errno($curl)) {
  567. $errmsg = curl_error($curl);
  568. curl_close($curl);
  569. return array(
  570. 'success' => false,
  571. 'msg' => $errmsg
  572. );
  573. }
  574. curl_close($curl);
  575. $response = json_decode($response, true);
  576. if($response['info'] != 'OK' || count($response['result']) <2 ){
  577. return array(
  578. 'success' => false,
  579. 'msg' => $response['info']
  580. );
  581. }
  582. $result = $response['result'];
  583. $location = $response['result']['location'];
  584. $location = explode(',', $location);
  585. $geometry = new \Jms\Algo\Geometry();
  586. $lngLat = $geometry->convertGcj02ToBd09($location[1],$location[0]);
  587. $response = array(
  588. 'lon' => $lngLat['lng'],
  589. 'lat' => $lngLat['lat'],
  590. 'address' => $result['desc']
  591. );
  592. return array(
  593. 'success' => true,
  594. 'data' => $response
  595. );
  596. }
  597. function requestWifiLBS_gaode_batch( $routeArr ){
  598. if(!$routeArr){
  599. return array(
  600. 'success' => false,
  601. 'msg' => '空数据'
  602. );
  603. }
  604. $mh = curl_multi_init();
  605. $curlArray = array();
  606. $t1=microtime(true);
  607. foreach($routeArr as $key=>$routeInfo){
  608. if(!isset( $routeInfo['WifiMacs'])){
  609. continue;
  610. }
  611. $WifiMacs = $routeInfo['WifiMacs'];
  612. $userKey = 'f107b0b3a513e6e37c6fb0424bed6633';
  613. $ch = curl_init();
  614. $url = 'http://apilocate.amap.com/position?accesstype=1&imei=' . $routeInfo['DeviceNumber'] . '&macs=' . $WifiMacs . '&key=' . $userKey;
  615. curl_setopt($ch, CURLOPT_URL, $url);
  616. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  617. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  618. curl_multi_add_handle($mh, $ch);
  619. $curlArray[$key] = $ch;
  620. }
  621. // echo count($curlArray);
  622. //debug_log('gaode_wifi_lbs','count:--'. (count($curlArray)));
  623. $running = NULL;
  624. do {
  625. // usleep(10000);
  626. curl_multi_exec($mh,$running);
  627. } while($running > 0);
  628. $response_data=array();
  629. $success=0;
  630. $fail=0;
  631. foreach($curlArray as $key => $info) {
  632. // $res[$key] = curl_multi_getcontent($curlArray[$key]);
  633. $result = curl_multi_getcontent($curlArray[$key]);
  634. $response = json_decode($result,true);
  635. if($response['info'] != 'OK' || count($response['result']) < 2 ){
  636. $fail++;
  637. $item=array(
  638. 'success' => false,
  639. 'msg' => '定位信息解析失败'
  640. );
  641. }else{
  642. $success++;
  643. $result = $response['result'];
  644. $location = $result['location'];
  645. $location = explode(',', $location);
  646. $address = array(
  647. 'lon' => $location[0],
  648. 'lat' => $location[1],
  649. 'address' => $result['desc']
  650. );
  651. $item=array(
  652. 'success' => true,
  653. 'data' => $address
  654. );
  655. }
  656. $response_data[$key]=$item;
  657. }
  658. debug_log('gaode_wifi_lbs','result:---success:--'.$success.'--fail:--'. $fail);
  659. foreach($curlArray as $key => $info){
  660. curl_multi_remove_handle($mh, $curlArray[$key]);
  661. }
  662. curl_multi_close($mh);
  663. $t3=microtime(true);
  664. debug_log('gaode_wifi_lbs','t3:---'. ($t3-$t1));
  665. // echo 't3:---'. ($t3-$t2).PHP_EOL;
  666. return $response_data;
  667. }
  668. function analyzeRouteInfoGaodeBatch( $list, $route_type = All ){
  669. if(!$list){
  670. return array();
  671. }
  672. $macLength=0;
  673. foreach($list as $item){
  674. if(isset($item['WifiMacs']) && $item['WifiMacs'] !=''){
  675. $macLength++;
  676. }
  677. }
  678. $lngLatAlter = new \Jms\Algo\Geometry();
  679. $arr= [];
  680. $preTime=0;
  681. $filter=0;//默认不过滤
  682. $wifiPreTime=0;
  683. $wifiFilter=0;//默认不过滤
  684. $count=count($list);
  685. $batch_wifi=array();
  686. foreach($list as $k=> $v){
  687. if($filter>0){//gps点过多时间也过滤部分
  688. $interval=abs($v['Timestamp']-$preTime);
  689. if( $interval < $filter){
  690. continue;
  691. }
  692. }
  693. if(isset($v['WifiMacs']) && $v['WifiMacs'] !=''){
  694. $wifiInterval=abs($v['Timestamp']-$wifiPreTime);
  695. if( $wifiInterval < $wifiFilter){
  696. continue;
  697. }
  698. if($route_type=='GPS'){
  699. continue;
  700. }
  701. $batch_wifi[$k]=$v;
  702. if(count($batch_wifi)==8|| $k==($count-1)){
  703. $wifiRes =requestWifiLBS_gaode_batch($batch_wifi);
  704. foreach($wifiRes as $key=>$value){
  705. if($value['success']){
  706. if($value['data']['lon'] == '' || is_null($value['data']['lat'])){
  707. continue;
  708. }
  709. $list[$key]['Latitude'] = $value['data']['lat'];
  710. $list[$key]['Longitude'] = $value['data']['lon'];
  711. $list[$key]['SignalType'] = 'WiFi';
  712. $list[$key]['WifiAddress'] = $value['data']['address'];
  713. $arr[$key]=$list[$key];
  714. //array_push($arr, $list[$k]);
  715. }else{
  716. continue;
  717. }
  718. }
  719. $batch_wifi=array();
  720. }
  721. $wifiPreTime=$v['Timestamp'];
  722. }else{
  723. if($route_type=='WiFi'){
  724. continue;
  725. }
  726. if($v['Altitude']=='0.003' || $v['Altitude']=='3'){
  727. continue;
  728. }
  729. if( !isset($v['Latitude']) || !isset($v['Longitude']) || $v['Latitude']<0.065 || $v['Longitude']<0.065){
  730. continue;
  731. }
  732. $latLng = $lngLatAlter->convertBd09ToGcj02($v['Latitude'], $v['Longitude']);
  733. $list[$k]['Latitude'] = $latLng['lat'];
  734. $list[$k]['Longitude'] = $latLng['lng'];
  735. $list[$k]['SignalType'] = 'GPS';
  736. $arr[$k]=$list[$k];
  737. //array_push($arr, $list[$k]);
  738. }
  739. $preTime=$v['Timestamp'];
  740. }
  741. //防止最后剩余wifi点未更新
  742. if($batch_wifi){
  743. $wifiRes =requestWifiLBS_gaode_batch($batch_wifi);
  744. foreach($wifiRes as $key=>$value){
  745. if($value['success']){
  746. if($value['data']['lon'] == '' || is_null($value['data']['lat'])){
  747. continue;
  748. }
  749. $list[$key]['Latitude'] = $value['data']['lat'];
  750. $list[$key]['Longitude'] = $value['data']['lon'];
  751. $list[$key]['SignalType'] = 'WiFi';
  752. $list[$key]['WifiAddress'] = $value['data']['address'];
  753. $arr[$key]=$list[$key];
  754. //array_push($arr, $list[$k]);
  755. }else{
  756. continue;
  757. }
  758. }
  759. $batch_wifi=array();
  760. }
  761. $response_data=[];
  762. ksort($arr);
  763. foreach($arr as $key=>$item){
  764. array_push($response_data, $item);
  765. }
  766. return $response_data;
  767. }
  768. function pb_abs_url( $url ){
  769. if(strpos($url,'http://') === 0)
  770. return $url;
  771. if(strpos($url,'https://') === 0)
  772. return $url;
  773. return 'http://'.$_SERVER['HTTP_HOST'] . U($url);
  774. }
  775. function hex2str( $hex ){
  776. $string="";
  777. for($i=0;$i<strlen($hex)-1;$i+=2)
  778. $string.=chr(hexdec($hex[$i].$hex[$i+1]));
  779. return $string;
  780. }
  781. function str2hex( $string ){
  782. $hex="";
  783. for($i=0;$i<strlen($string);$i++)
  784. $hex.=dechex(ord($string[$i]));
  785. $hex=strtoupper($hex);
  786. return $hex;
  787. }
  788. function wx_token_cache( $appid, $access_token, $expire_time ){
  789. $key = 'access_token_'.$appid;
  790. if(!$expire_time || $expire_time < 0)
  791. $expire_time = 3600;
  792. if($access_token){
  793. S($key,$access_token,$expire_time);
  794. }
  795. else{
  796. return S($key);
  797. }
  798. }