#
Junjie
2025-04-17 035c1e94569a2c043f232f3452d25ad2e51bcf23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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() {
    }
 
 
}