huang
2025-05-22 16b32f4511aa90b95b13a15751850cf6db0829e2
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.mes.job;
 
import com.mes.common.S7objectMachine;
import com.mes.device.PlcDevice;
import com.mes.md.service.RotatingRackService;
import com.mes.plcTaskThread.PlcRotingRack;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * PlcRotingRack管理器
 * 负责创建和管理多个PlcRotingRack实例
 */
@Slf4j
@Component
public class PlcRotingRackManager {
 
    @Resource
    private RotatingRackService rotatingRackService;
    
    @Resource
    private List<PlcDevice> plcDevices;
    
    @Resource
    private Map<String, S7objectMachine> s7objectMachines;
    
    private Map<String, PlcRotingRack> rackThreads = new HashMap<>();
    
    /**
     * 系统启动时自动初始化
     */
    @PostConstruct
    public void init() {
        log.info("初始化旋转料架PLC管理器");
        
        // 遍历所有配置的设备
        for (PlcDevice device : plcDevices) {
            // 只初始化旋转料架类型的设备
            if (device.getName().startsWith("R")) {
                startDeviceThread(device.getName());
            }
        }
    }
    
    /**
     * 启动指定设备的PLC线程
     * @param deviceName 设备名称
     * @return 成功返回true,失败返回false
     */
    public boolean startDeviceThread(String deviceName) {
        // 检查线程是否已经存在
        if (rackThreads.containsKey(deviceName)) {
            log.warn("设备{}的PLC线程已存在", deviceName);
            return false;
        }
        
        // 获取设备对应的S7objectMachine
        S7objectMachine s7machine = s7objectMachines.get(deviceName);
        if (s7machine == null) {
            log.error("找不到设备{}的PLC通信对象", deviceName);
            return false;
        }
        
        try {
            // 创建新的PlcRotingRack实例
            PlcRotingRack rackThread = new PlcRotingRack(rotatingRackService);
            // 设置S7objectMachine
            rackThread.setS7objectMachine(s7machine);
            // 设置设备名称
            rackThread.setDeviceName(deviceName);
            
            // 启动线程
            rackThread.start();
            
            // 添加到管理器
            rackThreads.put(deviceName, rackThread);
            
            log.info("成功启动设备{}的PLC线程", deviceName);
            return true;
        } catch (Exception e) {
            log.error("启动设备{}的PLC线程失败: {}", deviceName, e.getMessage(), e);
            return false;
        }
    }
    
    // /**
    //  * 停止指定设备的PLC线程
    //  * @param deviceName 设备名称
    //  * @return 成功返回true,失败返回false
    //  */
    // public boolean stopDeviceThread(String deviceName) {
    //     PlcRotingRack thread = rackThreads.get(deviceName);
    //     if (thread == null) {
    //         log.warn("设备{}的PLC线程不存在", deviceName);
    //         return false;
    //     }
        
    //     try {
    //         thread.interrupt();
    //         rackThreads.remove(deviceName);
    //         log.info("成功停止设备{}的PLC线程", deviceName);
    //         return true;
    //     } catch (Exception e) {
    //         log.error("停止设备{}的PLC线程失败: {}", deviceName, e.getMessage(), e);
    //         return false;
    //     }
    // }
    
    // /**
    //  * 重启指定设备的PLC线程
    //  * @param deviceName 设备名称
    //  * @return 成功返回true,失败返回false
    //  */
    // public boolean restartDeviceThread(String deviceName) {
    //     boolean stopped = stopDeviceThread(deviceName);
    //     if (!stopped) {
    //         return false;
    //     }
        
    //     return startDeviceThread(deviceName);
    // }
    
    /**
     * 获取所有正在运行的设备线程
     * @return 设备线程映射
     */
    public Map<String, PlcRotingRack> getAllThreads() {
        return rackThreads;
    }