mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-21 11:35:12 +08:00
update 调整相机相关Service结构
This commit is contained in:
@@ -1,227 +0,0 @@
|
||||
package com.jsowell.thirdparty.camera.service;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import com.jsowell.common.config.AliyunOssConfig;
|
||||
import com.jsowell.common.constant.CacheConstants;
|
||||
import com.jsowell.common.core.redis.RedisCache;
|
||||
import com.jsowell.common.util.StringUtils;
|
||||
import com.jsowell.common.util.file.AliyunOssUploadUtils;
|
||||
import com.jsowell.common.util.file.ImageUtils;
|
||||
import com.jsowell.pile.domain.PileCameraInfo;
|
||||
import com.jsowell.pile.dto.GenerateOccupyOrderDTO;
|
||||
import com.jsowell.pile.dto.camera.CameraHeartBeatDTO;
|
||||
import com.jsowell.pile.dto.camera.CameraIdentifyResultsDTO;
|
||||
import com.jsowell.pile.service.IPileCameraInfoService;
|
||||
import com.jsowell.pile.service.OrderPileOccupyService;
|
||||
import io.netty.channel.Channel;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Base64;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 相机管理系统 Service
|
||||
*
|
||||
* @author Lemon
|
||||
* @Date 2023/12/5 11:11:32
|
||||
*/
|
||||
@Service
|
||||
public class CameraService {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
@Autowired
|
||||
private IPileCameraInfoService pileCameraInfoService;
|
||||
|
||||
@Autowired
|
||||
private OrderPileOccupyService orderPileOccupyService;
|
||||
|
||||
|
||||
public void receiveIdentifyResults(JSONObject jsonObject) {
|
||||
// 区分入场和出场
|
||||
Integer parking_state = jsonObject.getJSONObject("parking").getInteger("parking_state");
|
||||
if (parking_state == 1) {
|
||||
// 入场
|
||||
String parkingState = "ENTRY";
|
||||
vehicleEntry(jsonObject, parkingState);
|
||||
}
|
||||
if (parking_state == 2) {
|
||||
// 在场
|
||||
}
|
||||
if (parking_state == 4) {
|
||||
// 出场
|
||||
}
|
||||
|
||||
// saveInfo2DataBase(jsonObject);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void saveHeartBeat2Redis(CameraHeartBeatDTO dto) {
|
||||
// 将基本信息存入缓存
|
||||
String redisKey = CacheConstants.CAMERA_HEARTBEAT + dto.getIp();
|
||||
redisCache.setCacheObject(redisKey, dto.getSn());
|
||||
}
|
||||
|
||||
/**
|
||||
* 车辆入场
|
||||
* @param jsonObject
|
||||
*/
|
||||
private void vehicleEntry(JSONObject jsonObject, String parkingState) {
|
||||
// 先将车牌图片信息存入缓存
|
||||
// boolean result = saveCarPicture2Redis(jsonObject, parkingState);
|
||||
|
||||
// 将信息存数据库
|
||||
saveInfo2DataBase(jsonObject);
|
||||
|
||||
// TODO 生成占桩订单
|
||||
// GenerateOccupyOrderDTO dto = new GenerateOccupyOrderDTO();
|
||||
// dto.setMemberId();
|
||||
// dto.setPileSn();
|
||||
// dto.setConnectorCode();
|
||||
// orderPileOccupyService.generateOccupyPileOrder()
|
||||
}
|
||||
|
||||
private boolean saveInfo2DataBase(JSONObject jsonObject) {
|
||||
// 车牌信息
|
||||
CameraIdentifyResultsDTO.ProductH.Plate plate = JSONObject.parseObject(jsonObject.getJSONObject("product_h").getJSONObject("plate").toJSONString(),
|
||||
CameraIdentifyResultsDTO.ProductH.Plate.class);
|
||||
if (plate == null) {
|
||||
return false;
|
||||
}
|
||||
// 停车位信息
|
||||
CameraIdentifyResultsDTO.ProductH.Parking parking = JSONObject.parseObject(jsonObject.getJSONObject("product_h").getJSONObject("parking").toJSONString(),
|
||||
CameraIdentifyResultsDTO.ProductH.Parking.class);
|
||||
if (parking == null) {
|
||||
return false;
|
||||
}
|
||||
// 设备信息
|
||||
CameraIdentifyResultsDTO.DeviceInfo deviceInfo = JSONObject.parseObject(jsonObject.getJSONObject("device_info").toJSONString(),
|
||||
CameraIdentifyResultsDTO.DeviceInfo.class);
|
||||
if (deviceInfo == null) {
|
||||
return false;
|
||||
}
|
||||
// 获取背景图片
|
||||
JSONArray bgImgs = jsonObject.getJSONArray("bg_img");
|
||||
List<CameraIdentifyResultsDTO.BgImg> bgImgList = bgImgs.toList(CameraIdentifyResultsDTO.BgImg.class);
|
||||
if (CollectionUtils.isEmpty(bgImgList)) {
|
||||
return false;
|
||||
}
|
||||
// Base64解密
|
||||
String plateNumber = cn.hutool.core.codec.Base64.decodeStr(plate.getPlate());
|
||||
String zoneName = cn.hutool.core.codec.Base64.decodeStr(parking.getZoneName());
|
||||
|
||||
for (CameraIdentifyResultsDTO.BgImg bgImg : bgImgList) {
|
||||
// 上传到阿里云OSS,获取图片上传成功后的地址
|
||||
String fileName = zoneName + "-" + System.currentTimeMillis() / 1000 + ".jpg";
|
||||
String url = saveImage(bgImg.getImage(), fileName);
|
||||
if (StringUtils.isBlank(url)) {
|
||||
logger.error("车位号:{} 图片上传失败", zoneName);
|
||||
continue;
|
||||
}
|
||||
PileCameraInfo pileCameraInfo = PileCameraInfo.builder()
|
||||
.deviceName(deviceInfo.getDevName())
|
||||
.deviceIp(deviceInfo.getIp())
|
||||
.deviceSn(deviceInfo.getSn())
|
||||
.plateNumber(plateNumber)
|
||||
.parkingState(String.valueOf(parking.getParkingState()))
|
||||
.zoneId(parking.getZoneId())
|
||||
.zoneName(zoneName)
|
||||
.color(plate.getColor())
|
||||
.plateType(plate.getType())
|
||||
.image(url)
|
||||
|
||||
.build();
|
||||
// 插入数据库
|
||||
pileCameraInfoService.insertPileCameraInfo(pileCameraInfo);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 保存图像
|
||||
* @param base64Image 图像的Base64编码
|
||||
* @param fileName 文件名
|
||||
* @return
|
||||
*/
|
||||
private String saveImage(String base64Image, String fileName) {
|
||||
try {
|
||||
// 将Base64编码的字符串解码为字节数组
|
||||
byte[] imageBytes = Base64.getDecoder().decode(base64Image);
|
||||
|
||||
// 将字节数组转换为 BufferedImage
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
|
||||
BufferedImage originalImage = ImageIO.read(inputStream);
|
||||
|
||||
// 对图像进行压缩
|
||||
double quality = 0.5; // 压缩质量
|
||||
BufferedImage compressedImage = ImageUtils.compressImage(originalImage, quality);
|
||||
|
||||
// 将压缩后的图片转换为字节数组
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
ImageIO.write(compressedImage, "jpg", outputStream);
|
||||
byte[] compressedImageBytes = outputStream.toByteArray();
|
||||
|
||||
// 上传图片到OSS
|
||||
String url = AliyunOssUploadUtils.upload2OSS(compressedImageBytes, fileName);
|
||||
if (StringUtils.isNotBlank(url)) {
|
||||
return url;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将车辆图片信息存入缓存
|
||||
* @param jsonObject
|
||||
*/
|
||||
// private boolean saveCarPicture2Redis(JSONObject jsonObject, String parkingState) {
|
||||
// // 获取车牌号
|
||||
// String plateNumber = jsonObject.getJSONObject("plate").getString("plate");
|
||||
// if (StringUtils.isBlank(plateNumber)) {
|
||||
// return false;
|
||||
// }
|
||||
// // 获取背景图片
|
||||
// JSONArray bgImgs = jsonObject.getJSONArray("bg_img");
|
||||
// List<CameraIdentifyResultsDTO.BgImg> bgImgList = bgImgs.toList(CameraIdentifyResultsDTO.BgImg.class);
|
||||
// if (CollectionUtils.isEmpty(bgImgList)) {
|
||||
// return false;
|
||||
// }
|
||||
// for (CameraIdentifyResultsDTO.BgImg bgImg : bgImgList) {
|
||||
// String image = bgImg.getImage(); // 图片的 base64 编码
|
||||
// // String key = bgImg.getKey(); // 索引id
|
||||
// // key: 前缀 + 车牌号 + 日期 + 入场/出场状态
|
||||
// String redisKey = CacheConstants.CAMERA_IMAGE_BY_PLATE_NUMBER + plateNumber + "_" + DateUtils.getDate() + "_" + parkingState;
|
||||
// // 存入缓存
|
||||
// // 暂时永久保存
|
||||
// redisCache.setCacheObject(redisKey, image);
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user