[功能添加]:物模型日志表创建.1
This commit is contained in:
@@ -55,7 +55,7 @@ public class IotDeviceDataController {
|
||||
@PostMapping("/simulator")
|
||||
@Operation(summary = "模拟设备")
|
||||
public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
|
||||
//TODO:先生成一下日志 后续完善模拟设备代码逻辑
|
||||
//TODO:先生成一下设备日志 后续完善模拟设备代码逻辑
|
||||
iotDeviceLogDataService.createDeviceLog(simulatorReqVO);
|
||||
return success(true);
|
||||
}
|
||||
|
@@ -4,7 +4,6 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.Data;
|
||||
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 模拟设备数据 Request VO")
|
||||
|
@@ -51,12 +51,12 @@ public class IotDeviceLogDO {
|
||||
/**
|
||||
* 上报时间戳
|
||||
*/
|
||||
private DateTime reportTime;
|
||||
private Long reportTime;
|
||||
|
||||
/**
|
||||
* 时序时间
|
||||
*/
|
||||
private DateTime ts;
|
||||
private Long ts;
|
||||
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,37 @@
|
||||
package cn.iocoder.yudao.module.iot.dal.tdengine;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
|
||||
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;
|
||||
|
||||
/**
|
||||
* IOT 设备日志数据 Mapper 接口
|
||||
*
|
||||
* 基于 TDengine 实现设备日志的存储
|
||||
*/
|
||||
@Mapper
|
||||
@TDengineDS
|
||||
@InterceptorIgnore(tenantLine = "true") // 避免 SQL 解析,因为 JSqlParser 对 TDengine 的 SQL 解析会报错
|
||||
public interface IotDeviceLogDataMapper {
|
||||
|
||||
/**
|
||||
* 创建设备日志超级表
|
||||
*
|
||||
* 注意:初始化时只需创建一次
|
||||
*/
|
||||
void createDeviceLogSTable();
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 插入设备日志数据
|
||||
*
|
||||
* 如果子表不存在,会自动创建子表
|
||||
*
|
||||
* @param log 设备日志数据
|
||||
*/
|
||||
void insert(@Param("log") IotDeviceLogDO log);
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package cn.iocoder.yudao.module.iot.service.device;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO;
|
||||
|
||||
/**
|
||||
* IoT 设备日志数据 Service 接口
|
||||
*
|
||||
* @author alwayssuper
|
||||
*/
|
||||
public interface IotDeviceLogDataService {
|
||||
|
||||
/**
|
||||
* 初始化 TDengine 超级表
|
||||
*
|
||||
*系统启动时,会自动初始化一次
|
||||
*/
|
||||
void initTDengineSTable();
|
||||
|
||||
/**
|
||||
* 插入设备日志
|
||||
*
|
||||
* 当该设备第一次插入日志时,自动创建该设备的设备日志子表
|
||||
*
|
||||
* @param simulatorReqVO 设备日志模拟数据
|
||||
*/
|
||||
void createDeviceLog(IotDeviceDataSimulatorSaveReqVO simulatorReqVO);
|
||||
|
||||
}
|
@@ -11,6 +11,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
/**
|
||||
* IoT 设备日志数据 Service 实现了
|
||||
@@ -19,6 +20,7 @@ import java.time.LocalDateTime;
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
@Validated
|
||||
public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
|
||||
|
||||
@Resource
|
||||
@@ -38,8 +40,28 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
|
||||
|
||||
@Override
|
||||
public void createDeviceLog(IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
|
||||
//TODO:讨论一下,iotkit这块TS和上报时间都是外部传入的 但是看TDengine文档 他是建议对TS在SQL中直接NOW 咱们的TS数据获取是走哪一种
|
||||
|
||||
// 1. 转换请求对象为 DO
|
||||
IotDeviceLogDO iotDeviceLogDO = BeanUtils.toBean(simulatorReqVO, IotDeviceLogDO.class);
|
||||
iotDeviceLogDO.setTs(DateTime.now());
|
||||
|
||||
// 2. 处理时间字段
|
||||
long currentTime = System.currentTimeMillis();
|
||||
// 2.1 设置时序时间为当前时间
|
||||
iotDeviceLogDO.setTs(currentTime);
|
||||
|
||||
// 2.2 处理上报时间
|
||||
if (simulatorReqVO.getReportTime() != null) {
|
||||
// 将 LocalDateTime 转换为时间戳
|
||||
iotDeviceLogDO.setReportTime(
|
||||
simulatorReqVO.getReportTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
|
||||
);
|
||||
} else {
|
||||
// 如果没有上报时间,使用当前时间
|
||||
iotDeviceLogDO.setReportTime(currentTime);
|
||||
}
|
||||
|
||||
// 3. 插入数据
|
||||
iotDeviceLogDataMapper.insert(iotDeviceLogDO);
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@
|
||||
|
||||
<!-- 创建设备日志超级表 初始化只创建一次-->
|
||||
<update id="createDeviceLogSTable">
|
||||
CREATE STABLE device_log(
|
||||
CREATE STABLE device_log (
|
||||
ts TIMESTAMP,
|
||||
id NCHAR(50),
|
||||
product_key NCHAR(50),
|
||||
|
Reference in New Issue
Block a user