V1Action.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. class V1Action extends Action {
  3. public function kafka2createFile_route( ){
  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 . '/route_' . $createTime . '.dat';
  74. $runTime = time() - $timeFram;
  75. $sum++;
  76. if($sum % 10000 ==0){
  77. echo $sum . PHP_EOL;
  78. }
  79. if ($runTime < $fileTimeInterval) {
  80. if ($sum % 50000 == 0) {
  81. //echo 'start write routefile...' . PHP_EOL;
  82. echo 'locationPack length ' .strlen($locationPack).PHP_EOL;
  83. $datRes = $this->writeRouteFile($fileName, $locationPack);
  84. if ($datRes) {
  85. $locationPack = '';
  86. $sum = 0;
  87. $total++;
  88. }
  89. }
  90. } else {
  91. $md5Res = $this->createRouteMD5file($fileName);
  92. $timeFram = time();
  93. $createTime = date('Ymd_His', $timeFram);
  94. }
  95. break;
  96. case RD_KAFKA_RESP_ERR__PARTITION_EOF:
  97. //echo "No more messages; will wait for more".PHP_EOL;
  98. break;
  99. case RD_KAFKA_RESP_ERR__TIMED_OUT:
  100. $fileName = $localDir . '/route_' . $createTime . '.dat';
  101. if ($locationPack != '') {
  102. $datRes = $this->writeRouteFile($fileName, $locationPack);
  103. if ($datRes) {
  104. $endsum = $sum;
  105. $locationPack = '';
  106. $sum = 0;
  107. }
  108. }
  109. $md5Res = $this->createRouteMD5file($fileName);
  110. $timeFram = time();
  111. $createTime = date('Ymd_His', $timeFram);
  112. echo "Timed out\n";
  113. echo "消息总数total:" . ($total * 50000 + $endsum) . PHP_EOL;
  114. break;
  115. default:
  116. throw new \Exception($message->errstr(), $message->err);
  117. break;
  118. }
  119. }
  120. }
  121. private function writeRouteFile( $fileName, $data ){
  122. //deviceid,positioninfo.longitude,positioninfo.latitude,receivetime
  123. $config = C('FTP_CONFIG');
  124. debug_log('route_info', $data);
  125. $res = Zmcoding\FtpFile::getInstance($config)->writeFile($fileName, $data);
  126. return $res;
  127. }
  128. private function createRouteMD5file( $fileName ){
  129. if(!file_exists($fileName)){
  130. return false;
  131. }
  132. $contents = strlen( file_get_contents($fileName, null, null, 0, 1) );
  133. if($contents == 0){
  134. unlink($fileName);
  135. return false;
  136. }
  137. $config = C('FTP_CONFIG');
  138. //$writeData = md5_file($fileName);
  139. $pathInfo = pathinfo($fileName);
  140. $md5File = $pathInfo['dirname'].'/'.$pathInfo['filename'].'.md5';
  141. //$writeData = $writeData. ' '. $pathInfo['basename'];
  142. $res = Zmcoding\FtpFile::getInstance($config)->writeFile($md5File, '');
  143. return $res;
  144. }
  145. private function getT61GpsParseReslut( $data ){
  146. if(!$data){
  147. echo 'data empty!'.PHP_EOL;
  148. return false;
  149. }
  150. $imei = $data['imei']['value'];
  151. if(!$imei){
  152. echo 'imei not existed!'.PHP_EOL;
  153. return false;
  154. }
  155. if(!isset($data['lng']['value'])){
  156. echo '['.$imei.']data.lng.value not set!'.PHP_EOL;
  157. return false;
  158. }
  159. //检测是否有打包位置参数
  160. $is_have_pkgfields = $data['extctrl']['extctrl_conf']['is_have_pkgfields'];
  161. if(!$is_have_pkgfields){
  162. echo '['.$imei.']is_have_pkgfields empty!'.PHP_EOL;
  163. return false;
  164. }
  165. //检测是否有打包位置参数
  166. $pkt_count = hexdec($data['pkt_count']['hex_value']);
  167. if(!$pkt_count){
  168. echo '['.$imei.']pkt_count empty!'.PHP_EOL;
  169. return false;
  170. }
  171. $pkt_position_params = $data['pkt_position_params'];
  172. if(!$pkt_position_params){
  173. echo '['.$imei.']pkt_position_params empty!'.PHP_EOL;
  174. return false;
  175. }
  176. //解析基准点定位信息
  177. $base_points_info = array(
  178. 'DeviceId' => $imei,
  179. 'AlarmStatus' => $data['alarm_status']['hex_value'],//告警状态
  180. 'DeviceStatus' => $data['device_status']['hex_value'],//设备状态
  181. 'Longitude' => $data['lng']['value'],
  182. 'Latitude' => $data['lat']['value'],
  183. 'DeviceTime' => $data['timestamp']['value'],//本次打包时,第一个终端位置数据采集时间
  184. 'Voltage' => $data['voltage']['value'],//终端外部供电电压
  185. );
  186. //var_dump($base_points_info);
  187. //echo 'lat_hex = '.$data['lat']['hex_value'].',lng_hex = '.$data['lng']['hex_value'].PHP_EOL;
  188. //解析打包位置参数
  189. $list = array();
  190. foreach($pkt_position_params as $key => $row){
  191. $tmp = array();
  192. $tmp['DeviceId'] = $base_points_info['DeviceId'];
  193. $tmp['Altitude'] = $row['altitude']['value'];
  194. $tmp['Speed'] = $row['speed']['value'];
  195. $tmp['Direction'] = $row['direction']['value'];
  196. $tmp['SatelliteCount'] = $row['satellite_count']['value'];
  197. if($key < 1){
  198. $tmp['DeviceTime'] = $row['relative_time']['value'] + $base_points_info['DeviceTime'];
  199. $tmp['Latitude'] = ($row['lat']['value'] + $base_points_info['Latitude'])/1000000;
  200. $tmp['Longitude'] = ($row['lng']['value'] + $base_points_info['Longitude'])/1000000;
  201. }else{
  202. $tmp['DeviceTime'] = $row['relative_time']['value'] + $list[$key-1]['DeviceTime'];
  203. $tmp['Latitude'] = $row['lat']['value']/1000000 + $list[$key-1]['Latitude'];
  204. $tmp['Longitude'] = $row['lng']['value']/1000000 + $list[$key-1]['Longitude'];
  205. }
  206. $list[$key] = $tmp;
  207. }
  208. return $list;
  209. }
  210. public function redis2createFile_station_vehicle( ){
  211. ini_set("memory_limit", "1024M");
  212. $redis = Redis('ningbo_fangdao_sync_data','queue');
  213. /**模拟数据
  214. for($i = 0;$i<23000;$i++){
  215. $redis->add('{"PLATE_NO":"余姚698951","RFID_SN":"031FABDC","CAR_TYPE":"1","CAR_BRAND":"1","NAME":"叶春红","ID_CARD_NUMBER":"330219197001274302","MOBILE_NUMBER":"13958363623","type":1,"creator_id":1,"OLD_NO":"余姚698951","DATA_TYPE":"vehicle_update"}');
  216. }
  217. exit;
  218. */
  219. $localDir = C('FTP_LOCAL_DIR');
  220. $timeFram = time();
  221. $createTime = date('Ymd_His', $timeFram);
  222. $fileTimeInterval = C('FTP_FILE_CREATE_INTERVAL');
  223. if(!$fileTimeInterval){
  224. $fileTimeInterval = 30;
  225. }
  226. $sum = 0;//消息数据数量
  227. $locationPack = '';
  228. $total = 0; //总数10000个数 消息总数 = total*10000+endsum
  229. $endsum = 0; //最后一次数量
  230. while (true) {
  231. $message = $redis->pop();
  232. if(!$message){
  233. echo 'waiting for data from redis'.PHP_EOL;
  234. $fileName = $localDir . '/station_' . $createTime . '.dat';
  235. if ($locationPack != '') {
  236. $datRes = $this->writeRouteFile($fileName, $locationPack);
  237. if ($datRes) {
  238. $endsum = $sum;
  239. $locationPack = '';
  240. $sum = 0;
  241. }
  242. $md5Res = $this->createRouteMD5file($fileName);
  243. $timeFram = time();
  244. $createTime = date('Ymd_His', $timeFram);
  245. echo "Timed out\n";
  246. echo "消息总数total:" . ($total * 10000 + $endsum) . PHP_EOL;
  247. }
  248. sleep(3);
  249. }else{
  250. $locationPack .= json_encode($message,JSON_UNESCAPED_UNICODE) . PHP_EOL;
  251. $fileName = $localDir . '/station_' . $createTime . '.dat';
  252. $runTime = time() - $timeFram;
  253. $sum++;
  254. if($sum % 10000 ==0){
  255. echo $sum . PHP_EOL;
  256. }
  257. if ($runTime < $fileTimeInterval) {
  258. if ($sum % 10000 == 0) {
  259. //echo 'start write routefile...' . PHP_EOL;
  260. echo 'locationPack length ' .strlen($locationPack).PHP_EOL;
  261. $datRes = $this->writeRouteFile($fileName, $locationPack);
  262. if ($datRes) {
  263. $locationPack = '';
  264. $sum = 0;
  265. $total++;
  266. }
  267. }
  268. } else {
  269. $md5Res = $this->createRouteMD5file($fileName);
  270. $timeFram = time();
  271. $createTime = date('Ymd_His', $timeFram);
  272. }
  273. }
  274. }
  275. }
  276. }