V1Action.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. class V1Action extends Action {
  3. public function kafka2createFile( ){
  4. $broker_list = C('KAFKA_BROKER_LIST');
  5. if (empty($broker_list)) {
  6. exit("KAFKA_BROKER_LIST must be config!".PHP_EOL);
  7. }
  8. $group = C('ROUTE_INDEX_KAFKA_GROUP_FTP');
  9. if (empty($group)) {
  10. exit("ROUTE_INDEX_KAFKA_GROUP_FTP must be config!".PHP_EOL);
  11. }
  12. $topics = C('ROUTE_INDEX_KAFKA_TOPIC');
  13. if (empty($topics)) {
  14. exit("ROUTE_INDEX_KAFKA_TOPIC must be config!".PHP_EOL);
  15. }
  16. $topics = explode(',',$topics);
  17. $conf = new RdKafka\Conf();
  18. // Set a rebalance callback to log partition assignments (optional)
  19. $conf->setRebalanceCb(function (RdKafka\KafkaConsumer $kafka, $err, array $partitions = null) {
  20. switch ($err) {
  21. case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS:
  22. echo "Assign: ";
  23. var_dump($partitions);
  24. $kafka->assign($partitions);
  25. break;
  26. case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS:
  27. echo "Revoke: ";
  28. var_dump($partitions);
  29. $kafka->assign(NULL);
  30. break;
  31. default:
  32. throw new \Exception($err);
  33. }
  34. });
  35. // Configure the group.id. All consumer with the same group.id will consume
  36. // different partitions.
  37. $conf->set('group.id', $group);
  38. $conf->set('metadata.broker.list', $broker_list);
  39. $topicConf = new RdKafka\TopicConf();
  40. // Set where to start consuming messages when there is no initial offset in
  41. // offset store or the desired offset is out of range.
  42. // 'smallest': start from the beginning
  43. $topicConf->set('auto.offset.reset', 'smallest');
  44. // Set the configuration to use for subscribed/assigned topics
  45. $conf->setDefaultTopicConf($topicConf);
  46. $consumer = new RdKafka\KafkaConsumer($conf);
  47. // Subscribe to topic 'test'
  48. $consumer->subscribe($topics);
  49. $tmp_list = array();
  50. $start = time();
  51. $count = 0;
  52. //HC_YYYYMMDD_HHIISS.dat
  53. //HC_YYYYMMDD_HHIISS.md5
  54. $localDir = C('FTP_LOCAL_DIR');
  55. $timeFram = time();
  56. $createTime = date('Ymd_His', $timeFram);
  57. $fileTimeInterval = C('FTP_FILE_CREATE_INTERVAL');
  58. if(!$fileTimeInterval){
  59. $fileTimeInterval = 30;
  60. }
  61. // $x = 0;
  62. // $sum = 0;
  63. while (true) {
  64. $message = $consumer->consume(30*1000);
  65. // $_st = microtime(TRUE);
  66. switch ($message->err) {
  67. case RD_KAFKA_RESP_ERR_NO_ERROR:
  68. $locationPack = ($message->payload).'\n';
  69. $fileName = $localDir.'/HC_'.json_decode($message->payload)->mac.$createTime . '.dat';
  70. $runTime = time() - $timeFram;
  71. if($runTime < $fileTimeInterval){
  72. $datRes = $this->writeRouteFile($fileName, $locationPack);
  73. }else{
  74. $md5Res = $this->createRouteMD5file($fileName);
  75. $timeFram = time();
  76. $createTime = date('Ymd_His', $timeFram);
  77. $datRes = $this->writeRouteFile($fileName);
  78. }
  79. break;
  80. case RD_KAFKA_RESP_ERR__PARTITION_EOF:
  81. echo "No more messages; will wait for more".PHP_EOL;
  82. break;
  83. case RD_KAFKA_RESP_ERR__TIMED_OUT:
  84. $fileName = $localDir.'/HC_'.json_decode($message->payload)->mac.$createTime . '.dat';
  85. $md5Res = $this->createRouteMD5file($fileName);
  86. $timeFram = time();
  87. $createTime = date('Ymd_His', $timeFram);
  88. echo "Timed out\n";
  89. break;
  90. default:
  91. throw new \Exception($message->errstr(), $message->err);
  92. break;
  93. }
  94. }
  95. }
  96. private function writeRouteFile( $fileName, $data ){
  97. //deviceid,positioninfo.longitude,positioninfo.latitude,receivetime
  98. $config = C('FTP_CONFIG');
  99. $sRedis = Redis('open_vehicle', 'hash');
  100. $writeData = $data;
  101. if(!file_exists($fileName)){
  102. $writeData = 'deviceid,positioninfo.longitude,positioninfo.latitude,receivetime'.PHP_EOL.$writeData;
  103. }
  104. debug_log('route_info.log',json_encode($writeData,JSON_UNESCAPED_UNICODE));
  105. $writeData = utf8_encode($writeData);
  106. $res = Zmcoding\FtpFile::getInstance($config)->writeFile($fileName, $writeData.PHP_EOL);
  107. return $res;
  108. }
  109. private function createRouteMD5file( $fileName ){
  110. if(!file_exists($fileName)){
  111. return false;
  112. }
  113. $contents = strlen( file_get_contents($fileName, null, null, 0, 1) );
  114. if($contents == 0){
  115. unlink($fileName);
  116. return false;
  117. }
  118. $config = C('FTP_CONFIG');
  119. $writeData = md5_file($fileName);
  120. $pathInfo = pathinfo($fileName);
  121. $md5File = $pathInfo['dirname'].'/'.$pathInfo['filename'].'.md5';
  122. $writeData = $writeData. ' '. $pathInfo['basename'];
  123. $res = Zmcoding\FtpFile::getInstance($config)->writeFile($md5File, $writeData);
  124. return $res;
  125. }
  126. private function getT61GpsParseReslut( $data ){
  127. if(!$data){
  128. echo 'data empty!'.PHP_EOL;
  129. return false;
  130. }
  131. $imei = $data['imei']['value'];
  132. if(!$imei){
  133. echo 'imei not existed!'.PHP_EOL;
  134. return false;
  135. }
  136. if(!isset($data['lng']['value'])){
  137. echo '['.$imei.']data.lng.value not set!'.PHP_EOL;
  138. return false;
  139. }
  140. //检测是否有打包位置参数
  141. $is_have_pkgfields = $data['extctrl']['extctrl_conf']['is_have_pkgfields'];
  142. if(!$is_have_pkgfields){
  143. echo '['.$imei.']is_have_pkgfields empty!'.PHP_EOL;
  144. return false;
  145. }
  146. //检测是否有打包位置参数
  147. $pkt_count = hexdec($data['pkt_count']['hex_value']);
  148. if(!$pkt_count){
  149. echo '['.$imei.']pkt_count empty!'.PHP_EOL;
  150. return false;
  151. }
  152. $pkt_position_params = $data['pkt_position_params'];
  153. if(!$pkt_position_params){
  154. echo '['.$imei.']pkt_position_params empty!'.PHP_EOL;
  155. return false;
  156. }
  157. //解析基准点定位信息
  158. $base_points_info = array(
  159. 'DeviceId' => $imei,
  160. 'AlarmStatus' => $data['alarm_status']['hex_value'],//告警状态
  161. 'DeviceStatus' => $data['device_status']['hex_value'],//设备状态
  162. 'Longitude' => $data['lng']['value'],
  163. 'Latitude' => $data['lat']['value'],
  164. 'DeviceTime' => $data['timestamp']['value'],//本次打包时,第一个终端位置数据采集时间
  165. 'Voltage' => $data['voltage']['value'],//终端外部供电电压
  166. );
  167. //var_dump($base_points_info);
  168. //echo 'lat_hex = '.$data['lat']['hex_value'].',lng_hex = '.$data['lng']['hex_value'].PHP_EOL;
  169. //解析打包位置参数
  170. $list = array();
  171. foreach($pkt_position_params as $key => $row){
  172. $tmp = array();
  173. $tmp['DeviceId'] = $base_points_info['DeviceId'];
  174. $tmp['Altitude'] = $row['altitude']['value'];
  175. $tmp['Speed'] = $row['speed']['value'];
  176. $tmp['Direction'] = $row['direction']['value'];
  177. $tmp['SatelliteCount'] = $row['satellite_count']['value'];
  178. if($key < 1){
  179. $tmp['DeviceTime'] = $row['relative_time']['value'] + $base_points_info['DeviceTime'];
  180. $tmp['Latitude'] = ($row['lat']['value'] + $base_points_info['Latitude'])/1000000;
  181. $tmp['Longitude'] = ($row['lng']['value'] + $base_points_info['Longitude'])/1000000;
  182. }else{
  183. $tmp['DeviceTime'] = $row['relative_time']['value'] + $list[$key-1]['DeviceTime'];
  184. $tmp['Latitude'] = $row['lat']['value']/1000000 + $list[$key-1]['Latitude'];
  185. $tmp['Longitude'] = $row['lng']['value']/1000000 + $list[$key-1]['Longitude'];
  186. }
  187. $list[$key] = $tmp;
  188. }
  189. return $list;
  190. }
  191. }