feat(iot): 增加事件记录查询功能并优化相关逻辑
- 在 EventController 中添加事件记录查询接口 - 在 EventLogRepository 中实现根据 ClientId 查询事件记录的方法 - 在 RecordDataServiceImpl 中优化数据处理逻辑 - 更新相关实体类和 mapper 文件以支持新功能
This commit is contained in:
parent
0835e859d0
commit
fdfa843603
@ -26,4 +26,6 @@ public interface DeviceRepository extends BaseMapper<Device> {
|
|||||||
@Param("status") Device.Status status,
|
@Param("status") Device.Status status,
|
||||||
@Param("online") Boolean online);
|
@Param("online") Boolean online);
|
||||||
|
|
||||||
|
@Select("select * from iot_device where client_id=#{clientId}")
|
||||||
|
Device findByClientId(@Param("clientId") String clientId);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.zsc.edu.gateway.modules.iot.record.service.impl;
|
package com.zsc.edu.gateway.modules.iot.record.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSON;
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
@ -44,7 +45,7 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
|
|||||||
recordData.setClientId(clientId);
|
recordData.setClientId(clientId);
|
||||||
recordData.setContent(data);
|
recordData.setContent(data);
|
||||||
recordData.setRecordTime(LocalDateTime.now());
|
recordData.setRecordTime(LocalDateTime.now());
|
||||||
baseMapper.insert(recordData);
|
this.save(recordData);
|
||||||
processRecordData(recordData);
|
processRecordData(recordData);
|
||||||
return recordData;
|
return recordData;
|
||||||
}
|
}
|
||||||
@ -52,7 +53,7 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
|
|||||||
@Override
|
@Override
|
||||||
public RecordDataStatusVo recordDataStatus() {
|
public RecordDataStatusVo recordDataStatus() {
|
||||||
RecordDataStatusVo recordDataStatusVo = new RecordDataStatusVo();
|
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)));
|
recordDataStatusVo.setDataCount(baseMapper.selectCount(new LambdaQueryWrapper<RecordData>().isNotNull(RecordData::getContent)));
|
||||||
return recordDataStatusVo;
|
return recordDataStatusVo;
|
||||||
}
|
}
|
||||||
@ -83,10 +84,10 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
|
|||||||
|
|
||||||
public void processRecordData(RecordData recordData) {
|
public void processRecordData(RecordData recordData) {
|
||||||
// 根据 clientId 查询设备信息
|
// 根据 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) {
|
for (Event event : events) {
|
||||||
@ -100,42 +101,38 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
|
|||||||
private void processParam(RecordData recordData, Param param, Event event) {
|
private void processParam(RecordData recordData, Param param, Event event) {
|
||||||
// 获取参数的默认值和比较类型
|
// 获取参数的默认值和比较类型
|
||||||
Double defaultValue = param.getDefaultValue();
|
Double defaultValue = param.getDefaultValue();
|
||||||
Param.CompareType compareType = param.getCompareType();
|
|
||||||
String identifier = param.getIdentifier();
|
String identifier = param.getIdentifier();
|
||||||
|
|
||||||
// 检查 recordData 的内容中是否包含该参数的标识符
|
// 检查 recordData 的内容中是否包含该参数的标识符
|
||||||
if (recordData.getContent().get(identifier) != null) {
|
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) {
|
switch (compareType) {
|
||||||
case GT:
|
case GT:
|
||||||
if (value > defaultValue) {
|
if (value > defaultValue) {
|
||||||
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
|
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LT:
|
case LT:
|
||||||
if (value < defaultValue) {
|
if (value < defaultValue) {
|
||||||
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
|
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case EQ:
|
case EQ:
|
||||||
if (value == defaultValue) {
|
if (value == defaultValue) {
|
||||||
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
|
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// 处理未知的比较类型
|
// 处理未知的比较类型
|
||||||
System.err.println("Unknown compare type: " + compareType);
|
System.err.println("Unknown compare type: " + compareType);
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
// 处理数值转换异常
|
|
||||||
System.err.println("Failed to parse value for identifier: " + identifier + ". Error: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package com.zsc.edu.gateway.modules.iot.tsl.controller;
|
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.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
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.dto.EventDto;
|
||||||
import com.zsc.edu.gateway.modules.iot.tsl.entity.Event;
|
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.query.EventQuery;
|
||||||
|
import com.zsc.edu.gateway.modules.iot.tsl.repo.EventLogRepository;
|
||||||
import com.zsc.edu.gateway.modules.iot.tsl.service.EventService;
|
import com.zsc.edu.gateway.modules.iot.tsl.service.EventService;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
@ -21,6 +24,8 @@ public class EventController {
|
|||||||
|
|
||||||
private final EventService service;
|
private final EventService service;
|
||||||
|
|
||||||
|
private final EventLogRepository eventLogRepository;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建事件
|
* 创建事件
|
||||||
@ -59,6 +64,19 @@ public class EventController {
|
|||||||
return service.page(page, query.wrapper());
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除事件
|
* 删除事件
|
||||||
|
@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
|
|||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
import com.zsc.edu.gateway.common.enums.IState;
|
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.product.entity.Product;
|
||||||
|
import com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
@ -40,26 +41,26 @@ public class Event extends BaseParam {
|
|||||||
@TableField(exist = false)
|
@TableField(exist = false)
|
||||||
private List<Param> outputs;
|
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;
|
private final String description;
|
||||||
|
|
||||||
Type(int value, String description) {
|
Type(String value, String description) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer getValue() {
|
public String getValue() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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, "=");
|
EQ("EQ", "EQ");
|
||||||
private final int value;
|
private final String value;
|
||||||
private final String description;
|
private final String description;
|
||||||
|
|
||||||
CompareType(int value, String description) {
|
CompareType(String value, String description) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer getValue() {
|
public String getValue() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,13 @@ package com.zsc.edu.gateway.modules.iot.tsl.repo;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.zsc.edu.gateway.modules.iot.tsl.entity.EventLog;
|
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> {
|
public interface EventLogRepository extends BaseMapper<EventLog> {
|
||||||
|
|
||||||
|
@Select("select * from iot_event_log where client_id=#{clientId}")
|
||||||
|
List<EventLog> selectByClientId(@Param("clientId") String clientId);
|
||||||
}
|
}
|
||||||
|
@ -24,4 +24,7 @@ public interface EventRepository extends BaseMapper<Event> {
|
|||||||
// IPage<Event> page(Page<Event> page, @Param("query") EventQuery query);
|
// IPage<Event> page(Page<Event> page, @Param("query") EventQuery query);
|
||||||
|
|
||||||
Event selectById(@Param("id") Long id);
|
Event selectById(@Param("id") Long id);
|
||||||
|
|
||||||
|
|
||||||
|
List<Event> selectByProductId(@Param("productId") Long id);
|
||||||
}
|
}
|
||||||
|
12
src/main/resources/mappers/iot/EventLogMapper.xml
Normal file
12
src/main/resources/mappers/iot/EventLogMapper.xml
Normal 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>
|
@ -21,6 +21,8 @@
|
|||||||
<result column="foreign_id" property="foreignId"/>
|
<result column="foreign_id" property="foreignId"/>
|
||||||
<result column="name" property="name"/>
|
<result column="name" property="name"/>
|
||||||
<result column="remark" property="remark"/>
|
<result column="remark" property="remark"/>
|
||||||
|
<result column="compare_type" property="compareType"/>
|
||||||
|
<result column="default_value" property="defaultValue"/>
|
||||||
</collection>
|
</collection>
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
@ -38,4 +40,20 @@
|
|||||||
and ip.foreign_type = 1
|
and ip.foreign_type = 1
|
||||||
where e.id = #{id}
|
where e.id = #{id}
|
||||||
</select>
|
</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>
|
</mapper>
|
Loading…
Reference in New Issue
Block a user