25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

565 satır
20 KiB

  1. <?php
  2. namespace backend\modules\motorcade\controllers;
  3. use backend\modules\motorcade\models\BaseBus;
  4. use yii\web\Controller;
  5. use backend\modules\motorcade\models\BusGps;
  6. use Yii;
  7. use yii\base\Module;
  8. // 指定允许其他域名访问
  9. header('Access-Control-Allow-Origin:*');
  10. class GpsController extends Controller
  11. {
  12. public $layout = "@backend/modules/motorcade/views/layouts/monitor";
  13. public $enableCsrfValidation = false;
  14. //恒栋GPS设备
  15. private $base_url = "http://api.hstsp.cn:8077/data/";
  16. private $account_id = "hdgps";
  17. private $account_pwd = "123456";
  18. //我们采购的GPS设备
  19. private $base_url_zz = 'http://api.gpsoo.net/';
  20. private $account_id_zz = "15618818219";
  21. private $account_pwd_zz = "123456";
  22. /*public function __construct(string $id, Module $module, array $config = [])
  23. {
  24. libxml_disable_entity_loader(false);
  25. parent::__construct($id, $module, $config);
  26. }*/
  27. /**
  28. * 车辆实时位置界面
  29. * User: Steven
  30. * @return string
  31. */
  32. public function actionMonitor()
  33. {
  34. return $this->render('gps', ['view' => 'monitor', 'data' => '']);
  35. }
  36. /**
  37. * 车辆行驶轨迹
  38. * User: Steven
  39. * @return string
  40. */
  41. public function actionHistoryTrack()
  42. {
  43. return $this->render('gps', ['view' => 'historyTrack', 'data' => '']);
  44. }
  45. /**
  46. * 获取GPS数据接口
  47. * User: Steven
  48. * @param $car_list 需要获取GPS数据的车辆列表
  49. * @return string
  50. */
  51. public function actionGetGpsData($car_no_list)
  52. {
  53. //获取资源数据
  54. /*$bus_list = BaseBus::find()->select(['bus_no'])->where(['org_id' => 628, 'cancel_flag' => 0])->asArray()->all();
  55. $car_list = '';
  56. foreach ($bus_list as $value) {
  57. $car_list .= $car_list == '' ? $value["bus_no"] : "," . $value["bus_no"];
  58. }*/
  59. $car_no_list = urlencode($car_no_list);
  60. //登录获取token
  61. $account_pwd = md5($this->account_pwd);
  62. $login_url = $this->base_url . "login/tokenDuration.do?account={$this->account_id}&passwd={$account_pwd}";
  63. $result = file_get_contents($login_url);
  64. $result_array = json_decode($result, true);
  65. $token_str = $result_array["tokenStr"];
  66. //获取需要查询的车牌号的ID
  67. $get_car_id_url = $this->base_url . "LogisticalCar/queryCarId.do?tokenStr={$token_str}&plateCodes={$car_no_list}";
  68. $result = file_get_contents($get_car_id_url);
  69. $result_array = json_decode($result, true);
  70. //查询车辆的GPS数据
  71. $car_id = '';
  72. foreach ($result_array["Data"] as $data_info) {
  73. $car_id .= $car_id == '' ? number_format($data_info["id"], 0, ".", "") : "," . number_format($data_info["id"], 0, ".", "");
  74. }
  75. $get_car_info_url = $this->base_url . "VehicleMonitor/queryMultiVehicleLocation.do?tokenStr={$token_str}&vehicleIds={$car_id}&mapType=1";
  76. $result = file_get_contents($get_car_info_url);
  77. //$result = json_decode($result, true);
  78. return $result;
  79. }
  80. /**
  81. * 获取蜘蛛gps数据
  82. * User: Steven
  83. */
  84. public function actionGetGpsZz()
  85. {
  86. $gpsModel = new BusGps();
  87. #region 1、获取access_token
  88. $login_url = $this->base_url_zz . "1/auth/access_token";
  89. $unix = time();
  90. $login_request = array(
  91. 'account' => $this->account_id_zz,
  92. 'time' => $unix,
  93. 'signature' => md5(md5($this->account_pwd_zz) . $unix)
  94. );
  95. $login_response = $gpsModel->httpsPost($login_url, $login_request);
  96. #endregion
  97. $login_response = json_decode($login_response, true);
  98. if ($login_response['ret'] == 0) {
  99. $common_arr = array(
  100. 'access_token' => $login_response['access_token'],
  101. 'account' => $this->account_id_zz,
  102. 'format' => 'json'
  103. );
  104. #region 2、获取一个账户名下所有设备最新位置信息
  105. /*$monitor_url = $this->base_url_zz . "1/account/monitor";
  106. $moni_request = array(
  107. 'target' => $this->account_id_zz,
  108. 'map_type' => 'BAIDU'
  109. );
  110. $new_request = array_merge($common_arr, $moni_request);
  111. $monitor_response = $gpsModel->httpsPost($monitor_url, $new_request);
  112. $monitor_response = json_decode($monitor_response, true);*/
  113. #endregion
  114. // 352477070153196 352477070154186 352477070154426 352477070156637 352477070156678
  115. $tracking_url = $this->base_url_zz . "1/devices/tracking";
  116. $tracking_request = array(
  117. 'imeis' => '352477070153196,352477070154186,352477070154426,352477070156637,352477070156678',
  118. 'map_type' => 'BAIDU'
  119. );
  120. $new_request = array_merge($common_arr, $tracking_request);
  121. $tracking_response = $gpsModel->httpsPost($tracking_url, $new_request);
  122. $tracking_response = json_decode($tracking_response, true);
  123. if ($tracking_response['ret'] == 0) {
  124. $res = $gpsModel->uploadGpsZz($tracking_response);
  125. }
  126. }
  127. return json_encode($res);
  128. }
  129. /**
  130. * 获取地图聚合数据接口
  131. * User: Steven
  132. */
  133. public function actionGetPolymer()
  134. {
  135. $bus_gps = new BusGps();
  136. $data = $bus_gps->getPolymerData();
  137. $bus_no_list = '';
  138. foreach ($data as $key => $item) {
  139. $x_y = $bus_gps->GeocodingAPI($item['title'], 1);
  140. $data[$key]['lat'] = $x_y['lat'];
  141. $data[$key]['lng'] = $x_y['lng'];
  142. $bus_no_list .= $bus_no_list == '' ? $item["car_list"] : "," . $item["car_list"];
  143. }
  144. $res = array(
  145. 'code' => '0',
  146. 'info' => '数据获取成功',
  147. 'data' => array(
  148. 'polymers' => $data,
  149. 'car_list' => $bus_no_list
  150. )
  151. );
  152. return json_encode($res);
  153. }
  154. /**
  155. * 点击聚合获取详细的车辆信息及运行状态
  156. * User: Steven
  157. * @param $position 市级名称 如果是空,则返回所有的车辆
  158. */
  159. public function actionGetCarData()
  160. {
  161. $bus_no_list = $_POST['car_list'];
  162. $first = isset($_POST['first']) ? $_POST['first'] : '';
  163. $gpsModel = new BusGps();
  164. // $data = $gpsModel->getNewGpsData($first, "'沪DP4017','沪DP4276', '沪DL6889', '沪D25696', '沪BL3478'");
  165. $data = $gpsModel->getNewGpsData($bus_no_list, $first);
  166. $arr = array();
  167. foreach ($data as $value) {
  168. $arr = $gpsModel->getCarDataService($value, $arr);
  169. }
  170. // $arr = [array (
  171. // 'plateCode' => '沪BL3478',
  172. // 'seat_desc' => '普通座45座',
  173. // 'bus_status' => 0,
  174. // 'bus_desc' => '空闲',
  175. // 'lat' => '31.1035846746921',
  176. // 'lng' => '121.67047346216',
  177. // 'dspeed' => '0',
  178. // 'direction' => '267',
  179. // 'driver' => '',
  180. // 'itinerary_name' => '',
  181. // 'start_time' => '',
  182. // 'end_time' => '',
  183. // )];
  184. // $arr = [array (
  185. // 'plateCode' => '沪BL3478',
  186. // 'seat_desc' => '普通座45座',
  187. // 'bus_status' => 0,
  188. // 'bus_desc' => '空闲',
  189. // 'lat' => '31.1463114948584',
  190. // 'lng' => '121.656098228153',
  191. // 'dspeed' => '0',
  192. // 'direction' => '267',
  193. // 'driver' => '',
  194. // 'itinerary_name' => '',
  195. // 'start_time' => '',
  196. // 'end_time' => '',
  197. // )];
  198. $res = array(
  199. 'code' => '0',
  200. 'info' => '数据获取成功',
  201. 'data' => array_values($arr)
  202. );
  203. return json_encode($res);
  204. }
  205. /**
  206. * 根据车牌号码获取当前车辆的实时数据
  207. * User: Steven
  208. * @param $car_no
  209. */
  210. public function actionGetCarDataByNo()
  211. {
  212. $car_no = isset($_POST['car_no']) ? '\'' . $_POST['car_no'] . '\'' : false;
  213. if (!$car_no) {
  214. $res = array(
  215. 'code' => '1',
  216. 'info' => '缺少必要参数',
  217. 'data' => array()
  218. );
  219. return json_encode($res);
  220. }
  221. $gpsModel = new BusGps();
  222. $data = $gpsModel->getNewGpsData($car_no);
  223. $arr = array();
  224. $arr = $gpsModel->getCarDataService($data[0], $arr);
  225. $res = array(
  226. 'code' => '0',
  227. 'info' => '数据获取成功',
  228. 'data' => array_values($arr)
  229. );
  230. return json_encode($res);
  231. }
  232. /**
  233. * 获取车辆运营概况统计数据接口
  234. * User: Steven
  235. */
  236. public function actionGetStatistics()
  237. {
  238. //获取资源数据
  239. $gpsModel = new BusGps();
  240. $data = $gpsModel->getNewGpsData();
  241. $car_list = array();
  242. if (empty($data)) {
  243. $res = array(
  244. 'code' => '0',
  245. 'info' => '无数据',
  246. 'data' => ''
  247. );
  248. } else {
  249. foreach ($data as $v) {
  250. $car_list[] = $v['bus_no'];
  251. }
  252. $gpsModel = new BusGps();
  253. $res = $gpsModel->getStatistics($car_list);
  254. $res = array(
  255. 'code' => '0',
  256. 'info' => '数据获取成功',
  257. 'data' => $res
  258. );
  259. }
  260. return json_encode($res);
  261. }
  262. /**
  263. * 获取车辆实时位置右侧面板的调派状态
  264. * User: Steven
  265. * @return string
  266. */
  267. public function actionDispatchState()
  268. {
  269. $main_cooperation_id = Yii::$app->user->identity->MAIN_CORP_ID2;
  270. //从内存获取需要显示的动态数据
  271. $res = \Yii::$app->cache->get('send_sure_' . $main_cooperation_id);
  272. $arr = array();
  273. if (!empty($res)) {
  274. if (count($res) > 3) {
  275. array_splice($res, 0, count($res) - 3);
  276. }
  277. \Yii::$app->cache->set('send_sure_' . $main_cooperation_id, $res);
  278. $date = date('Y-m-d H:i:s', time());
  279. foreach ($res as $key => $value) {
  280. $day = $this->timediff(strtotime($value['run_date']), strtotime($date));
  281. $arr[$key] = array(
  282. 'bus_number' => $value['bus_number'],
  283. 'bus_no' => $value['bus_no'],
  284. 'driver_name' => $value['driver_name'],
  285. 'line_name' => $value['line_name'],
  286. 'run_date' => date('Y-m-d', strtotime($value['run_date'])),
  287. 'time_desc' => (($day['day'] >= 1) ? $day['day'] . '天前' : ($day['hour'] >= 1 ? $day['hour'] . '小时前' : ($day['min'] >= 1 ? $day['min'] . '分钟前' : ($day['sec'] . '秒前'))))
  288. );
  289. }
  290. }
  291. $res = array(
  292. 'code' => '0',
  293. 'info' => '数据获取成功',
  294. 'data' => $arr
  295. );
  296. return json_encode($res);
  297. }
  298. /**
  299. * 获取历史轨迹页面左侧车辆列表
  300. * User: Steven
  301. * @param $date
  302. * @return string
  303. */
  304. public function actionGetCarList()
  305. {
  306. $date = $_POST['date'];
  307. if ($date == '') {
  308. $res = array(
  309. 'code' => '1',
  310. 'info' => '缺少必要参数',
  311. 'data' => array()
  312. );
  313. return json_encode($res);
  314. }
  315. $key_word = $_POST['key_word'];
  316. // $key_word = 3468;
  317. $gpsModel = new BusGps();
  318. $res = $gpsModel->getCarList($date, $key_word);
  319. $res = array(
  320. 'code' => '0',
  321. 'info' => '数据获取成功',
  322. 'data' => $res
  323. );
  324. return json_encode($res);
  325. }
  326. /**
  327. * 根据日期、车牌号获取
  328. * User: Steven
  329. * @param $bus_no
  330. * @param $run_date
  331. * @param $begin_time
  332. * @param $end_time
  333. * @return string
  334. */
  335. public function actionGetHistoryData()
  336. {
  337. $bus_no = isset($_POST['bus_no']) ? $_POST['bus_no'] : false;
  338. $run_date = isset($_POST['run_date']) ? $_POST['run_date'] : false;
  339. $begin_time = isset($_POST['begin_time']) ? $_POST['begin_time'] : '00:00';
  340. $end_time = isset($_POST['end_time']) ? $_POST['end_time'] : '23:59';
  341. if (!$bus_no || !$run_date) {
  342. $res = array(
  343. 'code' => '1',
  344. 'info' => '缺少必要参数',
  345. 'data' => ''
  346. );
  347. return json_encode($res);
  348. }
  349. $gpsModel = new BusGps();
  350. $res = $gpsModel->getHistoryData($bus_no, $run_date, $begin_time, $end_time);
  351. $arr = array();
  352. if (!empty($res)) {
  353. foreach ($res as $item) {
  354. if ($item['delay_time'] >= 300) { //停留时间在5分钟及以上的点
  355. $x_y = $item['latitude'] . ',' . $item['longitude'];
  356. $location = $gpsModel->GeocodingAPI($x_y, 2);
  357. $arr[] = array(
  358. 'lat' => $item['latitude'],
  359. 'lng' => $item['longitude'],
  360. 'delay_time' => $item['delay_time'],
  361. 'create_time' => $item['create_time'],
  362. 'location' => $location['formatted_address']
  363. );
  364. } else {
  365. $arr[] = array(
  366. 'lat' => $item['latitude'],
  367. 'lng' => $item['longitude'],
  368. 'delay_time' => 0,
  369. 'create_time' => $item['create_time'],
  370. 'location' => ''
  371. );
  372. }
  373. }
  374. }
  375. $res = array(
  376. 'code' => '0',
  377. 'info' => '数据获取成功',
  378. 'data' => $arr
  379. );
  380. return json_encode($res);
  381. }
  382. /**
  383. * 采集GPS数据并保存 历史
  384. * User: Steven
  385. * @return string
  386. */
  387. /*public function actionUploadGpsData()
  388. {
  389. //获取资源数据
  390. $bus_list = BaseBus::find()->select(['bus_no'])->where(['org_id' => 628, 'cancel_flag' => 0])->asArray()->all();
  391. $car_list = '';
  392. foreach ($bus_list as $value) {
  393. $car_list .= $car_list == '' ? $value["bus_no"] : "," . $value["bus_no"];
  394. }
  395. $data = $this->actionGetGpsData($car_list);
  396. $result = json_decode($data, true);
  397. $gpsModel = new BusGps();
  398. $res = $gpsModel->uploadGpsData1($result);
  399. return json_encode($res);
  400. }*/
  401. /**
  402. * 采集GPS数据并保存
  403. * User: Steven
  404. * @return string
  405. */
  406. public function actionUploadGps()
  407. {
  408. //获取资源数据
  409. $bus_list = BaseBus::find()->select(['bus_no'])->where(['org_id' => 628, 'cancel_flag' => 0])->asArray()->all();
  410. $car_list = '';
  411. $time = date('Y-m-d H:i:s', strtotime('"-5 minute"'));
  412. foreach ($bus_list as $value) {
  413. $bus_no_str = $value["bus_no"] . '@' . $time;
  414. $car_list .= $car_list == '' ? $bus_no_str : ";" . $bus_no_str;
  415. }
  416. //$car_list='沪B61353,沪B83533,沪BE4317,沪BE4369,沪BG6985,沪BL3468,沪BL3478,沪BL3502,沪BL3508,沪BL8498,沪BQ2216,沪BT7893,沪BT7913,沪BT8050,沪BT8068,沪BT8393,沪D25686,沪D25696,沪D28940,沪D43505,沪D73782,沪D73788,沪D77475,沪DL6540,沪DL6712,沪DL6728,沪DL6805,沪DL6821,沪DL6861,沪DL6862,沪DL6871,沪DL6877,沪DL6880,沪DL6882,沪DL6886,沪DL6889,沪DL6891,沪DL6896,沪DL6897,沪DL6898,沪DL6900,沪DL6903,沪DL6906,沪BJZ930,沪DP3738,沪DP3777,沪DP3783,沪DP3792,沪DP3795,沪DP3845,沪DP3902,沪DP3983,沪DP3990,沪DP3992,沪DP3997,沪DP4017,沪DP4106,沪DP4181,沪DP4188,沪DP4202,沪DP4218,沪DP4256,沪DP4270,沪DP4276,浙L61060,浙L61062 ,沪BJZ607,沪BJR355,沪BJR607,沪BJR055,沪BJR082,沪DL6900,沪DP4218,沪D44812,沪BL3488,沪BJR139,沪BJR087';
  417. // $data = $this->actionGetGpsData($car_list);
  418. libxml_disable_entity_loader(false);
  419. $soap_service = \Yii::$app->soapGps;
  420. $result = $soap_service->LoadCarsRealInfo(['pool_CarLPN_DateTime' => $car_list , "lastQueryTime" => date("Y-m-d H:i:s"), "serviceIP" => "61.129.251.7", "servicePort" => '8873']);
  421. $result = $result->LoadCarsRealInfoResult;
  422. $result = str_replace('totalCount',"'totalCount'",$result);
  423. $result = str_replace("'",'"',$result);
  424. $result = json_decode($result,true);
  425. $gpsModel = new BusGps();
  426. $res = $gpsModel->uploadGpsData($result['list']);
  427. return json_encode($res);
  428. }
  429. /**
  430. * 每辆车安装GPS后需要将GPS 的ID 填到base_bus中的gps_id中
  431. * User: Steven
  432. * @return string
  433. */
  434. public function actionUploadBusGpsId()
  435. {
  436. //获取资源数据
  437. $bus_list = BaseBus::find()->select(['bus_no'])->where(['org_id' => 628, 'cancel_flag' => 0])->asArray()->all();
  438. $car_list = '';
  439. foreach ($bus_list as $value) {
  440. $car_list .= $car_list == '' ? $value["bus_no"] : "," . $value["bus_no"];
  441. }
  442. //$car_list='沪B61353,沪B83533,沪BE4317,沪BE4369,沪BG6985,沪BL3468,沪BL3478,沪BL3502,沪BL3508,沪BL8498,沪BQ2216,沪BT7893,沪BT7913,沪BT8050,沪BT8068,沪BT8393,沪D25686,沪D25696,沪D28940,沪D43505,沪D73782,沪D73788,沪D77475,沪DL6540,沪DL6712,沪DL6728,沪DL6805,沪DL6821,沪DL6861,沪DL6862,沪DL6871,沪DL6877,沪DL6880,沪DL6882,沪DL6886,沪DL6889,沪DL6891,沪DL6896,沪DL6897,沪DL6898,沪DL6900,沪DL6903,沪DL6906,沪BJZ930,沪DP3738,沪DP3777,沪DP3783,沪DP3792,沪DP3795,沪DP3845,沪DP3902,沪DP3983,沪DP3990,沪DP3992,沪DP3997,沪DP4017,沪DP4106,沪DP4181,沪DP4188,沪DP4202,沪DP4218,沪DP4256,沪DP4270,沪DP4276,浙L61060,浙L61062 ,沪BJZ607,沪BJR355,沪BJR607,沪BJR055,沪BJR082,沪DL6900,沪DP4218,沪D44812,沪BL3488,沪BJR139,沪BJR087';
  443. $data = $this->actionGetGpsData($car_list);
  444. $result = json_decode($data, true);
  445. if ($result['Flag']) {
  446. $main_cooperation_id = Yii::$app->user->identity->MAIN_CORP_ID2;
  447. $main_cooperation_id = 7;
  448. foreach ($result['Data'] as $item) {
  449. $res = BaseBus::findOne(['BUS_NO' => $item['vehicle']['plateCode'], 'CANCEL_FLAG' => 0, 'MAIN_CORP_ID' => $main_cooperation_id]);
  450. if ($res) {
  451. BaseBus::updateAll([
  452. 'GPS_ID' => $item['vehicle']['vehicleId'],
  453. ], "BUS_NO='{$item['vehicle']['plateCode']}' and CANCEL_FLAG=0 and MAIN_CORP_ID=$main_cooperation_id");
  454. }
  455. }
  456. }
  457. return true;
  458. }
  459. /**
  460. * 求两个日期之间相差的天数
  461. * (针对1970年1月1日之后,求之前可以采用泰勒公式)
  462. * @param string $day1
  463. * @param string $day2
  464. * @return number
  465. */
  466. public function diffBetweenTwoDays($day1, $day2)
  467. {
  468. $second1 = strtotime($day1);
  469. $second2 = strtotime($day2);
  470. if ($second1 < $second2) {
  471. $tmp = $second2;
  472. $second2 = $second1;
  473. $second1 = $tmp;
  474. }
  475. return ($second1 - $second2) / 86400;
  476. }
  477. /**
  478. * 计算两个时间戳之间相差的日时分秒
  479. * User: Steven
  480. * @param $begin_time 开始时间戳
  481. * @param $end_time 结束时间戳
  482. * @return array
  483. */
  484. function timediff($begin_time, $end_time)
  485. {
  486. if ($begin_time < $end_time) {
  487. $starttime = $begin_time;
  488. $endtime = $end_time;
  489. } else {
  490. $starttime = $end_time;
  491. $endtime = $begin_time;
  492. }
  493. //计算天数
  494. $timediff = $endtime - $starttime;
  495. $days = intval($timediff / 86400);
  496. //计算小时数
  497. $remain = $timediff % 86400;
  498. $hours = intval($remain / 3600);
  499. //计算分钟数
  500. $remain = $remain % 3600;
  501. $mins = intval($remain / 60);
  502. //计算秒数
  503. $secs = $remain % 60;
  504. $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
  505. return $res;
  506. }
  507. public function actionUp()
  508. {
  509. $mo = new BusGps();
  510. $mo->test();
  511. }
  512. //获取新GPS的实时位置的尝试
  513. public function getNewGpsData()
  514. {
  515. libxml_disable_entity_loader(false);
  516. $soap_service = \Yii::$app->soapGps;
  517. $re = $soap_service->AdapterRequest(['carLPN' => "沪D73788", "lastQueryTime" => date("Y-m-d H:i:s"), "serviceIP" => "61.129.251.7", "servicePort" => 8873]);
  518. echo $re;
  519. }
  520. }
  521. ?>