feat(iot): 增加事件记录查询功能并优化相关逻辑

- 在 EventController 中添加事件记录查询接口
- 在 EventLogRepository 中实现根据 ClientId 查询事件记录的方法
- 在 RecordDataServiceImpl 中优化数据处理逻辑
- 更新相关实体类和 mapper 文件以支持新功能
This commit is contained in:
zhuangtianxiang 2025-03-11 23:46:32 +08:00
parent 0835e859d0
commit fdfa843603
9 changed files with 104 additions and 46 deletions

View File

@ -26,4 +26,6 @@ public interface DeviceRepository extends BaseMapper<Device> {
@Param("status") Device.Status status,
@Param("online") Boolean online);
@Select("select * from iot_device where client_id=#{clientId}")
Device findByClientId(@Param("clientId") String clientId);
}

View File

@ -1,5 +1,6 @@
package com.zsc.edu.gateway.modules.iot.record.service.impl;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
@ -44,7 +45,7 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
recordData.setClientId(clientId);
recordData.setContent(data);
recordData.setRecordTime(LocalDateTime.now());
baseMapper.insert(recordData);
this.save(recordData);
processRecordData(recordData);
return recordData;
}
@ -52,7 +53,7 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
@Override
public RecordDataStatusVo recordDataStatus() {
RecordDataStatusVo recordDataStatusVo = new RecordDataStatusVo();
recordDataStatusVo.setRecordCount(baseMapper.selectCount(null));
recordDataStatusVo.setRecordCount(baseMapper.selectCount(new LambdaQueryWrapper<RecordData>()));
recordDataStatusVo.setDataCount(baseMapper.selectCount(new LambdaQueryWrapper<RecordData>().isNotNull(RecordData::getContent)));
return recordDataStatusVo;
}
@ -83,10 +84,10 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
public void processRecordData(RecordData recordData) {
// 根据 clientId 查询设备信息
Device device = deviceRepository.selectOne(new LambdaQueryWrapper<Device>().eq(Device::getClientId, recordData.getClientId()));
Device device = deviceRepository.findByClientId(recordData.getClientId());
// 获取产品下的所有事件
List<Event> events = eventRepository.selectList(new LambdaQueryWrapper<Event>().eq(Event::getProductId, device.getProductId()));
List<Event> events = eventRepository.selectByProductId(device.getProductId());
// 遍历每个事件
for (Event event : events) {
@ -100,42 +101,38 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
private void processParam(RecordData recordData, Param param, Event event) {
// 获取参数的默认值和比较类型
Double defaultValue = param.getDefaultValue();
Param.CompareType compareType = param.getCompareType();
String identifier = param.getIdentifier();
// 检查 recordData 的内容中是否包含该参数的标识符
if (recordData.getContent().get(identifier) != null) {
try {
// 获取参数的实际值
double value = Double.parseDouble(recordData.getContent().get(identifier).toString());
// 获取参数的实际值
double value = Double.parseDouble(recordData.getContent().get(identifier).toString());
Param.CompareType compareType = param.getCompareType();
// 根据比较类型进行不同的处理
switch (compareType) {
case GT:
if (value > defaultValue) {
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
}
break;
case LT:
if (value < defaultValue) {
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
}
break;
case EQ:
if (value == defaultValue) {
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
}
break;
default:
// 处理未知的比较类型
System.err.println("Unknown compare type: " + compareType);
break;
}
} catch (NumberFormatException e) {
// 处理数值转换异常
System.err.println("Failed to parse value for identifier: " + identifier + ". Error: " + e.getMessage());
// 根据比较类型进行不同的处理
switch (compareType) {
case GT:
if (value > defaultValue) {
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
}
break;
case LT:
if (value < defaultValue) {
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
}
break;
case EQ:
if (value == defaultValue) {
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
}
break;
default:
// 处理未知的比较类型
System.err.println("Unknown compare type: " + compareType);
break;
}
}
}
}

View File

@ -1,10 +1,13 @@
package com.zsc.edu.gateway.modules.iot.tsl.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.modules.iot.tsl.dto.EventDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Event;
import com.zsc.edu.gateway.modules.iot.tsl.entity.EventLog;
import com.zsc.edu.gateway.modules.iot.tsl.query.EventQuery;
import com.zsc.edu.gateway.modules.iot.tsl.repo.EventLogRepository;
import com.zsc.edu.gateway.modules.iot.tsl.service.EventService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
@ -21,6 +24,8 @@ public class EventController {
private final EventService service;
private final EventLogRepository eventLogRepository;
/**
* 创建事件
@ -59,6 +64,19 @@ public class EventController {
return service.page(page, query.wrapper());
}
/**
* 查询事件记录
*
* @param clientId 事件ClientId
* @param page 分页参数
* @return Page<Device> 事件分页数据
*/
@GetMapping("/log")
@PreAuthorize("hasAuthority('iot:event:query')")
public IPage<EventLog> eventLogPage(Page<EventLog> page, String clientId) {
return eventLogRepository.selectPage(page, new LambdaQueryWrapper<EventLog>().eq(EventLog::getRecordId, clientId));
}
/**
* 删除事件

View File

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.zsc.edu.gateway.common.enums.IState;
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
import com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@ -40,26 +41,26 @@ public class Event extends BaseParam {
@TableField(exist = false)
private List<Param> outputs;
public enum Type implements IEnum<Integer>, IState<Type> {
public enum Type implements IEnum<String>, IState<Type> {
/**
* 主动
*/
ACTIVE(1, "主动"),
ACTIVE("ACTIVE", "主动"),
/**
* 被动
*/
PASSIVE(2, "被动");
PASSIVE("PASSIVE", "被动");
private final int value;
private final String value;
private final String description;
Type(int value, String description) {
Type(String value, String description) {
this.value = value;
this.description = description;
}
@Override
public Integer getValue() {
public String getValue() {
return value;
}

View File

@ -120,29 +120,29 @@ public class Param extends BaseParam {
}
}
public enum CompareType implements IEnum<Integer>, IState<CompareType> {
public enum CompareType implements IEnum<String>, IState<CompareType> {
/**
* 大于
*/
GT(1, ">"),
GT("GT", "GT"),
/**
* 小于
*/
LT(2, "<"),
LT("LT", "LT"),
/**
* 等于
*/
EQ(3, "=");
private final int value;
EQ("EQ", "EQ");
private final String value;
private final String description;
CompareType(int value, String description) {
CompareType(String value, String description) {
this.value = value;
this.description = description;
}
@Override
public Integer getValue() {
public String getValue() {
return value;
}

View File

@ -2,6 +2,13 @@ package com.zsc.edu.gateway.modules.iot.tsl.repo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zsc.edu.gateway.modules.iot.tsl.entity.EventLog;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface EventLogRepository extends BaseMapper<EventLog> {
@Select("select * from iot_event_log where client_id=#{clientId}")
List<EventLog> selectByClientId(@Param("clientId") String clientId);
}

View File

@ -24,4 +24,7 @@ public interface EventRepository extends BaseMapper<Event> {
// IPage<Event> page(Page<Event> page, @Param("query") EventQuery query);
Event selectById(@Param("id") Long id);
List<Event> selectByProductId(@Param("productId") Long id);
}

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zsc.edu.gateway.modules.iot.tsl.repo.EventLogRepository">
<select id="selectList" resultType="com.zsc.edu.gateway.modules.iot.tsl.entity.EventLog">
select iel.*, ird.content as recordDataContent, ird.client_id as clientId, ird.record_time as recordTime
from iot_event_log iel,
iot_record_data ird
where iel.record_id = ird.id
</select>
</mapper>

View File

@ -21,6 +21,8 @@
<result column="foreign_id" property="foreignId"/>
<result column="name" property="name"/>
<result column="remark" property="remark"/>
<result column="compare_type" property="compareType"/>
<result column="default_value" property="defaultValue"/>
</collection>
</resultMap>
@ -38,4 +40,20 @@
and ip.foreign_type = 1
where e.id = #{id}
</select>
<select id="selectByProductId" resultMap="EventMap">
select e.*,
ip.id as param_id,
ip.data_type as param_data_type,
ip.uint as param_uint,
ip.type as param_type,
ip.identifier as param_identifier,
ip.name as param_name,
ip.remark as param_remark,
ip.compare_type as param_compare_type,
ip."default_value " as param_default_value
from iot_event e
left join iot_param ip on e.id = ip.foreign_id
and ip.foreign_type = 1
where e.product_id = #{productId}
</select>
</mapper>