refactor(iot): 重构事件相关代码

- 将 Event 类中的 outputs 字段类型从 List<ParamDto> 改为 List<Param>
- 更新 EventMapper.xml 中的 resultMap 配置
- 修改 RecordDataServiceImpl 中的 recordData 方法,增加打印日志
-移除 TemperatureExceededEventListener 中的 @EventListener 注解
This commit is contained in:
zhuangtianxiang 2025-03-13 17:16:38 +08:00
parent e730c51be5
commit 8746fea3cc
4 changed files with 15 additions and 31 deletions

View File

@ -27,7 +27,6 @@ public class TemperatureExceededEventListener implements ApplicationListener<Tem
@Resource
private EventRepository eventRepository;
@EventListener
@Override
public void onApplicationEvent(TemperatureExceededEvent event) {
RecordData recordData = event.getRecordData();

View File

@ -23,6 +23,7 @@ import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
@ -43,6 +44,7 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
@Resource
private final ParamMapper paramMapper;
@Override
@Transactional
public RecordData recordData(String clientId, JSONObject data) {
RecordData recordData = new RecordData();
recordData.setClientId(clientId);
@ -88,29 +90,32 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
public void processRecordData(RecordData recordData) {
// 根据 clientId 查询设备信息
Device device = deviceRepository.findByClientId(recordData.getClientId());
if (device == null) {
return; // 如果设备不存在直接返回
}
// 获取产品下的所有事件
List<Event> events = eventRepository.selectByProductId(device.getProductId());
if (events.isEmpty()) {
return; // 如果没有事件直接返回
}
// 遍历每个事件
for (Event event : events) {
// 遍历事件的 outputs 列表中的每个参数
event.getOutputs().forEach(param -> {
processParam(recordData, param, event);
});
event.getOutputs().forEach(param -> processParam(recordData, param, event));
}
}
private void processParam(RecordData recordData, ParamDto paramDto, Event event) {
Param param = paramMapper.toEntity(paramDto);
private void processParam(RecordData recordData, Param param, Event event) {
// 获取参数的默认值和比较类型
Double defaultValue = param.getDefaultValue();
String identifier = param.getIdentifier();
// 检查 recordData 的内容中是否包含该参数的标识符
if (recordData.getContent().get(identifier) != null) {
Object valueObj = recordData.getContent().get(identifier);
if (valueObj != null) {
// 获取参数的实际值
double value = Double.parseDouble(recordData.getContent().get(identifier).toString());
double value = Double.parseDouble(valueObj.toString());
Param.CompareType compareType = param.getCompareType();
// 根据比较类型进行不同的处理

View File

@ -39,7 +39,7 @@ public class Event extends BaseParam {
* 服务输出的参数
*/
@TableField(exist = false)
private List<ParamDto> outputs;
private List<Param> outputs;
public enum Type implements IEnum<String>, IState<Type> {
/**

View File

@ -26,26 +26,6 @@
</collection>
</resultMap>
<resultMap id="EventMapper" type="com.zsc.edu.gateway.modules.iot.tsl.entity.Event">
<id column="id" property="id"/>
<result column="product_id" property="productId"/>
<result column="type" property="type"/>
<result column="identifier" property="identifier"/>
<result column="name" property="name"/>
<result column="remark" property="remark"/>
<collection property="outputs" ofType="com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto" autoMapping="true"
columnPrefix="param_">
<result column="data_type" property="dataType"/>
<result column="uint" property="uint"/>
<result column="type" property="type"/>
<result column="identifier" property="identifier"/>
<result column="name" property="name"/>
<result column="remark" property="remark"/>
<result column="compare_type" property="compareType"/>
<result column="default_value" property="defaultValue"/>
</collection>
</resultMap>
<select id="selectById" resultMap="EventMap">
select e.*,
ip.id as param_id,
@ -60,7 +40,7 @@
and ip.foreign_type = 1
where e.id = #{id}
</select>
<select id="selectByProductId" resultMap="EventMapper">
<select id="selectByProductId" resultMap="EventMap">
select e.*,
ip.id as param_id,
ip.data_type as param_data_type,