package com.gzlh.event; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import com.gzlh.config.ModuleEnum; import com.gzlh.dto.EventDTO; import com.gzlh.infrared.config.RedPropertiesConfig; import com.gzlh.infrared.factory.RedFactory; import com.gzlh.led.factory.LedFactory; import com.gzlh.led.properties.LedPropertiesConfig; import com.gzlh.weighbridge.config.WeighbridgePropertiesConfig; import com.gzlh.weighbridge.factory.WeighbridgeFactory; import lombok.extern.slf4j.Slf4j; import javax.annotation.Resource; import java.util.List; @Slf4j public class EventThread extends Thread{ private List eventDTOList; private String eventName; @Resource private LedFactory ledFactory; @Resource private RedFactory redFactory; @Resource WeighbridgeFactory weighbridgeFactory; @Resource private WeighbridgePropertiesConfig weighbridgePropertiesConfig; @Resource private RedPropertiesConfig redPropertiesConfig; @Resource private LedPropertiesConfig ledPropertiesConfig; public EventThread() { } public EventThread(String name, String eventName) { this.setName(name+"-"+ RandomUtil.randomNumber()); this.eventName=eventName; } public void setEventDTOList(List eventDTOList) { this.eventDTOList = eventDTOList; } public void setEventName(String eventName) { this.eventName = eventName; } /** * 处理事件 * * @param eventName 服务.事件名 比如led.ready */ public void handlerEvent(String eventName) { log.info("event:{}", eventName); System.out.println(eventName); eventDTOList.stream().filter(eventDTO -> StrUtil.equals(eventName, eventDTO.getName())) .findFirst().ifPresent(eventDTO -> { System.out.println(eventDTO); List actionList = eventDTO.getActionList().getAction(); //依次执行动作 actionList.forEach(action -> { String actionPrefix = StrUtil.subBefore(action, ".", true).toLowerCase(); String actionCommand = StrUtil.subAfter(action, ".", true).toUpperCase(); if (StrUtil.equals(actionPrefix, ModuleEnum.INFRARED_MODULE.getModuleEn())) { redFactory.handler(redPropertiesConfig.getBrand()).handlerAction(actionCommand); } else if (StrUtil.equals(actionPrefix, ModuleEnum.LED_MODULE.getModuleEn())) { ledFactory.handler(ledPropertiesConfig.getBrand()).handlerAction(actionCommand); } else if (StrUtil.equals(actionPrefix, ModuleEnum.WEIGHBRIDGE_MODULE.getModuleEn())) { weighbridgeFactory.handler(weighbridgePropertiesConfig.getBrand()).handlerAction(actionCommand); } else if (StrUtil.equals(actionPrefix, ModuleEnum.CENTER_MODULE.getModuleEn())) { handlerAction(actionCommand); } ThreadUtil.sleep(250); }); }); } /** * 调度中心命令处理 * * @param action */ private void handlerAction(String action) { if (StrUtil.contains(action, CenterActionCommand.SLEEP_COMMAND.getCommand())) { //休眠 handlerSleep(action); } } /** * 调度中心休眠 * * @param action */ private void handlerSleep(String action) { String time = StrUtil.subAfter(StrUtil.subAfter(action, "(", true), ")", true); if (NumberUtil.isInteger(time)) { ThreadUtil.sleep(Long.parseLong(time)); } } public void run(){ System.out.println("thread开始"); handlerEvent(eventName); System.out.println("thread结束"); } }