| package com.zy.asrs.wcs.rcs; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
| import com.zy.asrs.wcs.core.MainProcess; | 
| import com.zy.asrs.wcs.rcs.cache.MessageQueue; | 
| import com.zy.asrs.wcs.rcs.cache.SlaveConnection; | 
| import com.zy.asrs.wcs.rcs.model.enums.SlaveType; | 
| import com.zy.asrs.wcs.rcs.thread.ThreadHandler; | 
| import com.zy.asrs.wcs.core.utils.RedisUtil; | 
| import com.zy.asrs.wcs.rcs.entity.Device; | 
| import com.zy.asrs.wcs.rcs.entity.DevicePlc; | 
| import com.zy.asrs.wcs.rcs.entity.DeviceType; | 
| import com.zy.asrs.wcs.rcs.service.DevicePlcService; | 
| import com.zy.asrs.wcs.rcs.service.DeviceService; | 
| import com.zy.asrs.wcs.rcs.service.DeviceTypeService; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.scheduling.annotation.Async; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import javax.annotation.PostConstruct; | 
| import javax.annotation.PreDestroy; | 
| import java.lang.reflect.Constructor; | 
| import java.util.List; | 
|   | 
| /** | 
|  * Created by vincent on 2020/8/4 | 
|  */ | 
| @Slf4j | 
| @Component | 
| public class ServerBootstrap { | 
|   | 
|     @Autowired | 
|     private MainProcess mainProcess; | 
|     @Autowired | 
|     private RedisUtil redisUtil; | 
|     @Autowired | 
|     private DeviceService deviceService; | 
|     @Autowired | 
|     private DeviceTypeService deviceTypeService; | 
|     @Autowired | 
|     private DevicePlcService devicePlcService; | 
|   | 
|     @PostConstruct | 
|     @Async | 
|     public void init() throws InterruptedException { | 
|         News.info("核心控制层开始初始化..............................................."); | 
|         Thread.sleep(2000); | 
|         // 初始化消息队列 | 
|         initMq(); | 
|         // 初始化下位机线程 | 
|         initThread(); | 
|         // 开始主流程进程 | 
|         mainProcess.start(); | 
|         News.info("核心控制层已启动..............................................."); | 
|     } | 
|   | 
|     private void initMq(){ | 
|         // 初始化设备mq | 
|         for (Device device : deviceService.list()) { | 
|             DeviceType type = deviceTypeService.getById(device.getDeviceType()); | 
|             SlaveType slaveType = SlaveType.findInstance(type.getFlag()); | 
|             if (slaveType != null) { | 
|                 MessageQueue.init(slaveType, device.getId().intValue()); | 
|             } | 
|         } | 
|     } | 
|   | 
|     private void initThread(){ | 
|         for (DeviceType type : deviceTypeService.list()) { | 
|             List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>() | 
|                     .eq(Device::getDeviceType, type.getId()) | 
|                     .eq(Device::getStatus, 1) | 
|             ); | 
|             if (list.isEmpty()) { | 
|                 continue; | 
|             } | 
|   | 
|             News.info("初始化{}线程...................................................", type.getName()); | 
|             for (Device device : list) { | 
|                 DevicePlc devicePlc = devicePlcService.getById(device.getDevicePlc()); | 
|                 if (devicePlc == null) { | 
|                     continue; | 
|                 } | 
|   | 
|                 try { | 
|                     Class<?> clazz = Class.forName("com.zy.asrs.wcs.rcs.thread.impl." + devicePlc.getFlag()); | 
|                     Constructor<?> constructor = clazz.getConstructor(Device.class, RedisUtil.class); | 
|                     Object instance = constructor.newInstance(device, redisUtil); | 
|                     new Thread((Runnable) instance).start(); | 
|                     SlaveConnection.put(SlaveType.findInstance(type.getFlag()), device.getId().intValue(), (ThreadHandler) instance); | 
|                 } catch (Exception e) { | 
|                     e.printStackTrace(); | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|   | 
|     @PreDestroy | 
|     public void destroy() { | 
|     } | 
|   | 
|   | 
| } |