【功能优化】Bpm:完善设备属性的历史值

This commit is contained in:
YunaiV 2025-01-28 12:04:59 +08:00
parent dfa03d24fd
commit 0b16f1678c
14 changed files with 103 additions and 162 deletions

View File

@ -11,6 +11,7 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -27,9 +28,9 @@ public class IotDeviceLogController {
@Resource
private IotDeviceLogService deviceLogService;
// TODO:功能权限
@GetMapping("/page")
@Operation(summary = "获得设备日志分页")
@PreAuthorize("@ss.hasPermission('iot:device:log-query')")
public CommonResult<PageResult<IotDeviceLogRespVO>> getDeviceLogPage(@Valid IotDeviceLogPageReqVO pageReqVO) {
PageResult<IotDeviceLogDO> pageResult = deviceLogService.getDeviceLogPage(pageReqVO);
return success(BeanUtils.toBean(pageResult, IotDeviceLogRespVO.class));

View File

@ -3,10 +3,11 @@ package cn.iocoder.yudao.module.iot.controller.admin.device;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyHistoryPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyRespVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDevicePropertyDO;
@ -15,12 +16,16 @@ import cn.iocoder.yudao.module.iot.service.device.IotDeviceService;
import cn.iocoder.yudao.module.iot.service.device.data.IotDevicePropertyService;
import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@ -42,14 +47,22 @@ public class IotDevicePropertyController {
@Resource
private IotDeviceService deviceService;
// TODO @芋艿权限
@GetMapping("/latest")
@Operation(summary = "获取设备属性最新属性")
public CommonResult<List<IotDevicePropertyRespVO>> getLatestDeviceProperties(@Valid IotDevicePropertyPageReqVO pageReqVO) {
Map<String, IotDevicePropertyDO> properties = devicePropertyService.getLatestDeviceProperties(pageReqVO);
@Parameters({
@Parameter(name = "deviceId", description = "设备编号", required = true),
@Parameter(name = "identifier", description = "标识符"),
@Parameter(name = "name", description = "名称")
})
@PreAuthorize("@ss.hasPermission('iot:device:property-query')")
public CommonResult<List<IotDevicePropertyRespVO>> getLatestDeviceProperties(
@RequestParam("deviceId") Long deviceId,
@RequestParam(value = "identifier", required = false) String identifier,
@RequestParam(value = "name", required = false) String name) {
Map<String, IotDevicePropertyDO> properties = devicePropertyService.getLatestDeviceProperties(deviceId);
// 拼接数据
IotDeviceDO device = deviceService.getDevice(pageReqVO.getDeviceId());
IotDeviceDO device = deviceService.getDevice(deviceId);
Assert.notNull(device, "设备不存在");
List<IotThingModelDO> thingModels = thingModelService.getThingModelListByProductId(device.getProductId());
return success(convertList(properties.entrySet(), entry -> {
@ -58,6 +71,13 @@ public class IotDevicePropertyController {
if (thingModel == null || thingModel.getProperty() == null) {
return null;
}
if (StrUtil.isNotEmpty(identifier) && !StrUtil.contains(thingModel.getIdentifier(), identifier)) {
return null;
}
if (StrUtil.isNotEmpty(name) && !StrUtil.contains(thingModel.getName(), name)) {
return null;
}
// 构建对象
IotDevicePropertyDO property = entry.getValue();
return BeanUtils.toBean(thingModel, IotDevicePropertyRespVO.class)
.setDataType(thingModel.getProperty().getDataType())
@ -66,11 +86,11 @@ public class IotDevicePropertyController {
}));
}
// TODO @芋艿权限
@GetMapping("/history-page")
@Operation(summary = "获取设备属性历史数据")
@PreAuthorize("@ss.hasPermission('iot:device:property-query')")
public CommonResult<PageResult<IotDevicePropertyRespVO>> getHistoryDevicePropertyPage(
@Valid IotDevicePropertyPageReqVO pageReqVO) {
@Valid IotDevicePropertyHistoryPageReqVO pageReqVO) {
Assert.notEmpty(pageReqVO.getIdentifier(), "标识符不能为空");
return success(devicePropertyService.getHistoryDevicePropertyPage(pageReqVO));
}

View File

@ -0,0 +1,35 @@
package cn.iocoder.yudao.module.iot.controller.admin.device.vo.data;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - IoT 设备属性历史分页 Request VO")
@Data
public class IotDevicePropertyHistoryPageReqVO extends PageParam {
@Schema(description = "设备编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "177")
@NotNull(message = "设备编号不能为空")
private Long deviceId;
@Schema(description = "设备 Key", hidden = true)
private String deviceKey; // 非前端传递后端自己查询设置
@Schema(description = "属性标识符", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty(message = "属性标识符不能为空")
private String identifier;
@Schema(description = "时间范围", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@Size(min = 2, max = 2, message = "请选择时间范围")
private LocalDateTime[] times;
}

View File

@ -1,25 +0,0 @@
package cn.iocoder.yudao.module.iot.controller.admin.device.vo.data;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Schema(description = "管理后台 - IoT 设备属性 Request VO")
@Data
public class IotDevicePropertyPageReqVO extends PageParam {
@Schema(description = "设备编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "177")
@NotNull(message = "设备编号不能为空")
private Long deviceId;
@Schema(description = "设备 Key", hidden = true)
private String deviceKey; // 非前端传递后端自己查询设置
@Schema(description = "属性标识符", requiredMode = Schema.RequiredMode.REQUIRED)
private String identifier;
@Schema(description = "属性名称", requiredMode = Schema.RequiredMode.REQUIRED)
private String name;
}

View File

@ -14,7 +14,7 @@ public class IotDevicePropertyRespVO {
private Object value;
@Schema(description = "更新时间", requiredMode = Schema.RequiredMode.REQUIRED)
private Long updateTime;
private Long updateTime; // 由于从 TDengine 查询出来的是 Long 类型所以这里也使用 Long 类型
// ========== 基于 ThingModel 查询 ==========

View File

@ -31,10 +31,7 @@ public class ThingModelProperty {
*/
private String accessMode;
/**
* 是否是标准品类的必选服务
*
* - true
* - false
* 是否是标准品类的必选服务
*/
private Boolean required;
/**

View File

@ -1,32 +0,0 @@
package cn.iocoder.yudao.module.iot.convert.device;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
// TODO 是否要删除
@Mapper
public interface IotDeviceDataConvert {
IotDeviceDataConvert INSTANCE = Mappers.getMapper(IotDeviceDataConvert.class);
// default List<IotDeviceDataRespVO> convert(Map<String, Object> deviceData, IotDeviceDO device){
// List<IotDeviceDataRespVO> list = new ArrayList<>();
// deviceData.forEach((identifier, value) -> {
//// ThingModelProperty property = ThingModelService.INSTANCE.getProperty(device.getProductId(), identifier);
//// if (Objects.isNull(property)) {
//// return;
//// }
// IotDeviceDataRespVO vo = new IotDeviceDataRespVO();
// vo.setDeviceId(device.getId());
// vo.setProductKey(device.getProductKey());
// vo.setDeviceName(device.getDeviceName());
// vo.setIdentifier(identifier);
//// vo.setName(property.getName());
//// vo.setDataType(property.getDataType().getType());
// vo.setUpdateTime(device.getUpdateTime());
// vo.setValue(value.toString());
// list.add(vo);
// });
// return list;
// }
}

View File

@ -1 +1,4 @@
/**
* TODO 芋艿占位
*/
package cn.iocoder.yudao.module.iot.convert;

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.module.iot.dal.tdengine;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyHistoryPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyRespVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField;
@ -85,6 +85,6 @@ public interface IotDevicePropertyMapper {
@Param("reportTime") Long reportTime);
IPage<IotDevicePropertyRespVO> selectPageByHistory(IPage<?> page,
@Param("reqVO") IotDevicePropertyPageReqVO reqVO);
@Param("reqVO") IotDevicePropertyHistoryPageReqVO reqVO);
}

View File

@ -51,10 +51,16 @@ public class IotDeviceLogServiceImpl implements IotDeviceLogService {
@Override
public PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO) {
// TODO @芋艿增加一个表不存在的 try catch
IPage<IotDeviceLogDO> page = deviceLogMapper.selectPage(
new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO);
return new PageResult<>(page.getRecords(), page.getTotal());
try {
IPage<IotDeviceLogDO> page = deviceLogMapper.selectPage(
new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO);
return new PageResult<>(page.getRecords(), page.getTotal());
} catch (Exception exception) {
if (exception.getMessage().contains("Table does not exist")) {
return PageResult.empty();
}
throw exception;
}
}
}

View File

@ -1,7 +1,7 @@
package cn.iocoder.yudao.module.iot.service.device.data;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyHistoryPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyRespVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDevicePropertyDO;
import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
@ -36,7 +36,7 @@ public interface IotDevicePropertyService {
* @param deviceId 设备编号
* @return 设备属性最新数据
*/
Map<String, IotDevicePropertyDO> getLatestDeviceProperties(@Valid IotDevicePropertyPageReqVO deviceId);
Map<String, IotDevicePropertyDO> getLatestDeviceProperties(Long deviceId);
/**
* 获得设备属性历史数据
@ -44,6 +44,6 @@ public interface IotDevicePropertyService {
* @param pageReqVO 分页请求
* @return 设备属性历史数据
*/
PageResult<IotDevicePropertyRespVO> getHistoryDevicePropertyPage(@Valid IotDevicePropertyPageReqVO pageReqVO);
PageResult<IotDevicePropertyRespVO> getHistoryDevicePropertyPage(@Valid IotDevicePropertyHistoryPageReqVO pageReqVO);
}

View File

@ -6,7 +6,7 @@ import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyHistoryPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType.ThingModelDateOrTextDataSpecs;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
@ -25,7 +25,6 @@ import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@ -146,33 +145,38 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService {
// 3.1 保存设备属性数据
devicePropertyMapper.insert(device, properties,
LocalDateTimeUtil.toEpochMilli(message.getReportTime())); // TODO @芋艿后续要看看查询的时候能不能用 LocalDateTime
LocalDateTimeUtil.toEpochMilli(message.getReportTime()));
// 3.2 保存设备属性日志
deviceDataRedisDAO.set(message.getDeviceKey(), convertMap(properties.entrySet(), Map.Entry::getKey,
entry -> IotDevicePropertyDO.builder().value(entry.getValue()).updateTime(message.getReportTime()).build()));
}
// TODO @芋艿需要在优化下根据 name 之类的过滤
@Override
public Map<String, IotDevicePropertyDO> getLatestDeviceProperties(@Valid IotDevicePropertyPageReqVO pageReqVO) {
public Map<String, IotDevicePropertyDO> getLatestDeviceProperties(Long deviceId) {
// 获取设备信息
IotDeviceDO device = deviceService.validateDeviceExists(pageReqVO.getDeviceId());
IotDeviceDO device = deviceService.validateDeviceExists(deviceId);
// 获得设备属性
return deviceDataRedisDAO.get(device.getDeviceKey());
}
@Override
public PageResult<IotDevicePropertyRespVO> getHistoryDevicePropertyPage(IotDevicePropertyPageReqVO pageReqVO) {
public PageResult<IotDevicePropertyRespVO> getHistoryDevicePropertyPage(IotDevicePropertyHistoryPageReqVO pageReqVO) {
// 获取设备信息
IotDeviceDO device = deviceService.validateDeviceExists(pageReqVO.getDeviceId());
pageReqVO.setDeviceKey(device.getDeviceKey());
// TODO @芋艿增加一个表不存在的 try catch
IPage<IotDevicePropertyRespVO> page = devicePropertyMapper.selectPageByHistory(
new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO);
return new PageResult<>(page.getRecords(), page.getTotal());
try {
IPage<IotDevicePropertyRespVO> page = devicePropertyMapper.selectPageByHistory(
new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO);
return new PageResult<>(page.getRecords(), page.getTotal());
} catch (Exception exception) {
if (exception.getMessage().contains("Table does not exist")) {
return PageResult.empty();
}
throw exception;
}
}
}

View File

@ -1,69 +0,0 @@
package cn.iocoder.yudao.module.iot.util;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.iot.enums.IotConstants;
import cn.iocoder.yudao.module.iot.enums.product.IotProductDeviceTypeEnum;
// TODO @芋艿可能要思索下有没更好的处理方式
// TODO @芋艿怎么改成无状态
/**
* TD 数据库工具类
*
* @author AlwaysSuper
*/
public class IotTdDatabaseUtils {
/**
* 获取数据库名称
*/
public static String getDatabaseName(String url) {
// TODO @alwayssuper:StrUtil.subAfter("/")
return StrUtil.subAfter(url, "/", true);
}
/**
* 获取产品超级表表名
*
* @param deviceType 设备类型
* @param productKey 产品 Key
* @return 产品超级表表名
*/
public static String getProductSuperTableName(Integer deviceType, String productKey) {
Assert.notNull(deviceType, "deviceType 不能为空");
if (IotProductDeviceTypeEnum.GATEWAY_SUB.getType().equals(deviceType)) {
return String.format(IotConstants.GATEWAY_SUB_STABLE_NAME_FORMAT, productKey).toLowerCase();
}
if (IotProductDeviceTypeEnum.GATEWAY.getType().equals(deviceType)) {
return String.format(IotConstants.GATEWAY_STABLE_NAME_FORMAT, productKey).toLowerCase();
}
if (IotProductDeviceTypeEnum.DIRECT.getType().equals(deviceType)){
return String.format(IotConstants.DEVICE_STABLE_NAME_FORMAT, productKey).toLowerCase();
}
throw new IllegalArgumentException("deviceType 不正确");
}
/**
* 获取物模型日志超级表表名
*
* @param productKey 产品 Key
* @return 物模型日志超级表表名
*
*/
public static String getThingModelMessageSuperTableName(String productKey) {
return "thing_model_message_" + productKey.toLowerCase();
}
/**
* 获取物模型日志设备表名
*
* @param productKey 产品 Key
* @param deviceName 设备名称
* @return 物模型日志设备表名
*/
public static String getThingModelMessageDeviceTableName(String productKey, String deviceName) {
return String.format(IotConstants.THING_MODEL_MESSAGE_TABLE_NAME_FORMAT,
productKey.toLowerCase(), deviceName.toLowerCase());
}
}

View File

@ -66,11 +66,12 @@
DESCRIBE product_property_${productKey}
</select>
<!-- TODO 芋艿:缺少时间范围 AND ts BETWEEN #{reqVO.startTime} AND #{reqVO.endTime} -->
<select id="selectPageByHistory" resultType="cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDevicePropertyRespVO">
SELECT ${reqVO.identifier} AS `value`, report_time AS update_time
FROM device_property_${reqVO.deviceKey}
SELECT ${reqVO.identifier} AS `value`, ts AS update_time
FROM device_property_${reqVO.deviceKey}1
WHERE ${reqVO.identifier} IS NOT NULL
AND ts BETWEEN ${@cn.hutool.core.date.LocalDateTimeUtil@toEpochMilli(reqVO.times[0])}
AND ${@cn.hutool.core.date.LocalDateTimeUtil@toEpochMilli(reqVO.times[1])}
ORDER BY ts DESC
</select>