Browse Source

推送加入检测

git 5 years ago
parent
commit
0b5dab8203
4 changed files with 60 additions and 27 deletions
  1. 14 0
      Home/Common/pub_func.php
  2. 14 10
      Home/Lib/Action/CronAction.class.php
  3. 31 16
      Home/Lib/Action/NoticeAction.class.php
  4. 1 1
      czapp

+ 14 - 0
Home/Common/pub_func.php

@@ -36,4 +36,18 @@
 		kafkaProducer('gps_location_data',$msg_data);
 	}
 	
+ 
+	function debug_log( $filename, $data ){
+		if(!APP_DEBUG){
+			return;
+		}
+		$file = SOLUTION_LOG_PATH .APP_PREFIX .'/'.date("Ymd", time()) ."/".$filename.".log";
+		$folder=dirname($file);
+		if (!is_dir($folder)){
+			mkdir($folder,0777,true);
+		}
+		echo '[' . date('Y-m-d H:i:s') . ']' . $data . PHP_EOL;
+		file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $data . PHP_EOL,FILE_APPEND);
+	}
+	
 

+ 14 - 10
Home/Lib/Action/CronAction.class.php

@@ -202,8 +202,11 @@ class CronAction extends Action {
 		// 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');
+		if( !C('KAFKA_BROKER_LIST') ){
+			echo 'please set broker list !!! ';
+		}
 		// Initial list of Kafka brokers(添加 kafka集群服务器地址)
-		$conf->set('metadata.broker.list', '127.0.0.1');
+		$conf->set('metadata.broker.list', C('KAFKA_BROKER_LIST'));
 		$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.
@@ -227,6 +230,7 @@ class CronAction extends Action {
 						 $result = $this->produceAcrossAlarmData($route_info);
 						 if($result){
 							echo $result,PHP_EOL;
+							debug_log('across_alarm',$result['message']);
 						 }
 		            break;
 		        case RD_KAFKA_RESP_ERR__PARTITION_EOF:
@@ -325,17 +329,17 @@ class CronAction extends Action {
 		
 		// fly 轨迹数据
 		$msg_data = array(
-			'StationCode' => '30B5F10116D7',
+			'StationCode' => '30B5F101237D',
+			"Longitude" => "120.600889",
+			"Latitude" => "30.191478",
+			"Address" => "华城·和瑞科技广场(长河路475号)",
 			'StationType' => 0,
-			"CityId" => "10",
-			"StationName" => "测试基站2",
-			"Longitude" => "114.251604",
-			"Latitude" => "22.725217",
-			"Address" => "建设大厦1",
-			"AddTime" => "2019-05-21 15:00:01",
-			"VehicleNumber" => "FFFFFF000003",
+			"CityId" => "2902",
+			"StationName" => "基站名称",
+			"AddTime" => date('Y-m-d H:i:s',strtotime('-3 minutes')),
+			"VehicleNumber" => "86412717AC",
 			"VehicleStatus" =>  "1",
-		    "SignalCount" => 88,
+			"SignalCount" => 88,
 			"OnlineTime" => date('Y-m-d H:i:s',strtotime('-2 minutes'))
 		);
 		kafkaProducer('fly_vroute_data',$msg_data);

+ 31 - 16
Home/Lib/Action/NoticeAction.class.php

@@ -55,7 +55,10 @@ class NoticeAction extends Action {
 		$conf->set('group.id', 'myConsumerGroup');
 		
 		// Initial list of Kafka brokers(添加 kafka集群服务器地址)
-		$conf->set('metadata.broker.list', '127.0.0.1');
+		if( !C('KAFKA_BROKER_LIST') ){
+			echo 'please set broker list !!! ';
+		}
+		$conf->set('metadata.broker.list', C('KAFKA_BROKER_LIST'));
 		
 		$topicConf = new RdKafka\TopicConf();
 		
@@ -79,9 +82,17 @@ class NoticeAction extends Action {
 		           $msg_data = json_decode($message->payload,true);
 						if($msg_data){
 						   // 使用极光推送消息
+							if(C('JPUSH_APP_KEY') || C('JPUSH_MASTER_SECRET')){
+								echo 'jpush app key or secret not exists',PHP_EOL;
+								break;
+							}
 							$jpush_client = new \JPush\Client( C('JPUSH_APP_KEY'), C('JPUSH_MASTER_SECRET') );
 							// 电子围栏告警,上锁车辆异动告警,低电量告警,被盗告警,广播消息推送
-							$this->jpushMsg( $jpush_client, $msg_data );
+							$result = $this->jpushMsg( $jpush_client, $msg_data );
+							if($result){
+								echo $result['message'],PHP_EOL;
+								debug_log('jpush',$result['message']);
+							}
 						}
 		            break;
 		        case RD_KAFKA_RESP_ERR__PARTITION_EOF:
@@ -98,7 +109,7 @@ class NoticeAction extends Action {
 	}
 	
  
-	public  function jpushMsg( $client, $msg_data ){
+	private  function jpushMsg( $client, $msg_data ){
 		/*
 		$msg_data = '{
 			  "type":2,
@@ -123,10 +134,8 @@ class NoticeAction extends Action {
 			C('FENCE_ALARM'),        // 8-电子围栏告警
 		);
 		if(!is_array($msg_data)){
-			echo 'invalid message data format!'.$msg_data . PHP_EOL;
-			return;
+			return array( 'success' => false , 'message' => 'invalid message data format!'.$msg_data);
 		}
-		
 		// 通过传过来的车牌查出 JgClientRegistrationId
 		if( empty($client) ){
 			$client = new \JPush\Client( C('JPUSH_APP_KEY'), C('JPUSH_MASTER_SECRET') );
@@ -137,24 +146,24 @@ class NoticeAction extends Action {
 				$last_alarm_time = S('last_fence_alarm_'.$msg_data['device_number']);
 				$interval_time = C('FENCE_ALARM_INTERVAL')*60;
 				if( time() - $last_alarm_time < $interval_time ){
-					echo $msg_data['device_number'] . ' 围栏告警时间间隔:'.C('FENCE_ALARM_INTERVAL').'分钟,最后一次告警时间:'.$last_alarm_time . PHP_EOL;
-					return;
+					return array( 'success' => false , 'message' => $msg_data['device_number'] . ' 围栏告警时间间隔:'.C('FENCE_ALARM_INTERVAL').'分钟,最后一次告警时间:'.$last_alarm_time );
 				}
 			}
 			//  取车辆数据
 			$where = array('DeviceNumber|GpsDeviceNumber' => $msg_data['device_number']);
 			$fields= 'CityId,LicensePlate,FullName,JgClientRegistrationId';
-			
 			if( !S( 'jpush_vinfo_'.$msg_data['device_number'] ) ){ //如果不存在,则缓存
 				$vehicle_info = M('jms_vehicle')->field($fields)->where($where)->find();
 				S( 'jpush_vinfo_'.$msg_data['device_number'], $vehicle_info, 60 ); //缓存1分钟
 			}
 			$vehicle_info = S( 'jpush_vinfo_'.$msg_data['device_number'] ); //从缓存取
+			if(!$vehicle_info['JgClientRegistrationId']){
+				return array('success'=> false,'message'=>'jpush registration id not exists!');
+			}
 		}elseif( $msg_data['type'] == C('BROADCASTING') ){
 			//广播
 		}else{
-			echo '未知的告警类型:'.$msg_data['type'].PHP_EOL;
-			return;
+			return array( 'success' => false , 'message' => '未知的告警类型:'.$msg_data['type'] );
 		}
 		// 推送平台
 		$platform = array('ios', 'android');
@@ -203,13 +212,15 @@ class NoticeAction extends Action {
 			 			'code' => $response['http_code'],
 				 		'resp_msg' => 'ok',
 			 );
-			 $this->saveLog($msg_data);
-		    print_r($response);
+			 if(!$this->saveLog($msg_data)){
+				return array( 'success' => false , 'message' => 'add log failed 1' );
+			 }
 			if( $response['http_code'] == 200 ){ //如果成功了
 				if( C('FENCE_ALARM_INTERVAL') && $msg_data['type'] == C('FENCE_ALARM') ){
 					//设置最后围栏告警时间缓存
 					S('last_fence_alarm_'.$msg_data['device_number'], time(), C('FENCE_ALARM_INTERVAL')*60);
 				}
+				return array( 'success' => true , 'message' => 'ok' );
 			}
 		} catch (\JPush\Exceptions\APIConnectionException $e) {
 		    // try something here
@@ -217,7 +228,9 @@ class NoticeAction extends Action {
 			 			'code' => $e->getHttpCode(),
 				 		'resp_msg' => substr($e->__toString(), strpos($e->__toString(),'[')),
 			 );
-			$this->saveLog($msg_data);
+			if(!$this->saveLog($msg_data)){
+				return array( 'success' => false , 'message' => 'add log failed 2' );
+			 }
 			print $e;
 		} catch (\JPush\Exceptions\APIRequestException $e) {
 		    // try something here
@@ -225,13 +238,15 @@ class NoticeAction extends Action {
 			 			'code' => $e->getHttpCode(),
 				 		'resp_msg' => substr($e->__toString(), strpos($e->__toString(),'[')),
 			 );
-			$this->saveLog($msg_data);
+			if(!$this->saveLog($msg_data)){
+				return array( 'success' => false , 'message' => 'add log failed 3' );
+			 }
 			print $e;
 		}
 	}
 	
  
-	public  function saveLog( $msg_data ){
+	private  function saveLog( $msg_data ){
 		$fdls_app_message = M('jms_baojing_message');
 		
 		$log_data = array(

+ 1 - 1
czapp

@@ -1 +1 @@
-Subproject commit d650f1e87b9daf3c55e30d7eb27e8cab22e1b334
+Subproject commit 547dd02d7f89b75036148c6c3303d1faa81347de