diff --git a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/controller/OutSideController.java b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/controller/OutSideController.java index 07e152e..a8b1980 100644 --- a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/controller/OutSideController.java +++ b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/controller/OutSideController.java @@ -16,17 +16,28 @@ public class OutSideController extends BaseController { @Autowired private OutSideService outSideService; + @PostMapping("/sensorData") public AjaxResult receiveSensorData(@RequestBody String data) { try { - Integer succ=outSideService.insertDevice(data); - return AjaxResult.success(succ); - } catch (Exception e) { + Integer succ = outSideService.insertDevice(data); + return AjaxResult.success(succ); + } catch (Exception e) { System.out.println(e); - return AjaxResult.error(); - } - + return AjaxResult.error(); } + } + + @PostMapping("/insertOldDeviceData") + public AjaxResult insertOldDeviceData(@RequestBody String data) { + try { + Integer succ = outSideService.insertOldDeviceData(data); + return AjaxResult.success(succ); + } catch (Exception e) { + System.out.println(e); + return AjaxResult.error(); + } + } // @PostMapping("/monthTask") // public AjaxResult monthTask(@RequestBody String date) { // try { @@ -40,5 +51,5 @@ public class OutSideController extends BaseController { // } - } +} diff --git a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorData.java b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorData.java index f45adc2..00b8705 100644 --- a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorData.java +++ b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorData.java @@ -154,6 +154,7 @@ public class SensorData { ", dbz='" + dbz + '\'' + ", gbz='" + gbz + '\'' + ", lc='" + lc + '\'' + + ", reportTime='" + reportTime + '\'' + '}'; } } diff --git a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/OutSideService.java b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/OutSideService.java index 9915f14..2690a7e 100644 --- a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/OutSideService.java +++ b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/OutSideService.java @@ -1,5 +1,8 @@ package com.ruoyi.project.outside.service; +import com.fasterxml.jackson.core.JsonProcessingException; + + public interface OutSideService { /** * 插入设备表 @@ -14,4 +17,11 @@ public interface OutSideService { * @return */ Integer monthTask(String date); + + /** + * 旧数据补全 + * @param data + * @return + */ + Integer insertOldDeviceData(String data) throws JsonProcessingException; } diff --git a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/impl/OutSideServiceImpl.java b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/impl/OutSideServiceImpl.java index bf46665..407a56c 100644 --- a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/impl/OutSideServiceImpl.java +++ b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/impl/OutSideServiceImpl.java @@ -1,9 +1,9 @@ package com.ruoyi.project.outside.service.impl; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.ruoyi.common.utils.DateUtils; -import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.project.outside.domain.SensorData; import com.ruoyi.project.outside.domain.SensorMonth; import com.ruoyi.project.outside.mapper.OutSideMapper; @@ -20,6 +20,7 @@ public class OutSideServiceImpl implements OutSideService { @Autowired private OutSideMapper outSideMapper; + @Override public Integer insertDevice(String data) { try { @@ -51,7 +52,7 @@ public class OutSideServiceImpl implements OutSideService { outSideMapper.insertDevice(sensorData); } return 1; - }catch (Exception e) { + } catch (Exception e) { System.out.println(e); return 0; } @@ -69,11 +70,47 @@ public class OutSideServiceImpl implements OutSideService { // 格式化回字符串 String lastMonth = previousMonth.format(formatter); - List list=outSideMapper.queryMonth(lastMonth); + List list = outSideMapper.queryMonth(lastMonth); outSideMapper.insertMonth(list); return 1; } + @Override + public Integer insertOldDeviceData(String data) throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode rootNode = objectMapper.readTree(data); + // 判断是否是数组 + if (rootNode.isArray()) { + for (JsonNode node : rootNode) { + SensorData sd = insertOldData(node); + outSideMapper.insertDevice(sd); + } + } else { + SensorData sd = insertOldData(rootNode); + outSideMapper.insertDevice(sd); + } + return 0; + } + + private static SensorData insertOldData(JsonNode node) { + /** + * sn: equipmentUId + * ds: vocRtd + * report_time: vocTime + * zt: vocRtd > 5 ? '一级报警' : '正常' + */ + SensorData sensorData = new SensorData(); + sensorData.setSn(node.get("equipmentUId").asText()); + sensorData.setDs(node.get("vocRtd").asText()); + sensorData.setDw(getUnitDescription(5)); + // "vocTime":"2024-10-30T07:19:20.000+0000" 转为 yyyy-MM-dd HH:mm:ss + String reportTime = node.get("vocTime").asText().replace("T", " ").substring(0, 19); + sensorData.setReportTime(reportTime); + + sensorData.setZt(node.get("vocRtd").asInt() > 5 ? "一级报警" : "正常"); + return sensorData; + } + // 将 gas_unit 值转换为对应的单位描述 private static String getUnitDescription(int gasUnit) { switch (gasUnit) { @@ -99,6 +136,7 @@ public class OutSideServiceImpl implements OutSideService { return "未知单位"; // 如果有其他未定义的单位,返回默认值 } } + // 将 gas_status 值转换为对应的中文描述 private static String getStatusDescription(int gasStatus) { switch (gasStatus) {