Commit 6aad4545a89faecf91cf4b8f9af442ab4ea44c0e
1 parent
0249ca99
[功能添加]:物模型日志表查询与创建 模拟设备基础逻辑
Showing
8 changed files
with
165 additions
and
20 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceDataController.java
| ... | ... | @@ -4,11 +4,9 @@ import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 6 | 6 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.device.IotDeviceSaveReqVO; |
| 7 | -import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO; | |
| 8 | -import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataRespVO; | |
| 9 | -import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO; | |
| 7 | +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.*; | |
| 10 | 8 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO; |
| 11 | -import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotTimeDataRespVO; | |
| 9 | +import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO; | |
| 12 | 10 | import cn.iocoder.yudao.module.iot.service.device.IotDeviceLogDataService; |
| 13 | 11 | import cn.iocoder.yudao.module.iot.service.device.IotDevicePropertyDataService; |
| 14 | 12 | import io.swagger.v3.oas.annotations.Operation; |
| ... | ... | @@ -36,6 +34,9 @@ public class IotDeviceDataController { |
| 36 | 34 | @Resource |
| 37 | 35 | private IotDeviceLogDataService iotDeviceLogDataService; |
| 38 | 36 | |
| 37 | + @Resource | |
| 38 | + private IotDeviceLogDataService deviceLogDataService; | |
| 39 | + | |
| 39 | 40 | // TODO @浩浩:这里的 /latest-list,包括方法名。 |
| 40 | 41 | @GetMapping("/latest") |
| 41 | 42 | @Operation(summary = "获取设备属性最新数据") |
| ... | ... | @@ -51,7 +52,7 @@ public class IotDeviceDataController { |
| 51 | 52 | PageResult<Map<String, Object>> list = deviceDataService.getHistoryDeviceProperties(deviceDataReqVO); |
| 52 | 53 | return success(BeanUtils.toBean(list, IotTimeDataRespVO.class)); |
| 53 | 54 | } |
| 54 | - | |
| 55 | + // TODO:数据权限 | |
| 55 | 56 | @PostMapping("/simulator") |
| 56 | 57 | @Operation(summary = "模拟设备") |
| 57 | 58 | public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulatorReqVO) { |
| ... | ... | @@ -59,5 +60,12 @@ public class IotDeviceDataController { |
| 59 | 60 | iotDeviceLogDataService.createDeviceLog(simulatorReqVO); |
| 60 | 61 | return success(true); |
| 61 | 62 | } |
| 63 | + // TODO:数据权限 | |
| 64 | + @GetMapping("/log/page") | |
| 65 | + @Operation(summary = "获得设备日志分页") | |
| 66 | + public CommonResult<PageResult<IotDeviceLogRespVO>> getDeviceLogPage(@Valid IotDeviceLogPageReqVO pageReqVO) { | |
| 67 | + PageResult<IotDeviceLogDO> pageResult = deviceLogDataService.getDeviceLogPage(pageReqVO); | |
| 68 | + return success(BeanUtils.toBean(pageResult, IotDeviceLogRespVO.class)); | |
| 69 | + } | |
| 62 | 70 | |
| 63 | 71 | } |
| 64 | 72 | \ No newline at end of file | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/deviceData/IotDeviceDataSimulatorSaveReqVO.java
| ... | ... | @@ -3,9 +3,12 @@ package cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData; |
| 3 | 3 | import io.swagger.v3.oas.annotations.media.Schema; |
| 4 | 4 | import jakarta.validation.constraints.NotEmpty; |
| 5 | 5 | import lombok.Data; |
| 6 | +import org.springframework.format.annotation.DateTimeFormat; | |
| 6 | 7 | |
| 7 | 8 | import java.time.LocalDateTime; |
| 8 | 9 | |
| 10 | +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | |
| 11 | + | |
| 9 | 12 | @Schema(description = "管理后台 - IoT 模拟设备数据 Request VO") |
| 10 | 13 | @Data |
| 11 | 14 | public class IotDeviceDataSimulatorSaveReqVO { |
| ... | ... | @@ -34,6 +37,6 @@ public class IotDeviceDataSimulatorSaveReqVO { |
| 34 | 37 | private String content; |
| 35 | 38 | |
| 36 | 39 | @Schema(description = "上报时间", requiredMode = Schema.RequiredMode.REQUIRED) |
| 37 | - private LocalDateTime reportTime; | |
| 40 | + private Long reportTime; | |
| 38 | 41 | |
| 39 | 42 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/deviceData/IotDeviceLogPageReqVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.common.pojo.PageParam; | |
| 4 | +import io.swagger.v3.oas.annotations.media.Schema; | |
| 5 | +import jakarta.validation.constraints.NotEmpty; | |
| 6 | +import lombok.Data; | |
| 7 | +import org.springframework.format.annotation.DateTimeFormat; | |
| 8 | + | |
| 9 | +import java.time.LocalDateTime; | |
| 10 | + | |
| 11 | +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | |
| 12 | + | |
| 13 | +@Schema(description = "管理后台 - IoT 设备日志分页查询 Request VO") | |
| 14 | +@Data | |
| 15 | +public class IotDeviceLogPageReqVO extends PageParam { | |
| 16 | + | |
| 17 | + @Schema(description = "设备标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "device123") | |
| 18 | + @NotEmpty(message = "设备标识不能为空") | |
| 19 | + private String deviceKey; | |
| 20 | + | |
| 21 | + @Schema(description = "消息类型", example = "property") | |
| 22 | + private String type; | |
| 23 | + | |
| 24 | + @Schema(description = "标识符", example = "temperature") | |
| 25 | + private String subType; | |
| 26 | + | |
| 27 | + @Schema(description = "创建时间") | |
| 28 | + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) | |
| 29 | + private LocalDateTime[] createTime; | |
| 30 | +} | |
| 0 | 31 | \ No newline at end of file | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/deviceData/IotDeviceLogRespVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData; | |
| 2 | + | |
| 3 | +import io.swagger.v3.oas.annotations.media.Schema; | |
| 4 | +import lombok.Data; | |
| 5 | + | |
| 6 | +import java.time.LocalDateTime; | |
| 7 | + | |
| 8 | +@Schema(description = "管理后台 - IoT 设备日志 Response VO") | |
| 9 | +@Data | |
| 10 | +public class IotDeviceLogRespVO { | |
| 11 | + | |
| 12 | + @Schema(description = "日志编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") | |
| 13 | + private String id; | |
| 14 | + @Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "product123") | |
| 15 | + private String productKey; | |
| 16 | + @Schema(description = "设备标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "device123") | |
| 17 | + private String deviceKey; | |
| 18 | + | |
| 19 | + @Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "property") | |
| 20 | + private String type; | |
| 21 | + | |
| 22 | + @Schema(description = "标识符", requiredMode = Schema.RequiredMode.REQUIRED, example = "temperature") | |
| 23 | + private String subType; | |
| 24 | + | |
| 25 | + @Schema(description = "日志内容", requiredMode = Schema.RequiredMode.REQUIRED) | |
| 26 | + private String content; | |
| 27 | + | |
| 28 | + @Schema(description = "上报时间", requiredMode = Schema.RequiredMode.REQUIRED) | |
| 29 | + private LocalDateTime reportTime; | |
| 30 | + | |
| 31 | + @Schema(description = "记录时间戳", requiredMode = Schema.RequiredMode.REQUIRED) | |
| 32 | + private LocalDateTime ts; | |
| 33 | +} | |
| 0 | 34 | \ No newline at end of file | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/IotDeviceLogDataMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.dal.tdengine; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 4 | +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceLogPageReqVO; | |
| 3 | 5 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO; |
| 4 | 6 | import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS; |
| 5 | 7 | import com.baomidou.mybatisplus.annotation.InterceptorIgnore; |
| 6 | 8 | import org.apache.ibatis.annotations.Mapper; |
| 7 | 9 | import org.apache.ibatis.annotations.Param; |
| 8 | 10 | |
| 11 | +import java.util.List; | |
| 12 | + | |
| 9 | 13 | /** |
| 10 | 14 | * IOT 设备日志数据 Mapper 接口 |
| 11 | 15 | * |
| ... | ... | @@ -23,8 +27,12 @@ public interface IotDeviceLogDataMapper { |
| 23 | 27 | */ |
| 24 | 28 | void createDeviceLogSTable(); |
| 25 | 29 | |
| 26 | - | |
| 27 | - | |
| 30 | + /** | |
| 31 | + * 创建设备日志子表 | |
| 32 | + * | |
| 33 | + * @param deviceKey 设备标识 | |
| 34 | + */ | |
| 35 | + void createDeviceLogTable(@Param("deviceKey") String deviceKey); | |
| 28 | 36 | |
| 29 | 37 | /** |
| 30 | 38 | * 插入设备日志数据 |
| ... | ... | @@ -34,4 +42,20 @@ public interface IotDeviceLogDataMapper { |
| 34 | 42 | * @param log 设备日志数据 |
| 35 | 43 | */ |
| 36 | 44 | void insert(@Param("log") IotDeviceLogDO log); |
| 45 | + | |
| 46 | + /** | |
| 47 | + * 获得设备日志分页 | |
| 48 | + * | |
| 49 | + * @param reqVO 分页查询条件 | |
| 50 | + * @return 设备日志列表 | |
| 51 | + */ | |
| 52 | + List<IotDeviceLogDO> selectPage(@Param("reqVO") IotDeviceLogPageReqVO reqVO); | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * 获得设备日志总数 | |
| 56 | + * | |
| 57 | + * @param reqVO 查询条件 | |
| 58 | + * @return 日志总数 | |
| 59 | + */ | |
| 60 | + Long selectCount(@Param("reqVO") IotDeviceLogPageReqVO reqVO); | |
| 37 | 61 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceLogDataService.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.service.device; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 3 | 4 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO; |
| 5 | +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceLogPageReqVO; | |
| 6 | +import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO; | |
| 7 | + | |
| 4 | 8 | |
| 5 | 9 | /** |
| 6 | 10 | * IoT 设备日志数据 Service 接口 |
| ... | ... | @@ -25,4 +29,12 @@ public interface IotDeviceLogDataService { |
| 25 | 29 | */ |
| 26 | 30 | void createDeviceLog(IotDeviceDataSimulatorSaveReqVO simulatorReqVO); |
| 27 | 31 | |
| 32 | + /** | |
| 33 | + * 获得设备日志分页 | |
| 34 | + * | |
| 35 | + * @param pageReqVO 分页查询 | |
| 36 | + * @return 设备日志分页 | |
| 37 | + */ | |
| 38 | + PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO); | |
| 39 | + | |
| 28 | 40 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceLogDataServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.service.device; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.date.DateTime; |
| 4 | +import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 4 | 5 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 5 | 6 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO; |
| 7 | +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceLogPageReqVO; | |
| 6 | 8 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO; |
| 7 | 9 | import cn.iocoder.yudao.module.iot.dal.tdengine.IotDeviceLogDataMapper; |
| 8 | 10 | import jakarta.annotation.Resource; |
| ... | ... | @@ -12,6 +14,7 @@ import org.springframework.validation.annotation.Validated; |
| 12 | 14 | |
| 13 | 15 | import java.time.LocalDateTime; |
| 14 | 16 | import java.time.ZoneId; |
| 17 | +import java.util.List; | |
| 15 | 18 | |
| 16 | 19 | /** |
| 17 | 20 | * IoT 设备日志数据 Service 实现了 |
| ... | ... | @@ -49,19 +52,17 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{ |
| 49 | 52 | long currentTime = System.currentTimeMillis(); |
| 50 | 53 | // 2.1 设置时序时间为当前时间 |
| 51 | 54 | iotDeviceLogDO.setTs(currentTime); |
| 52 | - | |
| 53 | - // 2.2 处理上报时间 | |
| 54 | - if (simulatorReqVO.getReportTime() != null) { | |
| 55 | - // 将 LocalDateTime 转换为时间戳 | |
| 56 | - iotDeviceLogDO.setReportTime( | |
| 57 | - simulatorReqVO.getReportTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() | |
| 58 | - ); | |
| 59 | - } else { | |
| 60 | - // 如果没有上报时间,使用当前时间 | |
| 61 | - iotDeviceLogDO.setReportTime(currentTime); | |
| 62 | - } | |
| 63 | - | |
| 55 | + | |
| 64 | 56 | // 3. 插入数据 |
| 65 | 57 | iotDeviceLogDataMapper.insert(iotDeviceLogDO); |
| 66 | 58 | } |
| 59 | + | |
| 60 | + @Override | |
| 61 | + public PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO) { | |
| 62 | + // 查询数据 | |
| 63 | + List<IotDeviceLogDO> list = iotDeviceLogDataMapper.selectPage(pageReqVO); | |
| 64 | + Long total = iotDeviceLogDataMapper.selectCount(pageReqVO); | |
| 65 | + // 构造分页结果 | |
| 66 | + return new PageResult<>(list, total); | |
| 67 | + } | |
| 67 | 68 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDeviceLogDataMapper.xml
| ... | ... | @@ -41,4 +41,38 @@ |
| 41 | 41 | ) |
| 42 | 42 | </insert> |
| 43 | 43 | |
| 44 | + <select id="selectPage" resultType="cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO"> | |
| 45 | + SELECT ts, id, device_key, product_key, type, subType, content, report_time | |
| 46 | + FROM device_log_${reqVO.deviceKey} | |
| 47 | + <where> | |
| 48 | + <if test="reqVO.type != null and reqVO.type != ''"> | |
| 49 | + AND type = #{reqVO.type} | |
| 50 | + </if> | |
| 51 | + <if test="reqVO.subType != null and reqVO.subType != ''"> | |
| 52 | + AND subType = #{reqVO.subType} | |
| 53 | + </if> | |
| 54 | + <if test="reqVO.createTime != null"> | |
| 55 | + AND ts BETWEEN #{reqVO.createTime[0]} AND #{reqVO.createTime[1]} | |
| 56 | + </if> | |
| 57 | + </where> | |
| 58 | + ORDER BY ts DESC | |
| 59 | + LIMIT #{reqVO.pageSize} OFFSET #{reqVO.pageNo} | |
| 60 | + </select> | |
| 61 | + | |
| 62 | + <select id="selectCount" resultType="Long"> | |
| 63 | + SELECT COUNT(*) | |
| 64 | + FROM device_log_${reqVO.deviceKey} | |
| 65 | + <where> | |
| 66 | + <if test="reqVO.type != null and reqVO.type != ''"> | |
| 67 | + AND type = #{reqVO.type} | |
| 68 | + </if> | |
| 69 | + <if test="reqVO.subType != null and reqVO.subType != ''"> | |
| 70 | + AND subType = #{reqVO.subType} | |
| 71 | + </if> | |
| 72 | + <if test="reqVO.createTime != null"> | |
| 73 | + AND ts BETWEEN #{reqVO.createTime[0]} AND #{reqVO.createTime[1]} | |
| 74 | + </if> | |
| 75 | + </where> | |
| 76 | + </select> | |
| 77 | + | |
| 44 | 78 | </mapper> |
| 45 | 79 | \ No newline at end of file | ... | ... |