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;
|
}
|
}
|