V1Action.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. $locationPack = '';
  64. while (true) {
  65. $message = $consumer->consume(30*1000);
  66. // $_st = microtime(TRUE);
  67. switch ($message->err) {
  68. case RD_KAFKA_RESP_ERR_NO_ERROR:
  69. $locationPack .= ($message->payload).PHP_EOL;
  70. $fileName = $localDir.'/HC_'.$createTime . '.dat';
  71. $runTime = time() - $timeFram;
  72. $sum++;
  73. echo $sum.PHP_EOL;
  74. if($runTime < $fileTimeInterval){
  75. if($sum % 10000 == 0){
  76. echo 'start write routefile...'.PHP_EOL;
  77. $datRes = $this->writeRouteFile($fileName,$locationPack);
  78. if($datRes){
  79. $locationPack = '';
  80. $sum = 0;
  81. }
  82. }
  83. }else{
  84. $md5Res = $this->createRouteMD5file($fileName);
  85. $timeFram = time();
  86. $createTime = date('Ymd_His', $timeFram);
  87. }
  88. break;
  89. case RD_KAFKA_RESP_ERR__PARTITION_EOF:
  90. echo "No more messages; will wait for more".PHP_EOL;
  91. break;
  92. case RD_KAFKA_RESP_ERR__TIMED_OUT:
  93. $fileName = $localDir.'/HC_'.$createTime . '.dat';
  94. if($locationPack != ''){
  95. $datRes = $this->writeRouteFile($fileName,$locationPack);
  96. if($datRes){
  97. $locationPack = '';
  98. $sum = 0;
  99. }
  100. }
  101. $md5Res = $this->createRouteMD5file($fileName);
  102. $timeFram = time();
  103. $createTime = date('Ymd_His', $timeFram);
  104. echo "Timed out\n";
  105. break;
  106. default:
  107. throw new \Exception($message->errstr(), $message->err);
  108. break;
  109. }
  110. }
  111. }
  112. private function writeRouteFile( $fileName, $data ){
  113. //deviceid,positioninfo.longitude,positioninfo.latitude,receivetime
  114. $config = C('FTP_CONFIG');
  115. $writeData = $data;
  116. if(!file_exists($fileName)){
  117. }
  118. debug_log('route_info',$writeData);
  119. $writeData = utf8_encode($writeData);
  120. $cur_time = time();
  121. while(time()-$cur_time<10){
  122. $writeData .= $writeData.PHP_EOL;
  123. }
  124. $res = Zmcoding\FtpFile::getInstance($config)->writeFile($fileName, $writeData);
  125. return $res;
  126. }
  127. private function createRouteMD5file( $fileName ){
  128. if(!file_exists($fileName)){
  129. return false;
  130. }
  131. $contents = strlen( file_get_contents($fileName, null, null, 0, 1) );
  132. if($contents == 0){
  133. unlink($fileName);
  134. return false;
  135. }
  136. $config = C('FTP_CONFIG');
  137. $writeData = md5_file($fileName);
  138. $pathInfo = pathinfo($fileName);
  139. $md5File = $pathInfo['dirname'].'/'.$pathInfo['filename'].'.md5';
  140. $writeData = $writeData. ' '. $pathInfo['basename'];
  141. $res = Zmcoding\FtpFile::getInstance($config)->writeFile($md5File, $writeData);
  142. return $res;
  143. }
  144. private function getT61GpsParseReslut( $data ){
  145. if(!$data){
  146. echo 'data empty!'.PHP_EOL;
  147. return false;
  148. }
  149. $imei = $data['imei']['value'];
  150. if(!$imei){
  151. echo 'imei not existed!'.PHP_EOL;
  152. return false;
  153. }
  154. if(!isset($data['lng']['value'])){
  155. echo '['.$imei.']data.lng.value not set!'.PHP_EOL;
  156. return false;
  157. }
  158. //检测是否有打包位置参数
  159. $is_have_pkgfields = $data['extctrl']['extctrl_conf']['is_have_pkgfields'];
  160. if(!$is_have_pkgfields){
  161. echo '['.$imei.']is_have_pkgfields empty!'.PHP_EOL;
  162. return false;
  163. }
  164. //检测是否有打包位置参数
  165. $pkt_count = hexdec($data['pkt_count']['hex_value']);
  166. if(!$pkt_count){
  167. echo '['.$imei.']pkt_count empty!'.PHP_EOL;
  168. return false;
  169. }
  170. $pkt_position_params = $data['pkt_position_params'];
  171. if(!$pkt_position_params){
  172. echo '['.$imei.']pkt_position_params empty!'.PHP_EOL;
  173. return false;
  174. }
  175. //解析基准点定位信息
  176. $base_points_info = array(
  177. 'DeviceId' => $imei,
  178. 'AlarmStatus' => $data['alarm_status']['hex_value'],//告警状态
  179. 'DeviceStatus' => $data['device_status']['hex_value'],//设备状态
  180. 'Longitude' => $data['lng']['value'],
  181. 'Latitude' => $data['lat']['value'],
  182. 'DeviceTime' => $data['timestamp']['value'],//本次打包时,第一个终端位置数据采集时间
  183. 'Voltage' => $data['voltage']['value'],//终端外部供电电压
  184. );
  185. //var_dump($base_points_info);
  186. //echo 'lat_hex = '.$data['lat']['hex_value'].',lng_hex = '.$data['lng']['hex_value'].PHP_EOL;
  187. //解析打包位置参数
  188. $list = array();
  189. foreach($pkt_position_params as $key => $row){
  190. $tmp = array();
  191. $tmp['DeviceId'] = $base_points_info['DeviceId'];
  192. $tmp['Altitude'] = $row['altitude']['value'];
  193. $tmp['Speed'] = $row['speed']['value'];
  194. $tmp['Direction'] = $row['direction']['value'];
  195. $tmp['SatelliteCount'] = $row['satellite_count']['value'];
  196. if($key < 1){
  197. $tmp['DeviceTime'] = $row['relative_time']['value'] + $base_points_info['DeviceTime'];
  198. $tmp['Latitude'] = ($row['lat']['value'] + $base_points_info['Latitude'])/1000000;
  199. $tmp['Longitude'] = ($row['lng']['value'] + $base_points_info['Longitude'])/1000000;
  200. }else{
  201. $tmp['DeviceTime'] = $row['relative_time']['value'] + $list[$key-1]['DeviceTime'];
  202. $tmp['Latitude'] = $row['lat']['value']/1000000 + $list[$key-1]['Latitude'];
  203. $tmp['Longitude'] = $row['lng']['value']/1000000 + $list[$key-1]['Longitude'];
  204. }
  205. $list[$key] = $tmp;
  206. }
  207. return $list;
  208. }
  209. }