CronAction.class.php 12 KB

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