|
- <?php
-
- namespace backend\modules\motorcade\controllers;
-
- use backend\modules\motorcade\models\BaseBus;
- use yii\web\Controller;
- use backend\modules\motorcade\models\BusGps;
- use Yii;
- use yii\base\Module;
- // 指定允许其他域名访问
- header('Access-Control-Allow-Origin:*');
-
- class GpsController extends Controller
- {
- public $layout = "@backend/modules/motorcade/views/layouts/monitor";
- public $enableCsrfValidation = false;
- //恒栋GPS设备
- private $base_url = "http://api.hstsp.cn:8077/data/";
- private $account_id = "hdgps";
- private $account_pwd = "123456";
- //我们采购的GPS设备
- private $base_url_zz = 'http://api.gpsoo.net/';
- private $account_id_zz = "15618818219";
- private $account_pwd_zz = "123456";
-
- /*public function __construct(string $id, Module $module, array $config = [])
- {
- libxml_disable_entity_loader(false);
- parent::__construct($id, $module, $config);
- }*/
-
- /**
- * 车辆实时位置界面
- * User: Steven
- * @return string
- */
- public function actionMonitor()
- {
- return $this->render('gps', ['view' => 'monitor', 'data' => '']);
- }
-
- /**
- * 车辆行驶轨迹
- * User: Steven
- * @return string
- */
- public function actionHistoryTrack()
- {
- return $this->render('gps', ['view' => 'historyTrack', 'data' => '']);
- }
-
-
- /**
- * 获取GPS数据接口
- * User: Steven
- * @param $car_list 需要获取GPS数据的车辆列表
- * @return string
- */
- public function actionGetGpsData($car_no_list)
- {
- //获取资源数据
- /*$bus_list = BaseBus::find()->select(['bus_no'])->where(['org_id' => 628, 'cancel_flag' => 0])->asArray()->all();
- $car_list = '';
- foreach ($bus_list as $value) {
- $car_list .= $car_list == '' ? $value["bus_no"] : "," . $value["bus_no"];
- }*/
- $car_no_list = urlencode($car_no_list);
- //登录获取token
- $account_pwd = md5($this->account_pwd);
- $login_url = $this->base_url . "login/tokenDuration.do?account={$this->account_id}&passwd={$account_pwd}";
- $result = file_get_contents($login_url);
- $result_array = json_decode($result, true);
- $token_str = $result_array["tokenStr"];
-
- //获取需要查询的车牌号的ID
- $get_car_id_url = $this->base_url . "LogisticalCar/queryCarId.do?tokenStr={$token_str}&plateCodes={$car_no_list}";
- $result = file_get_contents($get_car_id_url);
- $result_array = json_decode($result, true);
- //查询车辆的GPS数据
- $car_id = '';
- foreach ($result_array["Data"] as $data_info) {
- $car_id .= $car_id == '' ? number_format($data_info["id"], 0, ".", "") : "," . number_format($data_info["id"], 0, ".", "");
- }
- $get_car_info_url = $this->base_url . "VehicleMonitor/queryMultiVehicleLocation.do?tokenStr={$token_str}&vehicleIds={$car_id}&mapType=1";
- $result = file_get_contents($get_car_info_url);
- //$result = json_decode($result, true);
- return $result;
- }
-
- /**
- * 获取蜘蛛gps数据
- * User: Steven
- */
- public function actionGetGpsZz()
- {
- $gpsModel = new BusGps();
- #region 1、获取access_token
- $login_url = $this->base_url_zz . "1/auth/access_token";
- $unix = time();
- $login_request = array(
- 'account' => $this->account_id_zz,
- 'time' => $unix,
- 'signature' => md5(md5($this->account_pwd_zz) . $unix)
- );
- $login_response = $gpsModel->httpsPost($login_url, $login_request);
- #endregion
-
- $login_response = json_decode($login_response, true);
- if ($login_response['ret'] == 0) {
- $common_arr = array(
- 'access_token' => $login_response['access_token'],
- 'account' => $this->account_id_zz,
- 'format' => 'json'
- );
- #region 2、获取一个账户名下所有设备最新位置信息
- /*$monitor_url = $this->base_url_zz . "1/account/monitor";
- $moni_request = array(
- 'target' => $this->account_id_zz,
- 'map_type' => 'BAIDU'
- );
- $new_request = array_merge($common_arr, $moni_request);
- $monitor_response = $gpsModel->httpsPost($monitor_url, $new_request);
- $monitor_response = json_decode($monitor_response, true);*/
- #endregion
- // 352477070153196 352477070154186 352477070154426 352477070156637 352477070156678
- $tracking_url = $this->base_url_zz . "1/devices/tracking";
- $tracking_request = array(
- 'imeis' => '352477070153196,352477070154186,352477070154426,352477070156637,352477070156678',
- 'map_type' => 'BAIDU'
- );
- $new_request = array_merge($common_arr, $tracking_request);
- $tracking_response = $gpsModel->httpsPost($tracking_url, $new_request);
- $tracking_response = json_decode($tracking_response, true);
- if ($tracking_response['ret'] == 0) {
- $res = $gpsModel->uploadGpsZz($tracking_response);
- }
-
- }
- return json_encode($res);
-
- }
-
- /**
- * 获取地图聚合数据接口
- * User: Steven
- */
- public function actionGetPolymer()
- {
- $bus_gps = new BusGps();
- $data = $bus_gps->getPolymerData();
- $bus_no_list = '';
- foreach ($data as $key => $item) {
- $x_y = $bus_gps->GeocodingAPI($item['title'], 1);
- $data[$key]['lat'] = $x_y['lat'];
- $data[$key]['lng'] = $x_y['lng'];
- $bus_no_list .= $bus_no_list == '' ? $item["car_list"] : "," . $item["car_list"];
- }
- $res = array(
- 'code' => '0',
- 'info' => '数据获取成功',
- 'data' => array(
- 'polymers' => $data,
- 'car_list' => $bus_no_list
- )
-
- );
- return json_encode($res);
- }
-
- /**
- * 点击聚合获取详细的车辆信息及运行状态
- * User: Steven
- * @param $position 市级名称 如果是空,则返回所有的车辆
- */
- public function actionGetCarData()
- {
- $bus_no_list = $_POST['car_list'];
- $first = isset($_POST['first']) ? $_POST['first'] : '';
- $gpsModel = new BusGps();
- // $data = $gpsModel->getNewGpsData($first, "'沪DP4017','沪DP4276', '沪DL6889', '沪D25696', '沪BL3478'");
- $data = $gpsModel->getNewGpsData($bus_no_list, $first);
-
- $arr = array();
- foreach ($data as $value) {
- $arr = $gpsModel->getCarDataService($value, $arr);
- }
- // $arr = [array (
- // 'plateCode' => '沪BL3478',
- // 'seat_desc' => '普通座45座',
- // 'bus_status' => 0,
- // 'bus_desc' => '空闲',
- // 'lat' => '31.1035846746921',
- // 'lng' => '121.67047346216',
- // 'dspeed' => '0',
- // 'direction' => '267',
- // 'driver' => '',
- // 'itinerary_name' => '',
- // 'start_time' => '',
- // 'end_time' => '',
- // )];
- // $arr = [array (
- // 'plateCode' => '沪BL3478',
- // 'seat_desc' => '普通座45座',
- // 'bus_status' => 0,
- // 'bus_desc' => '空闲',
- // 'lat' => '31.1463114948584',
- // 'lng' => '121.656098228153',
- // 'dspeed' => '0',
- // 'direction' => '267',
- // 'driver' => '',
- // 'itinerary_name' => '',
- // 'start_time' => '',
- // 'end_time' => '',
- // )];
-
- $res = array(
- 'code' => '0',
- 'info' => '数据获取成功',
- 'data' => array_values($arr)
- );
- return json_encode($res);
-
- }
-
- /**
- * 根据车牌号码获取当前车辆的实时数据
- * User: Steven
- * @param $car_no
- */
- public function actionGetCarDataByNo()
- {
- $car_no = isset($_POST['car_no']) ? '\'' . $_POST['car_no'] . '\'' : false;
- if (!$car_no) {
- $res = array(
- 'code' => '1',
- 'info' => '缺少必要参数',
- 'data' => array()
- );
- return json_encode($res);
- }
- $gpsModel = new BusGps();
- $data = $gpsModel->getNewGpsData($car_no);
- $arr = array();
- $arr = $gpsModel->getCarDataService($data[0], $arr);
- $res = array(
- 'code' => '0',
- 'info' => '数据获取成功',
- 'data' => array_values($arr)
- );
- return json_encode($res);
-
- }
-
- /**
- * 获取车辆运营概况统计数据接口
- * User: Steven
- */
- public function actionGetStatistics()
- {
- //获取资源数据
- $gpsModel = new BusGps();
- $data = $gpsModel->getNewGpsData();
- $car_list = array();
- if (empty($data)) {
-
- $res = array(
- 'code' => '0',
- 'info' => '无数据',
- 'data' => ''
- );
- } else {
- foreach ($data as $v) {
- $car_list[] = $v['bus_no'];
- }
- $gpsModel = new BusGps();
- $res = $gpsModel->getStatistics($car_list);
- $res = array(
- 'code' => '0',
- 'info' => '数据获取成功',
- 'data' => $res
- );
- }
-
- return json_encode($res);
- }
-
- /**
- * 获取车辆实时位置右侧面板的调派状态
- * User: Steven
- * @return string
- */
- public function actionDispatchState()
- {
- $main_cooperation_id = Yii::$app->user->identity->MAIN_CORP_ID2;
- //从内存获取需要显示的动态数据
- $res = \Yii::$app->cache->get('send_sure_' . $main_cooperation_id);
- $arr = array();
- if (!empty($res)) {
- if (count($res) > 3) {
- array_splice($res, 0, count($res) - 3);
- }
- \Yii::$app->cache->set('send_sure_' . $main_cooperation_id, $res);
- $date = date('Y-m-d H:i:s', time());
- foreach ($res as $key => $value) {
- $day = $this->timediff(strtotime($value['run_date']), strtotime($date));
- $arr[$key] = array(
- 'bus_number' => $value['bus_number'],
- 'bus_no' => $value['bus_no'],
- 'driver_name' => $value['driver_name'],
- 'line_name' => $value['line_name'],
- 'run_date' => date('Y-m-d', strtotime($value['run_date'])),
- 'time_desc' => (($day['day'] >= 1) ? $day['day'] . '天前' : ($day['hour'] >= 1 ? $day['hour'] . '小时前' : ($day['min'] >= 1 ? $day['min'] . '分钟前' : ($day['sec'] . '秒前'))))
- );
- }
- }
- $res = array(
- 'code' => '0',
- 'info' => '数据获取成功',
- 'data' => $arr
- );
- return json_encode($res);
- }
-
- /**
- * 获取历史轨迹页面左侧车辆列表
- * User: Steven
- * @param $date
- * @return string
- */
- public function actionGetCarList()
- {
- $date = $_POST['date'];
- if ($date == '') {
- $res = array(
- 'code' => '1',
- 'info' => '缺少必要参数',
- 'data' => array()
- );
- return json_encode($res);
- }
- $key_word = $_POST['key_word'];
- // $key_word = 3468;
- $gpsModel = new BusGps();
- $res = $gpsModel->getCarList($date, $key_word);
- $res = array(
- 'code' => '0',
- 'info' => '数据获取成功',
- 'data' => $res
- );
- return json_encode($res);
- }
-
- /**
- * 根据日期、车牌号获取
- * User: Steven
- * @param $bus_no
- * @param $run_date
- * @param $begin_time
- * @param $end_time
- * @return string
- */
- public function actionGetHistoryData()
- {
- $bus_no = isset($_POST['bus_no']) ? $_POST['bus_no'] : false;
- $run_date = isset($_POST['run_date']) ? $_POST['run_date'] : false;
- $begin_time = isset($_POST['begin_time']) ? $_POST['begin_time'] : '00:00';
- $end_time = isset($_POST['end_time']) ? $_POST['end_time'] : '23:59';
- if (!$bus_no || !$run_date) {
- $res = array(
- 'code' => '1',
- 'info' => '缺少必要参数',
- 'data' => ''
- );
- return json_encode($res);
- }
- $gpsModel = new BusGps();
- $res = $gpsModel->getHistoryData($bus_no, $run_date, $begin_time, $end_time);
- $arr = array();
- if (!empty($res)) {
- foreach ($res as $item) {
- if ($item['delay_time'] >= 300) { //停留时间在5分钟及以上的点
- $x_y = $item['latitude'] . ',' . $item['longitude'];
- $location = $gpsModel->GeocodingAPI($x_y, 2);
- $arr[] = array(
- 'lat' => $item['latitude'],
- 'lng' => $item['longitude'],
- 'delay_time' => $item['delay_time'],
- 'create_time' => $item['create_time'],
- 'location' => $location['formatted_address']
- );
- } else {
- $arr[] = array(
- 'lat' => $item['latitude'],
- 'lng' => $item['longitude'],
- 'delay_time' => 0,
- 'create_time' => $item['create_time'],
- 'location' => ''
- );
- }
- }
- }
- $res = array(
- 'code' => '0',
- 'info' => '数据获取成功',
- 'data' => $arr
- );
- return json_encode($res);
- }
-
-
- /**
- * 采集GPS数据并保存 历史
- * User: Steven
- * @return string
- */
- /*public function actionUploadGpsData()
- {
- //获取资源数据
- $bus_list = BaseBus::find()->select(['bus_no'])->where(['org_id' => 628, 'cancel_flag' => 0])->asArray()->all();
- $car_list = '';
- foreach ($bus_list as $value) {
- $car_list .= $car_list == '' ? $value["bus_no"] : "," . $value["bus_no"];
- }
- $data = $this->actionGetGpsData($car_list);
- $result = json_decode($data, true);
- $gpsModel = new BusGps();
- $res = $gpsModel->uploadGpsData1($result);
- return json_encode($res);
- }*/
-
-
- /**
- * 采集GPS数据并保存
- * User: Steven
- * @return string
- */
- public function actionUploadGps()
- {
- //获取资源数据
- $bus_list = BaseBus::find()->select(['bus_no'])->where(['org_id' => 628, 'cancel_flag' => 0])->asArray()->all();
- $car_list = '';
- $time = date('Y-m-d H:i:s', strtotime('"-5 minute"'));
- foreach ($bus_list as $value) {
- $bus_no_str = $value["bus_no"] . '@' . $time;
- $car_list .= $car_list == '' ? $bus_no_str : ";" . $bus_no_str;
- }
- //$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';
- // $data = $this->actionGetGpsData($car_list);
- libxml_disable_entity_loader(false);
- $soap_service = \Yii::$app->soapGps;
- $result = $soap_service->LoadCarsRealInfo(['pool_CarLPN_DateTime' => $car_list , "lastQueryTime" => date("Y-m-d H:i:s"), "serviceIP" => "61.129.251.7", "servicePort" => '8873']);
- $result = $result->LoadCarsRealInfoResult;
- $result = str_replace('totalCount',"'totalCount'",$result);
- $result = str_replace("'",'"',$result);
- $result = json_decode($result,true);
- $gpsModel = new BusGps();
- $res = $gpsModel->uploadGpsData($result['list']);
- return json_encode($res);
- }
-
-
- /**
- * 每辆车安装GPS后需要将GPS 的ID 填到base_bus中的gps_id中
- * User: Steven
- * @return string
- */
- public function actionUploadBusGpsId()
- {
- //获取资源数据
- $bus_list = BaseBus::find()->select(['bus_no'])->where(['org_id' => 628, 'cancel_flag' => 0])->asArray()->all();
- $car_list = '';
- foreach ($bus_list as $value) {
- $car_list .= $car_list == '' ? $value["bus_no"] : "," . $value["bus_no"];
- }
- //$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';
- $data = $this->actionGetGpsData($car_list);
- $result = json_decode($data, true);
- if ($result['Flag']) {
- $main_cooperation_id = Yii::$app->user->identity->MAIN_CORP_ID2;
- $main_cooperation_id = 7;
- foreach ($result['Data'] as $item) {
- $res = BaseBus::findOne(['BUS_NO' => $item['vehicle']['plateCode'], 'CANCEL_FLAG' => 0, 'MAIN_CORP_ID' => $main_cooperation_id]);
- if ($res) {
- BaseBus::updateAll([
- 'GPS_ID' => $item['vehicle']['vehicleId'],
- ], "BUS_NO='{$item['vehicle']['plateCode']}' and CANCEL_FLAG=0 and MAIN_CORP_ID=$main_cooperation_id");
- }
- }
-
- }
- return true;
- }
-
-
- /**
- * 求两个日期之间相差的天数
- * (针对1970年1月1日之后,求之前可以采用泰勒公式)
- * @param string $day1
- * @param string $day2
- * @return number
- */
- public function diffBetweenTwoDays($day1, $day2)
- {
- $second1 = strtotime($day1);
- $second2 = strtotime($day2);
-
- if ($second1 < $second2) {
- $tmp = $second2;
- $second2 = $second1;
- $second1 = $tmp;
- }
- return ($second1 - $second2) / 86400;
- }
-
- /**
- * 计算两个时间戳之间相差的日时分秒
- * User: Steven
- * @param $begin_time 开始时间戳
- * @param $end_time 结束时间戳
- * @return array
- */
- function timediff($begin_time, $end_time)
- {
- if ($begin_time < $end_time) {
- $starttime = $begin_time;
- $endtime = $end_time;
- } else {
- $starttime = $end_time;
- $endtime = $begin_time;
- }
- //计算天数
- $timediff = $endtime - $starttime;
- $days = intval($timediff / 86400);
- //计算小时数
- $remain = $timediff % 86400;
- $hours = intval($remain / 3600);
- //计算分钟数
- $remain = $remain % 3600;
- $mins = intval($remain / 60);
- //计算秒数
- $secs = $remain % 60;
- $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
- return $res;
- }
-
- public function actionUp()
- {
- $mo = new BusGps();
- $mo->test();
- }
-
- //获取新GPS的实时位置的尝试
- public function getNewGpsData()
- {
- libxml_disable_entity_loader(false);
- $soap_service = \Yii::$app->soapGps;
- $re = $soap_service->AdapterRequest(['carLPN' => "沪D73788", "lastQueryTime" => date("Y-m-d H:i:s"), "serviceIP" => "61.129.251.7", "servicePort" => 8873]);
- echo $re;
- }
-
-
- }
-
-
- ?>
|