|
|
|
@ -0,0 +1,90 @@
|
|
|
|
|
package com.shxy.xymanager_framework.mqtt;
|
|
|
|
|
|
|
|
|
|
import com.shxy.xymanager_common.entity.TerminalPhoto;
|
|
|
|
|
import com.shxy.xymanager_common.entity.Terminals;
|
|
|
|
|
import com.shxy.xymanager_common.model.mqtt.DeviceUploadImageDataEvent;
|
|
|
|
|
import com.shxy.xymanager_common.util.DateUtil;
|
|
|
|
|
import com.shxy.xymanager_dao.dao.TerminalPhotoDao;
|
|
|
|
|
import com.shxy.xymanager_service.service.TerminalExtService;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.util.Base64Utils;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class PhotoHandler {
|
|
|
|
|
|
|
|
|
|
@Value("${mqtt.photodir}")
|
|
|
|
|
private String photodir;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
TerminalExtService terminalExtService;
|
|
|
|
|
@Resource
|
|
|
|
|
TerminalPhotoDao terminalPhotoDao;
|
|
|
|
|
|
|
|
|
|
public void handleUpload(DeviceUploadImageDataEvent upload, String deviceId) throws Exception {
|
|
|
|
|
Terminals term = terminalExtService.getByCmdid(deviceId);
|
|
|
|
|
if (term == null) {
|
|
|
|
|
log.error("mqtt收到图片,但是该装置" + deviceId + "不存在");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int length = deviceId.length();
|
|
|
|
|
Date now = new Date();
|
|
|
|
|
String folder = DateUtil.format(now, "yyyy/MM/dd") + "/" + deviceId.substring(length - 2);
|
|
|
|
|
File dir = new File(photodir + folder);
|
|
|
|
|
dir.mkdirs();
|
|
|
|
|
String hexC = String.format("%02X", upload.getPresetPosition());
|
|
|
|
|
String time = upload.getTime().replace("T", "").replace("Z", "");
|
|
|
|
|
Date photoTime = DateUtil.parse(time, "yyyyMMddHHmmss");
|
|
|
|
|
String filename = String.join("_",
|
|
|
|
|
deviceId, String.valueOf(upload.getChannelNumber()), hexC, time) + ".jpg";
|
|
|
|
|
String filePath = folder + "/" + filename;
|
|
|
|
|
|
|
|
|
|
byte[] data = Base64Utils.decodeFromString(upload.getData());
|
|
|
|
|
try (FileOutputStream fos = new FileOutputStream(photodir + filePath)) {
|
|
|
|
|
fos.write(data);
|
|
|
|
|
}
|
|
|
|
|
log.info("mqtt消息保存图片到" + filePath);
|
|
|
|
|
|
|
|
|
|
TerminalPhoto record = new TerminalPhoto();
|
|
|
|
|
record.setTermId(term.getId());
|
|
|
|
|
record.setChannelId(upload.getChannelNumber());
|
|
|
|
|
record.setPresetId(upload.getPresetPosition());
|
|
|
|
|
// record.setOrginalId(BigInteger.valueOf(upload.getPackageNumber()));
|
|
|
|
|
record.setPhotoTime(BigInteger.valueOf(photoTime.getTime() / 1000));
|
|
|
|
|
record.setMediaType(0);
|
|
|
|
|
record.setPath(filePath);
|
|
|
|
|
record.setRecvTime(BigInteger.valueOf(now.getTime() / 1000));
|
|
|
|
|
record.setRecvEndTime(BigInteger.valueOf(now.getTime() / 1000));
|
|
|
|
|
record.setCreateTime(now);
|
|
|
|
|
record.setFlags(0);
|
|
|
|
|
record.setIsMark(0);
|
|
|
|
|
try (ByteArrayInputStream bis = new ByteArrayInputStream(data)) {
|
|
|
|
|
BufferedImage image = ImageIO.read(bis);
|
|
|
|
|
if (image != null) {
|
|
|
|
|
int width = image.getWidth();
|
|
|
|
|
int height = image.getHeight();
|
|
|
|
|
record.setWidth(width);
|
|
|
|
|
record.setHeight(height);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException ignore) {
|
|
|
|
|
}
|
|
|
|
|
long fileSize = Files.size(Paths.get(photodir + filePath));
|
|
|
|
|
record.setFileSize((int) fileSize);
|
|
|
|
|
|
|
|
|
|
terminalPhotoDao.insert(record);
|
|
|
|
|
}
|
|
|
|
|
}
|