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

Merge pull request !1211 from alwayssuper/feature/iot
This commit is contained in:
芋道源码 2025-01-25 03:27:53 +00:00 committed by Gitee
commit 03462a103c
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
25 changed files with 381 additions and 101 deletions

View File

@ -30,6 +30,7 @@ public interface ErrorCodeConstants {
ErrorCode DEVICE_GATEWAY_NOT_EXISTS = new ErrorCode(1_050_003_004, "网关设备不存在");
ErrorCode DEVICE_NOT_GATEWAY = new ErrorCode(1_050_003_005, "设备不是网关设备");
ErrorCode DEVICE_IMPORT_LIST_IS_EMPTY = new ErrorCode(1_050_003_006, "导入设备数据不能为空!");
ErrorCode DEVICE_DATA_CONTENT_JSON_PARSE_ERROR = new ErrorCode(1_050_003_007, "导入设备数据格式错误!");
// ========== 产品分类 1-050-004-000 ==========
ErrorCode PRODUCT_CATEGORY_NOT_EXISTS = new ErrorCode(1_050_004_000, "产品分类不存在");

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.iot.controller.admin.device;
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.api.device.dto.IotDevicePropertyReportReqDTO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
@ -55,9 +56,9 @@ public class IotDeviceDataController {
@PostMapping("/simulator")
@Operation(summary = "模拟设备")
public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
//TODO:生成一下设备日志 后续完善模拟设备代码逻辑
//TODO:使用 IotDeviceDataSimulatorSaveReqVO 另外content里数据类型的效验前端也没做后端应该要要效验一下这块后续看看怎么安排
// TODO @super应该 deviceDataService 里面有个 simulatorDevice然后里面去 insert 日志
iotDeviceLogDataService.createDeviceLog(simulatorReqVO);
deviceDataService.simulatorSend(simulatorReqVO);
return success(true);
}

View File

@ -14,16 +14,18 @@ public class IotDeviceDataSimulatorSaveReqVO {
private String id;
// TODO @super不用传递 productKey因为 deviceKey 可以推导出来
@Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "product123")
@NotEmpty(message = "产品ID不能为空")
// TODO 讨论: 日志记录的时候要记录一下productKey目前是前端已经有productKey了所以前端传入如果不传入的话后端要根据deviceKey查询productKey感觉直传是不是效率高一些
@Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "product123")
@NotEmpty(message = "产品标识不能为空")
private String productKey;
// TODO @super中文写作规范中英文之间要有空格例如说设备 IDps这里应该是设备标识
@Schema(description = "设备ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "device123")
@NotEmpty(message = "设备ID不能为空")
@Schema(description = "设备标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "device123")
@NotEmpty(message = "设备标识不能为空")
private String deviceKey;
// TODO @supertypesubType是不是不用传递因为模拟只有属性
// TODO 讨论: 不只模拟属性
@Schema(description = "消息/日志类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "property")
@NotEmpty(message = "消息类型不能为空")
private String type;
@ -36,7 +38,6 @@ public class IotDeviceDataSimulatorSaveReqVO {
@NotEmpty(message = "数据内容不能为空")
private String content;
// TODO @芋艿需要讨论下reportTime 到底以那个为准
@Schema(description = "上报时间", requiredMode = Schema.RequiredMode.REQUIRED)
private Long reportTime;

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.iot.dal.dataobject.device;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -27,12 +28,16 @@ public class IotDeviceLogDO {
// TODO @super关联要 @下
/**
* 产品标识
* <p>
* 关联 {@link IotProductDO#getProductKey()}
*/
private String productKey;
// TODO @super关联要 @下
/**
* 设备标识
* <p>
* 关联 {@link IotDeviceDO#getDeviceKey()}}
*/
private String deviceKey;

View File

@ -19,6 +19,9 @@ public class SelectVisualDO {
*/
private String tableName;
private String deviceKey;
/**
* 属性
*/

View File

@ -33,6 +33,10 @@ public class TdTableDO {
*/
private String tableName;
private String productKey;
private String deviceKey;
/**
* COLUMN 字段
*/

View File

@ -21,20 +21,25 @@ public interface IotDeviceLogDataMapper {
/**
* 创建设备日志超级表
*
* 注意初始化时只需创建一次
* 初始化只创建一次
*/
void createDeviceLogSTable();
// TODO @super是不是删除哈
/**
* 创建设备日志子表
*
* @param deviceKey 设备标识
*/
void createDeviceLogTable(@Param("deviceKey") String deviceKey);
// TODO @super单个参数不用加 @Param
//讨论艿菇这里有些特殊情况我也学习了一下这块知识
// 如果使用的是Java 8及以上版本并且编译器保留了参数名通过编译器选项-parameters启用则可以去掉@Param注解MyBatis会自动使用参数的实际名称
// 但在TDengine中 @Param去掉后TDengine会报错以下是大模型的回答
// 不用加 @Param在普通的 MySQL 场景下是正确的 - 对于 MyBatis当方法只有一个参数时确实可以不用添加 @Param 注解
//但是在 TDengine 的场景下情况不同
//TDengine 的特殊性
//TDengine 使用特殊的 SQL 语法
//需要处理超级表(STable)和子表的概念
//参数绑定的方式与普通 MySQL 不同
//为什么这里必须要 @Param
//XML 中使用了 ${log.deviceKey} 这样的参数引用方式
//需要在 SQL 中动态构建表名device_log_${log.deviceKey}
//没有 @Param("log") 的话MyBatis 无法正确解析参数
/**
* 插入设备日志数据
*
@ -60,4 +65,18 @@ public interface IotDeviceLogDataMapper {
*/
Long selectCount(@Param("reqVO") IotDeviceLogPageReqVO reqVO);
/**
* 查询设备日志表是否存在
*
* @return 不存在返回null
*/
Object checkDeviceLogSTableExists();
/**
* 检查设备日志子表是否存在
*
* @param deviceKey 设备标识
* @return 不存在返回null
*/
Object checkDeviceLogTableExists(@Param("deviceKey") String deviceKey);
}

View File

@ -2,6 +2,9 @@ package cn.iocoder.yudao.module.iot.dal.tdengine;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TdTableDO;
import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField;
import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
@ -10,6 +13,7 @@ import org.apache.ibatis.annotations.Param;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Mapper
@ -74,4 +78,43 @@ public interface IotDevicePropertyDataMapper {
void alterProductPropertySTableDropField(@Param("productKey") String productKey,
@Param("field") TDengineTableField field);
//TODO:先参考一下老逻辑后续改进
/**
* 插入数据 - 指定列插入数据
*
* @param table 数据
* productKey 产品 key
* deviceKey 设备 key
* columns
*/
void insertDevicePropertyData(TdTableDO table);
//TODO:先参考一下老逻辑后续改进
/**
* 查看超级表 - 获取超级表的结构信息
* SQLDESCRIBE [db_name.]stb_name;
*
* @param superTable 超级表信息
* productKey 产品 key
*/
List<Map<String, Object>> describeSuperTable(TdTableDO superTable);
/**
* 获取历史数据列表
*
* @param selectVisualDO 查询条件
* @return 历史数据列表
*/
@TenantIgnore
List<Map<String, Object>> selectHistoryDataList(SelectVisualDO selectVisualDO);
/**
* 获取历史数据条数
*
* @param selectVisualDO 查询条件
* @return 数据条数
*/
@TenantIgnore
Long selectHistoryCount(SelectVisualDO selectVisualDO);
}

View File

@ -1,31 +0,0 @@
package cn.iocoder.yudao.module.iot.dal.tdengine;
import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 处理 TD 中物模型消息日志的操作
*/
@Mapper
@Deprecated // TODO super什么时候删除下哈
@TDengineDS
@InterceptorIgnore(tenantLine = "true") // 避免 SQL 解析因为 JSqlParser TDengine SQL 解析会报错
public interface TdThingModelMessageMapper {
/**
* 创建物模型消息日志超级表超级表
*
*/
void createSuperTable(@Param("productKey") String productKey);
/**
* 创建子表
*
*/
void createTableWithTag(@Param("productKey") String productKey,@Param("deviceKey") String deviceKey);
}

View File

@ -25,18 +25,13 @@ public class TDengineTableInitConfiguration implements ApplicationRunner {
public void run(ApplicationArguments args) {
try {
// 初始化设备日志表
deviceLogService.initTDengineSTable();
deviceLogService.defineDeviceLog();
// TODO @super这个日志是不是不用打不然重复啦
log.info("[run]初始化 设备日志表 TDengine 表结构成功");
} catch (Exception ex) {
// TODO @super初始化失败打印 error 日志退出系统不然跑起来就初始啦
if (ex.getMessage().contains("Table already exists")) {
log.info("TDengine 设备日志超级表已存在,跳过创建");
return;
} else{
log.error("初始化 设备日志表 TDengine 表结构失败", ex);
}
throw ex;
// 初始化失败时打印错误日志并退出系统
log.error("[TDengine] 初始化设备日志表结构失败,系统无法正常运行,即将退出", ex);
System.exit(1);
}
}

View File

@ -0,0 +1,41 @@
package cn.iocoder.yudao.module.iot.mq.consumer.deviceconsumer;
import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
import cn.iocoder.yudao.module.iot.service.device.IotDeviceLogDataService;
import cn.iocoder.yudao.module.iot.service.device.IotDevicePropertyDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 针对 {@link ThingModelMessage} 的消费者
*
* @author alwayssuper
*/
@Component
@Slf4j
public class DeviceConsumer {
@Resource
private IotDeviceLogDataService iotDeviceLogDataService;
@Resource
private IotDevicePropertyDataService deviceDataService;
// TODO @芋艿这块先用ThingModelMessage后续看看用啥替代
@EventListener
@Async
public void onMessage(ThingModelMessage message) {
log.info("[onMessage][消息内容({})]", message);
//TODO:数据插入这块整体写的比较混乱整体借鉴了浩浩哥之前写的逻辑目前是通过模拟设备科插入数据了但之前的逻辑有大量弃用的部分后续看看怎么完善
// 设备数据记录
deviceDataService.saveDeviceDataTest(message);
// 设备日志记录
iotDeviceLogDataService.saveDeviceLog(message);
}
}

View File

@ -1,10 +0,0 @@
package cn.iocoder.yudao.module.iot.mq.consumer.simulatesend;
/**
* TODO @alwayssuper记得实现还有类注释哈
*
* @author alwayssuper
* @since 2024/12/20 8:04
*/
public class SimulateSendConsumer {
}

View File

@ -0,0 +1,4 @@
/**
* 消息队列的消息
*/
package cn.iocoder.yudao.module.iot.mq.message;

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.iot.mq.producer.simulatesend;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
@ -19,4 +20,12 @@ public class SimulateSendProducer {
@Resource
private ApplicationContext applicationContext;
/**
* 发送 {@link ThingModelMessage} 消息
*
* @param thingModelMessage 物模型消息
*/
public void sendSimulateMessage(ThingModelMessage thingModelMessage) {
applicationContext.publishEvent(thingModelMessage);
}
}

View File

@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceLogPageReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
/**
* IoT 设备日志数据 Service 接口
@ -17,7 +18,7 @@ public interface IotDeviceLogDataService {
*
*系统启动时会自动初始化一次
*/
void initTDengineSTable();
void defineDeviceLog();
/**
* 插入设备日志
@ -36,4 +37,11 @@ public interface IotDeviceLogDataService {
*/
PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO);
/**
* 插入设备日志
*
* @param msg 设备数据
*/
void saveDeviceLog(ThingModelMessage msg);
}

View File

@ -1,16 +1,19 @@
package cn.iocoder.yudao.module.iot.service.device;
import cn.hutool.json.JSONUtil;
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.deviceData.IotDeviceDataSimulatorSaveReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceLogPageReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
import cn.iocoder.yudao.module.iot.dal.tdengine.IotDeviceLogDataMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.Date;
import java.util.List;
/**
@ -28,9 +31,24 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
// TODO @super方法名defineDeviceLog未来有可能别人使用别的记录日志例如说 es 之类的
@Override
public void initTDengineSTable() {
public void defineDeviceLog() {
// TODO @super改成不存在才创建
iotDeviceLogDataMapper.createDeviceLogSTable();
// try {
// // 创建超级表使用 IF NOT EXISTS 语句避免重复创建错误
// iotDeviceLogDataMapper.createDeviceLogSTable();
// } catch (Exception e) {
// if (e.getMessage().contains("already exists")) {
// log.info("[TDengine] 设备日志超级表已存在,跳过创建");
// return;
// }
// throw e;
// }
if(iotDeviceLogDataMapper.checkDeviceLogSTableExists()==null){
log.info("[TDengine] 设备日志超级表不存在,开始创建");
iotDeviceLogDataMapper.createDeviceLogSTable();
}else{
log.info("[TDengine] 设备日志超级表已存在,跳过创建");
}
}
@Override
@ -40,18 +58,24 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
// 2. 处理时间字段
// TODO @super一次性的字段不用单独给个变量
long currentTime = System.currentTimeMillis();
// long currentTime = System.currentTimeMillis();
// 2.1 设置时序时间为当前时间
iotDeviceLogDO.setTs(currentTime); // TODO @superTS在SQL中直接NOW 咱们的TS数据获取是走哪一种 now()
// iotDeviceLogDO.setTs(currentTime); // TODO @superTS在SQL中直接NOW 咱们的TS数据获取是走哪一种 now()
// 3. 插入数据
// TODO @super不要直接调用对方的 IotDeviceLogDataMapper通过 service
// 讨论艿菇 这就是iotDeviceLogDataService的Impl
iotDeviceLogDataMapper.insert(iotDeviceLogDO);
}
// TODO @super iotDeviceLogDataService
// 讨论艿菇 这就是iotDeviceLogDataService的Impl
@Override
public PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO) {
// 当设备日志表未创建时查询会出现报错
if(iotDeviceLogDataMapper.checkDeviceLogTableExists(pageReqVO.getDeviceKey())==null){
return null;
}
// 查询数据
List<IotDeviceLogDO> list = iotDeviceLogDataMapper.selectPage(pageReqVO);
Long total = iotDeviceLogDataMapper.selectCount(pageReqVO);
@ -59,4 +83,21 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
return new PageResult<>(list, total);
}
@Override
public void saveDeviceLog(ThingModelMessage msg) {
// 1. 构建设备日志对象
IotDeviceLogDO iotDeviceLogDO = IotDeviceLogDO.builder()
.id(msg.getId()) // 消息ID
.deviceKey(msg.getDeviceKey()) // 设备标识
.productKey(msg.getProductKey()) // 产品标识
.type(msg.getMethod()) // 消息类型使用method作为类型
.subType("property") // TODO:这块先写死后续优化
.content(JSONUtil.toJsonStr(msg)) // TODO:后续优化
.reportTime(msg.getTime()) // 上报时间
.build();
// 2. 插入设备日志
iotDeviceLogDataMapper.insert(iotDeviceLogDO);
}
}

View File

@ -3,7 +3,9 @@ package cn.iocoder.yudao.module.iot.service.device;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
import jakarta.validation.Valid;
import java.util.List;
@ -30,6 +32,22 @@ public interface IotDevicePropertyDataService {
*/
void saveDeviceData(IotDevicePropertyReportReqDTO createDTO);
/**
* 保存设备数据
*
* @param thingModelMessage 设备数据
*/
void saveDeviceDataTest(ThingModelMessage thingModelMessage);
/**
* 模拟设备
*
* @param simulatorReqVO 设备数据
*/
void simulatorSend(IotDeviceDataSimulatorSaveReqVO simulatorReqVO);
/**
* 获得设备属性最新数据
*

View File

@ -3,11 +3,14 @@ package cn.iocoder.yudao.module.iot.service.device;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType.ThingModelDateOrTextDataSpecs;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO;
@ -22,6 +25,7 @@ import cn.iocoder.yudao.module.iot.enums.IotConstants;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum;
import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField;
import cn.iocoder.yudao.module.iot.mq.producer.simulatesend.SimulateSendProducer;
import cn.iocoder.yudao.module.iot.service.product.IotProductService;
import cn.iocoder.yudao.module.iot.service.tdengine.IotThingModelMessageService;
import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService;
@ -37,8 +41,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_DATA_CONTENT_JSON_PARSE_ERROR;
/**
* IoT 设备属性数据 Service 实现类
@ -76,6 +82,9 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
@Resource
private IotProductService productService;
@Resource
private SimulateSendProducer simulateSendProducer;
@Resource
private TdEngineDMLMapper tdEngineDMLMapper;
@ -150,6 +159,45 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
thingModelMessageService.saveThingModelMessage(device, thingModelMessage);
}
//TODO:后续捋一捋这块逻辑先借鉴一下目前的代码
@Override
public void saveDeviceDataTest(ThingModelMessage thingModelMessage) {
// 1. 根据产品 key 和设备名称获得设备信息
IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceName(thingModelMessage.getProductKey(), thingModelMessage.getDeviceName());
// 2. 保存数据
thingModelMessageService.saveThingModelMessage(device, thingModelMessage);
}
//TODO: copy saveDeviceData 的逻辑后续看看这块怎么优化
@Override
public void simulatorSend(IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
// 1. 根据设备 key 获得设备信息
IotDeviceDO device = deviceService.getDeviceByDeviceKey(simulatorReqVO.getDeviceKey());
// 2. 解析 content JSON 对象
JSONObject contentJson;
try {
contentJson = JSONUtil.parseObj(simulatorReqVO.getContent());
} catch (Exception e) {
throw exception(DEVICE_DATA_CONTENT_JSON_PARSE_ERROR);
}
// 3. 构建物模型消息
ThingModelMessage thingModelMessage = ThingModelMessage.builder()
.id(IdUtil.fastSimpleUUID()) // TODO:后续优化
.sys(null)// TODO:这块先写死后续优化
.method("thing.event.property.post") // TODO:这块先写死后续优化
.params(contentJson) // content 作为 params
.time(simulatorReqVO.getReportTime()) // 使用上报时间
.productKey(simulatorReqVO.getProductKey())
.deviceName(device.getDeviceName())
.deviceKey(device.getDeviceKey())
.build();
// 4. 发送模拟消息
simulateSendProducer.sendSimulateMessage(thingModelMessage);
}
@Override
public List<IotDeviceDataDO> getLatestDeviceProperties(@Valid IotDeviceDataPageReqVO deviceDataReqVO) {
List<IotDeviceDataDO> list = new ArrayList<>();
@ -196,6 +244,7 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
SelectVisualDO selectVisualDO = new SelectVisualDO();
selectVisualDO.setDataBaseName(getDatabaseName());
selectVisualDO.setTableName(getDeviceTableName(device.getProductKey(), device.getDeviceName()));
selectVisualDO.setDeviceKey(device.getDeviceKey());
selectVisualDO.setFieldName(deviceDataReqVO.getIdentifier());
selectVisualDO.setStartTime(DateUtil.date(deviceDataReqVO.getTimes()[0].atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()).getTime());
selectVisualDO.setEndTime(DateUtil.date(deviceDataReqVO.getTimes()[1].atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()).getTime());
@ -203,8 +252,8 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
params.put("rows", deviceDataReqVO.getPageSize());
params.put("page", (deviceDataReqVO.getPageNo() - 1) * deviceDataReqVO.getPageSize());
selectVisualDO.setParams(params);
pageResult.setList(tdEngineDMLMapper.selectHistoryDataList(selectVisualDO));
pageResult.setTotal(tdEngineDMLMapper.selectHistoryCount(selectVisualDO));
pageResult.setList(devicePropertyDataMapper.selectHistoryDataList(selectVisualDO));
pageResult.setTotal(devicePropertyDataMapper.selectHistoryCount(selectVisualDO));
return pageResult;
}

View File

@ -72,6 +72,15 @@ public interface IotDeviceService {
*/
IotDeviceDO getDevice(Long id);
/**
* 根据设备 key 获得设备
*
* @param deviceKey 编号
* @return IoT 设备
*/
IotDeviceDO getDeviceByDeviceKey(String deviceKey);
/**
* <EFBFBD><EFBFBD>得设备分页
*

View File

@ -197,6 +197,11 @@ public class IotDeviceServiceImpl implements IotDeviceService {
return deviceMapper.selectById(id);
}
@Override
public IotDeviceDO getDeviceByDeviceKey(String deviceKey) {
return deviceMapper.selectByDeviceKey(deviceKey);
}
@Override
public PageResult<IotDeviceDO> getDevicePage(IotDevicePageReqVO pageReqVO) {
return deviceMapper.selectPage(pageReqVO);

View File

@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TdTableDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO;
import cn.iocoder.yudao.module.iot.dal.tdengine.IotDevicePropertyDataMapper;
import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDDLMapper;
import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper;
import cn.iocoder.yudao.module.iot.enums.IotConstants;
@ -61,6 +62,9 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
@Resource
private TdEngineDMLMapper tdEngineDMLMapper;
@Resource
private IotDevicePropertyDataMapper iotDevicePropertyDataMapper;
@Resource
private DeviceDataRedisDAO deviceDataRedisDAO;
@ -71,7 +75,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
// 1. 判断设备状态如果为未激活状态创建数据表并更新设备状态
if (IotDeviceStatusEnum.INACTIVE.getStatus().equals(device.getStatus())) {
// 1.1 创建设备表
createDeviceTable(device.getDeviceType(), device.getProductKey(), device.getDeviceName(), device.getDeviceKey());
// createDeviceTable(device.getDeviceType(), device.getProductKey(), device.getDeviceName(), device.getDeviceKey());
iotDeviceService.updateDeviceStatus(new IotDeviceStatusUpdateReqVO()
.setId(device.getId()).setStatus(IotDeviceStatusEnum.ONLINE.getStatus()));
}
@ -85,14 +89,20 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
// 3. 过滤并收集有效的属性字段缓存设备属性
List<TdFieldDO> schemaFieldValues = filterAndCollectValidFields(params, thingModelList, device, thingModelMessage.getTime());
if (schemaFieldValues.size() == 1) { // 仅有时间字段无需保存
if (schemaFieldValues.size() == 0) { // 没有字段无需保存
return;
}
// 4. 构建并保存设备属性数据
tdEngineDMLMapper.insertData(TdTableDO.builder()
.dataBaseName(getDatabaseName())
.tableName(getDeviceTableName(device.getProductKey(), device.getDeviceName()))
// tdEngineDMLMapper.insertData(TdTableDO.builder()
// .dataBaseName(getDatabaseName())
// .tableName(getDeviceTableName(device.getProductKey(), device.getDeviceName()))
// .columns(schemaFieldValues)
// .build());
// TODO:复用了旧逻辑,先过渡一下
iotDevicePropertyDataMapper.insertDevicePropertyData(TdTableDO.builder()
.productKey(device.getProductKey())
.deviceKey(device.getDeviceKey())
.columns(schemaFieldValues)
.build());
}
@ -145,7 +155,8 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
// 3. 过滤并收集有效的属性字段
List<TdFieldDO> schemaFieldValues = new ArrayList<>();
schemaFieldValues.add(new TdFieldDO(TIME, time));
//TODO:新版本是使用ts字段
// schemaFieldValues.add(new TdFieldDO(TIME, time));
params.forEach((key, val) -> {
if (propertyIdentifiers.contains(key)) {
schemaFieldValues.add(new TdFieldDO(key.toLowerCase(), val));

View File

@ -6,13 +6,13 @@
<!-- 创建设备日志超级表 初始化只创建一次-->
<update id="createDeviceLogSTable">
CREATE STABLE device_log (
CREATE STABLE IF NOT EXISTS device_log (
ts TIMESTAMP,
id NCHAR(50),
product_key NCHAR(50),
type NCHAR(50),
<!-- TODO @super下划线 sub_type -->
subType NCHAR(50),
sub_type NCHAR(50),
content NCHAR(1024),
report_time TIMESTAMP
) TAGS (
@ -31,7 +31,7 @@
USING device_log
TAGS ('${log.deviceKey}')
VALUES (
#{log.ts},
NOW,
#{log.id},
#{log.productKey},
#{log.type},
@ -75,4 +75,15 @@
</where>
</select>
<!-- 检查设备日志超级表是否存在 -->
<select id="checkDeviceLogSTableExists" resultType="Object">
SHOW STABLES LIKE 'device_log'
</select>
<!-- 检查设备日志子表是否存在 -->
<select id="checkDeviceLogTableExists" resultType="Object">
SHOW TABLES LIKE 'device_log_${deviceKey}'
</select>
</mapper>

View File

@ -42,4 +42,46 @@
DROP COLUMN ${field.field}
</update>
<insert id="insertDevicePropertyData">
INSERT INTO device_property_${deviceKey}
USING product_property_${productKey}
TAGS ('${deviceKey}')
(ts
<foreach item="item" collection="columns" separator=",">
,${item.fieldName}
</foreach>
)
VALUES
(NOW
<foreach item="item" collection="columns" separator=",">
,#{item.fieldValue}
</foreach>
)
</insert>
<!-- 描述超级表结构 -->
<select id="describeSuperTable" resultType="java.util.Map">
DESCRIBE product_property_${productKey}
</select>
<!-- 获取历史数据 -->
<select id="selectHistoryDataList" resultType="java.util.Map"
parameterType="cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO">
SELECT ${fieldName} AS data, ts AS time
FROM device_property_${deviceKey}
WHERE ts BETWEEN #{startTime} AND #{endTime}
AND ${fieldName} IS NOT NULL
ORDER BY ts DESC
LIMIT #{params.rows} OFFSET #{params.page}
</select>
<!-- 统计历史数据总数 -->
<select id="selectHistoryCount" resultType="java.lang.Long"
parameterType="cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO">
SELECT COUNT(*)
FROM device_property_${deviceKey}
WHERE ts BETWEEN #{startTime} AND #{endTime}
AND ${fieldName} IS NOT NULL
</select>
</mapper>

View File

@ -45,7 +45,7 @@ spring:
primary: master
datasource:
master:
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 连接的示例
url: jdbc:mysql://chaojiniu.top:23306/ruoyi-vue-pro?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
# 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 连接的示例
# url: jdbc:postgresql://127.0.0.1:5432/ruoyi-vue-pro # PostgreSQL 连接的示例
# url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
@ -53,8 +53,8 @@ spring:
# url: jdbc:dm://127.0.0.1:5236?schema=RUOYI_VUE_PRO # DM 连接的示例
# url: jdbc:kingbase8://127.0.0.1:54321/test # 人大金仓 KingbaseES 连接的示例
# url: jdbc:postgresql://127.0.0.1:5432/postgres # OpenGauss 连接的示例
username: root
password: 123456
username: ruoyi-vue-pro
password: ruoyi-@h2ju02hebp
# username: sa # SQL Server 连接的示例
# password: Yudao@2024 # SQL Server 连接的示例
# username: SYSDBA # DM 连接的示例
@ -63,12 +63,12 @@ spring:
# password: Yudao@2024 # OpenGauss 连接的示例
slave: # 模拟从库,可根据自己需要修改
lazy: true # 开启懒加载,保证启动速度
url: jdbc:mysql://127.0.0.1:3306/ruoyi-vue-pro?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
username: root
password: 123456
url: jdbc:mysql://chaojiniu.top:23306/ruoyi-vue-pro?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
username: ruoyi-vue-pro
password: ruoyi-@h2ju02hebp
tdengine: # IOT 数据库
# lazy: true # 开启懒加载,保证启动速度
url: jdbc:TAOS-RS://127.0.0.1:6041/ruoyi_vue_pro
url: jdbc:TAOS-RS://chaojiniu.top:6041/ruoyi_vue_pro
driver-class-name: com.taosdata.jdbc.rs.RestfulDriver
username: root
password: taosdata
@ -78,10 +78,10 @@ spring:
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
data:
redis:
host: 400-infra.server.iocoder.cn # 地址
host: chaojiniu.top # 地址
port: 6379 # 端口
database: 1 # 数据库索引
# password: 123456 # 密码,建议生产环境开启
database: 15 # 数据库索引
password: fsknKD7UvQYZsyf2hXXn # 密码,建议生产环境开启
--- #################### 定时任务相关配置 ####################
@ -183,10 +183,8 @@ logging:
cn.iocoder.yudao.module.crm.dal.mysql: debug
cn.iocoder.yudao.module.erp.dal.mysql: debug
cn.iocoder.yudao.module.iot.dal.mysql: debug
cn.iocoder.yudao.module.iot.dal.tdengine: DEBUG
cn.iocoder.yudao.module.ai.dal.mysql: debug
org.springframework.context.support.PostProcessorRegistrationDelegate: ERROR # TODO 芋艿先禁用Spring Boot 3.X 存在部分错误的 WARN 提示
com.taosdata: DEBUG # TDengine 的日志级别
debug: false
@ -269,11 +267,11 @@ justauth:
iot:
emq:
# 账号
username: root
username: haohao
# 密码
password: 123456
password: ahh@123456
# 主机地址
hostUrl: tcp://127.0.0.1:1883
hostUrl: tcp://chaojiniu.top:1883
# 客户端Id不能相同采用随机数 ${random.value}
client-id: ${random.int}
# 默认主题
@ -285,9 +283,9 @@ iot:
# MQTT-RPC 配置
mqtt:
broker: tcp://127.0.0.1:1883
username: root
password: 123456
broker: tcp://chaojiniu.top:1883
username: haohao
password: ahh@123456
clientId: mqtt-rpc-server-${random.int}
requestTopic: rpc/request
responseTopicPrefix: rpc/response/

View File

@ -328,4 +328,7 @@ yudao:
key: pLXUGAwK5305
customer: E77DF18BE109F454A5CD319E44BF5177
debug: false
debug: false
# 插件配置
pf4j:
pluginsDir: /Users/anhaohao/code/gitee/ruoyi-vue-pro/plugins # 插件目录