feat(iot): 添加温度异常事件日志功能
- 在 RecordDataServiceImpl 中增加事件名称参数 - 更新 TemperatureExceededEvent 类,添加事件名称字段 - 新增 EventLog 类用于记录事件日志- 创建 EventLogRepository 用于存储事件日志 - 实现 TemperatureExceededEventListener 监听温度异常事件并记录日志
This commit is contained in:
parent
640e7f5286
commit
0835e859d0
@ -12,10 +12,12 @@ import org.springframework.context.ApplicationEvent;
|
|||||||
public class TemperatureExceededEvent extends ApplicationEvent {
|
public class TemperatureExceededEvent extends ApplicationEvent {
|
||||||
private final RecordData recordData;
|
private final RecordData recordData;
|
||||||
private final double reducedParameter;
|
private final double reducedParameter;
|
||||||
|
private final String eventName;
|
||||||
|
|
||||||
public TemperatureExceededEvent(Object source, RecordData recordData, double reducedParameter) {
|
public TemperatureExceededEvent(Object source, RecordData recordData, double reducedParameter, String eventName) {
|
||||||
super(source);
|
super(source);
|
||||||
this.recordData = recordData;
|
this.recordData = recordData;
|
||||||
this.reducedParameter = reducedParameter;
|
this.reducedParameter = reducedParameter;
|
||||||
|
this.eventName = eventName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,12 +92,12 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
|
|||||||
for (Event event : events) {
|
for (Event event : events) {
|
||||||
// 遍历事件的 outputs 列表中的每个参数
|
// 遍历事件的 outputs 列表中的每个参数
|
||||||
event.getOutputs().forEach(param -> {
|
event.getOutputs().forEach(param -> {
|
||||||
processParam(recordData, param);
|
processParam(recordData, param, event);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processParam(RecordData recordData, Param param) {
|
private void processParam(RecordData recordData, Param param, Event event) {
|
||||||
// 获取参数的默认值和比较类型
|
// 获取参数的默认值和比较类型
|
||||||
Double defaultValue = param.getDefaultValue();
|
Double defaultValue = param.getDefaultValue();
|
||||||
Param.CompareType compareType = param.getCompareType();
|
Param.CompareType compareType = param.getCompareType();
|
||||||
@ -113,17 +113,17 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
|
|||||||
switch (compareType) {
|
switch (compareType) {
|
||||||
case GT:
|
case GT:
|
||||||
if (value > defaultValue) {
|
if (value > defaultValue) {
|
||||||
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value));
|
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));
|
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));
|
eventPublisher.publishEvent(new TemperatureExceededEvent(this, recordData, value, event.getName()));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.zsc.edu.gateway.modules.iot.record.service.impl;
|
||||||
|
|
||||||
|
import com.zsc.edu.gateway.modules.iot.device.repo.DeviceRepository;
|
||||||
|
import com.zsc.edu.gateway.modules.iot.record.entity.RecordData;
|
||||||
|
import com.zsc.edu.gateway.modules.iot.record.entity.TemperatureExceededEvent;
|
||||||
|
import com.zsc.edu.gateway.modules.iot.tsl.entity.EventLog;
|
||||||
|
import com.zsc.edu.gateway.modules.iot.tsl.repo.EventLogRepository;
|
||||||
|
import com.zsc.edu.gateway.modules.iot.tsl.repo.EventRepository;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhuang
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class TemperatureExceededEventListener implements ApplicationListener<TemperatureExceededEvent> {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private EventLogRepository eventLogRepository;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DeviceRepository deviceRepository;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private EventRepository eventRepository;
|
||||||
|
|
||||||
|
@EventListener
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(TemperatureExceededEvent event) {
|
||||||
|
RecordData recordData = event.getRecordData();
|
||||||
|
System.out.println(event.getEventName() + "数值如下:" + event.getReducedParameter());
|
||||||
|
// 创建 EventLog 对象
|
||||||
|
EventLog eventLog = new EventLog();
|
||||||
|
eventLog.setName(event.getEventName());
|
||||||
|
eventLog.setRecordId(recordData.getId());
|
||||||
|
eventLog.setTriggerTime(LocalDateTime.now());
|
||||||
|
eventLog.setIsRead(false);
|
||||||
|
// 保存 EventLog 对象到数据库
|
||||||
|
eventLogRepository.insert(eventLog);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.zsc.edu.gateway.modules.iot.tsl.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.zsc.edu.gateway.modules.iot.record.entity.RecordData;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhuang
|
||||||
|
*/
|
||||||
|
@Setter
|
||||||
|
@Getter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@TableName("iot_event_log")
|
||||||
|
public class EventLog {
|
||||||
|
/**
|
||||||
|
* 序列化主键(有数据库提供,非自增)
|
||||||
|
*/
|
||||||
|
@TableId(type = IdType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 名称
|
||||||
|
*/
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关联上报数据id
|
||||||
|
*/
|
||||||
|
private String recordId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关联上报数据
|
||||||
|
*/
|
||||||
|
@TableField(exist = false)
|
||||||
|
private RecordData recordData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 触发时间
|
||||||
|
*/
|
||||||
|
private LocalDateTime triggerTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否已读
|
||||||
|
*/
|
||||||
|
private Boolean isRead;
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
public interface EventLogRepository extends BaseMapper<EventLog> {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user