feat (farm): 设备中心

- 修复因设备类型id导致的数据紊乱问题
This commit is contained in:
达富斌
2025-09-30 17:46:24 +08:00
parent 671e12b130
commit fc0f0e3ea3
14 changed files with 139 additions and 87 deletions

View File

@@ -16,13 +16,13 @@ import cn.iocoder.yudao.module.farm.dal.dataobject.mqtt.MqttConnectionDO;
import cn.iocoder.yudao.module.farm.dal.dataobject.plotinfo.PlotInfoDO;
import cn.iocoder.yudao.module.farm.enums.CommonConstants;
import cn.iocoder.yudao.module.farm.service.baseinfo.BaseInfoService;
import cn.iocoder.yudao.module.farm.service.deviceclassify.DeviceClassifyService;
import cn.iocoder.yudao.module.farm.service.deviceinfo.DeviceInfoService;
import cn.iocoder.yudao.module.farm.service.mqtt.DeviceMqttService;
import cn.iocoder.yudao.module.farm.service.plotinfo.PlotInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
@@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
@@ -45,14 +46,19 @@ import static cn.iocoder.yudao.module.farm.enums.CommonConstants.DEVICE_STATE_MA
@Validated
public class DeviceInfoController {
@Resource
private DeviceInfoService deviceInfoService;
@Resource
private DeviceMqttService deviceMqttService;
@Resource
private BaseInfoService baseInfoService;
@Resource
private PlotInfoService plotInfoService;
private final DeviceInfoService deviceInfoService;
private final DeviceClassifyService deviceClassifyService;
private final DeviceMqttService deviceMqttService;
private final BaseInfoService baseInfoService;
private final PlotInfoService plotInfoService;
public DeviceInfoController(DeviceInfoService deviceInfoService, DeviceClassifyService deviceClassifyService, DeviceMqttService deviceMqttService, BaseInfoService baseInfoService, PlotInfoService plotInfoService) {
this.deviceInfoService = deviceInfoService;
this.deviceClassifyService = deviceClassifyService;
this.deviceMqttService = deviceMqttService;
this.baseInfoService = baseInfoService;
this.plotInfoService = plotInfoService;
}
@PostMapping("/create")
@Operation(summary = "创建设备信息")
@@ -115,9 +121,9 @@ public class DeviceInfoController {
@GetMapping("/get")
@Operation(summary = "获得设备信息")
@Parameter(name = "id", description = "编号", required = true)
// @Parameter(name = "dateType", description = "日期类型", required = false)
public CommonResult<DeviceInfoDetailV2RespVO> getDeviceInfo(@RequestParam("id") String id) {
DeviceInfoDetailV2RespVO res = deviceInfoService.getDeviceRealTimeInfo(id,"dateType");
@Parameter(name = "dateType", description = "日期类型", required = false)
public CommonResult<DeviceInfoDetailV2RespVO> getDeviceInfo(@RequestParam("id") String id,@RequestParam("dateType") String dateType) {
DeviceInfoDetailV2RespVO res = deviceInfoService.getDeviceRealTimeInfo(id,dateType);
return success(res);
}
@@ -144,6 +150,10 @@ public class DeviceInfoController {
d.setPlotPoint(p.getPoint());
}
Map<String, String> typeNameMap = deviceClassifyService.getTypeNameMap();
Map<String, String> typeCodeMap = deviceClassifyService.getTypeCodeMap();
d.setDeviceTypeName(typeNameMap.get(d.getDeviceType()));
d.setDeviceTypeCode(typeCodeMap.get(d.getDeviceType()));
d.setDeviceStateName(DEVICE_STATE_MAP.get(d.getDeviceState()));
d.setBindingStateName(BINDING_STATE_MAP.get(d.getBindingState()));
}

View File

@@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.farm.controller.admin.deviceinfo.vo.v2;
import cn.iocoder.yudao.module.farm.manager.equipmentproducer.dto.HistoryDataResponse;
import cn.iocoder.yudao.module.farm.manager.equipmentproducer.dto.RealTimeDataResponse;
import cn.iocoder.yudao.module.farm.manager.equipmentproducer.dto.soil.realtime.RealTimeDataRespVO;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -31,6 +29,8 @@ public class DeviceInfoDetailV2RespVO {
private String deviceType;
@Schema(description = "设备类型 (设备类型-名称)")
private String deviceTypeName;
@Schema(description = "设备类型 (设备类型-唯一编码)")
private String deviceTypeCode;
@Schema(description = "设备分类名称 (设备型号,字符拼接)")
private String deviceCategoryName;

View File

@@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.farm.controller.admin.deviceinfo.vo.v2;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@@ -21,6 +20,8 @@ public class DeviceInfoPageV2RespVO {
@Schema(description = "设备类型 (设备类型ID)")
private String deviceType;
private String deviceTypeCode;
private String deviceTypeName;
@Schema(description = "设备分类名称 (设备型号,字符拼接)")
private String deviceCategoryName;

View File

@@ -38,6 +38,10 @@ public class DeviceClassifyDO extends BaseDO {
* 名称
*/
private String name;
/**
* 类型编号
*/
private String typeCode;
/**
* 图片
*/

View File

@@ -93,8 +93,8 @@ public interface TraceabilityCodeMapper extends BaseMapperX<TraceabilityCodeDO>
@Select("<script>" +
"SELECT " +
"COUNT(*) as totalAmount, " +
"SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) as normalAmount, " +
"SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as wasteAmount " +
"COALESCE(SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END),0) as normalAmount, " +
"COALESCE(SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END),0) as wasteAmount " +
"FROM farm_traceability_code " +
"WHERE 1=1 " +
"<if test='tenantId != null'> AND tenant_id = #{tenantId} </if>" +

View File

@@ -100,4 +100,6 @@ public class CommonConstants {
public final static Integer FARM_MODEL_FARMING_OPERATION_USABLE_TYPE_1 = 1; // 可用
public final static Integer FARM_MODEL_FARMING_OPERATION_USABLE_TYPE_2 = 2; // 不可用
public final static String DEVICE_TYPE_SOIL = "soil";
public final static String DEVICE_TYPE__WF = "water_fertilizer";
}

View File

@@ -3,15 +3,18 @@ package cn.iocoder.yudao.module.farm.manager.equipmentproducer.dto.soil.history;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
/**
* @description: 墒情设备, 获取历史数据 请求实体
* @author: binda
* @date: 2025/9/17 20:53
*/
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
@@ -40,20 +43,39 @@ public class HistoryDataListPageReqVO {
* 必选
* 页码
*/
private Integer pages;
private Integer pages = 1;
/**
* 必选
* 每页数量
* 每页数量 最大值 1000
*/
private Integer limit;
private Integer limit = 1000;
public HistoryDataListPageReqVO(String beginTime, String endTime) {
this.beginTime = beginTime;
this.endTime = endTime;
public HistoryDataListPageReqVO(Long beginTime, Long endTime) {
this.beginTime = String.valueOf(beginTime);
this.endTime = String.valueOf(endTime);
}
public HistoryDataListPageReqVO(String deviceAddr, Long beginTime, Long endTime) {
this.deviceAddr = deviceAddr;
this.beginTime = String.valueOf(beginTime);
this.endTime = String.valueOf(endTime);
}
public static HistoryDataListPageReqVO buildHistoryData(String deviceAddr, Integer dateType) {
HistoryDataListPageReqVO target = new HistoryDataListPageReqVO();
switch (dateType) {
case 12 -> HistoryDataTimeRangeUtil.getLast12Hours();
case 24 -> HistoryDataTimeRangeUtil.getLast24Hours();
case 3 -> HistoryDataTimeRangeUtil.getLast3Days();
case 7 -> HistoryDataTimeRangeUtil.getLast7Days();
}
target.setDeviceAddr(deviceAddr);
return target;
}
public static class HistoryDataTimeRangeUtil {
private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* 获取近12小时的时间范围
@@ -62,6 +84,7 @@ public class HistoryDataListPageReqVO {
LocalDateTime endTime = LocalDateTime.now();
LocalDateTime startTime = endTime.minusHours(12);
log.info("近12小时:{}-{}", startTime.format(formatter), endTime.format(formatter));
return buildRequestVO(startTime, endTime);
}
@@ -72,6 +95,7 @@ public class HistoryDataListPageReqVO {
LocalDateTime endTime = LocalDateTime.now();
LocalDateTime startTime = endTime.minusDays(1);
log.info("近24小时:{}-{}", startTime.format(formatter), endTime.format(formatter));
return buildRequestVO(startTime, endTime);
}
@@ -82,6 +106,7 @@ public class HistoryDataListPageReqVO {
LocalDateTime endTime = LocalDateTime.now();
LocalDateTime startTime = endTime.minusDays(3);
log.info("近3天:{}-{}", startTime.format(formatter), endTime.format(formatter));
return buildRequestVO(startTime, endTime);
}
@@ -92,6 +117,7 @@ public class HistoryDataListPageReqVO {
LocalDateTime endTime = LocalDateTime.now();
LocalDateTime startTime = endTime.minusDays(7);
log.info("近7天:{}-{}", startTime.format(formatter), endTime.format(formatter));
return buildRequestVO(startTime, endTime);
}
@@ -99,9 +125,10 @@ public class HistoryDataListPageReqVO {
* 构建请求VO对象
*/
private static HistoryDataListPageReqVO buildRequestVO(LocalDateTime startTime, LocalDateTime endTime) {
String beginTimeStamp = String.valueOf(startTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
String endTimeStamp = String.valueOf(endTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
// long startTime = startLocalDateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
// long endTime = endLocalDateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
Long beginTimeStamp = startTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
Long endTimeStamp = endTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
return new HistoryDataListPageReqVO(beginTimeStamp, endTimeStamp);
}
}

View File

@@ -46,7 +46,7 @@ public class HistoryDataListPageRespVO {
private String temStr;
private Double temValue;
/**
* 模拟狼 2
* 模拟量 2
*/
private String humStr;
private Double humValue;

View File

@@ -67,4 +67,7 @@ public interface DeviceClassifyService {
List<DeviceTypeRespVO> getDeviceTypeList();
List<DeviceModelRespVO> getDeviceModelList(String deviceTypeId);
Map<String, String> getTypeNameMap();
Map<String, String> getTypeCodeMap();
}

View File

@@ -17,6 +17,7 @@ import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -211,4 +212,22 @@ public class DeviceClassifyServiceImpl implements DeviceClassifyService {
imageUrl.startsWith("/") || imageUrl.startsWith("data:image/");
}
public Map<String, String> getTypeNameMap() {
LambdaQueryWrapperX<DeviceClassifyDO> x = new LambdaQueryWrapperX<>();
x.eq(DeviceClassifyDO::getTenantId, getLoginUser().getTenantId());
List<DeviceClassifyDO> deviceClassifyDOS = deviceClassifyMapper.selectList(x);
Map<String, String> typeNameMap = deviceClassifyDOS.stream().filter(device -> device.getParentId() != null && device.getParentId() == 0).collect(Collectors.toMap(e -> e.getId().toString(), DeviceClassifyDO::getName));
return typeNameMap;
}
public Map<String, String> getTypeCodeMap() {
LambdaQueryWrapperX<DeviceClassifyDO> x = new LambdaQueryWrapperX<>();
x.eq(DeviceClassifyDO::getTenantId, getLoginUser().getTenantId());
List<DeviceClassifyDO> deviceClassifyDOS = deviceClassifyMapper.selectList(x);
Map<String, String> typeCodeMap = deviceClassifyDOS.stream().filter(device -> device.getParentId() != null && device.getParentId() == 0).collect(Collectors.toMap(e -> e.getId().toString(), DeviceClassifyDO::getTypeCode));
return typeCodeMap;
}
}

View File

@@ -8,7 +8,6 @@ import cn.iocoder.yudao.module.farm.controller.admin.deviceinfo.vo.v2.DeviceInfo
import cn.iocoder.yudao.module.farm.controller.admin.deviceinfo.vo.v2.DeviceStatisticsRespVO;
import cn.iocoder.yudao.module.farm.dal.dataobject.deviceinfo.DeviceInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.module.farm.manager.equipmentproducer.dto.RealTimeDataResponse;
import jakarta.validation.Valid;
@@ -119,4 +118,5 @@ public interface DeviceInfoService {
* @return
*/
int countDevice(String baseId, String tenantId);
}

View File

@@ -34,9 +34,6 @@ import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.module.farm.dal.mysql.deviceinfo.DeviceInfoMapper;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -46,6 +43,7 @@ import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionU
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static cn.iocoder.yudao.module.farm.enums.CommonConstants.DEVICE_TYPE_SOIL;
import static cn.iocoder.yudao.module.farm.enums.ErrorCodeConstants.DEVICE_CLASS_TYPE_ERROR;
import static cn.iocoder.yudao.module.farm.enums.ErrorCodeConstants.DEVICE_INFO_NOT_EXISTS;
@@ -140,71 +138,58 @@ public class DeviceInfoServiceImpl implements DeviceInfoService {
public DeviceInfoDetailV2RespVO getDeviceRealTimeInfo(String id,String dateType) {
List<DeviceClassifyDO> deviceClassifyDOS = deviceClassifyMapper.selectList();
Map<String, String> map = deviceClassifyDOS.stream()
.filter(device -> device.getParentId() != null && device.getParentId() == 0)
.collect(Collectors.toMap(e -> e.getId().toString(), DeviceClassifyDO::getName));
Map<String, String> typeNameMap = deviceClassifyDOS.stream().filter(device -> device.getParentId() != null && device.getParentId() == 0).collect(Collectors.toMap(e -> e.getId().toString(), DeviceClassifyDO::getName));
Map<String, String> typeCodeMap = deviceClassifyDOS.stream().filter(device -> device.getParentId() != null && device.getParentId() == 0).collect(Collectors.toMap(e -> e.getId().toString(), DeviceClassifyDO::getTypeCode));
DeviceInfoDO deviceInfo = this.getDeviceInfo(id);
DeviceInfoDetailV2RespVO target = BeanUtils.toBean(deviceInfo, DeviceInfoDetailV2RespVO.class);
// 设备类型名称
target.setDeviceTypeName(map.getOrDefault(target.getDeviceType(), ""));
String deviceUniqueId = deviceInfo.getDeviceUniqueId();
String deviceType = deviceInfo.getDeviceType();
String soil = "190"; // soil
String wf = "189"; // water_fertilizer
// 设备类型名称
target.setDeviceTypeName(typeNameMap.getOrDefault(target.getDeviceType(), ""));
// 设备唯一编号
target.setDeviceTypeCode(typeCodeMap.getOrDefault(target.getDeviceType(), ""));
String type = "";
if (target.getDeviceType().equals(soil)) {
type = "soil";
// soil
UserLoginReqVO userLoginReqVO = new UserLoginReqVO(deviceConf.getFourSituations().getUsername(), deviceConf.getFourSituations().getPassword());
UnifiedLoginResponse login = equipmentProducerManager.login(type, getLoginUser().getTenantId(), deviceUniqueId, userLoginReqVO);
String token = login.getData().getToken();
RealTimeDataReqVO soilRequest = new RealTimeDataReqVO();
soilRequest.setDeviceAddrs(deviceUniqueId);
RealTimeDataResponse realTimeData = equipmentProducerManager.getRealTimeData(type, getLoginUser().getTenantId(), token, soilRequest);
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
LocalDateTime startLocalDateTime = now.minusDays(3);
LocalDateTime endLocalDateTime = now;
String startStr = startLocalDateTime.format(formatter1);
String endStr=endLocalDateTime.format(formatter1);
System.out.println("标准格式-结束: " + startStr);
System.out.println("标准格式-开始: " + endStr);
long startTime = startLocalDateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
long endTime = endLocalDateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
HistoryDataListPageReqVO req = new HistoryDataListPageReqVO(String.valueOf(startTime),String.valueOf(endTime));
req.setDeviceAddr(deviceUniqueId);
req.setPages(1);
req.setLimit(500);
HistoryDataResponse historyData = equipmentProducerManager.getHistoryData(type, getLoginUser().getTenantId(), token, req);
DeviceInfoDetailV2RespVO.SoilInfo soilInfo = new DeviceInfoDetailV2RespVO.SoilInfo(deviceUniqueId, realTimeData, historyData);
target.setSoilInfo(soilInfo);
} else {
// type = "water_fertilizer";
// LoginReqVO loginReqVO = new LoginReqVO();
// UnifiedLoginResponse login = equipmentProducerManager.login(type, getLoginUser().getTenantId(), deviceUniqueId, loginReqVO);
// String token = login.getData().getToken();
// 获取设备商数据
switch (typeCodeMap.getOrDefault(deviceType, "other")) {
case "soil" :
DeviceInfoDetailV2RespVO.SoilInfo soilInfo = getSoilInfo(deviceUniqueId,Integer.parseInt(dateType));
target.setSoilInfo(soilInfo);
break;
case "water_fertilizer" :
DeviceInfoDetailV2RespVO.WfInfo wfInfo = getWfInfo(deviceUniqueId);
target.setWfInfo(wfInfo);
break;
}
return target;
}
private DeviceInfoDetailV2RespVO.SoilInfo getSoilInfo(String deviceUniqueId,Integer dateType) {
// 登录 todo 这里需要优化 2025-09-30 16:42:47
UserLoginReqVO userLoginReqVO = new UserLoginReqVO(deviceConf.getFourSituations().getUsername(), deviceConf.getFourSituations().getPassword());
UnifiedLoginResponse login = equipmentProducerManager.login(DEVICE_TYPE_SOIL, getLoginUser().getTenantId(), deviceUniqueId, userLoginReqVO);
String token = login.getData().getToken();
// 实时数据
RealTimeDataReqVO soilRequest = new RealTimeDataReqVO();
soilRequest.setDeviceAddrs(deviceUniqueId);
RealTimeDataResponse realTimeData = equipmentProducerManager.getRealTimeData(DEVICE_TYPE_SOIL, getLoginUser().getTenantId(), token, soilRequest);
// 历史数据
HistoryDataListPageReqVO req = HistoryDataListPageReqVO.buildHistoryData(deviceUniqueId, dateType);
HistoryDataResponse historyData = equipmentProducerManager.getHistoryData(DEVICE_TYPE_SOIL, getLoginUser().getTenantId(), token, req);
return new DeviceInfoDetailV2RespVO.SoilInfo(deviceUniqueId, realTimeData, historyData);
}
private DeviceInfoDetailV2RespVO.WfInfo getWfInfo(String deviceUniqueId) {
return new DeviceInfoDetailV2RespVO.WfInfo(deviceUniqueId);
}
@Override
public PageResult<DeviceInfoDO> getDeviceInfoPage(DeviceInfoPageReqVO pageReqVO) {
PageResult<DeviceInfoDO> pageResult = deviceInfoMapper.selectPage(pageReqVO);
@@ -314,4 +299,5 @@ public class DeviceInfoServiceImpl implements DeviceInfoService {
return deviceInfoMapper.selectCount(wrapperX).intValue();
}
}

View File

@@ -18,7 +18,7 @@
<select id="getTotalBaseArea" resultType="java.math.BigDecimal">
SELECT
SUM(area) AS total_area
COALESCE(SUM(area),0) AS total_area
FROM
farm_base_info
<where>