Commit 8a7e14644549de7ab39887624c6ecb663db1b266
1 parent
51b3e31e
feat:simulator
Showing
9 changed files
with
246 additions
and
31 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceDataController.java
| ... | ... | @@ -3,19 +3,21 @@ package cn.iocoder.yudao.module.iot.controller.admin.device; |
| 3 | 3 | 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 | +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.device.IotDeviceSaveReqVO; | |
| 6 | 7 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO; |
| 7 | 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; | |
| 8 | 10 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO; |
| 9 | 11 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotTimeDataRespVO; |
| 12 | +import cn.iocoder.yudao.module.iot.service.device.IotDeviceLogDataService; | |
| 10 | 13 | import cn.iocoder.yudao.module.iot.service.device.IotDevicePropertyDataService; |
| 11 | 14 | import io.swagger.v3.oas.annotations.Operation; |
| 12 | 15 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 13 | 16 | import jakarta.annotation.Resource; |
| 14 | 17 | import jakarta.validation.Valid; |
| 18 | +import org.springframework.security.access.prepost.PreAuthorize; | |
| 15 | 19 | import org.springframework.validation.annotation.Validated; |
| 16 | -import org.springframework.web.bind.annotation.GetMapping; | |
| 17 | -import org.springframework.web.bind.annotation.RequestMapping; | |
| 18 | -import org.springframework.web.bind.annotation.RestController; | |
| 20 | +import org.springframework.web.bind.annotation.*; | |
| 19 | 21 | |
| 20 | 22 | import java.util.List; |
| 21 | 23 | import java.util.Map; |
| ... | ... | @@ -31,6 +33,9 @@ public class IotDeviceDataController { |
| 31 | 33 | @Resource |
| 32 | 34 | private IotDevicePropertyDataService deviceDataService; |
| 33 | 35 | |
| 36 | + @Resource | |
| 37 | + private IotDeviceLogDataService iotDeviceLogDataService; | |
| 38 | + | |
| 34 | 39 | // TODO @浩浩:这里的 /latest-list,包括方法名。 |
| 35 | 40 | @GetMapping("/latest") |
| 36 | 41 | @Operation(summary = "获取设备属性最新数据") |
| ... | ... | @@ -47,4 +52,12 @@ public class IotDeviceDataController { |
| 47 | 52 | return success(BeanUtils.toBean(list, IotTimeDataRespVO.class)); |
| 48 | 53 | } |
| 49 | 54 | |
| 55 | + @PostMapping("/simulator") | |
| 56 | + @Operation(summary = "模拟设备") | |
| 57 | + public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulatorReqVO) { | |
| 58 | + //TODO:先生成一下日志 后续完善模拟设备代码逻辑 | |
| 59 | + iotDeviceLogDataService.createDeviceLog(simulatorReqVO); | |
| 60 | + return success(true); | |
| 61 | + } | |
| 62 | + | |
| 50 | 63 | } |
| 51 | 64 | \ 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
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 jakarta.validation.constraints.NotEmpty; | |
| 5 | +import lombok.Data; | |
| 6 | + | |
| 7 | + | |
| 8 | +import java.time.LocalDateTime; | |
| 9 | + | |
| 10 | +@Schema(description = "管理后台 - IoT 模拟设备数据 Request VO") | |
| 11 | +@Data | |
| 12 | +public class IotDeviceDataSimulatorSaveReqVO { | |
| 13 | + | |
| 14 | + @Schema(description = "消息ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "msg123") | |
| 15 | + private String id; | |
| 16 | + | |
| 17 | + @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "product123") | |
| 18 | + @NotEmpty(message = "产品ID不能为空") | |
| 19 | + private String productKey; | |
| 20 | + | |
| 21 | + @Schema(description = "设备ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "device123") | |
| 22 | + @NotEmpty(message = "设备ID不能为空") | |
| 23 | + private String deviceKey; | |
| 24 | + | |
| 25 | + @Schema(description = "消息/日志类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "property") | |
| 26 | + @NotEmpty(message = "消息类型不能为空") | |
| 27 | + private String type; | |
| 28 | + | |
| 29 | + @Schema(description = "标识符", requiredMode = Schema.RequiredMode.REQUIRED, example = "temperature") | |
| 30 | + @NotEmpty(message = "标识符不能为空") | |
| 31 | + private String subType; | |
| 32 | + | |
| 33 | + @Schema(description = "数据内容", requiredMode = Schema.RequiredMode.REQUIRED, example = "{\"value\": 25.6}") | |
| 34 | + @NotEmpty(message = "数据内容不能为空") | |
| 35 | + private String content; | |
| 36 | + | |
| 37 | + @Schema(description = "上报时间", requiredMode = Schema.RequiredMode.REQUIRED) | |
| 38 | + private LocalDateTime reportTime; | |
| 39 | + | |
| 40 | +} | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/device/IotDeviceLogDO.java
| 1 | -package cn.iocoder.yudao.module.iot.dal.dataobject.device;/** | |
| 2 | - * @Author alwayssuper | |
| 3 | - * @Date 2025/1/5 12:32 | |
| 4 | -*/public class IotDeviceLogDO { | |
| 1 | +package cn.iocoder.yudao.module.iot.dal.dataobject.device; | |
| 2 | + | |
| 3 | +import cn.hutool.core.date.DateTime; | |
| 4 | +import lombok.AllArgsConstructor; | |
| 5 | +import lombok.Builder; | |
| 6 | +import lombok.Data; | |
| 7 | +import lombok.NoArgsConstructor; | |
| 8 | + | |
| 9 | +import java.time.LocalDateTime; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * IoT 设备日志数据 DO | |
| 13 | + * | |
| 14 | + * @author alwayssuper | |
| 15 | + */ | |
| 16 | +@Data | |
| 17 | +@Builder | |
| 18 | +@NoArgsConstructor | |
| 19 | +@AllArgsConstructor | |
| 20 | +public class IotDeviceLogDO { | |
| 21 | + /** | |
| 22 | + * 消息ID | |
| 23 | + */ | |
| 24 | + private String id; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * 产品ID | |
| 28 | + */ | |
| 29 | + private String productKey; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * 设备ID | |
| 33 | + */ | |
| 34 | + private String deviceKey; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * 消息/日志类型 | |
| 38 | + */ | |
| 39 | + private String type; | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * 标识符:用于标识具体的属性、事件或服务 | |
| 43 | + */ | |
| 44 | + private String subType; | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * 数据内容:存储具体的消息数据内容,通常是JSON格式 | |
| 48 | + */ | |
| 49 | + private String content; | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * 上报时间戳 | |
| 53 | + */ | |
| 54 | + private DateTime reportTime; | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * 时序时间 | |
| 58 | + */ | |
| 59 | + private DateTime ts; | |
| 60 | + | |
| 61 | + | |
| 5 | 62 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/TdThingModelMessageMapper.java
| ... | ... | @@ -13,6 +13,7 @@ import org.apache.ibatis.annotations.Param; |
| 13 | 13 | * 处理 TD 中物模型消息日志的操作 |
| 14 | 14 | */ |
| 15 | 15 | @Mapper |
| 16 | +@Deprecated | |
| 16 | 17 | @TDengineDS |
| 17 | 18 | @InterceptorIgnore(tenantLine = "true") // 避免 SQL 解析,因为 JSqlParser 对 TDengine 的 SQL 解析会报错 |
| 18 | 19 | public interface TdThingModelMessageMapper { | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/tdengine/config/TDengineTableInitConfiguration.java
| 1 | -package cn.iocoder.yudao.module.iot.framework.tdengine.config;/** | |
| 2 | - * @Author alwayssuper | |
| 3 | - * @Date 2025/1/5 17:37 | |
| 4 | -*/public class TDengineTableInitConfiguration { | |
| 1 | +package cn.iocoder.yudao.module.iot.framework.tdengine.config; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.module.iot.service.device.IotDeviceLogDataService; | |
| 4 | +import lombok.RequiredArgsConstructor; | |
| 5 | +import lombok.extern.slf4j.Slf4j; | |
| 6 | +import org.springframework.boot.ApplicationArguments; | |
| 7 | +import org.springframework.boot.ApplicationRunner; | |
| 8 | +import org.springframework.context.annotation.Configuration; | |
| 9 | +import org.springframework.core.annotation.Order; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * TDengine 表初始化的 Configuration | |
| 13 | + * | |
| 14 | + * @author alwayssuper | |
| 15 | + */ | |
| 16 | +@Slf4j | |
| 17 | +@RequiredArgsConstructor | |
| 18 | +@Configuration | |
| 19 | +@Order(Integer.MAX_VALUE) // 保证在最后执行 | |
| 20 | +public class TDengineTableInitConfiguration implements ApplicationRunner { | |
| 21 | + | |
| 22 | + private final IotDeviceLogDataService deviceLogService; | |
| 23 | + | |
| 24 | + @Override | |
| 25 | + public void run(ApplicationArguments args) { | |
| 26 | + try { | |
| 27 | + // 初始化设备日志表 | |
| 28 | + deviceLogService.initTDengineSTable(); | |
| 29 | + log.info("初始化 设备日志表 TDengine 表结构成功"); | |
| 30 | + } catch (Exception ex) { | |
| 31 | + if (ex.getMessage().contains("Table already exists")) { | |
| 32 | + log.info("TDengine 设备日志超级表已存在,跳过创建"); | |
| 33 | + return; | |
| 34 | + }else{ | |
| 35 | + log.error("初始化 设备日志表 TDengine 表结构失败", ex); | |
| 36 | + } | |
| 37 | + throw ex; | |
| 38 | + } | |
| 39 | + } | |
| 5 | 40 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceLogDataServiceImpl.java
| 1 | -package cn.iocoder.yudao.module.iot.service.device;/** | |
| 2 | - * @Author alwayssuper | |
| 3 | - * @Date 2025/1/5 11:50 | |
| 4 | -*/public class IotDeviceLogDataServiceImpl { | |
| 1 | +package cn.iocoder.yudao.module.iot.service.device; | |
| 2 | + | |
| 3 | +import cn.hutool.core.date.DateTime; | |
| 4 | +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | |
| 5 | +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO; | |
| 6 | +import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO; | |
| 7 | +import cn.iocoder.yudao.module.iot.dal.tdengine.IotDeviceLogDataMapper; | |
| 8 | +import jakarta.annotation.Resource; | |
| 9 | +import lombok.extern.slf4j.Slf4j; | |
| 10 | +import org.springframework.stereotype.Service; | |
| 11 | +import org.springframework.validation.annotation.Validated; | |
| 12 | + | |
| 13 | +import java.time.LocalDateTime; | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * IoT 设备日志数据 Service 实现了 | |
| 17 | + * | |
| 18 | + * @author alwayssuper | |
| 19 | + */ | |
| 20 | +@Service | |
| 21 | +@Slf4j | |
| 22 | +public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{ | |
| 23 | + | |
| 24 | + @Resource | |
| 25 | + private IotDeviceLogDataMapper iotDeviceLogDataMapper; | |
| 26 | + | |
| 27 | + | |
| 28 | + @Override | |
| 29 | + public void initTDengineSTable() { | |
| 30 | + try { | |
| 31 | + // 创建设备日志超级表 | |
| 32 | + iotDeviceLogDataMapper.createDeviceLogSTable(); | |
| 33 | + log.info("创建设备日志超级表成功"); | |
| 34 | + } catch (Exception ex) { | |
| 35 | + throw ex; | |
| 36 | + } | |
| 37 | + } | |
| 38 | + | |
| 39 | + @Override | |
| 40 | + public void createDeviceLog(IotDeviceDataSimulatorSaveReqVO simulatorReqVO) { | |
| 41 | + IotDeviceLogDO iotDeviceLogDO = BeanUtils.toBean(simulatorReqVO, IotDeviceLogDO.class); | |
| 42 | + iotDeviceLogDO.setTs(DateTime.now()); | |
| 43 | + iotDeviceLogDataMapper.insert(iotDeviceLogDO); | |
| 44 | + } | |
| 5 | 45 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDevicePropertyDataServiceImpl.java
| ... | ... | @@ -85,8 +85,6 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe |
| 85 | 85 | @Resource |
| 86 | 86 | private IotDevicePropertyDataMapper devicePropertyDataMapper; |
| 87 | 87 | |
| 88 | - @Resource | |
| 89 | - private TdThingModelMessageMapper tdThingModelMessageMapper; | |
| 90 | 88 | |
| 91 | 89 | @Override |
| 92 | 90 | public void defineDevicePropertyData(Long productId) { |
| ... | ... | @@ -114,8 +112,6 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe |
| 114 | 112 | newFields.add(0, new TDengineTableField(TDengineTableField.FIELD_TS, TDengineTableField.TYPE_TIMESTAMP)); |
| 115 | 113 | // 2.1.1 创建产品超级表 |
| 116 | 114 | devicePropertyDataMapper.createProductPropertySTable(product.getProductKey(), newFields); |
| 117 | - // 2.1.2 创建物模型日志超级表 | |
| 118 | - tdThingModelMessageMapper.createSuperTable(product.getProductKey()); | |
| 119 | 115 | return; |
| 120 | 116 | } |
| 121 | 117 | // 2.2 情况二:如果是修改的时候,需要更新表 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/tdengine/IotThingModelMessageServiceImpl.java
| ... | ... | @@ -78,8 +78,6 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ |
| 78 | 78 | createDeviceTable(device.getDeviceType(), device.getProductKey(), device.getDeviceName(), device.getDeviceKey()); |
| 79 | 79 | iotDeviceService.updateDeviceStatus(new IotDeviceStatusUpdateReqVO() |
| 80 | 80 | .setId(device.getId()).setStatus(IotDeviceStatusEnum.ONLINE.getStatus())); |
| 81 | - // 1.2 创建物模型日志设备表 | |
| 82 | - tdThingModelMessageMapper.createTableWithTag(device.getProductKey(), device.getDeviceKey()); | |
| 83 | 81 | } |
| 84 | 82 | |
| 85 | 83 | // 2. 获取设备属性并进行物模型校验,过滤非物模型属性 |
| ... | ... | @@ -231,16 +229,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ |
| 231 | 229 | .setTags(tagsFieldValues)); |
| 232 | 230 | } |
| 233 | 231 | |
| 234 | - /** | |
| 235 | - * 创建物模型日志设备数据表 | |
| 236 | - * | |
| 237 | - * @param productKey 产品 Key | |
| 238 | - * @param deviceKey 设备 Key | |
| 239 | - * | |
| 240 | - */ | |
| 241 | - private void createThingModelMessageDeviceTable(String productKey, String deviceKey){ | |
| 242 | - tdThingModelMessageMapper.createTableWithTag(productKey, deviceKey); | |
| 243 | - } | |
| 232 | + | |
| 244 | 233 | |
| 245 | 234 | /** |
| 246 | 235 | * 获取数据库名称 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDeviceLogDataMapper.xml
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | +<!DOCTYPE mapper | |
| 3 | + PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |
| 4 | + "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
| 5 | +<mapper namespace="cn.iocoder.yudao.module.iot.dal.tdengine.IotDeviceLogDataMapper"> | |
| 6 | + | |
| 7 | + <!-- 创建设备日志超级表 初始化只创建一次--> | |
| 8 | + <update id="createDeviceLogSTable"> | |
| 9 | + CREATE STABLE device_log( | |
| 10 | + ts TIMESTAMP, | |
| 11 | + id NCHAR(50), | |
| 12 | + product_key NCHAR(50), | |
| 13 | + type NCHAR(50), | |
| 14 | + subType NCHAR(50), | |
| 15 | + content NCHAR(1024), | |
| 16 | + report_time TIMESTAMP | |
| 17 | + )TAGS ( | |
| 18 | + device_key NCHAR(50) | |
| 19 | + ) | |
| 20 | + </update> | |
| 21 | + | |
| 22 | + | |
| 23 | + <!-- 创建设备日志子表 讨论:TDengine 在子表不存在的情况下 可在数据插入时 自动建表 要不要去掉创建子表的逻辑 由第一次插入数据时自动创建--> | |
| 24 | + <update id="createDeviceLogTable"> | |
| 25 | + CREATE TABLE device_log_${deviceKey} USING device_log TAGS('${deviceKey}') | |
| 26 | + </update> | |
| 27 | + | |
| 28 | + <!-- 插入设备日志数据 在子表不存在的情况下 可在数据插入时 自动建表 --> | |
| 29 | + <insert id="insert"> | |
| 30 | + INSERT INTO device_log_${log.deviceKey} (ts, id, product_key, type, subType, content, report_time) | |
| 31 | + USING device_log | |
| 32 | + TAGS ('${log.deviceKey}') | |
| 33 | + VALUES ( | |
| 34 | + #{log.ts}, | |
| 35 | + #{log.id}, | |
| 36 | + #{log.productKey}, | |
| 37 | + #{log.type}, | |
| 38 | + #{log.subType}, | |
| 39 | + #{log.content}, | |
| 40 | + #{log.reportTime} | |
| 41 | + ) | |
| 42 | + </insert> | |
| 43 | + | |
| 44 | +</mapper> | |
| 0 | 45 | \ No newline at end of file | ... | ... |