|
|
@@ -69,9 +69,21 @@ public class BaseDataTransferService {
|
|
|
this.idGenerator = new SnowflakeIdGenerator(1L, 1L);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取当前时间字符串
|
|
|
- */
|
|
|
+ private String convertFloor(String floor) {
|
|
|
+ if (floor == null || floor.trim().isEmpty()) {
|
|
|
+ return floor;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ int floorNum = Integer.parseInt(floor.trim());
|
|
|
+ if (floorNum < 0) {
|
|
|
+ return "B" + Math.abs(floorNum);
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.warn("楼层格式转换失败,原始值: {}", floor);
|
|
|
+ }
|
|
|
+ return floor;
|
|
|
+ }
|
|
|
+
|
|
|
private String getCurrentTime() {
|
|
|
return timeFormat.format(new Date());
|
|
|
}
|
|
|
@@ -156,62 +168,53 @@ public class BaseDataTransferService {
|
|
|
vo.setDataPacketID(generateDataPacketID());
|
|
|
}
|
|
|
if (vo.getPublishTime() == null) {
|
|
|
- vo.setPublishTime(timeFormat.format(new Date()));
|
|
|
+ vo.setPublishTime(getCurrentTime());
|
|
|
}
|
|
|
|
|
|
- // ========== 2. 读取本地图片 ==========
|
|
|
- String imagePath = "C:\\Users\\f\\Downloads\\45_平面图.jpg";
|
|
|
+ String imagePath = "D://k2-04.jpg";
|
|
|
+ // 将图片文件读取为字节数组
|
|
|
byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
|
|
|
|
|
|
- // 大小校验 ≤5MB
|
|
|
- if (imageBytes.length > 5 * 1024 * 1024) {
|
|
|
- System.err.println("文件超过5MB");
|
|
|
+ // 检查文件大小(不超过5MB)
|
|
|
+ if (vo.getFloorFile() != null && imageBytes.length > 5 * 1024 * 1024) {
|
|
|
+ log.error("楼层平面图文件大小超过5MB限制,FileID: {}", vo.getFloorFileID());
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 格式校验
|
|
|
- if (!Arrays.asList("jpg", "jpeg", "png").contains(vo.getFloorFileSuffix().toLowerCase())) {
|
|
|
- System.err.println("不支持的格式");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取宽高
|
|
|
- BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
|
|
|
- int width = image.getWidth();
|
|
|
- int height = image.getHeight();
|
|
|
-
|
|
|
- // ========== 3. 时间格式化 ==========
|
|
|
-
|
|
|
- // ========== 4. 构建标准JSON消息体 ==========
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
map.put("dataPacketID", vo.getDataPacketID());
|
|
|
map.put("engineeringID", vo.getEngineeringID());
|
|
|
map.put("floor", vo.getFloor());
|
|
|
map.put("floorFileID", vo.getFloorFileID());
|
|
|
map.put("floorFileName", vo.getFloorFileName());
|
|
|
map.put("floorFileSuffix", vo.getFloorFileSuffix());
|
|
|
- map.put("filePixWidth", width);
|
|
|
- map.put("filePixHeight", height);
|
|
|
+ map.put("filePixWidth", vo.getFilePixWidth());
|
|
|
+ map.put("filePixHeight", vo.getFilePixHeight());
|
|
|
map.put("floorFile", imageBytes);
|
|
|
map.put("publishTime", vo.getPublishTime());
|
|
|
-
|
|
|
- //使用Gson:
|
|
|
Gson gson = new Gson();
|
|
|
+ // 将字节数组转换为Base64编码
|
|
|
+ JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
|
|
|
+ vo.setFloorFile(imageBytes);
|
|
|
+// jsonObject.put("floorFile", imageBytes);
|
|
|
+ if (vo.getFloorFile() != null) {
|
|
|
+ // 使用Base64编码传输二进制数据
|
|
|
+ String base64File = java.util.Base64.getEncoder().encodeToString(vo.getFloorFile());
|
|
|
+ jsonObject.put("floorFile", imageBytes);
|
|
|
+ }
|
|
|
|
|
|
- // ========== 5. MQTT发送(修复版) ==========
|
|
|
+ String json = jsonObject.toJSONString();
|
|
|
+ System.out.println(gson.toJson(map));
|
|
|
String topic = "base/floorPlane";
|
|
|
- MqttConnectionTool.MqttGateway gateway = mqttConnectionTool.connectOrRefresh("3101100021", "SIixzph1");
|
|
|
|
|
|
- // 发送JSON字符串
|
|
|
- gateway.sendToMqtt(topic, gson.toJson(map));
|
|
|
+ log.info("发送楼层平面图信息,Topic: {}, FileID: {}, FileSize: {} bytes",
|
|
|
+ topic, vo.getFloorFileID(),
|
|
|
+ vo.getFloorFile() != null ? vo.getFloorFile().length : 0);
|
|
|
+ mqttGateway.sendToMqtt(topic, gson.toJson(map));
|
|
|
|
|
|
- System.out.println("✅ MQTT发送成功 TOPIC: " + topic);
|
|
|
return true;
|
|
|
-
|
|
|
} catch (Exception e) {
|
|
|
- // 打印完整异常
|
|
|
- e.printStackTrace();
|
|
|
- System.err.println("❌ 发送失败:" + e.getMessage());
|
|
|
+ log.error("发送楼层平面图信息失败,FileID: {}", vo.getFloorFileID(), e);
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -258,7 +261,7 @@ public class BaseDataTransferService {
|
|
|
HashMap<String, Object> map = new HashMap<>();
|
|
|
map.put("dataPacketID", generateDataPacketID());
|
|
|
map.put("engineeringID", vo.getEngineeringID());
|
|
|
- map.put("floor", vo.getFloor());
|
|
|
+ map.put("floor", convertFloor(vo.getFloor()));
|
|
|
map.put("floorFileID", 1);
|
|
|
map.put("sensorID", Integer.parseInt(vo.getDeviceId()));
|
|
|
map.put("sensorNo", vo.getDeviceUuid());
|