package com.tievd.jyz.handler.alg;
|
|
import cn.hutool.core.util.ObjectUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.tievd.jyz.mqtt.dto.EventInfoDTO;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.context.ApplicationContext;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.PostConstruct;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 算法检测处理器分配类
|
* @author timi
|
*/
|
@Slf4j
|
@Component
|
public class AlgHandleManager {
|
|
private final Map<String, AlgHandleInterface> handlerMap = new HashMap<>();
|
@Autowired
|
public ApplicationContext applicationContext;
|
|
@PostConstruct
|
public void init(){
|
Map<String,AlgHandleInterface> map = applicationContext.getBeansOfType(AlgHandleInterface.class);
|
//遍历该接口的所有实现,将其放入map中
|
for(AlgHandleInterface algHandleInterface:map.values()){
|
String[] algTagArr = algHandleInterface.getAlgTag();
|
for(String algTag:algTagArr){
|
handlerMap.put(algTag,algHandleInterface);
|
}
|
}
|
}
|
|
/**
|
* 事件处理
|
* @param eventInfoDTO
|
* @param sn
|
*/
|
public void eventHandle(EventInfoDTO<JSONObject> eventInfoDTO,String sn,String time){
|
AlgHandleInterface handler = handlerMap.get(eventInfoDTO.getAlgCode());
|
if(ObjectUtil.isNull(handler)){
|
log.error("不存在算法相关的事件处理类,algCode:{}",eventInfoDTO.getAlgCode());
|
return;
|
}
|
handler.handle(eventInfoDTO,sn,time);
|
}
|
|
}
|