V1Action.class.php 7.1 KB

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