Browse Source

从redis获取主页信息

git 6 years ago
parent
commit
19d5da2f0b

+ 2 - 0
.gitignore

@@ -3,3 +3,5 @@ vendor/
 .data/
 composer.lock
 .debug
+uploadimage/
+boke/

+ 2 - 3
Home/Common/pub_func.php

@@ -15,14 +15,13 @@
 		$topic = $rk->newTopic($topic);
 		/********************* 初始化生产者配置项 end **************************/
 		if( empty($msg_data) ){
-			var_dump($msg_data);
 			return;
 		}
 		if( is_array($msg_data) || is_object($msg_data) ){ // 对象、数组转换
 			$msg_data = json_encode($msg_data);
 		}
 		$topic->produce(RD_KAFKA_PARTITION_UA, 0, $msg_data);
-		$rk->poll(0);
+		var_dump($rk->poll(0));
 		
 	}
 	
@@ -33,7 +32,7 @@
 		  "title":"群推消息",
 		  "content":"这是一个广播"
 		}';
-		kafkaProducer('gps_alarm_msg_queue',$msg_data);
+		kafkaProducer('gps_location_data',$msg_data);
 	}
 	
 

+ 0 - 4
Home/Conf/config.php

@@ -4,10 +4,6 @@ return array(
 	
 	'TOKEN_OVERTIME' => 410,
 	'FAIL' => 600,
-
-	######### 极光推 #########
-	'JPUSH_APP_KEY' => '58e15e345e0c21b7e638e186',
-	'JPUSH_MASTER_SECRET' => 'a91d0b1c1fa1b327db832506',
 	######### 消息推送类型 #########
     'OUTAGE_ALARM' 			=> 1, // 断电报警
     'SOS_ALARM' 			=> 2, // SOS报警

+ 60 - 34
Home/Lib/Action/ApiAction.class.php

@@ -124,17 +124,38 @@ class ApiAction extends Action {
 	public  function get_map_index_data(  ){
 		$this->token_verify();
 		$deviceNumber = $_GET['data']['deviceNumber'];
+		$licensePlate = $_GET['data']['licensePlate'];
 		
 		if(!$deviceNumber){
 			$this->api_fail(C('FAIL'),'faild, no deviceNumber !');
 		}
 		
-		//获取车辆最新位置
+		//获取车辆最新位置mysql
+		/*
 		$result = M('jms_vehicle')->field('GpsLongitude, GpsLatitude, Address, FenceShapeInfo, LockStatus, FenceAlarmEnable, Battery, GpsOnlineTime, Speed, CityId')->where(array('DeviceNumber' => $deviceNumber))->find();
+		*/
+		
+		//redis获取
+		$vehicle = Redis('fdrc_vehicle','hash');
+		//var_dump($vehicle);
+		$vehicleInfo = $vehicle->get($deviceNumber);
+		$vehicleInfo = json_decode($vehicleInfo, true);
+		
+		
+		$rlfd_vehicle_fence = Redis('rlfd_vehicle_fence','hash');
+		$fence = $rlfd_vehicle_fence->get($licensePlate);
+		//$fence = json_decode($fence, true);
+		
+		$lastLocReis = Redis('rfld_gps_last_location','hash');
+		$lastLoction = $lastLocReis->get(strtoupper($gpsNumber));
+		$lastLoction = $lastLoction = json_decode($lastLocation, true);
+		
+		/*
 		if(!$result){
-			$this->api_fail(C('FAIL'),'cant find this vehicl !');
+			$this->api_fail(C('FAIL'),'cant find this vehicle !');
 		}
-		$onlineTimeStamp = strtotime($result['GpsOnlineTime']);
+		*/
+		$onlineTimeStamp = strtotime($lastLoction['GpsOnlineTime']);
 		if(!$onlineTimeStamp){
 			$onlineTimeStamp = 0;
 		}
@@ -149,12 +170,12 @@ class ApiAction extends Action {
 		}
 		*/
 		$mapData = array(
-			'vehicleLocation' => array('longitude' => $result['GpsLongitude'], 'latitude' => $result['GpsLatitude']),
-			'vehicleAddress' => $result['Address'],
-			'lockStatus' => $result['LockStatus'],
-			'fenceShapeInfo' => $result['FenceShapeInfo'],
-			'battery' => $result['Battery'],
-			'fenceAlarmStatus' => $result['FenceAlarmEnable'],
+			'vehicleLocation' => array('longitude' => $lastLoction['GpsLongitude'], 'latitude' => $lastLoction['GpsLatitude']),
+		//	'vehicleAddress' => $result['Address'],
+			'lockStatus' => $vehicleInfo['LockStatus'],
+			'fenceShapeInfo' => $fence,
+			'battery' => $lastLoction['Battery'],
+		//	'fenceAlarmStatus' => $result['FenceAlarmEnable'],
 			'onlineTime' => $onlineTimeStamp,
 			'speed' => $result['Speed']
 		);
@@ -521,14 +542,15 @@ class ApiAction extends Action {
 		if( !$gps_number || !$search_date){
 			json_fail('请检查GPS标签或者日期 !');
 		}
-		$list = $this->getBTGpsRoute($search_date,$gps_number);
-		//$list = $this->getBTGpsRoute($search_date, '1');
-		
+		//$list = $this->getBTGpsRoute($search_date,'1');
+		$list = $this->getBTGpsRoute($search_date, $gps_number);  //test
+		$lngLatAlter = new \Jms\Algo\Geometry();
 		$respData = array();
 		foreach($list as $v){
+			$alterRes = $lngLatAlter->convertBd09ToGcj02($v['Latitude'], $v['Longitude']);
 			$nv = array(
-				'lat' => $v['Latitude'],
-				'lng' => $v['Longitude'],
+				'lat' => $alterRes['lat'],
+				'lng' => $alterRes['lng'],
 				'speed' => $v['Speed'],
 				'deviceTime' => $v['OnlineTime']
 			);
@@ -582,27 +604,10 @@ class ApiAction extends Action {
 		}
 		$drivingData = array();
 		
-		$todayDate = date('Y-m-d');
-		$todayCache = S($gpsNumber.'_cache'.$todayDate);
-		if(!$todayCache){
-			$todayData = $this->searchDrivingData($gpsNumber, $date);
-			array_push($drivingData, $todayData);
-			$todayCache = S($gpsNumber.'_cache'.$todayData, $todayData, 3600*24);
-		}else{
-			$newOnlineTime = M('jms_vehicle')->where(array('GpsDeviceNumber' => $gpsNumber))->getField('GpsOnlineTime');
-			$nTime = strtotime($newOnlineTime);
-			$oTime = strtotime($lastOnlineTime);
-			if($nTime == $oTime){
-				array_push($drivingData, $todayCache);
-			}else{
-				$todayData = $this->searchDrivingData($gpsNumber, $date);
-				array_push($drivingData, $todayData);
-				$todayCache = S($gpsNumber.'_cache'.$todayDate, $todayData, 3600*24);
-			}
-		}
+		
 		
 		for($i = $days-1; $i > 0; --$i){
-			$preStr2time = strtotime('-'.$i.'days', 1555689600);
+			$preStr2time = strtotime('-'.$i.'days');
 			$date = date('Y-m-d', $preStr2time);
 		/*	
 			$where = array(
@@ -632,6 +637,26 @@ class ApiAction extends Action {
 			}
 			
 		}
+		//当日
+		$todayDate = date('Y-m-d');
+		
+		$todayCache = S($gpsNumber.'_cache'.$todayDate);
+		if(!$todayCache){
+			$todayData = $this->searchDrivingData($gpsNumber, $todayDate);
+			array_push($drivingData, $todayData);
+			$todayCache = S($gpsNumber.'_cache'.$todayData, $todayData, 3600*24);
+		}else{
+			$newOnlineTime = M('jms_vehicle')->where(array('GpsDeviceNumber' => $gpsNumber))->getField('GpsOnlineTime');
+			$nTime = strtotime($newOnlineTime);
+			$oTime = strtotime($lastOnlineTime);
+			if($nTime == $oTime){
+				array_push($drivingData, $todayCache);
+			}else{
+				$todayData = $this->searchDrivingData($gpsNumber, $date);
+				array_push($drivingData, $todayData);
+				$todayCache = S($gpsNumber.'_cache'.$todayDate, $todayData, 3600*24);
+			}
+		}
 		
 		//dump($drivingData);
 		if(!$drivingData){
@@ -721,7 +746,8 @@ class ApiAction extends Action {
 	public  function get_vehicle_ElectronicPlateUrl(  ){
 		$this->token_verify();
 		$userid = I('userid');
-		$electronicPlateUrl = M('jms_vehicle')->where(array('ID'=>$userid))->field('FrontElectronicPlateUrl,BackElectronicPlateUrl')->cache(true,24*60*60)->select();
+		$result = M('jms_vehicle')->where(array('ID'=>$userid))->field('FrontElectronicPlateUrl,BackElectronicPlateUrl')->cache('key',86400)->select();
+		$electronicPlateUrl = S('key');
 		$this->api_success('成功',$electronicPlateUrl[0]);
 	}
 	

+ 136 - 3
Home/Lib/Action/CronAction.class.php

@@ -21,7 +21,7 @@ class CronAction extends Action {
 			if($licensPlate){
 				echo 'pop licensPlate:'.$licensPlate.PHP_EOL;
 				$field = 'LicensPlate,VehicleColor, FullName, Address, FrameNumber, MotorNumber, VehicleBrand';
-				$vehicleInfo = M('jms_vehicle')->where('LicensPlate' => $licensPlate)->field($field);
+				$vehicleInfo = M('jms_vehicle')->where(array('LicensPlate' => $licensPlate))->field($field);
 				$localPath = '生成函数';
 				$up2ossWait = json_encode(array('licensPlate' => $licensPlate, 'localPath' => $localPath));
 				$plateLocalPath->push($up2ossWait);
@@ -93,8 +93,141 @@ class CronAction extends Action {
  
 	public  function acrossAlarm2Kafka(  ){
 		// 从 topic :gps_location_data 取轨迹
-			
-		// 存入 topic:gps_alarm_msg_queue 
+		$conf = new RdKafka\Conf();
+		// Set a rebalance callback to log partition assignments (optional)(当有新的消费进程加入或者退出消费组时,kafka 会自动重新分配分区给消费者进程,这里注册了一个回调函数,当分区被重新分配时触发)
+		$conf->setRebalanceCb(function (RdKafka\KafkaConsumer $kafka, $err, array $partitions = null) {
+		    switch ($err) {
+		        case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS:
+		            echo "Assign: ";
+		            var_dump($partitions);
+		            $kafka->assign($partitions);
+		            break;
+		
+		         case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS:
+		             echo "Revoke: ";
+		             var_dump($partitions);
+		             $kafka->assign(NULL);
+		             break;
+		
+		         default:
+		            throw new \Exception($err);
+		    }
+		});
+		// Configure the group.id. All consumer with the same group.id will consume( 配置groud.id 具有相同 group.id 的consumer 将会处理不同分区的消息,所以同一个组内的消费者数量如果订阅了一个topic, 那么消费者进程的数量多于这个topic 分区的数量是没有意义的。)
+		// different partitions.
+		$conf->set('group.id', 'myConsumerGroup');
+		// Initial list of Kafka brokers(添加 kafka集群服务器地址)
+		$conf->set('metadata.broker.list', '127.0.0.1');
+		$topicConf = new RdKafka\TopicConf();
+		// Set where to start consuming messages when there is no initial offset in
+		// offset store or the desired offset is out of range.
+		// 'smallest': start from the beginning
+		$topicConf->set('auto.offset.reset', 'smallest');
+		// Set the configuration to use for subscribed/assigned topics
+		$conf->setDefaultTopicConf($topicConf);
+		$consumer = new RdKafka\KafkaConsumer($conf);
+		// 订阅轨迹数据topic
+		$consumer->subscribe(['gps_location_data']);
+		while (true) {
+		    $message = $consumer->consume(120*1000);
+		    switch ($message->err) {
+		        case RD_KAFKA_RESP_ERR_NO_ERROR:
+		           // 判断是否超出围栏范围 ,存入 topic:gps_alarm_msg_queue 
+						 $route_info = json_decode($message->payload,true);
+						 if( empty($route_info) ){
+							 echo 'empty route info.';
+							 break;
+						 }
+						 $result = $this->produceAcrossAlarmData($route_info);
+						 if($result){
+							echo $result;
+						 }
+		            break;
+		        case RD_KAFKA_RESP_ERR__PARTITION_EOF:
+		            echo "No more messages; will wait for more\n";
+		            break;
+		        case RD_KAFKA_RESP_ERR__TIMED_OUT:
+		            echo "Timed out\n";
+		            break;
+		        default:
+		            throw new \Exception($message->errstr(), $message->err);
+		            break;
+		    }
+		}
+	}
+	
+ 
+	private  function produceAcrossAlarmData( $route_info ){
+		if( !$route_info['DeviceId'] ){
+			return 'device id is not exists.';
+		}
+		if( !$route_info['Longitude'] ){
+			return 'longitude is not exists.';
+		}
+		if( !$route_info['Latitude'] ){
+			return 'latitude is not exists.';
+		}
+		// 从数据库中取出车牌号,缓存1天
+		if( S('plate-'.$route_info['DeviceId']) ){
+			$plate = S('plate-'.$route_info['DeviceId']);
+		}else{
+			$where = array('GpsDeviceNumber'=>$route_info['DeviceId']);
+			$plate = M('jms_vehicle')->where($where)->getField('LicensePlate');
+			S('plate-'.$route_info['DeviceId'], $plate, 24*60*60);
+		}
+		// 是否启用围栏
+		$rlfd_vehicle_fence = Redis('rlfd_vehicle_fence','hash');
+		$fence = $rlfd_vehicle_fence->get($plate);
+		$fence = json_decode($fence, true);
+		if( empty($fence) ){
+			return false;
+		}
+		if( !$fence['fenceStatus'] ){
+			return false;
+		}
+		$fence_info = json_decode($fence['fenceInfo'], true);
+		if( empty($fence_info['data']) ){
+			return false;
+		}
+		// 是否越界
+		$route_point = array(
+			'lng' => $route_info['Longitude'],
+			'lat' => $route_info['Latitude']
+		);
+		$result = true; // 默认在围栏内
+		if( $fence_info['type'] == 'circle' ){ // 圆形围栏
+			$distance = \Jms\Algo\Geometry::distanceBetween2BdPoints($fence_info['data']['center'], $route_point); //获取轨迹点到围栏中心点间距离,km
+			$result = $distance/1000 < $fence_info['data']['radius'];// 距离圆心大于半径说明越界了
+		}elseif( $fence_info['type'] == 'polygon' ){ // 多边形围栏
+			$result = \Jms\Algo\Geometry::isInPolygon($fence_info['data']['vertex'], $route_point);
+		}else{
+			return false;
+		}
+		if( !$result ){
+			$alarm_data = array(
+			  "type" => C('FENCE_ALARM'),
+			  "title" => "超出电子围栏",
+			  "content" => "您的车辆已超过设置的电子围栏范围,请前往停车处确认是否被盗。",
+			  "device_number" => $route_info['DeviceId']
+			);
+			kafkaProducer('gps_alarm_msg_queue', $alarm_data); // 添加到kafka
+		}
+	}
+	
+ 
+	public  function mockProduce(  ){
+		$msg_data = array(
+			'DeviceId' => 110119120,
+			'State' => 1,
+			'Speed' => 1.2,
+			'Longitude' => 120.20638,
+			'Latitude' => 30.18852,
+			'DeviceTime' => date('Y-m-d H:i:s'),
+			'LBS' => 'LBS',
+			'Direction' => 's',
+		);
+		$msg_data = json_encode($msg_data, JSON_UNESCAPED_UNICODE);
+		kafkaProducer('gps_location_data',$msg_data);
 	}
 	
 

+ 15 - 41
Home/Lib/Action/NoticeAction.class.php

@@ -112,12 +112,22 @@ class NoticeAction extends Action {
 		  "content":"这是一个广播"
 		}'; 
 		*/ 
-		
+		/*
+			 'OUTAGE_ALARM' 			=> 1, // 断电报警
+		    'SOS_ALARM' 			=> 2, // SOS报警
+		    'LOWWER_BATTERY_ALARM' 	=> 3, // 电池低电报警
+		    'SHAKE_ALARM' 			=> 4, // 震动报警
+		    'SHIFT_ALARM' 			=> 5, // 位移报警
+			 'LOCK_VEHICLE_ALARM'    => 6, // 上锁车辆异动告警
+		    'STOLEN_ALARM' 		    => 7, // 被盗告警
+		    'FENCE_ALARM' 			=> 8, // 电子围栏告警
+		    'BROADCASTING'			=> 9, // 广播
+		*/
 		$single_push_type = array(
-			C('LOCK_VEHICLE_ALARM'),   // 1-锁车告警
-			C('STOLEN_ALARM'),       // 2-被盗告警
-			C('FENCE_ALARM'),        // 3-电子围栏告警
-			C('LOWWER_BATTERY_ALARM'),  // 4-低电量告警
+			C('LOCK_VEHICLE_ALARM'),   // 6-锁车告警
+			C('STOLEN_ALARM'),       // 7-被盗告警
+			C('FENCE_ALARM'),        // 8-电子围栏告警
+			C('LOWWER_BATTERY_ALARM'),  // 3-低电量告警
 		);
 		$msg_data = json_decode($msg_data,true);
 		if(!$msg_data){
@@ -232,41 +242,5 @@ class NoticeAction extends Action {
 		
 	}
 	
- 
-	public  function kafkaProducer( $topic, $msg_data ){
-		if (!extension_loaded('rdkafka')){
-			echo 'rdkafka extension is not installed!!'.PHP_EOL;
-			return false;
-		}
-		/********************* 初始化生产者配置项 start **************************/
-		//考勤记录分析结果生产者
-		$rk = new RdKafka\Producer();
-		$rk->setLogLevel(LOG_DEBUG);
-		$rk->addBrokers("127.0.0.1");
-		$start = microtime(true);
-		$topic = $rk->newTopic($topic);
-		/********************* 初始化生产者配置项 end **************************/
-		if( empty($msg_data) ){
-			var_dump($msg_data);
-			return;
-		}
-		if( is_array($msg_data) || is_object($msg_data) ){ // 对象、数组转换
-			$msg_data = json_encode($msg_data);
-		}
-		$topic->produce(RD_KAFKA_PARTITION_UA, 0, $msg_data);
-		$rk->poll(0);
-		
-	}
-	
- 
-	public  function mockProduce(  ){
-		$msg_data = '{
-		  "type":9,
-		  "title":"群推消息",
-		  "content":"这是一个广播"
-		}';
-		$this->kafkaProducer('gps_alarm_msg_queue',$msg_data);
-	}
-	
 
 }