feat: 添加旧设备数据插入功能

This commit is contained in:
LokerL 2024-11-04 16:41:00 +08:00
parent 10ac11005a
commit 86d43a562d
4 changed files with 70 additions and 10 deletions

View File

@ -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 {
// }
}
}

View File

@ -154,6 +154,7 @@ public class SensorData {
", dbz='" + dbz + '\'' +
", gbz='" + gbz + '\'' +
", lc='" + lc + '\'' +
", reportTime='" + reportTime + '\'' +
'}';
}
}

View File

@ -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;
}

View File

@ -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<SensorMonth> list=outSideMapper.queryMonth(lastMonth);
List<SensorMonth> 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) {