代码优化
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.dromara.mica.mqtt.server.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.dromara.mica.mqtt.server.service.impl.ServerService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -14,10 +15,18 @@ public class ServerController {
|
||||
private ServerService service;
|
||||
|
||||
@PostMapping("publish")
|
||||
public boolean publish(@RequestBody String body) {
|
||||
return service.publish(body);
|
||||
public JSONObject publish(@RequestBody JSONObject js) {
|
||||
boolean publish = service.publish(js);
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
if (publish) {
|
||||
jsonObject.put("code", 200);
|
||||
} else {
|
||||
jsonObject.put("code", 500);
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("open")
|
||||
public boolean open(@RequestBody String body) {
|
||||
return service.open(body);
|
||||
@@ -38,4 +47,9 @@ public class ServerController {
|
||||
return service.offline_record(body);
|
||||
}
|
||||
|
||||
@PostMapping("set_time")
|
||||
public boolean set_time(@RequestBody String body) {
|
||||
return service.set_time(body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.dromara.mica.mqtt.server.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 在场记录对象 car_pass_gather
|
||||
*
|
||||
*/
|
||||
@Data
|
||||
@TableName("car_pass_gather")
|
||||
public class CarPassGather implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 停车场id
|
||||
*/
|
||||
private String parkId;
|
||||
|
||||
/**
|
||||
* 区域
|
||||
*/
|
||||
private String area;
|
||||
|
||||
/**
|
||||
* 车牌号码
|
||||
*/
|
||||
private String license;
|
||||
|
||||
/**
|
||||
* 入场时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date joinTime;
|
||||
|
||||
/**
|
||||
* 设备序列号
|
||||
*/
|
||||
private String sn;
|
||||
|
||||
}
|
||||
@@ -1,28 +1,33 @@
|
||||
package org.dromara.mica.mqtt.server.listener;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dromara.mica.mqtt.core.annotation.MqttServerFunction;
|
||||
import org.dromara.mica.mqtt.server.constant.CacheConstants;
|
||||
import org.dromara.mica.mqtt.server.entity.CarParkItem;
|
||||
import org.dromara.mica.mqtt.server.entity.CarParkRecord;
|
||||
import org.dromara.mica.mqtt.server.entity.CarPassGather;
|
||||
import org.dromara.mica.mqtt.server.entity.CarPassRecord;
|
||||
import org.dromara.mica.mqtt.server.entity.Equipment;
|
||||
import org.dromara.mica.mqtt.server.enums.FlagEnums;
|
||||
import org.dromara.mica.mqtt.server.pojo.WhiteListOperatorPO;
|
||||
import org.dromara.mica.mqtt.server.redis.RedisService;
|
||||
import org.dromara.mica.mqtt.server.service.ICarParkItemService;
|
||||
import org.dromara.mica.mqtt.server.service.ICarParkRecordService;
|
||||
import org.dromara.mica.mqtt.server.service.IEquipmentService;
|
||||
import org.dromara.mica.mqtt.server.service.ICarPassGatherService;
|
||||
import org.dromara.mica.mqtt.server.service.ICarPassRecordService;
|
||||
import org.dromara.mica.mqtt.server.utils.AESUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.tio.utils.hutool.StrUtil;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -42,13 +47,18 @@ public class CarMessageListener {
|
||||
ICarParkRecordService carParkRecordService;
|
||||
|
||||
@Autowired
|
||||
IEquipmentService equipmentService;
|
||||
ICarPassRecordService carPassRecordService;
|
||||
|
||||
@Autowired
|
||||
ICarParkItemService carParkItemService;
|
||||
|
||||
@Autowired
|
||||
ICarPassGatherService carPassGatherService;
|
||||
|
||||
private static String key = "1234567898765432";
|
||||
|
||||
private String jinjiangUrl = "http://127.0.0.1:6609/";
|
||||
|
||||
/**
|
||||
* 心跳
|
||||
* @param topic
|
||||
@@ -101,13 +111,15 @@ public class CarMessageListener {
|
||||
@MqttServerFunction("device/${sn}/message/up/ivs_result")
|
||||
public void ivs_result(String topic, Map<String, String> topicVars, byte[] message) throws Exception {
|
||||
String sn = topicVars.get("sn");
|
||||
log.info("接收到车辆识别消息 -> Topic: {}, message: {}", topic, new String(message));
|
||||
log.info("接收到车辆识别消息 -> Topic: {}", topic);
|
||||
String data = new String(message, StandardCharsets.UTF_8);
|
||||
JSONObject jsonObject = JSONObject.parseObject(data);
|
||||
JSONObject payload = jsonObject.getJSONObject("payload");
|
||||
String id = jsonObject.getString("id");
|
||||
JSONObject alarmInfoPlate = payload.getJSONObject("AlarmInfoPlate");
|
||||
JSONObject result = alarmInfoPlate.getJSONObject("result");
|
||||
JSONObject plateResult = result.getJSONObject("PlateResult");
|
||||
JSONObject carBrand = plateResult.getJSONObject("car_brand");
|
||||
String license = plateResult.getString("license");
|
||||
String colorType = plateResult.getString("colorType");
|
||||
String str = "";
|
||||
@@ -119,6 +131,46 @@ public class CarMessageListener {
|
||||
|
||||
license = AESUtil.UTF8decode(str);
|
||||
log.info("解密前车牌:{},解谜后的车牌:{}", plateResult.getString("license"), license);
|
||||
|
||||
//保存通行记录
|
||||
CarPassRecord record = new CarPassRecord();
|
||||
record.setCarColor(plateResult.getString("carColor"));
|
||||
record.setColorType(colorType);
|
||||
record.setDirection(plateResult.getString("direction"));
|
||||
record.setLicense(license);
|
||||
record.setDataType("0");
|
||||
Map<String, Object> paramMap = new HashMap<>();
|
||||
paramMap.put("url", plateResult.getString("full_image_content"));
|
||||
String repose = HttpUtil.createPost(jinjiangUrl + "file/uploadMinioCarBase64")
|
||||
.body(JSON.toJSONString(paramMap))
|
||||
.execute()
|
||||
.body();
|
||||
log.info("imgRsp:{}", repose);
|
||||
JSONObject imgRsp = JSONObject.parseObject(repose);
|
||||
if (null != imgRsp && imgRsp.getInteger("code") == 200) {
|
||||
record.setUrl(imgRsp.getJSONObject("data").getString("url"));
|
||||
}
|
||||
record.setPassTime(DateUtil.date(plateResult.getLong("start_time")));
|
||||
record.setSn(sn);
|
||||
record.setUniqueNo(id);
|
||||
record.setTriggerType(plateResult.getString("triggerType"));
|
||||
record.setType(carBrand.getString("type"));
|
||||
carPassRecordService.save(record);
|
||||
|
||||
//保存/删除在场数据
|
||||
CarParkItem carParkItem = carParkItemService.selectBySn(sn);
|
||||
//入场新增数据
|
||||
if (StrUtil.equals(carParkItem.getWay(), "0")) {
|
||||
CarPassGather carPassGather = new CarPassGather();
|
||||
carPassGather.setLicense(license);
|
||||
carPassGather.setParkId(carParkItem.getParkId());
|
||||
carPassGather.setArea(carParkItem.getArea());
|
||||
carPassGather.setJoinTime(record.getPassTime());
|
||||
carPassGather.setSn(sn);
|
||||
carPassGatherService.save(carPassGather);
|
||||
} else {//出场删除数据
|
||||
carPassGatherService.deleteByLicense(license);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,14 +182,16 @@ public class CarMessageListener {
|
||||
@MqttServerFunction("device/${sn}/message/up/ivs_result_offline")
|
||||
public void ivs_result_offline(String topic, Map<String, String> topicVars, byte[] message) throws Exception {
|
||||
String sn = topicVars.get("sn");
|
||||
log.info("接收到车辆离线识别消息 -> Topic: {}, message: {}", topic, new String(message));
|
||||
log.info("接收到车辆离线识别消息 -> Topic: {}", topic);
|
||||
String data = new String(message, StandardCharsets.UTF_8);
|
||||
log.info("车牌离线识别监听:{}", data);
|
||||
JSONObject jsonObject = JSONObject.parseObject(data);
|
||||
JSONObject payload = jsonObject.getJSONObject("payload");
|
||||
String id = jsonObject.getString("id");
|
||||
JSONObject alarmInfoPlate = payload.getJSONObject("AlarmInfoPlate");
|
||||
JSONObject result = alarmInfoPlate.getJSONObject("result");
|
||||
JSONObject plateResult = result.getJSONObject("PlateResult");
|
||||
JSONObject carBrand = plateResult.getJSONObject("car_brand");
|
||||
String license = plateResult.getString("license");
|
||||
String colorType = plateResult.getString("colorType");
|
||||
String str = "";
|
||||
@@ -146,15 +200,34 @@ public class CarMessageListener {
|
||||
} else {
|
||||
str = AESUtil.decrptyAES_ECB(license, key).substring(0, 18);
|
||||
}
|
||||
|
||||
license = AESUtil.UTF8decode(str);
|
||||
log.info("离线-解密前车牌:{},解谜后的车牌:{}", plateResult.getString("license"), license);
|
||||
CarParkItem carParkItem = carParkItemService.selectBySn(sn);
|
||||
CarPassRecord carPassRecord = new CarPassRecord();
|
||||
carPassRecord.setSn(sn);
|
||||
carPassRecord.setParkId(carParkItem.getParkId());
|
||||
|
||||
//保存通行记录
|
||||
CarPassRecord record = new CarPassRecord();
|
||||
record.setCarColor(plateResult.getString("carColor"));
|
||||
record.setColorType(colorType);
|
||||
record.setDirection(plateResult.getString("direction"));
|
||||
record.setLicense(license);
|
||||
record.setDataType("0");
|
||||
|
||||
Map<String, Object> paramMap = new HashMap<>();
|
||||
paramMap.put("url", plateResult.getString("full_image_content"));
|
||||
String repose = HttpUtil.createPost(jinjiangUrl + "file/uploadMinioCarBase64")
|
||||
.body(JSON.toJSONString(paramMap))
|
||||
.execute()
|
||||
.body();
|
||||
log.info("离线-imgRsp:{}", repose);
|
||||
JSONObject imgRsp = JSONObject.parseObject(repose);
|
||||
if (null != imgRsp && imgRsp.getInteger("code") == 200) {
|
||||
record.setUrl(imgRsp.getJSONObject("data").getString("url"));
|
||||
}
|
||||
record.setPassTime(DateUtil.date(plateResult.getLong("start_time")));
|
||||
record.setSn(sn);
|
||||
record.setUniqueNo(id);
|
||||
record.setTriggerType(plateResult.getString("triggerType"));
|
||||
record.setType(carBrand.getString("type"));
|
||||
carPassRecordService.save(record);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.dromara.mica.mqtt.server.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.dromara.mica.mqtt.server.entity.CarPassGather;
|
||||
|
||||
@Mapper
|
||||
public interface CarPassGatherMapper extends BaseMapper<CarPassGather> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.dromara.mica.mqtt.server.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.dromara.mica.mqtt.server.entity.CarPassRecord;
|
||||
|
||||
@Mapper
|
||||
public interface CarPassRecordMapper extends BaseMapper<CarPassRecord> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.dromara.mica.mqtt.server.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.dromara.mica.mqtt.server.entity.CarPassGather;
|
||||
|
||||
public interface ICarPassGatherService extends IService<CarPassGather> {
|
||||
|
||||
void deleteByLicense(String license);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.dromara.mica.mqtt.server.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.dromara.mica.mqtt.server.entity.CarPassRecord;
|
||||
|
||||
public interface ICarPassRecordService extends IService<CarPassRecord> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.dromara.mica.mqtt.server.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.dromara.mica.mqtt.server.entity.CarPassGather;
|
||||
import org.dromara.mica.mqtt.server.mapper.CarPassGatherMapper;
|
||||
import org.dromara.mica.mqtt.server.service.ICarPassGatherService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class CarPassGatherServiceImpl extends ServiceImpl<CarPassGatherMapper, CarPassGather> implements ICarPassGatherService {
|
||||
|
||||
@Autowired
|
||||
CarPassGatherMapper carPassGatherMapper;
|
||||
|
||||
@Override
|
||||
public void deleteByLicense(String license) {
|
||||
carPassGatherMapper.delete(new LambdaQueryWrapper<CarPassGather>().eq(CarPassGather::getLicense, license));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.dromara.mica.mqtt.server.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.dromara.mica.mqtt.server.entity.CarParkRecord;
|
||||
import org.dromara.mica.mqtt.server.entity.CarPassRecord;
|
||||
import org.dromara.mica.mqtt.server.mapper.CarParkRecordMapper;
|
||||
import org.dromara.mica.mqtt.server.mapper.CarPassRecordMapper;
|
||||
import org.dromara.mica.mqtt.server.service.ICarParkRecordService;
|
||||
import org.dromara.mica.mqtt.server.service.ICarPassRecordService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class CarPassRecordServiceImpl extends ServiceImpl<CarPassRecordMapper, CarPassRecord> implements ICarPassRecordService {
|
||||
|
||||
@Autowired
|
||||
CarPassRecordMapper carPassRecordMapper;
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.dromara.mica.mqtt.server.service.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.mica.mqtt.spring.server.MqttServerTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -16,9 +17,12 @@ public class ServerService {
|
||||
@Autowired
|
||||
private MqttServerTemplate server;
|
||||
|
||||
public boolean publish(String body) {
|
||||
boolean result = server.publish("61e70b04-8e68be6a","device/61e70b04-8e68be6a/message/down/white_list_operator", body.getBytes(StandardCharsets.UTF_8));
|
||||
log.info("测试body:{},result:{}", body, result);
|
||||
public boolean publish(JSONObject js) {
|
||||
String sn = js.getString("sn");
|
||||
String topic = js.getString("topic");
|
||||
String body = js.getString("body");
|
||||
boolean result = server.publish(sn,topic, body.getBytes(StandardCharsets.UTF_8));
|
||||
log.info("publish-topic:{},body:{},result:{}", topic, body, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -45,4 +49,10 @@ public class ServerService {
|
||||
log.info("发布离线数据数量body:{},result:{}", body, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean set_time(String body) {
|
||||
boolean result = server.publish("61e70b04-8e68be6a","device/61e70b04-8e68be6a/message/up/offline_record", body.getBytes(StandardCharsets.UTF_8));
|
||||
log.info("发布离线数据数量body:{},result:{}", body, result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.dromara.mica.mqtt.server.utils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import java.io.*;
|
||||
import java.util.Base64;
|
||||
|
||||
public class Base64MultipartFile implements MultipartFile {
|
||||
|
||||
private final byte[] fileContent;
|
||||
private final String fileName;
|
||||
private final String contentType;
|
||||
|
||||
public Base64MultipartFile(String base64Data, String fileName) {
|
||||
// 清理Base64数据
|
||||
String cleanedBase64 = base64Data.contains(",")
|
||||
? base64Data.substring(base64Data.indexOf(",") + 1)
|
||||
: base64Data;
|
||||
|
||||
this.fileContent = Base64.getDecoder().decode(cleanedBase64);
|
||||
this.fileName = fileName;
|
||||
this.contentType = extractContentType(base64Data);
|
||||
}
|
||||
|
||||
private String extractContentType(String base64Data) {
|
||||
if (base64Data.startsWith("data:image/jpeg")) {
|
||||
return "image/jpeg";
|
||||
} else if (base64Data.startsWith("data:image/png")) {
|
||||
return "image/png";
|
||||
} else if (base64Data.startsWith("data:image/gif")) {
|
||||
return "image/gif";
|
||||
}
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "file";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOriginalFilename() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return fileContent == null || fileContent.length == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize() {
|
||||
return fileContent.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBytes() throws IOException {
|
||||
return fileContent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(fileContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transferTo(File dest) throws IOException, IllegalStateException {
|
||||
try (FileOutputStream fos = new FileOutputStream(dest)) {
|
||||
fos.write(fileContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.dromara.mica.mqtt.server.utils;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public class Base64ToMultipartFileUtil {
|
||||
|
||||
public static MultipartFile convertToMultipartFile(String base64Data, String fileName) {
|
||||
return new Base64MultipartFile(base64Data, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动生成文件名
|
||||
*/
|
||||
public static MultipartFile convertToMultipartFile(String base64Data) {
|
||||
String fileName = generateFileName(base64Data);
|
||||
return convertToMultipartFile(base64Data, fileName);
|
||||
}
|
||||
|
||||
private static String generateFileName(String base64Data) {
|
||||
String extension = ".jpg";
|
||||
if (base64Data.startsWith("data:image/png")) {
|
||||
extension = ".png";
|
||||
} else if (base64Data.startsWith("data:image/gif")) {
|
||||
extension = ".gif";
|
||||
}
|
||||
return "image_" + System.currentTimeMillis() + extension;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package org.dromara.mica.mqtt.server.utils;
|
||||
|
||||
public class SNUtils {
|
||||
|
||||
public String getSn(String topic) {
|
||||
return topic;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user