浏览代码

线程采集

985653680@qq.com 1 年之前
父节点
当前提交
a928394414

+ 1 - 1
src/main/java/com/gzlh/api/OpenApi.java

@@ -18,7 +18,7 @@ public class OpenApi {
 
     @RequestMapping("test")
     public ResultJson test(){
-        eventBus.handlerEvent("led.ready");
+        eventBus.start("test","LED_SHOW_CONTENT");
         return ResultJson.success();
     }
 }

+ 5 - 64
src/main/java/com/gzlh/event/EventBus.java

@@ -2,6 +2,7 @@ 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;
@@ -21,74 +22,14 @@ import java.util.List;
 @Slf4j
 public class EventBus {
     private List<EventDTO> eventDTOList;
-    @Resource
-    private LedFactory ledFactory;
-    @Resource
-    private RedFactory redFactory;
-    @Resource
-    WeighbridgeFactory weighbridgeFactory;
-
-    @Resource
-    private WeighbridgePropertiesConfig weighbridgePropertiesConfig;
-    @Resource
-    private RedPropertiesConfig redPropertiesConfig;
-    @Resource
-    private LedPropertiesConfig ledPropertiesConfig;
 
     public void setEventDTOList(List<EventDTO> eventDTOList) {
         this.eventDTOList = eventDTOList;
     }
 
-    /**
-     * 处理事件
-     *
-     * @param eventName 服务.事件名 比如led.ready
-     */
-    public void handlerEvent(String eventName) {
-        log.info("event:{}", eventName);
-        eventDTOList.stream().filter(eventDTO -> StrUtil.equals(eventName, eventDTO.getName()))
-                .findFirst().ifPresent(eventDTO -> {
-                    List<String> 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 start(String name,String eventName){
+        EventThread thread = new EventThread(name,eventName);
+        thread.setEventDTOList(eventDTOList);
+        thread.start();
     }
 }

+ 114 - 0
src/main/java/com/gzlh/event/EventThread.java

@@ -0,0 +1,114 @@
+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<EventDTO> 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<EventDTO> 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<String> 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结束");
+    }
+}

+ 3 - 2
src/main/java/com/gzlh/infrared/handler/impl/RedHandlerSoarDAM0404.java

@@ -48,8 +48,9 @@ public class RedHandlerSoarDAM0404 implements IRedHandler {
             //todo test msg
         }else if (StrUtil.equals(StrUtil.replace(FIRST_LINK_SIGNAL, " ", ""), content)){
             EventBus eventBus= SpringUtil.getBean(EventBus.class);
-            //入红外被挡住
-            eventBus.handlerEvent(ModuleEnum.INFRARED_MODULE.getModuleEn()+"."+ InfraredAction.IN_BREAK);
+//            //入红外被挡住
+//            eventBus.handlerEvent(ModuleEnum.INFRARED_MODULE.getModuleEn()+"."+ InfraredAction.IN_BREAK);
+            eventBus.start(ModuleEnum.INFRARED_MODULE.getModuleEn(),ModuleEnum.INFRARED_MODULE.getModuleEn()+"."+ InfraredAction.IN_BREAK);
         }
     }
 

+ 1 - 1
src/main/java/com/gzlh/weighbridge/handler/impl/CommonWeighbridgeHandler.java

@@ -62,7 +62,7 @@ public class CommonWeighbridgeHandler {
             if (averaWeight == 0 || weighbridgePropertiesConfig.getMinKg() > averaWeight) {
                 //todo read timeout
                 //读取超时
-                eventBus.handlerEvent(ModuleEnum.WEIGHBRIDGE_MODULE.getModuleEn() + "." + WeighbridgeEvent.TIMEOUT);
+                eventBus.start(ModuleEnum.WEIGHBRIDGE_MODULE.getModuleEn(),ModuleEnum.WEIGHBRIDGE_MODULE.getModuleEn() + "." + WeighbridgeEvent.TIMEOUT);
                 return;
             }
             isStart=false;