CronAction.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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','queue');
  9. $plateLocalPath = Redis('jyzl_wait_upload2oss_eplate', 'queue');
  10. while( (time() - $start) < 60 ){
  11. $licensPlate = $pendingElectricPlate->pop();
  12. if(!$licensPlate){
  13. echo 'no message!'.PHP_EOL;
  14. sleep(1);
  15. continue;
  16. }
  17. echo 'pop licensPlate:'.$licensPlate.PHP_EOL;
  18. $field = 'LicensePlate,VehicleColor, FullName, DetailedAdd, FrameNumber, MotorNumber, VehicleBrand, RegistrationTime';
  19. $vehicleInfo = M('jms_vehicle')->where(array('LicensePlate' => $licensPlate))->field($field)->find();
  20. if(!$vehicleInfo){
  21. echo 'vehicleInfo not existed,$licensPlate = '.$licensPlate .PHP_EOL;
  22. continue;
  23. }
  24. //生成电子车牌到本地
  25. $localPath = $this->createLocalElectronicPlate($vehicleInfo);
  26. if(!$localPath){
  27. echo 'createLocalElectronicPlate failed,$licensPlate = '.$licensPlate .PHP_EOL;
  28. //生成失败的重新放回队列
  29. $result = $pendingElectricPlate->add($licensPlate);
  30. if(!$result){
  31. echo 'pendingElectricPlate->add() failed,$licensPlate = '.$licensPlate .PHP_EOL;
  32. continue;
  33. }
  34. }
  35. //生成的本地电子车牌,加入到待上传oss队列
  36. $up2ossWait = json_encode(array('LicensePlate' => $licensPlate, 'localPath' => $localPath));
  37. $reslut = $plateLocalPath->add($up2ossWait);//push方法没有返回值,用add代替
  38. if(!$result){
  39. echo 'plateLocalPath->add() failed,$licensPlate = '.$licensPlate .PHP_EOL;
  40. continue;
  41. }
  42. usleep(100000);
  43. }
  44. }
  45. private function uploadElectronicPlate2Oss( ){
  46. $config = array(
  47. 'OssDsn' => C('OSS_DSN'),
  48. 'filePathDir' => '/data/wwwroot/czapp.rltest.cn/1.0.0//uploadimage/',
  49. "SaveRule" => "/electronic_plate/{Y}{m}{d}/{uid}.{ext}",
  50. "AllowExts" => array('jpg', 'png', 'jpeg'), // 允许上传的文件后缀(留空为不限制)
  51. "ResizeImage" => true, // 是否自动压缩
  52. "MaxImageWidth" => 1024,
  53. "MaxImageHeight" => 1024,
  54. "IsCheckRgb" => true,
  55. "MinImgAverageRgb" => 70
  56. );
  57. $upload = new \Jms\File\Oss2($config);
  58. $start = time();
  59. $plateLocalPath = Redis('jyzl_wait_upload2oss_eplate');
  60. while( (time() - $start) < 60 ){
  61. $data = $plateLocalPath->pop();
  62. if(!$data){
  63. echo 'no more data, waiting for next mesaage.'.PHP_EOL;
  64. sleep(1);
  65. continue;
  66. }
  67. $data = json_decode($data, true);
  68. if(!$data){
  69. echo 'json_decode failed, please check the format of the json data';
  70. continue;
  71. }
  72. $fileName = 'gravatar.jpg';//TEST
  73. //$fileName = $data['localPath'];
  74. if(!$fileName){
  75. echo 'cant get loacl file';
  76. continue;
  77. }
  78. $uploadRes = $upload->localFileUpload($fileName);
  79. if(!$uploadRes['success']){
  80. echo $uploadRes['message'].PHP_EOL;
  81. continue;
  82. }
  83. $frontImageUrl = $uploadRes['objectname'];
  84. if(!$frontImageUrl){
  85. echo 'cant get img_oss_url';
  86. continue;
  87. }
  88. $licensPlate = $data['licensPlate'];
  89. if(!$licensPlate)
  90. $updateRes = M('jms_vehicle')->where(array('LicensePlate' => $licensPlate))->setField('FrontElectronicPlateUrl',$frontImageUrl);
  91. if(!$updateRes){
  92. echo 'update failed'.PHP_EOL;
  93. continue;
  94. }
  95. //删除本地图片
  96. $filePath = $config['filePathDir'].$filename;
  97. if(file_exists(!$filePath)){
  98. $flag = unlink($filePath);
  99. if(!$flag){
  100. echo 'delete localImg failed'.PHP;
  101. }
  102. }
  103. sleep(1);
  104. }
  105. /*
  106. $licensPlate = $pendingElectricPlate->pop();
  107. if(!$licensPlate){
  108. echo 'no message!'.PHP_EOL;
  109. sleep(1);
  110. continue;
  111. }
  112. echo 'pop licensPlate:'.$licensPlate.PHP_EOL;
  113. $field = 'LicensePlate,VehicleColor, FullName, Address, FrameNumber, MotorNumber, VehicleBrand, RegistrationTime';
  114. $vehicleInfo = M('jms_vehicle')->where(array('LicensePlate' => $licensPlate))->field($field)->find();
  115. if(!$vehicleInfo){
  116. echo 'vehicleInfo not existed,$licensPlate = '.$licensPlate .PHP_EOL;
  117. continue;
  118. }
  119. //生成电子车牌到本地
  120. $localPath = $this->createLocalElectronicPlate($vehicleInfo);
  121. if(!$localPath){
  122. echo 'createLocalElectronicPlate failed,$licensPlate = '.$licensPlate .PHP_EOL;
  123. //生成失败的重新放回队列
  124. $result = $pendingElectricPlate->add($licensPlate);
  125. if(!$result){
  126. echo 'pendingElectricPlate->add() failed,$licensPlate = '.$licensPlate .PHP_EOL;
  127. continue;
  128. }
  129. }
  130. //生成的本地电子车牌,加入到待上传oss队列
  131. $up2ossWait = json_encode(array('LicensePlate' => $licensPlate, 'localPath' => $localPath));
  132. $reslut = $plateLocalPath->add($up2ossWait);//push方法没有返回值,用add代替
  133. if(!$result){
  134. echo 'plateLocalPath->add() failed,$licensPlate = '.$licensPlate .PHP_EOL;
  135. continue;
  136. }
  137. usleep(100000);
  138. */
  139. }
  140. public function uploadEplate_index( ){
  141. $this->uploadElectronicPlate2Oss();
  142. }
  143. public function test_function( ){
  144. $vehicleInfo = array(
  145. 'LicensePlate' => 'LY100021',
  146. 'VehicleColor' => '白色',
  147. 'FullName' => '张三',
  148. 'Address' => '浙江杭州滨江195号',
  149. 'FrameNumber' => '123456789',
  150. 'MotorNumber' => '233456789',
  151. 'VehicleBrand'=> '雅迪z123',
  152. 'RegistrationTime' => '2019-05-15 12:12:00'
  153. );
  154. $localPath = $this->createLocalElectronicPlate($vehicleInfo);
  155. echo $localPath .PHP_EOL;
  156. exit;
  157. $plate = 'BJ000100';
  158. $pendingElectricPlate = Redis("jyzl_wait_create_eplate","queue");
  159. $licensePlate = $plate;
  160. $pendingElectricPlate->push($licensePlate);
  161. }
  162. public function acrossAlarm2Kafka( ){
  163. // 从 topic :gps_location_data 取轨迹
  164. $conf = new RdKafka\Conf();
  165. // Set a rebalance callback to log partition assignments (optional)(当有新的消费进程加入或者退出消费组时,kafka 会自动重新分配分区给消费者进程,这里注册了一个回调函数,当分区被重新分配时触发)
  166. $conf->setRebalanceCb(function (RdKafka\KafkaConsumer $kafka, $err, array $partitions = null) {
  167. switch ($err) {
  168. case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS:
  169. echo "Assign: ";
  170. var_dump($partitions);
  171. $kafka->assign($partitions);
  172. break;
  173. case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS:
  174. echo "Revoke: ";
  175. var_dump($partitions);
  176. $kafka->assign(NULL);
  177. break;
  178. default:
  179. throw new \Exception($err);
  180. }
  181. });
  182. // Configure the group.id. All consumer with the same group.id will consume( 配置groud.id 具有相同 group.id 的consumer 将会处理不同分区的消息,所以同一个组内的消费者数量如果订阅了一个topic, 那么消费者进程的数量多于这个topic 分区的数量是没有意义的。)
  183. // different partitions.
  184. $conf->set('group.id', 'myConsumerGroup');
  185. // Initial list of Kafka brokers(添加 kafka集群服务器地址)
  186. $conf->set('metadata.broker.list', '127.0.0.1');
  187. $topicConf = new RdKafka\TopicConf();
  188. // Set where to start consuming messages when there is no initial offset in
  189. // offset store or the desired offset is out of range.
  190. // 'smallest': start from the beginning
  191. $topicConf->set('auto.offset.reset', 'smallest');
  192. // Set the configuration to use for subscribed/assigned topics
  193. $conf->setDefaultTopicConf($topicConf);
  194. $consumer = new RdKafka\KafkaConsumer($conf);
  195. // 订阅轨迹数据topic
  196. $consumer->subscribe(['gps_location_data']);
  197. while (true) {
  198. $message = $consumer->consume(120*1000);
  199. switch ($message->err) {
  200. case RD_KAFKA_RESP_ERR_NO_ERROR:
  201. // 判断是否超出围栏范围 ,存入 topic:gps_alarm_msg_queue
  202. $route_info = json_decode($message->payload,true);
  203. if( empty($route_info) ){
  204. echo 'empty route info.';
  205. break;
  206. }
  207. $result = $this->produceAcrossAlarmData($route_info);
  208. if($result){
  209. echo $result,PHP_EOL;
  210. }
  211. break;
  212. case RD_KAFKA_RESP_ERR__PARTITION_EOF:
  213. echo "No more messages; will wait for more\n";
  214. break;
  215. case RD_KAFKA_RESP_ERR__TIMED_OUT:
  216. echo "Timed out\n";
  217. break;
  218. default:
  219. throw new \Exception($message->errstr(), $message->err);
  220. break;
  221. }
  222. }
  223. }
  224. private function produceAcrossAlarmData( $route_info ){
  225. if( !$route_info['DeviceId'] ){
  226. return 'device id is not exists.';
  227. }
  228. if( !$route_info['Longitude'] ){
  229. return 'longitude is not exists.';
  230. }
  231. if( !$route_info['Latitude'] ){
  232. return 'latitude is not exists.';
  233. }
  234. if( !$route_info['DeviceTime'] ){
  235. return 'device time is not exists.';
  236. }
  237. // 从数据库中取出车牌号,缓存1天
  238. if( S('plate-'.$route_info['DeviceId']) ){
  239. $plate = S('plate-'.$route_info['DeviceId']);
  240. }else{
  241. $where = array('GpsDeviceNumber'=>$route_info['DeviceId']);
  242. $plate = M('jms_vehicle')->where($where)->getField('LicensePlate');
  243. S('plate-'.$route_info['DeviceId'], $plate, 24*60*60);
  244. }
  245. // 是否启用围栏
  246. $rlfd_vehicle_fence = Redis('rlfd_vehicle_fence','hash');
  247. $fence = $rlfd_vehicle_fence->get($plate);
  248. $fence = json_decode($fence, true);
  249. if( empty($fence) ){
  250. return '围栏信息不存在';
  251. }
  252. if( !$fence['fenceStatus'] ){
  253. return '未启用围栏';
  254. }
  255. $fence_info = $fence['fenceInfo'];
  256. if( empty($fence_info['data']) ){
  257. return '围栏坐标数据不存在';
  258. }
  259. // 是否越界
  260. $route_point = array(
  261. 'lng' => $route_info['Longitude'],
  262. 'lat' => $route_info['Latitude']
  263. );
  264. $result = true; // 默认在围栏内
  265. if( $fence_info['type'] == 'circle' ){ // 圆形围栏
  266. $distance = \Jms\Algo\Geometry::distanceBetween2BdPoints($fence_info['data']['center'], $route_point); //获取轨迹点到围栏中心点间距离,km
  267. $result = $distance*1000 < $fence_info['data']['radius'];// 距离圆心大于半径说明越界了
  268. }elseif( $fence_info['type'] == 'polygon' ){ // 多边形围栏
  269. $result = \Jms\Algo\Geometry::isInPolygon($fence_info['data']['vertex'], $route_point);
  270. }else{
  271. return '未知围栏类型';
  272. }
  273. if( !$result ){
  274. $alarm_data = array(
  275. "type" => C('FENCE_ALARM'),
  276. "title" => "超出电子围栏",
  277. "content" => "车辆 {$plate} 已超出设置的电子围栏范围,请前往停车处确认是否被盗。",
  278. "device_number" => $route_info['DeviceId']
  279. );
  280. kafkaProducer('gps_alarm_msg_queue', $alarm_data); // 添加到kafka
  281. return '添加告警消息到 gps_alarm_msg_queue ';
  282. }
  283. return '没有超出围栏';
  284. }
  285. public function mockProduce( ){
  286. $msg_data = array(
  287. 'DeviceId' => FFFFFF123122,
  288. 'State' => 1,
  289. 'Speed' => 1.2,
  290. 'Longitude' => 121.20638,
  291. 'Latitude' => 30.18852,
  292. 'DeviceTime' => date('Y-m-d H:i:s'),
  293. 'LBS' => 'LBS',
  294. 'Direction' => 's',
  295. );
  296. $msg_data = json_encode($msg_data, JSON_UNESCAPED_UNICODE);
  297. kafkaProducer('gps_location_data',$msg_data);
  298. }
  299. private function createLocalElectronicPlate( $params ){
  300. $license_plate = $params['LicensePlate'];
  301. if(!$license_plate){
  302. echo "LicensePlate empty!".PHP_EOL;
  303. return false;
  304. }
  305. $vehicle_color = $params['VehicleColor'];
  306. if(!$vehicle_color){
  307. echo "VehicleColor empty!".PHP_EOL;
  308. return false;
  309. }
  310. $real_name = $params['FullName'];
  311. if(!$real_name){
  312. echo "FullName empty!".PHP_EOL;
  313. return false;
  314. }
  315. $address = $params['DetailedAdd'];
  316. if(!$address){
  317. echo "DetailedAdd empty!".PHP_EOL;
  318. return false;
  319. }
  320. $cjh = $params['FrameNumber'];
  321. if(!$cjh){
  322. echo "FrameNumber empty!".PHP_EOL;
  323. return false;
  324. }
  325. $djh = $params['MotorNumber'];
  326. if(!$djh){
  327. echo "MotorNumber empty!".PHP_EOL;
  328. return false;
  329. }
  330. $cph = $params['VehicleBrand'];
  331. if(!$cph){
  332. echo "VehicleBrand empty!".PHP_EOL;
  333. return false;
  334. }
  335. $regist_time = strtotime($params['RegistrationTime']);
  336. if($regist_time < 1546272000){
  337. echo "RegistrationTime invalid! RegistrationTime: ".$params['RegistrationTime'].PHP_EOL;
  338. return false;
  339. }
  340. $date = date('Y-m-d',$regist_time);
  341. if(!$date){
  342. echo "date empty! RegistrationTime: ".$params['RegistrationTime'].PHP_EOL;
  343. return false;
  344. }
  345. $reg_date = $date;
  346. $fz_date = $date;
  347. $fz_org = "包头市公安局";
  348. $im = imagecreatetruecolor(500, 278); // 设置画布
  349. //$bg = imagecreatefromjpeg('bg.jpg'); // 设置背景图片
  350. $front_img = './Public/images/front.jpg';
  351. if(!is_file($front_img)){
  352. echo "front_img not existed! front_img: ".$front_img.PHP_EOL;
  353. return false;
  354. }
  355. $bg = imagecreatefromjpeg($front_img); // 设置背景图片
  356. imagecopy($im,$bg,0,0,0,0,500,278); // 将背景图片拷贝到画布相应位置
  357. imagedestroy($bg); // 销毁背景图片
  358. $font = './Public/font/stsong.ttf'; // 设置字体 // 设置字体,这里可以指向ttf文件
  359. if(!is_file($font)){
  360. echo "font file not existed! font: ".$font.PHP_EOL;
  361. return false;
  362. }
  363. $blacka = imagecolorallocate($im, 15, 23, 25); // 颜色
  364. /* 写入内容 */
  365. imagettftext($im, 12, 0, 135, 66, $blacka, $font,$license_plate ); // 车牌号
  366. imagettftext($im, 12, 0, 335, 66, $blacka, $font,$vehicle_color ); // 车辆颜色
  367. imagettftext($im, 12, 0, 135, 96, $blacka, $font,$real_name ); // 姓名
  368. imagettftext($im, 12, 0, 135, 128, $blacka, $font,$address ); // 住址
  369. imagettftext($im, 12, 0, 135, 160, $blacka, $font,$cjh ); // 车架号
  370. imagettftext($im, 12, 0, 335, 160, $blacka, $font,$djh ); // 电机号
  371. imagettftext($im, 12, 0, 263, 192, $blacka, $font,$cph ); // 厂牌型号
  372. imagettftext($im, 11, 0, 260, 222, $blacka, $font,$reg_date ); // 注册日期
  373. imagettftext($im, 11, 0, 376, 222, $blacka, $font,$fz_date ); // 发证期
  374. imagettftext($im, 12, 0, 263, 255, $blacka, $font,$fz_org ); // 发证机关
  375. $img_file_dir = SOLUTION_LOG_PATH ."/images/".date('Y-m-d')."/";
  376. if(!is_dir($img_file_dir)){
  377. $res = mkdir($img_file_dir,0777,true);
  378. if (!$res){
  379. echo "目录 $img_file_dir 创建失败!".PHP_EOL;
  380. return false;
  381. }
  382. }
  383. $img_file = $img_file_dir .$license_plate.".jpg";
  384. $result = imagejpeg($im, $img_file); // 生成jpeg格式图片
  385. imagedestroy($im); // 销毁图片
  386. if(!$result){
  387. echo "生成电子车牌失败, license_plate: ".$license_plate .PHP_EOL;
  388. return false;
  389. }
  390. echo "生成电子车牌完成, license_plate: ".$license_plate .PHP_EOL;
  391. return $img_file;
  392. }
  393. }