CronAction.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. class CronAction extends Action {
  3. public function createEplate_index( ){
  4. $this->createElectronicPlate();
  5. }
  6. private function createElectronicPlate( ){
  7. $start = time();
  8. $pendingElectricPlate = Redis('jyzl_wait_create_eplate');
  9. $plateLocalPath = Redis('jyzl_wait_upload2oss_eplate', 'queue');
  10. while( (time() - $start) < 60 ){
  11. $licensPlate = $pendingElectricPlate->pop();
  12. if($licensPlate){
  13. echo 'pop licensPlate:'.$licensPlate.PHP_EOL;
  14. $field = 'LicensPlate,VehicleColor, FullName, Address, FrameNumber, MotorNumber, VehicleBrand, RegistrationTime';
  15. $vehicleInfo = M('jms_vehicle')->where(array('LicensPlate' => $licensPlate))->field($field)->find();
  16. if(!$vehicleInfo){
  17. echo 'vehicleInfo not existed,$licensPlate = '.$licensPlate .PHP_EOL;
  18. continue;
  19. }
  20. $localPath = $this->createLocalElectronicPlate($vehicleInfo);
  21. if(!$localPath){
  22. echo 'createLocalElectronicPlate failed,$licensPlate = '.$licensPlate .PHP_EOL;
  23. //生成失败的重新放回队列
  24. $result = $pendingElectricPlate->add($licensPlate);
  25. if(!$result){
  26. echo 'pendingElectricPlate->add() failed,$licensPlate = '.$licensPlate .PHP_EOL;
  27. }
  28. continue;
  29. }
  30. $up2ossWait = json_encode(array('licensPlate' => $licensPlate, 'localPath' => $localPath));
  31. $plateLocalPath->push($up2ossWait);
  32. }
  33. sleep(1);
  34. }
  35. }
  36. private function uploadElectronicPlate2Oss( ){
  37. $config = array(
  38. 'OssDsn' => C('OSS_DSN'),
  39. 'filePathDir' => '/data/wwwroot/czapp.rltest.cn/1.0.0//uploadimage/',
  40. "SaveRule" => "/electronic_plate/{Y}{m}{d}/{uid}.{ext}",
  41. "AllowExts" => array('jpg', 'png', 'jpeg'), // 允许上传的文件后缀(留空为不限制)
  42. "ResizeImage" => true, // 是否自动压缩
  43. "MaxImageWidth" => 1024,
  44. "MaxImageHeight" => 1024,
  45. "IsCheckRgb" => true,
  46. "MinImgAverageRgb" => 70
  47. );
  48. $upload = new \Jms\File\Oss2($config);
  49. $start = time();
  50. $plateLocalPath = Redis('jyzl_wait_upload2oss_eplate');
  51. while( (time() - $start) < 60 ){
  52. $data = $plateLocalPath->pop();
  53. var_dump($data);
  54. json_decode($data,true);
  55. var_dump($data);
  56. if($data){
  57. //TEST
  58. $fileName = 'gravatar.jpg';
  59. //$fileName = $data['localPath'];
  60. $uploadRes = $upload->localFileUpload($fileName);
  61. if(!$uploadRes['success']){
  62. echo $uploadRes['message'].PHP_EOL;
  63. continue;
  64. }
  65. $frontImageUrl = $uploadRes['objectname'];
  66. $updateRes = M('jms_vehicle')->where(array('LicensePlate' => $data['licensPlate']))->setField('FrontElectronicPlateUrl',$frontImageUrl);
  67. if(!$updateRes){
  68. echo 'update failed'.PHP_EOL;
  69. continue;
  70. }
  71. if(file_exists($filename)){
  72. unlink($filename);
  73. }
  74. }
  75. sleep(1);
  76. }
  77. }
  78. public function uploadEplate_index( ){
  79. $this->uploadElectronicPlate2Oss();
  80. }
  81. public function test_function( ){
  82. $plate = 'BJ000100';
  83. $pendingElectricPlate = Redis("jyzl_wait_create_eplate","queue");
  84. $licensePlate = $plate;
  85. $pendingElectricPlate->push($licensePlate);
  86. }
  87. public function acrossAlarm2Kafka( ){
  88. // 从 topic :gps_location_data 取轨迹
  89. $conf = new RdKafka\Conf();
  90. // Set a rebalance callback to log partition assignments (optional)(当有新的消费进程加入或者退出消费组时,kafka 会自动重新分配分区给消费者进程,这里注册了一个回调函数,当分区被重新分配时触发)
  91. $conf->setRebalanceCb(function (RdKafka\KafkaConsumer $kafka, $err, array $partitions = null) {
  92. switch ($err) {
  93. case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS:
  94. echo "Assign: ";
  95. var_dump($partitions);
  96. $kafka->assign($partitions);
  97. break;
  98. case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS:
  99. echo "Revoke: ";
  100. var_dump($partitions);
  101. $kafka->assign(NULL);
  102. break;
  103. default:
  104. throw new \Exception($err);
  105. }
  106. });
  107. // Configure the group.id. All consumer with the same group.id will consume( 配置groud.id 具有相同 group.id 的consumer 将会处理不同分区的消息,所以同一个组内的消费者数量如果订阅了一个topic, 那么消费者进程的数量多于这个topic 分区的数量是没有意义的。)
  108. // different partitions.
  109. $conf->set('group.id', 'myConsumerGroup');
  110. // Initial list of Kafka brokers(添加 kafka集群服务器地址)
  111. $conf->set('metadata.broker.list', '127.0.0.1');
  112. $topicConf = new RdKafka\TopicConf();
  113. // Set where to start consuming messages when there is no initial offset in
  114. // offset store or the desired offset is out of range.
  115. // 'smallest': start from the beginning
  116. $topicConf->set('auto.offset.reset', 'smallest');
  117. // Set the configuration to use for subscribed/assigned topics
  118. $conf->setDefaultTopicConf($topicConf);
  119. $consumer = new RdKafka\KafkaConsumer($conf);
  120. // 订阅轨迹数据topic
  121. $consumer->subscribe(['gps_location_data']);
  122. while (true) {
  123. $message = $consumer->consume(120*1000);
  124. switch ($message->err) {
  125. case RD_KAFKA_RESP_ERR_NO_ERROR:
  126. // 判断是否超出围栏范围 ,存入 topic:gps_alarm_msg_queue
  127. $route_info = json_decode($message->payload,true);
  128. if( empty($route_info) ){
  129. echo 'empty route info.';
  130. break;
  131. }
  132. $result = $this->produceAcrossAlarmData($route_info);
  133. if($result){
  134. echo $result,PHP_EOL;
  135. }
  136. break;
  137. case RD_KAFKA_RESP_ERR__PARTITION_EOF:
  138. echo "No more messages; will wait for more\n";
  139. break;
  140. case RD_KAFKA_RESP_ERR__TIMED_OUT:
  141. echo "Timed out\n";
  142. break;
  143. default:
  144. throw new \Exception($message->errstr(), $message->err);
  145. break;
  146. }
  147. }
  148. }
  149. private function produceAcrossAlarmData( $route_info ){
  150. if( !$route_info['DeviceId'] ){
  151. return 'device id is not exists.';
  152. }
  153. if( !$route_info['Longitude'] ){
  154. return 'longitude is not exists.';
  155. }
  156. if( !$route_info['Latitude'] ){
  157. return 'latitude is not exists.';
  158. }
  159. if( !$route_info['DeviceTime'] ){
  160. return 'device time is not exists.';
  161. }
  162. // 从数据库中取出车牌号,缓存1天
  163. if( S('plate-'.$route_info['DeviceId']) ){
  164. $plate = S('plate-'.$route_info['DeviceId']);
  165. }else{
  166. $where = array('GpsDeviceNumber'=>$route_info['DeviceId']);
  167. $plate = M('jms_vehicle')->where($where)->getField('LicensePlate');
  168. S('plate-'.$route_info['DeviceId'], $plate, 24*60*60);
  169. }
  170. // 是否启用围栏
  171. $rlfd_vehicle_fence = Redis('rlfd_vehicle_fence','hash');
  172. $fence = $rlfd_vehicle_fence->get($plate);
  173. $fence = json_decode($fence, true);
  174. if( empty($fence) ){
  175. return '围栏信息不存在';
  176. }
  177. if( !$fence['fenceStatus'] ){
  178. return '未启用围栏';
  179. }
  180. $fence_info = $fence['fenceInfo'];
  181. if( empty($fence_info['data']) ){
  182. return '围栏坐标数据不存在';
  183. }
  184. // 是否越界
  185. $route_point = array(
  186. 'lng' => $route_info['Longitude'],
  187. 'lat' => $route_info['Latitude']
  188. );
  189. $result = true; // 默认在围栏内
  190. if( $fence_info['type'] == 'circle' ){ // 圆形围栏
  191. $distance = \Jms\Algo\Geometry::distanceBetween2BdPoints($fence_info['data']['center'], $route_point); //获取轨迹点到围栏中心点间距离,km
  192. $result = $distance*1000 < $fence_info['data']['radius'];// 距离圆心大于半径说明越界了
  193. }elseif( $fence_info['type'] == 'polygon' ){ // 多边形围栏
  194. $result = \Jms\Algo\Geometry::isInPolygon($fence_info['data']['vertex'], $route_point);
  195. }else{
  196. return '未知围栏类型';
  197. }
  198. if( !$result ){
  199. $alarm_data = array(
  200. "type" => C('FENCE_ALARM'),
  201. "title" => "超出电子围栏",
  202. "content" => "车辆 {$plate} 已超出设置的电子围栏范围,请前往停车处确认是否被盗。",
  203. "device_number" => $route_info['DeviceId']
  204. );
  205. kafkaProducer('gps_alarm_msg_queue', $alarm_data); // 添加到kafka
  206. return '添加告警消息到 gps_alarm_msg_queue ';
  207. }
  208. return '没有超出围栏';
  209. }
  210. public function mockProduce( ){
  211. $msg_data = array(
  212. 'DeviceId' => FFFFFF123122,
  213. 'State' => 1,
  214. 'Speed' => 1.2,
  215. 'Longitude' => 121.20638,
  216. 'Latitude' => 30.18852,
  217. 'DeviceTime' => date('Y-m-d H:i:s'),
  218. 'LBS' => 'LBS',
  219. 'Direction' => 's',
  220. );
  221. $msg_data = json_encode($msg_data, JSON_UNESCAPED_UNICODE);
  222. kafkaProducer('gps_location_data',$msg_data);
  223. }
  224. private function createLocalElectronicPlate( $params ){
  225. $license_plate = $params['LicensPlate'];//LicensPlate,VehicleColor, FullName, Address, FrameNumber, MotorNumber, VehicleBrand
  226. $vehicle_color = $params['VehicleColor'];
  227. $real_name = $params['FullName'];
  228. $address = $params['Address'];
  229. $cjh = $params['FrameNumber'];
  230. $djh = $params['MotorNumber'];
  231. $cph = $params['VehicleBrand'];
  232. $date = date('Y-m-d',$params['RegistrationTime']);
  233. $reg_date = $date;
  234. $fz_date = $date;
  235. $fz_org = "包头市公安局";
  236. $im = imagecreatetruecolor(500, 278); // 设置画布
  237. //$bg = imagecreatefromjpeg('bg.jpg'); // 设置背景图片
  238. $bg = imagecreatefromjpeg('./Public/images/front.jpg'); // 设置背景图片
  239. imagecopy($im,$bg,0,0,0,0,500,278); // 将背景图片拷贝到画布相应位置
  240. imagedestroy($bg); // 销毁背景图片
  241. $font = './Public/font/stsong.ttf'; // 设置字体 // 设置字体,这里可以指向ttf文件
  242. $blacka = imagecolorallocate($im, 15, 23, 25); // 颜色
  243. /* 写入内容 */
  244. imagettftext($im, 12, 0, 135, 66, $blacka, $font,$license_plate ); // 车牌号
  245. imagettftext($im, 12, 0, 335, 66, $blacka, $font,$vehicle_color ); // 车辆颜色
  246. imagettftext($im, 12, 0, 135, 96, $blacka, $font,$real_name ); // 姓名
  247. imagettftext($im, 12, 0, 135, 128, $blacka, $font,$address ); // 住址
  248. imagettftext($im, 12, 0, 135, 160, $blacka, $font,$cjh ); // 车架号
  249. imagettftext($im, 12, 0, 335, 160, $blacka, $font,$djh ); // 电机号
  250. imagettftext($im, 12, 0, 263, 192, $blacka, $font,$cph ); // 厂牌型号
  251. imagettftext($im, 11, 0, 260, 222, $blacka, $font,$reg_date ); // 注册日期
  252. imagettftext($im, 11, 0, 376, 222, $blacka, $font,$fz_date ); // 发证期
  253. imagettftext($im, 12, 0, 263, 255, $blacka, $font,$fz_org ); // 发证机关
  254. $img_file = "./images/myplate.jpg";
  255. $result = imagejpeg($im, $img_file); // 生成jpeg格式图片
  256. imagedestroy($im); // 销毁图片
  257. if(!$result){
  258. echo "生成电子车牌失败";
  259. exit;
  260. }
  261. echo '生成电子车牌完成';
  262. }
  263. }