Commit 03462a103ca452f4a40261fe48f026cc68e30be0

Authored by 芋道源码
Committed by Gitee
2 parents 5264de07 03d4f60e

!1211 [feat]:模拟设备加入 spring event

Merge pull request !1211 from alwayssuper/feature/iot
Showing 25 changed files with 381 additions and 101 deletions
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java
@@ -30,6 +30,7 @@ public interface ErrorCodeConstants { @@ -30,6 +30,7 @@ public interface ErrorCodeConstants {
30 ErrorCode DEVICE_GATEWAY_NOT_EXISTS = new ErrorCode(1_050_003_004, "网关设备不存在"); 30 ErrorCode DEVICE_GATEWAY_NOT_EXISTS = new ErrorCode(1_050_003_004, "网关设备不存在");
31 ErrorCode DEVICE_NOT_GATEWAY = new ErrorCode(1_050_003_005, "设备不是网关设备"); 31 ErrorCode DEVICE_NOT_GATEWAY = new ErrorCode(1_050_003_005, "设备不是网关设备");
32 ErrorCode DEVICE_IMPORT_LIST_IS_EMPTY = new ErrorCode(1_050_003_006, "导入设备数据不能为空!"); 32 ErrorCode DEVICE_IMPORT_LIST_IS_EMPTY = new ErrorCode(1_050_003_006, "导入设备数据不能为空!");
  33 + ErrorCode DEVICE_DATA_CONTENT_JSON_PARSE_ERROR = new ErrorCode(1_050_003_007, "导入设备数据格式错误!");
33 34
34 // ========== 产品分类 1-050-004-000 ========== 35 // ========== 产品分类 1-050-004-000 ==========
35 ErrorCode PRODUCT_CATEGORY_NOT_EXISTS = new ErrorCode(1_050_004_000, "产品分类不存在"); 36 ErrorCode PRODUCT_CATEGORY_NOT_EXISTS = new ErrorCode(1_050_004_000, "产品分类不存在");
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceDataController.java
@@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.iot.controller.admin.device; @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.iot.controller.admin.device;
3 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 3 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
4 import cn.iocoder.yudao.framework.common.pojo.PageResult; 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 import cn.iocoder.yudao.framework.common.util.object.BeanUtils; 5 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
  6 +import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
6 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.*; 7 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.*;
7 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO; 8 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO;
8 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO; 9 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
@@ -55,9 +56,9 @@ public class IotDeviceDataController { @@ -55,9 +56,9 @@ public class IotDeviceDataController {
55 @PostMapping("/simulator") 56 @PostMapping("/simulator")
56 @Operation(summary = "模拟设备") 57 @Operation(summary = "模拟设备")
57 public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulatorReqVO) { 58 public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
58 - //TODO:先生成一下设备日志 后续完善模拟设备代码逻辑 59 + //TODO:先使用 IotDeviceDataSimulatorSaveReqVO 另外content里数据类型的效验前端也没做,后端应该要要效验一下,这块后续看看怎么安排
59 // TODO @super:应该 deviceDataService 里面有个 simulatorDevice,然后里面去 insert 日志! 60 // TODO @super:应该 deviceDataService 里面有个 simulatorDevice,然后里面去 insert 日志!
60 - iotDeviceLogDataService.createDeviceLog(simulatorReqVO); 61 + deviceDataService.simulatorSend(simulatorReqVO);
61 return success(true); 62 return success(true);
62 } 63 }
63 64
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/deviceData/IotDeviceDataSimulatorSaveReqVO.java
@@ -14,16 +14,18 @@ public class IotDeviceDataSimulatorSaveReqVO { @@ -14,16 +14,18 @@ public class IotDeviceDataSimulatorSaveReqVO {
14 private String id; 14 private String id;
15 15
16 // TODO @super:不用传递 productKey,因为 deviceKey 可以推导出来 16 // TODO @super:不用传递 productKey,因为 deviceKey 可以推导出来
17 - @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "product123")  
18 - @NotEmpty(message = "产品ID不能为空") 17 + // TODO 讨论: 日志记录的时候要记录一下productKey,目前是前端已经有productKey了,所以前端传入,如果不传入的话,后端要根据deviceKey查询productKey,感觉直传是不是效率高一些
  18 + @Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "product123")
  19 + @NotEmpty(message = "产品标识不能为空")
19 private String productKey; 20 private String productKey;
20 21
21 // TODO @super:中文写作规范,中英文之间,要有空格。例如说,设备 ID。ps:这里应该是设备标识 22 // TODO @super:中文写作规范,中英文之间,要有空格。例如说,设备 ID。ps:这里应该是设备标识
22 - @Schema(description = "设备ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "device123")  
23 - @NotEmpty(message = "设备ID不能为空") 23 + @Schema(description = "设备标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "device123")
  24 + @NotEmpty(message = "设备标识不能为空")
24 private String deviceKey; 25 private String deviceKey;
25 26
26 // TODO @super:type、subType,是不是不用传递,因为模拟只有属性??? 27 // TODO @super:type、subType,是不是不用传递,因为模拟只有属性???
  28 + // TODO 讨论: 不只模拟属性
27 @Schema(description = "消息/日志类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "property") 29 @Schema(description = "消息/日志类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "property")
28 @NotEmpty(message = "消息类型不能为空") 30 @NotEmpty(message = "消息类型不能为空")
29 private String type; 31 private String type;
@@ -36,7 +38,6 @@ public class IotDeviceDataSimulatorSaveReqVO { @@ -36,7 +38,6 @@ public class IotDeviceDataSimulatorSaveReqVO {
36 @NotEmpty(message = "数据内容不能为空") 38 @NotEmpty(message = "数据内容不能为空")
37 private String content; 39 private String content;
38 40
39 - // TODO @芋艿:需要讨论下,reportTime 到底以那个为准!  
40 @Schema(description = "上报时间", requiredMode = Schema.RequiredMode.REQUIRED) 41 @Schema(description = "上报时间", requiredMode = Schema.RequiredMode.REQUIRED)
41 private Long reportTime; 42 private Long reportTime;
42 43
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; 1 package cn.iocoder.yudao.module.iot.dal.dataobject.device;
2 2
  3 +import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
3 import lombok.AllArgsConstructor; 4 import lombok.AllArgsConstructor;
4 import lombok.Builder; 5 import lombok.Builder;
5 import lombok.Data; 6 import lombok.Data;
@@ -27,12 +28,16 @@ public class IotDeviceLogDO { @@ -27,12 +28,16 @@ public class IotDeviceLogDO {
27 // TODO @super:关联要 @下 28 // TODO @super:关联要 @下
28 /** 29 /**
29 * 产品标识 30 * 产品标识
  31 + * <p>
  32 + * 关联 {@link IotProductDO#getProductKey()}
30 */ 33 */
31 private String productKey; 34 private String productKey;
32 35
33 // TODO @super:关联要 @下 36 // TODO @super:关联要 @下
34 /** 37 /**
35 * 设备标识 38 * 设备标识
  39 + * <p>
  40 + * 关联 {@link IotDeviceDO#getDeviceKey()}}
36 */ 41 */
37 private String deviceKey; 42 private String deviceKey;
38 43
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/tdengine/SelectVisualDO.java
@@ -19,6 +19,9 @@ public class SelectVisualDO { @@ -19,6 +19,9 @@ public class SelectVisualDO {
19 */ 19 */
20 private String tableName; 20 private String tableName;
21 21
  22 +
  23 + private String deviceKey;
  24 +
22 /** 25 /**
23 * 属性 26 * 属性
24 */ 27 */
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/tdengine/TdTableDO.java
@@ -33,6 +33,10 @@ public class TdTableDO { @@ -33,6 +33,10 @@ public class TdTableDO {
33 */ 33 */
34 private String tableName; 34 private String tableName;
35 35
  36 + private String productKey;
  37 +
  38 + private String deviceKey;
  39 +
36 /** 40 /**
37 * COLUMN 字段 41 * COLUMN 字段
38 */ 42 */
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/IotDeviceLogDataMapper.java
@@ -21,20 +21,25 @@ public interface IotDeviceLogDataMapper { @@ -21,20 +21,25 @@ public interface IotDeviceLogDataMapper {
21 21
22 /** 22 /**
23 * 创建设备日志超级表 23 * 创建设备日志超级表
24 - *  
25 - * 注意:初始化时只需创建一次 24 + * 初始化只创建一次
26 */ 25 */
27 void createDeviceLogSTable(); 26 void createDeviceLogSTable();
28 27
29 - // TODO @super:是不是删除哈  
30 - /**  
31 - * 创建设备日志子表  
32 - *  
33 - * @param deviceKey 设备标识  
34 - */  
35 - void createDeviceLogTable(@Param("deviceKey") String deviceKey);  
36 28
37 // TODO @super:单个参数,不用加 @Param 29 // TODO @super:单个参数,不用加 @Param
  30 + //讨论:艿菇这里有些特殊情况,我也学习了一下这块知识:
  31 + // 如果使用的是Java 8及以上版本,并且编译器保留了参数名(通过编译器选项-parameters启用),则可以去掉@Param注解。MyBatis会自动使用参数的实际名称
  32 + // 但在TDengine中 @Param去掉后TDengine会报错,以下是大模型的回答:
  33 + // 不用加 @Param在普通的 MySQL 场景下是正确的 - 对于 MyBatis,当方法只有一个参数时,确实可以不用添加 @Param 注解。
  34 + //但是在 TDengine 的场景下,情况不同:
  35 + //TDengine 的特殊性:
  36 + //TDengine 使用特殊的 SQL 语法
  37 + //需要处理超级表(STable)和子表的概念
  38 + //参数绑定的方式与普通 MySQL 不同
  39 + //为什么这里必须要 @Param:
  40 + //XML 中使用了 ${log.deviceKey} 这样的参数引用方式
  41 + //需要在 SQL 中动态构建表名(device_log_${log.deviceKey})
  42 + //没有 @Param("log") 的话,MyBatis 无法正确解析参数
38 /** 43 /**
39 * 插入设备日志数据 44 * 插入设备日志数据
40 * 45 *
@@ -60,4 +65,18 @@ public interface IotDeviceLogDataMapper { @@ -60,4 +65,18 @@ public interface IotDeviceLogDataMapper {
60 */ 65 */
61 Long selectCount(@Param("reqVO") IotDeviceLogPageReqVO reqVO); 66 Long selectCount(@Param("reqVO") IotDeviceLogPageReqVO reqVO);
62 67
  68 + /**
  69 + * 查询设备日志表是否存在
  70 + *
  71 + * @return 不存在返回null
  72 + */
  73 + Object checkDeviceLogSTableExists();
  74 +
  75 + /**
  76 + * 检查设备日志子表是否存在
  77 + *
  78 + * @param deviceKey 设备标识
  79 + * @return 不存在返回null
  80 + */
  81 + Object checkDeviceLogTableExists(@Param("deviceKey") String deviceKey);
63 } 82 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/IotDevicePropertyDataMapper.java
@@ -2,6 +2,9 @@ package cn.iocoder.yudao.module.iot.dal.tdengine; @@ -2,6 +2,9 @@ package cn.iocoder.yudao.module.iot.dal.tdengine;
2 2
3 import cn.hutool.core.collection.CollUtil; 3 import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.util.ObjectUtil; 4 import cn.hutool.core.util.ObjectUtil;
  5 +import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
  6 +import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO;
  7 +import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TdTableDO;
5 import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField; 8 import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField;
6 import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS; 9 import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS;
7 import com.baomidou.mybatisplus.annotation.InterceptorIgnore; 10 import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
@@ -10,6 +13,7 @@ import org.apache.ibatis.annotations.Param; @@ -10,6 +13,7 @@ import org.apache.ibatis.annotations.Param;
10 13
11 import java.util.ArrayList; 14 import java.util.ArrayList;
12 import java.util.List; 15 import java.util.List;
  16 +import java.util.Map;
13 import java.util.stream.Collectors; 17 import java.util.stream.Collectors;
14 18
15 @Mapper 19 @Mapper
@@ -74,4 +78,43 @@ public interface IotDevicePropertyDataMapper { @@ -74,4 +78,43 @@ public interface IotDevicePropertyDataMapper {
74 void alterProductPropertySTableDropField(@Param("productKey") String productKey, 78 void alterProductPropertySTableDropField(@Param("productKey") String productKey,
75 @Param("field") TDengineTableField field); 79 @Param("field") TDengineTableField field);
76 80
  81 + //TODO:先参考一下老逻辑,后续改进
  82 + /**
  83 + * 插入数据 - 指定列插入数据
  84 + *
  85 + * @param table 数据
  86 + * productKey 产品 key
  87 + * deviceKey 设备 key
  88 + * columns 列
  89 + */
  90 + void insertDevicePropertyData(TdTableDO table);
  91 +
  92 + //TODO:先参考一下老逻辑,后续改进
  93 + /**
  94 + * 查看超级表 - 获取超级表的结构信息
  95 + * SQL:DESCRIBE [db_name.]stb_name;
  96 + *
  97 + * @param superTable 超级表信息
  98 + * productKey 产品 key
  99 + */
  100 + List<Map<String, Object>> describeSuperTable(TdTableDO superTable);
  101 +
  102 + /**
  103 + * 获取历史数据列表
  104 + *
  105 + * @param selectVisualDO 查询条件
  106 + * @return 历史数据列表
  107 + */
  108 + @TenantIgnore
  109 + List<Map<String, Object>> selectHistoryDataList(SelectVisualDO selectVisualDO);
  110 +
  111 + /**
  112 + * 获取历史数据条数
  113 + *
  114 + * @param selectVisualDO 查询条件
  115 + * @return 数据条数
  116 + */
  117 + @TenantIgnore
  118 + Long selectHistoryCount(SelectVisualDO selectVisualDO);
  119 +
77 } 120 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/TdThingModelMessageMapper.java deleted
1 -package cn.iocoder.yudao.module.iot.dal.tdengine;  
2 -  
3 -import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS;  
4 -import com.baomidou.mybatisplus.annotation.InterceptorIgnore;  
5 -import org.apache.ibatis.annotations.Mapper;  
6 -import org.apache.ibatis.annotations.Param;  
7 -  
8 -/**  
9 - * 处理 TD 中物模型消息日志的操作  
10 - */  
11 -@Mapper  
12 -@Deprecated // TODO super:什么时候,删除下哈。  
13 -@TDengineDS  
14 -@InterceptorIgnore(tenantLine = "true") // 避免 SQL 解析,因为 JSqlParser 对 TDengine 的 SQL 解析会报错  
15 -public interface TdThingModelMessageMapper {  
16 -  
17 - /**  
18 - * 创建物模型消息日志超级表超级表  
19 - *  
20 - */  
21 -  
22 - void createSuperTable(@Param("productKey") String productKey);  
23 -  
24 - /**  
25 - * 创建子表  
26 - *  
27 - */  
28 -  
29 - void createTableWithTag(@Param("productKey") String productKey,@Param("deviceKey") String deviceKey);  
30 -  
31 -}  
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/tdengine/config/TDengineTableInitConfiguration.java
@@ -25,18 +25,13 @@ public class TDengineTableInitConfiguration implements ApplicationRunner { @@ -25,18 +25,13 @@ public class TDengineTableInitConfiguration implements ApplicationRunner {
25 public void run(ApplicationArguments args) { 25 public void run(ApplicationArguments args) {
26 try { 26 try {
27 // 初始化设备日志表 27 // 初始化设备日志表
28 - deviceLogService.initTDengineSTable(); 28 + deviceLogService.defineDeviceLog();
29 // TODO @super:这个日志,是不是不用打,不然重复啦!!! 29 // TODO @super:这个日志,是不是不用打,不然重复啦!!!
30 - log.info("[run]初始化 设备日志表 TDengine 表结构成功");  
31 } catch (Exception ex) { 30 } catch (Exception ex) {
32 // TODO @super:初始化失败,打印 error 日志,退出系统。。不然跑起来,就初始啦!!! 31 // TODO @super:初始化失败,打印 error 日志,退出系统。。不然跑起来,就初始啦!!!
33 - if (ex.getMessage().contains("Table already exists")) {  
34 - log.info("TDengine 设备日志超级表已存在,跳过创建");  
35 - return;  
36 - } else{  
37 - log.error("初始化 设备日志表 TDengine 表结构失败", ex);  
38 - }  
39 - throw ex; 32 + // 初始化失败时打印错误日志并退出系统
  33 + log.error("[TDengine] 初始化设备日志表结构失败,系统无法正常运行,即将退出", ex);
  34 + System.exit(1);
40 } 35 }
41 } 36 }
42 37
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/mq/consumer/deviceconsumer/DeviceConsumer.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.mq.consumer.deviceconsumer;
  2 +
  3 +
  4 +import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
  5 +import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
  6 +import cn.iocoder.yudao.module.iot.service.device.IotDeviceLogDataService;
  7 +import cn.iocoder.yudao.module.iot.service.device.IotDevicePropertyDataService;
  8 +import lombok.extern.slf4j.Slf4j;
  9 +import org.springframework.context.event.EventListener;
  10 +import org.springframework.scheduling.annotation.Async;
  11 +import org.springframework.stereotype.Component;
  12 +
  13 +import javax.annotation.Resource;
  14 +
  15 +/**
  16 + * 针对 {@link ThingModelMessage} 的消费者
  17 + *
  18 + * @author alwayssuper
  19 + */
  20 +@Component
  21 +@Slf4j
  22 +public class DeviceConsumer {
  23 + @Resource
  24 + private IotDeviceLogDataService iotDeviceLogDataService;
  25 + @Resource
  26 + private IotDevicePropertyDataService deviceDataService;
  27 +
  28 + // TODO @芋艿:这块先用ThingModelMessage,后续看看用啥替代
  29 + @EventListener
  30 + @Async
  31 + public void onMessage(ThingModelMessage message) {
  32 + log.info("[onMessage][消息内容({})]", message);
  33 + //TODO:数据插入这块整体写的比较混乱,整体借鉴了浩浩哥之前写的逻辑,目前是通过模拟设备科插入数据了,但之前的逻辑有大量弃用的部分,后续看看怎么完善
  34 +
  35 + // 设备数据记录
  36 + deviceDataService.saveDeviceDataTest(message);
  37 + // 设备日志记录
  38 + iotDeviceLogDataService.saveDeviceLog(message);
  39 + }
  40 +
  41 +}
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/mq/consumer/simulatesend/SimulateSendConsumer.java deleted
1 -package cn.iocoder.yudao.module.iot.mq.consumer.simulatesend;  
2 -  
3 -/**  
4 - * TODO @alwayssuper:记得实现,还有类注释哈  
5 - *  
6 - * @author alwayssuper  
7 - * @since 2024/12/20 8:04  
8 - */  
9 -public class SimulateSendConsumer {  
10 -}  
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/mq/message/package-info.java 0 → 100644
  1 +/**
  2 + * 消息队列的消息
  3 + */
  4 +package cn.iocoder.yudao.module.iot.mq.message;
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/mq/producer/simulatesend/SimulateSendProducer.java
1 package cn.iocoder.yudao.module.iot.mq.producer.simulatesend; 1 package cn.iocoder.yudao.module.iot.mq.producer.simulatesend;
2 2
  3 +import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
3 import jakarta.annotation.Resource; 4 import jakarta.annotation.Resource;
4 import lombok.extern.slf4j.Slf4j; 5 import lombok.extern.slf4j.Slf4j;
5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.ApplicationContext;
@@ -19,4 +20,12 @@ public class SimulateSendProducer { @@ -19,4 +20,12 @@ public class SimulateSendProducer {
19 @Resource 20 @Resource
20 private ApplicationContext applicationContext; 21 private ApplicationContext applicationContext;
21 22
  23 + /**
  24 + * 发送 {@link ThingModelMessage} 消息
  25 + *
  26 + * @param thingModelMessage 物模型消息
  27 + */
  28 + public void sendSimulateMessage(ThingModelMessage thingModelMessage) {
  29 + applicationContext.publishEvent(thingModelMessage);
  30 + }
22 } 31 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceLogDataService.java
@@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; @@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO; 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; 5 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceLogPageReqVO;
6 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO; 6 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
  7 +import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
7 8
8 /** 9 /**
9 * IoT 设备日志数据 Service 接口 10 * IoT 设备日志数据 Service 接口
@@ -17,7 +18,7 @@ public interface IotDeviceLogDataService { @@ -17,7 +18,7 @@ public interface IotDeviceLogDataService {
17 * 18 *
18 *系统启动时,会自动初始化一次 19 *系统启动时,会自动初始化一次
19 */ 20 */
20 - void initTDengineSTable(); 21 + void defineDeviceLog();
21 22
22 /** 23 /**
23 * 插入设备日志 24 * 插入设备日志
@@ -36,4 +37,11 @@ public interface IotDeviceLogDataService { @@ -36,4 +37,11 @@ public interface IotDeviceLogDataService {
36 */ 37 */
37 PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO); 38 PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO);
38 39
  40 + /**
  41 + * 插入设备日志
  42 + *
  43 + * @param msg 设备数据
  44 + */
  45 + void saveDeviceLog(ThingModelMessage msg);
  46 +
39 } 47 }
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; 1 package cn.iocoder.yudao.module.iot.service.device;
2 2
  3 +import cn.hutool.json.JSONUtil;
3 import cn.iocoder.yudao.framework.common.pojo.PageResult; 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 import cn.iocoder.yudao.framework.common.util.object.BeanUtils; 5 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.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO;
6 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceLogPageReqVO; 7 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceLogPageReqVO;
7 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO; 8 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
  9 +import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
8 import cn.iocoder.yudao.module.iot.dal.tdengine.IotDeviceLogDataMapper; 10 import cn.iocoder.yudao.module.iot.dal.tdengine.IotDeviceLogDataMapper;
9 import jakarta.annotation.Resource; 11 import jakarta.annotation.Resource;
10 import lombok.extern.slf4j.Slf4j; 12 import lombok.extern.slf4j.Slf4j;
11 import org.springframework.stereotype.Service; 13 import org.springframework.stereotype.Service;
12 import org.springframework.validation.annotation.Validated; 14 import org.springframework.validation.annotation.Validated;
13 15
  16 +import java.util.Date;
14 import java.util.List; 17 import java.util.List;
15 18
16 /** 19 /**
@@ -28,9 +31,24 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{ @@ -28,9 +31,24 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
28 31
29 // TODO @super:方法名。defineDeviceLog。。未来,有可能别人使用别的记录日志,例如说 es 之类的。 32 // TODO @super:方法名。defineDeviceLog。。未来,有可能别人使用别的记录日志,例如说 es 之类的。
30 @Override 33 @Override
31 - public void initTDengineSTable() { 34 + public void defineDeviceLog() {
32 // TODO @super:改成不存在才创建。 35 // TODO @super:改成不存在才创建。
33 - iotDeviceLogDataMapper.createDeviceLogSTable(); 36 +// try {
  37 +// // 创建超级表(使用 IF NOT EXISTS 语句避免重复创建错误)
  38 +// iotDeviceLogDataMapper.createDeviceLogSTable();
  39 +// } catch (Exception e) {
  40 +// if (e.getMessage().contains("already exists")) {
  41 +// log.info("[TDengine] 设备日志超级表已存在,跳过创建");
  42 +// return;
  43 +// }
  44 +// throw e;
  45 +// }
  46 + if(iotDeviceLogDataMapper.checkDeviceLogSTableExists()==null){
  47 + log.info("[TDengine] 设备日志超级表不存在,开始创建");
  48 + iotDeviceLogDataMapper.createDeviceLogSTable();
  49 + }else{
  50 + log.info("[TDengine] 设备日志超级表已存在,跳过创建");
  51 + }
34 } 52 }
35 53
36 @Override 54 @Override
@@ -40,18 +58,24 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{ @@ -40,18 +58,24 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
40 58
41 // 2. 处理时间字段 59 // 2. 处理时间字段
42 // TODO @super:一次性的字段,不用单独给个变量 60 // TODO @super:一次性的字段,不用单独给个变量
43 - long currentTime = System.currentTimeMillis(); 61 +// long currentTime = System.currentTimeMillis();
44 // 2.1 设置时序时间为当前时间 62 // 2.1 设置时序时间为当前时间
45 - iotDeviceLogDO.setTs(currentTime); // TODO @super:TS在SQL中直接NOW 咱们的TS数据获取是走哪一种;走 now() 63 +// iotDeviceLogDO.setTs(currentTime); // TODO @super:TS在SQL中直接NOW 咱们的TS数据获取是走哪一种;走 now()
46 64
47 // 3. 插入数据 65 // 3. 插入数据
48 // TODO @super:不要直接调用对方的 IotDeviceLogDataMapper,通过 service 哈! 66 // TODO @super:不要直接调用对方的 IotDeviceLogDataMapper,通过 service 哈!
  67 + // 讨论:艿菇 这就是iotDeviceLogDataService的Impl
49 iotDeviceLogDataMapper.insert(iotDeviceLogDO); 68 iotDeviceLogDataMapper.insert(iotDeviceLogDO);
50 } 69 }
51 70
52 // TODO @super:在 iotDeviceLogDataService 写 71 // TODO @super:在 iotDeviceLogDataService 写
  72 + // 讨论:艿菇 这就是iotDeviceLogDataService的Impl
53 @Override 73 @Override
54 public PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO) { 74 public PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO) {
  75 + // 当设备日志表未创建时,查询会出现报错
  76 + if(iotDeviceLogDataMapper.checkDeviceLogTableExists(pageReqVO.getDeviceKey())==null){
  77 + return null;
  78 + }
55 // 查询数据 79 // 查询数据
56 List<IotDeviceLogDO> list = iotDeviceLogDataMapper.selectPage(pageReqVO); 80 List<IotDeviceLogDO> list = iotDeviceLogDataMapper.selectPage(pageReqVO);
57 Long total = iotDeviceLogDataMapper.selectCount(pageReqVO); 81 Long total = iotDeviceLogDataMapper.selectCount(pageReqVO);
@@ -59,4 +83,21 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{ @@ -59,4 +83,21 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
59 return new PageResult<>(list, total); 83 return new PageResult<>(list, total);
60 } 84 }
61 85
  86 + @Override
  87 + public void saveDeviceLog(ThingModelMessage msg) {
  88 + // 1. 构建设备日志对象
  89 + IotDeviceLogDO iotDeviceLogDO = IotDeviceLogDO.builder()
  90 + .id(msg.getId()) // 消息ID
  91 + .deviceKey(msg.getDeviceKey()) // 设备标识
  92 + .productKey(msg.getProductKey()) // 产品标识
  93 + .type(msg.getMethod()) // 消息类型,使用method作为类型
  94 + .subType("property") // TODO:这块先写死,后续优化
  95 + .content(JSONUtil.toJsonStr(msg)) // TODO:后续优化
  96 + .reportTime(msg.getTime()) // 上报时间
  97 + .build();
  98 +
  99 + // 2. 插入设备日志
  100 + iotDeviceLogDataMapper.insert(iotDeviceLogDO);
  101 + }
  102 +
62 } 103 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDevicePropertyDataService.java
@@ -3,7 +3,9 @@ package cn.iocoder.yudao.module.iot.service.device; @@ -3,7 +3,9 @@ package cn.iocoder.yudao.module.iot.service.device;
3 import cn.iocoder.yudao.framework.common.pojo.PageResult; 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; 4 import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
5 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO; 5 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO;
  6 +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO;
6 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO; 7 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO;
  8 +import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
7 import jakarta.validation.Valid; 9 import jakarta.validation.Valid;
8 10
9 import java.util.List; 11 import java.util.List;
@@ -30,6 +32,22 @@ public interface IotDevicePropertyDataService { @@ -30,6 +32,22 @@ public interface IotDevicePropertyDataService {
30 */ 32 */
31 void saveDeviceData(IotDevicePropertyReportReqDTO createDTO); 33 void saveDeviceData(IotDevicePropertyReportReqDTO createDTO);
32 34
  35 +
  36 + /**
  37 + * 保存设备数据
  38 + *
  39 + * @param thingModelMessage 设备数据
  40 + */
  41 + void saveDeviceDataTest(ThingModelMessage thingModelMessage);
  42 +
  43 + /**
  44 + * 模拟设备
  45 + *
  46 + * @param simulatorReqVO 设备数据
  47 + */
  48 +
  49 + void simulatorSend(IotDeviceDataSimulatorSaveReqVO simulatorReqVO);
  50 +
33 /** 51 /**
34 * 获得设备属性最新数据 52 * 获得设备属性最新数据
35 * 53 *
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDevicePropertyDataServiceImpl.java
@@ -3,11 +3,14 @@ package cn.iocoder.yudao.module.iot.service.device; @@ -3,11 +3,14 @@ package cn.iocoder.yudao.module.iot.service.device;
3 import cn.hutool.core.collection.CollUtil; 3 import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.date.DateUtil; 4 import cn.hutool.core.date.DateUtil;
5 import cn.hutool.core.map.MapUtil; 5 import cn.hutool.core.map.MapUtil;
  6 +import cn.hutool.core.util.IdUtil;
6 import cn.hutool.core.util.StrUtil; 7 import cn.hutool.core.util.StrUtil;
7 import cn.hutool.json.JSONObject; 8 import cn.hutool.json.JSONObject;
  9 +import cn.hutool.json.JSONUtil;
8 import cn.iocoder.yudao.framework.common.pojo.PageResult; 10 import cn.iocoder.yudao.framework.common.pojo.PageResult;
9 import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; 11 import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
10 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO; 12 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO;
  13 +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO;
11 import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType.ThingModelDateOrTextDataSpecs; 14 import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType.ThingModelDateOrTextDataSpecs;
12 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; 15 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
13 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO; 16 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO;
@@ -22,6 +25,7 @@ import cn.iocoder.yudao.module.iot.enums.IotConstants; @@ -22,6 +25,7 @@ import cn.iocoder.yudao.module.iot.enums.IotConstants;
22 import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum; 25 import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum;
23 import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum; 26 import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum;
24 import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField; 27 import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField;
  28 +import cn.iocoder.yudao.module.iot.mq.producer.simulatesend.SimulateSendProducer;
25 import cn.iocoder.yudao.module.iot.service.product.IotProductService; 29 import cn.iocoder.yudao.module.iot.service.product.IotProductService;
26 import cn.iocoder.yudao.module.iot.service.tdengine.IotThingModelMessageService; 30 import cn.iocoder.yudao.module.iot.service.tdengine.IotThingModelMessageService;
27 import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService; 31 import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService;
@@ -37,8 +41,10 @@ import java.util.HashMap; @@ -37,8 +41,10 @@ import java.util.HashMap;
37 import java.util.List; 41 import java.util.List;
38 import java.util.Map; 42 import java.util.Map;
39 43
  44 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
40 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; 45 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
41 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; 46 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
  47 +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_DATA_CONTENT_JSON_PARSE_ERROR;
42 48
43 /** 49 /**
44 * IoT 设备【属性】数据 Service 实现类 50 * IoT 设备【属性】数据 Service 实现类
@@ -77,6 +83,9 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe @@ -77,6 +83,9 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
77 private IotProductService productService; 83 private IotProductService productService;
78 84
79 @Resource 85 @Resource
  86 + private SimulateSendProducer simulateSendProducer;
  87 +
  88 + @Resource
80 private TdEngineDMLMapper tdEngineDMLMapper; 89 private TdEngineDMLMapper tdEngineDMLMapper;
81 90
82 @Resource 91 @Resource
@@ -150,6 +159,45 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe @@ -150,6 +159,45 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
150 thingModelMessageService.saveThingModelMessage(device, thingModelMessage); 159 thingModelMessageService.saveThingModelMessage(device, thingModelMessage);
151 } 160 }
152 161
  162 + //TODO:后续捋一捋这块逻辑,先借鉴一下目前的代码
  163 + @Override
  164 + public void saveDeviceDataTest(ThingModelMessage thingModelMessage) {
  165 + // 1. 根据产品 key 和设备名称,获得设备信息
  166 + IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceName(thingModelMessage.getProductKey(), thingModelMessage.getDeviceName());
  167 + // 2. 保存数据
  168 + thingModelMessageService.saveThingModelMessage(device, thingModelMessage);
  169 + }
  170 +
  171 + //TODO: copy 了 saveDeviceData 的逻辑,后续看看这块怎么优化
  172 + @Override
  173 + public void simulatorSend(IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
  174 + // 1. 根据设备 key ,获得设备信息
  175 + IotDeviceDO device = deviceService.getDeviceByDeviceKey(simulatorReqVO.getDeviceKey());
  176 +
  177 + // 2. 解析 content 为 JSON 对象
  178 + JSONObject contentJson;
  179 + try {
  180 + contentJson = JSONUtil.parseObj(simulatorReqVO.getContent());
  181 + } catch (Exception e) {
  182 + throw exception(DEVICE_DATA_CONTENT_JSON_PARSE_ERROR);
  183 + }
  184 +
  185 + // 3. 构建物模型消息
  186 + ThingModelMessage thingModelMessage = ThingModelMessage.builder()
  187 + .id(IdUtil.fastSimpleUUID()) // TODO:后续优化
  188 + .sys(null)// TODO:这块先写死,后续优化
  189 + .method("thing.event.property.post") // TODO:这块先写死,后续优化
  190 + .params(contentJson) // 将 content 作为 params
  191 + .time(simulatorReqVO.getReportTime()) // 使用上报时间
  192 + .productKey(simulatorReqVO.getProductKey())
  193 + .deviceName(device.getDeviceName())
  194 + .deviceKey(device.getDeviceKey())
  195 + .build();
  196 +
  197 + // 4. 发送模拟消息
  198 + simulateSendProducer.sendSimulateMessage(thingModelMessage);
  199 + }
  200 +
153 @Override 201 @Override
154 public List<IotDeviceDataDO> getLatestDeviceProperties(@Valid IotDeviceDataPageReqVO deviceDataReqVO) { 202 public List<IotDeviceDataDO> getLatestDeviceProperties(@Valid IotDeviceDataPageReqVO deviceDataReqVO) {
155 List<IotDeviceDataDO> list = new ArrayList<>(); 203 List<IotDeviceDataDO> list = new ArrayList<>();
@@ -196,6 +244,7 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe @@ -196,6 +244,7 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
196 SelectVisualDO selectVisualDO = new SelectVisualDO(); 244 SelectVisualDO selectVisualDO = new SelectVisualDO();
197 selectVisualDO.setDataBaseName(getDatabaseName()); 245 selectVisualDO.setDataBaseName(getDatabaseName());
198 selectVisualDO.setTableName(getDeviceTableName(device.getProductKey(), device.getDeviceName())); 246 selectVisualDO.setTableName(getDeviceTableName(device.getProductKey(), device.getDeviceName()));
  247 + selectVisualDO.setDeviceKey(device.getDeviceKey());
199 selectVisualDO.setFieldName(deviceDataReqVO.getIdentifier()); 248 selectVisualDO.setFieldName(deviceDataReqVO.getIdentifier());
200 selectVisualDO.setStartTime(DateUtil.date(deviceDataReqVO.getTimes()[0].atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()).getTime()); 249 selectVisualDO.setStartTime(DateUtil.date(deviceDataReqVO.getTimes()[0].atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()).getTime());
201 selectVisualDO.setEndTime(DateUtil.date(deviceDataReqVO.getTimes()[1].atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()).getTime()); 250 selectVisualDO.setEndTime(DateUtil.date(deviceDataReqVO.getTimes()[1].atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()).getTime());
@@ -203,8 +252,8 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe @@ -203,8 +252,8 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
203 params.put("rows", deviceDataReqVO.getPageSize()); 252 params.put("rows", deviceDataReqVO.getPageSize());
204 params.put("page", (deviceDataReqVO.getPageNo() - 1) * deviceDataReqVO.getPageSize()); 253 params.put("page", (deviceDataReqVO.getPageNo() - 1) * deviceDataReqVO.getPageSize());
205 selectVisualDO.setParams(params); 254 selectVisualDO.setParams(params);
206 - pageResult.setList(tdEngineDMLMapper.selectHistoryDataList(selectVisualDO));  
207 - pageResult.setTotal(tdEngineDMLMapper.selectHistoryCount(selectVisualDO)); 255 + pageResult.setList(devicePropertyDataMapper.selectHistoryDataList(selectVisualDO));
  256 + pageResult.setTotal(devicePropertyDataMapper.selectHistoryCount(selectVisualDO));
208 return pageResult; 257 return pageResult;
209 } 258 }
210 259
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceService.java
@@ -72,6 +72,15 @@ public interface IotDeviceService { @@ -72,6 +72,15 @@ public interface IotDeviceService {
72 */ 72 */
73 IotDeviceDO getDevice(Long id); 73 IotDeviceDO getDevice(Long id);
74 74
  75 +
  76 + /**
  77 + * 根据设备 key 获得设备
  78 + *
  79 + * @param deviceKey 编号
  80 + * @return IoT 设备
  81 + */
  82 + IotDeviceDO getDeviceByDeviceKey(String deviceKey);
  83 +
75 /** 84 /**
76 * ��得设备分页 85 * ��得设备分页
77 * 86 *
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java
@@ -198,6 +198,11 @@ public class IotDeviceServiceImpl implements IotDeviceService { @@ -198,6 +198,11 @@ public class IotDeviceServiceImpl implements IotDeviceService {
198 } 198 }
199 199
200 @Override 200 @Override
  201 + public IotDeviceDO getDeviceByDeviceKey(String deviceKey) {
  202 + return deviceMapper.selectByDeviceKey(deviceKey);
  203 + }
  204 +
  205 + @Override
201 public PageResult<IotDeviceDO> getDevicePage(IotDevicePageReqVO pageReqVO) { 206 public PageResult<IotDeviceDO> getDevicePage(IotDevicePageReqVO pageReqVO) {
202 return deviceMapper.selectPage(pageReqVO); 207 return deviceMapper.selectPage(pageReqVO);
203 } 208 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/tdengine/IotThingModelMessageServiceImpl.java
@@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TdTableDO; @@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TdTableDO;
13 import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage; 13 import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
14 import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO; 14 import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
15 import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO; 15 import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO;
  16 +import cn.iocoder.yudao.module.iot.dal.tdengine.IotDevicePropertyDataMapper;
16 import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDDLMapper; 17 import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDDLMapper;
17 import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper; 18 import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper;
18 import cn.iocoder.yudao.module.iot.enums.IotConstants; 19 import cn.iocoder.yudao.module.iot.enums.IotConstants;
@@ -62,6 +63,9 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ @@ -62,6 +63,9 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
62 private TdEngineDMLMapper tdEngineDMLMapper; 63 private TdEngineDMLMapper tdEngineDMLMapper;
63 64
64 @Resource 65 @Resource
  66 + private IotDevicePropertyDataMapper iotDevicePropertyDataMapper;
  67 +
  68 + @Resource
65 private DeviceDataRedisDAO deviceDataRedisDAO; 69 private DeviceDataRedisDAO deviceDataRedisDAO;
66 70
67 // TODO @haohao:这个方法,可以考虑加下 1. 2. 3. 更有层次感 71 // TODO @haohao:这个方法,可以考虑加下 1. 2. 3. 更有层次感
@@ -71,7 +75,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ @@ -71,7 +75,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
71 // 1. 判断设备状态,如果为未激活状态,创建数据表并更新设备状态 75 // 1. 判断设备状态,如果为未激活状态,创建数据表并更新设备状态
72 if (IotDeviceStatusEnum.INACTIVE.getStatus().equals(device.getStatus())) { 76 if (IotDeviceStatusEnum.INACTIVE.getStatus().equals(device.getStatus())) {
73 // 1.1 创建设备表 77 // 1.1 创建设备表
74 - createDeviceTable(device.getDeviceType(), device.getProductKey(), device.getDeviceName(), device.getDeviceKey()); 78 +// createDeviceTable(device.getDeviceType(), device.getProductKey(), device.getDeviceName(), device.getDeviceKey());
75 iotDeviceService.updateDeviceStatus(new IotDeviceStatusUpdateReqVO() 79 iotDeviceService.updateDeviceStatus(new IotDeviceStatusUpdateReqVO()
76 .setId(device.getId()).setStatus(IotDeviceStatusEnum.ONLINE.getStatus())); 80 .setId(device.getId()).setStatus(IotDeviceStatusEnum.ONLINE.getStatus()));
77 } 81 }
@@ -85,14 +89,20 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ @@ -85,14 +89,20 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
85 89
86 // 3. 过滤并收集有效的属性字段,缓存设备属性 90 // 3. 过滤并收集有效的属性字段,缓存设备属性
87 List<TdFieldDO> schemaFieldValues = filterAndCollectValidFields(params, thingModelList, device, thingModelMessage.getTime()); 91 List<TdFieldDO> schemaFieldValues = filterAndCollectValidFields(params, thingModelList, device, thingModelMessage.getTime());
88 - if (schemaFieldValues.size() == 1) { // 仅有时间字段,无需保存 92 + if (schemaFieldValues.size() == 0) { // 没有字段,无需保存
89 return; 93 return;
90 } 94 }
91 95
92 // 4. 构建并保存设备属性数据 96 // 4. 构建并保存设备属性数据
93 - tdEngineDMLMapper.insertData(TdTableDO.builder()  
94 - .dataBaseName(getDatabaseName())  
95 - .tableName(getDeviceTableName(device.getProductKey(), device.getDeviceName())) 97 +// tdEngineDMLMapper.insertData(TdTableDO.builder()
  98 +// .dataBaseName(getDatabaseName())
  99 +// .tableName(getDeviceTableName(device.getProductKey(), device.getDeviceName()))
  100 +// .columns(schemaFieldValues)
  101 +// .build());
  102 + // TODO:复用了旧逻辑,先过渡一下
  103 + iotDevicePropertyDataMapper.insertDevicePropertyData(TdTableDO.builder()
  104 + .productKey(device.getProductKey())
  105 + .deviceKey(device.getDeviceKey())
96 .columns(schemaFieldValues) 106 .columns(schemaFieldValues)
97 .build()); 107 .build());
98 } 108 }
@@ -145,7 +155,8 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ @@ -145,7 +155,8 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
145 155
146 // 3. 过滤并收集有效的属性字段 156 // 3. 过滤并收集有效的属性字段
147 List<TdFieldDO> schemaFieldValues = new ArrayList<>(); 157 List<TdFieldDO> schemaFieldValues = new ArrayList<>();
148 - schemaFieldValues.add(new TdFieldDO(TIME, time)); 158 + //TODO:新版本是使用ts字段
  159 +// schemaFieldValues.add(new TdFieldDO(TIME, time));
149 params.forEach((key, val) -> { 160 params.forEach((key, val) -> {
150 if (propertyIdentifiers.contains(key)) { 161 if (propertyIdentifiers.contains(key)) {
151 schemaFieldValues.add(new TdFieldDO(key.toLowerCase(), val)); 162 schemaFieldValues.add(new TdFieldDO(key.toLowerCase(), val));
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDeviceLogDataMapper.xml
@@ -6,13 +6,13 @@ @@ -6,13 +6,13 @@
6 6
7 <!-- 创建设备日志超级表 初始化只创建一次--> 7 <!-- 创建设备日志超级表 初始化只创建一次-->
8 <update id="createDeviceLogSTable"> 8 <update id="createDeviceLogSTable">
9 - CREATE STABLE device_log ( 9 + CREATE STABLE IF NOT EXISTS device_log (
10 ts TIMESTAMP, 10 ts TIMESTAMP,
11 id NCHAR(50), 11 id NCHAR(50),
12 product_key NCHAR(50), 12 product_key NCHAR(50),
13 type NCHAR(50), 13 type NCHAR(50),
14 <!-- TODO @super:下划线 sub_type --> 14 <!-- TODO @super:下划线 sub_type -->
15 - subType NCHAR(50), 15 + sub_type NCHAR(50),
16 content NCHAR(1024), 16 content NCHAR(1024),
17 report_time TIMESTAMP 17 report_time TIMESTAMP
18 ) TAGS ( 18 ) TAGS (
@@ -31,7 +31,7 @@ @@ -31,7 +31,7 @@
31 USING device_log 31 USING device_log
32 TAGS ('${log.deviceKey}') 32 TAGS ('${log.deviceKey}')
33 VALUES ( 33 VALUES (
34 - #{log.ts}, 34 + NOW,
35 #{log.id}, 35 #{log.id},
36 #{log.productKey}, 36 #{log.productKey},
37 #{log.type}, 37 #{log.type},
@@ -75,4 +75,15 @@ @@ -75,4 +75,15 @@
75 </where> 75 </where>
76 </select> 76 </select>
77 77
  78 + <!-- 检查设备日志超级表是否存在 -->
  79 + <select id="checkDeviceLogSTableExists" resultType="Object">
  80 + SHOW STABLES LIKE 'device_log'
  81 + </select>
  82 +
  83 + <!-- 检查设备日志子表是否存在 -->
  84 + <select id="checkDeviceLogTableExists" resultType="Object">
  85 + SHOW TABLES LIKE 'device_log_${deviceKey}'
  86 + </select>
  87 +
  88 +
78 </mapper> 89 </mapper>
79 \ No newline at end of file 90 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDevicePropertyDataMapper.xml
@@ -42,4 +42,46 @@ @@ -42,4 +42,46 @@
42 DROP COLUMN ${field.field} 42 DROP COLUMN ${field.field}
43 </update> 43 </update>
44 44
  45 + <insert id="insertDevicePropertyData">
  46 + INSERT INTO device_property_${deviceKey}
  47 + USING product_property_${productKey}
  48 + TAGS ('${deviceKey}')
  49 + (ts
  50 + <foreach item="item" collection="columns" separator=",">
  51 + ,${item.fieldName}
  52 + </foreach>
  53 + )
  54 + VALUES
  55 + (NOW
  56 + <foreach item="item" collection="columns" separator=",">
  57 + ,#{item.fieldValue}
  58 + </foreach>
  59 + )
  60 + </insert>
  61 +
  62 + <!-- 描述超级表结构 -->
  63 + <select id="describeSuperTable" resultType="java.util.Map">
  64 + DESCRIBE product_property_${productKey}
  65 + </select>
  66 +
  67 + <!-- 获取历史数据 -->
  68 + <select id="selectHistoryDataList" resultType="java.util.Map"
  69 + parameterType="cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO">
  70 + SELECT ${fieldName} AS data, ts AS time
  71 + FROM device_property_${deviceKey}
  72 + WHERE ts BETWEEN #{startTime} AND #{endTime}
  73 + AND ${fieldName} IS NOT NULL
  74 + ORDER BY ts DESC
  75 + LIMIT #{params.rows} OFFSET #{params.page}
  76 + </select>
  77 +
  78 + <!-- 统计历史数据总数 -->
  79 + <select id="selectHistoryCount" resultType="java.lang.Long"
  80 + parameterType="cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO">
  81 + SELECT COUNT(*)
  82 + FROM device_property_${deviceKey}
  83 + WHERE ts BETWEEN #{startTime} AND #{endTime}
  84 + AND ${fieldName} IS NOT NULL
  85 + </select>
  86 +
45 </mapper> 87 </mapper>
46 \ No newline at end of file 88 \ No newline at end of file
yudao-server/src/main/resources/application-local.yaml
@@ -45,7 +45,7 @@ spring: @@ -45,7 +45,7 @@ spring:
45 primary: master 45 primary: master
46 datasource: 46 datasource:
47 master: 47 master:
48 - url: jdbc:mysql://127.0.0.1:3306/ruoyi-vue-pro?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例 48 + url: jdbc:mysql://chaojiniu.top:23306/ruoyi-vue-pro?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
49 # url: jdbc:mysql://127.0.0.1:3306/ruoyi-vue-pro?useSSL=true&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai # MySQL Connector/J 5.X 连接的示例 49 # url: jdbc:mysql://127.0.0.1:3306/ruoyi-vue-pro?useSSL=true&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai # MySQL Connector/J 5.X 连接的示例
50 # url: jdbc:postgresql://127.0.0.1:5432/ruoyi-vue-pro # PostgreSQL 连接的示例 50 # url: jdbc:postgresql://127.0.0.1:5432/ruoyi-vue-pro # PostgreSQL 连接的示例
51 # url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例 51 # url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
@@ -53,8 +53,8 @@ spring: @@ -53,8 +53,8 @@ spring:
53 # url: jdbc:dm://127.0.0.1:5236?schema=RUOYI_VUE_PRO # DM 连接的示例 53 # url: jdbc:dm://127.0.0.1:5236?schema=RUOYI_VUE_PRO # DM 连接的示例
54 # url: jdbc:kingbase8://127.0.0.1:54321/test # 人大金仓 KingbaseES 连接的示例 54 # url: jdbc:kingbase8://127.0.0.1:54321/test # 人大金仓 KingbaseES 连接的示例
55 # url: jdbc:postgresql://127.0.0.1:5432/postgres # OpenGauss 连接的示例 55 # url: jdbc:postgresql://127.0.0.1:5432/postgres # OpenGauss 连接的示例
56 - username: root  
57 - password: 123456 56 + username: ruoyi-vue-pro
  57 + password: ruoyi-@h2ju02hebp
58 # username: sa # SQL Server 连接的示例 58 # username: sa # SQL Server 连接的示例
59 # password: Yudao@2024 # SQL Server 连接的示例 59 # password: Yudao@2024 # SQL Server 连接的示例
60 # username: SYSDBA # DM 连接的示例 60 # username: SYSDBA # DM 连接的示例
@@ -63,12 +63,12 @@ spring: @@ -63,12 +63,12 @@ spring:
63 # password: Yudao@2024 # OpenGauss 连接的示例 63 # password: Yudao@2024 # OpenGauss 连接的示例
64 slave: # 模拟从库,可根据自己需要修改 64 slave: # 模拟从库,可根据自己需要修改
65 lazy: true # 开启懒加载,保证启动速度 65 lazy: true # 开启懒加载,保证启动速度
66 - url: jdbc:mysql://127.0.0.1:3306/ruoyi-vue-pro?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true  
67 - username: root  
68 - password: 123456 66 + url: jdbc:mysql://chaojiniu.top:23306/ruoyi-vue-pro?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
  67 + username: ruoyi-vue-pro
  68 + password: ruoyi-@h2ju02hebp
69 tdengine: # IOT 数据库 69 tdengine: # IOT 数据库
70 # lazy: true # 开启懒加载,保证启动速度 70 # lazy: true # 开启懒加载,保证启动速度
71 - url: jdbc:TAOS-RS://127.0.0.1:6041/ruoyi_vue_pro 71 + url: jdbc:TAOS-RS://chaojiniu.top:6041/ruoyi_vue_pro
72 driver-class-name: com.taosdata.jdbc.rs.RestfulDriver 72 driver-class-name: com.taosdata.jdbc.rs.RestfulDriver
73 username: root 73 username: root
74 password: taosdata 74 password: taosdata
@@ -78,10 +78,10 @@ spring: @@ -78,10 +78,10 @@ spring:
78 # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 78 # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
79 data: 79 data:
80 redis: 80 redis:
81 - host: 400-infra.server.iocoder.cn # 地址 81 + host: chaojiniu.top # 地址
82 port: 6379 # 端口 82 port: 6379 # 端口
83 - database: 1 # 数据库索引  
84 - # password: 123456 # 密码,建议生产环境开启 83 + database: 15 # 数据库索引
  84 + password: fsknKD7UvQYZsyf2hXXn # 密码,建议生产环境开启
85 85
86 --- #################### 定时任务相关配置 #################### 86 --- #################### 定时任务相关配置 ####################
87 87
@@ -183,10 +183,8 @@ logging: @@ -183,10 +183,8 @@ logging:
183 cn.iocoder.yudao.module.crm.dal.mysql: debug 183 cn.iocoder.yudao.module.crm.dal.mysql: debug
184 cn.iocoder.yudao.module.erp.dal.mysql: debug 184 cn.iocoder.yudao.module.erp.dal.mysql: debug
185 cn.iocoder.yudao.module.iot.dal.mysql: debug 185 cn.iocoder.yudao.module.iot.dal.mysql: debug
186 - cn.iocoder.yudao.module.iot.dal.tdengine: DEBUG  
187 cn.iocoder.yudao.module.ai.dal.mysql: debug 186 cn.iocoder.yudao.module.ai.dal.mysql: debug
188 org.springframework.context.support.PostProcessorRegistrationDelegate: ERROR # TODO 芋艿:先禁用,Spring Boot 3.X 存在部分错误的 WARN 提示 187 org.springframework.context.support.PostProcessorRegistrationDelegate: ERROR # TODO 芋艿:先禁用,Spring Boot 3.X 存在部分错误的 WARN 提示
189 - com.taosdata: DEBUG # TDengine 的日志级别  
190 188
191 debug: false 189 debug: false
192 190
@@ -269,11 +267,11 @@ justauth: @@ -269,11 +267,11 @@ justauth:
269 iot: 267 iot:
270 emq: 268 emq:
271 # 账号 269 # 账号
272 - username: root 270 + username: haohao
273 # 密码 271 # 密码
274 - password: 123456 272 + password: ahh@123456
275 # 主机地址 273 # 主机地址
276 - hostUrl: tcp://127.0.0.1:1883 274 + hostUrl: tcp://chaojiniu.top:1883
277 # 客户端Id,不能相同,采用随机数 ${random.value} 275 # 客户端Id,不能相同,采用随机数 ${random.value}
278 client-id: ${random.int} 276 client-id: ${random.int}
279 # 默认主题 277 # 默认主题
@@ -285,9 +283,9 @@ iot: @@ -285,9 +283,9 @@ iot:
285 283
286 # MQTT-RPC 配置 284 # MQTT-RPC 配置
287 mqtt: 285 mqtt:
288 - broker: tcp://127.0.0.1:1883  
289 - username: root  
290 - password: 123456 286 + broker: tcp://chaojiniu.top:1883
  287 + username: haohao
  288 + password: ahh@123456
291 clientId: mqtt-rpc-server-${random.int} 289 clientId: mqtt-rpc-server-${random.int}
292 requestTopic: rpc/request 290 requestTopic: rpc/request
293 responseTopicPrefix: rpc/response/ 291 responseTopicPrefix: rpc/response/
yudao-server/src/main/resources/application.yaml
@@ -328,4 +328,7 @@ yudao: @@ -328,4 +328,7 @@ yudao:
328 key: pLXUGAwK5305 328 key: pLXUGAwK5305
329 customer: E77DF18BE109F454A5CD319E44BF5177 329 customer: E77DF18BE109F454A5CD319E44BF5177
330 330
331 -debug: false  
332 \ No newline at end of file 331 \ No newline at end of file
  332 +debug: false
  333 +# 插件配置
  334 +pf4j:
  335 + pluginsDir: /Users/anhaohao/code/gitee/ruoyi-vue-pro/plugins # 插件目录
333 \ No newline at end of file 336 \ No newline at end of file