Files
FT542-WCS/ruoyi-admin/src/main/java/com/ruoyi/schedule/ScheduleThread.java
2026-01-07 13:30:08 +08:00

359 lines
14 KiB
Java

package com.ruoyi.schedule;
import HslCommunication.Core.Types.OperateResultExOne;
import com.ruoyi.common.domain.CustomThread;
import com.ruoyi.common.domain.PlcConnectInfo;
import com.ruoyi.device.cdz.domain.DeviceCdz;
import com.ruoyi.device.cdz.service.IDeviceCdzService;
import com.ruoyi.device.ddj.domain.MsgDdj;
import com.ruoyi.device.ddj.service.IMsgDdjService;
import com.ruoyi.device.srgv.domain.DeviceSrgv;
import com.ruoyi.device.srgv.service.IDeviceSrgvService;
import com.ruoyi.device.ssj.domain.DeviceSsj;
import com.ruoyi.device.ssj.service.IDeviceSsjService;
import com.ruoyi.device.tsj.domain.DeviceTsj;
import com.ruoyi.device.tsj.service.impl.DeviceTsjServiceImpl;
import com.ruoyi.schedule.plcRead.cdz.PlcCdzRead;
import com.ruoyi.schedule.plcRead.ddj.PlcDdjRead;
import com.ruoyi.schedule.plcRead.srgv.PlcSrgvRead;
import com.ruoyi.schedule.plcRead.ssj.PlcSsjRead;
import com.ruoyi.schedule.plcRead.tsj.PlcTsjRead;
import com.ruoyi.schedule.scheduleTask.cdz.CdzThread;
import com.ruoyi.schedule.scheduleTask.ddj.impl.Ddj1Thread;
import com.ruoyi.schedule.scheduleTask.monit.MonitoringThread;
import com.ruoyi.schedule.scheduleTask.srgv.impl.SRgv1Thread;
import com.ruoyi.schedule.scheduleTask.ssj.SsjThread;
import com.ruoyi.schedule.scheduleTask.tsj.TsjThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Component
@Slf4j
public class ScheduleThread {
public Map<String, CustomThread<?>> threadMap = new ConcurrentHashMap<>();
public Map<String, String> threadDescMap = new ConcurrentHashMap<>();
public Map<String, String> threadTypeMap = new ConcurrentHashMap<>();
public CustomThread<PlcSsjRead> plcSsjReadCustomThread;
public CustomThread<PlcCdzRead> plcCdzjReadCustomThread;
public CustomThread<PlcTsjRead> plcTsjReadCustomThread;
public CustomThread<PlcSrgvRead> plcSrgvReadCustomThread1;
public CustomThread<PlcSrgvRead> plcSrgvReadCustomThread2;
public CustomThread<PlcSrgvRead> plcSrgvReadCustomThread3;
public CustomThread<MonitoringThread> monitoringCustomThread;
public CustomThread<SsjThread> ssjThreadCustomThread;
public CustomThread<SRgv1Thread> sRgvThreadCustomThread;
public CustomThread<TsjThread> tsjThreadCustomThread;
public CustomThread<CdzThread> cdzThreadCustomThread;
@Autowired
private IDeviceSsjService deviceSsjService;
@Autowired
private IDeviceSrgvService deviceSrgvService;
@Autowired
private IDeviceCdzService deviceCdzService;
private boolean isRun = false;
private boolean isRun2 = false;
@Autowired
private DeviceTsjServiceImpl deviceTsjServiceImpl;
/**
* 项目初始化启动
*/
@PostConstruct
public void init() {
log.info("初始化调度服务");
//读取信息初始化
plcReadInit();
//调度初始化
scheduleInit();
startAllThread();
startAllThread2();
log.info("调度服务执行成功");
}
@PreDestroy
public void destroy() {
try {
for (String key : threadMap.keySet()) {
CustomThread<?> ct = threadMap.get(key);
if (ct != null && ct.getRunnable() != null) {
setRunnableIsRun(ct.getRunnable(), false);
}
}
for (String key : threadMap.keySet()) {
CustomThread<?> ct = threadMap.get(key);
if (ct != null && ct.getThread() != null) {
try {
ct.getThread().join(2000);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
}
}
} catch (Exception e) {
log.error("调度服务销毁时停止线程异常", e);
}
}
/**
* 调度初始化
*/
private void scheduleInit() {
//输送机线程
SsjThread ssjThread = new SsjThread();
ssjThreadCustomThread = new CustomThread<SsjThread>(new Thread(ssjThread, "ssjThreadCustomThread"), ssjThread);
//四向车线程
SRgv1Thread srgvThread = new SRgv1Thread();
sRgvThreadCustomThread = new CustomThread<>(new Thread(srgvThread, "sRgvThreadCustomThread"), srgvThread);
//提升机线程
TsjThread tsjThread = new TsjThread();
tsjThreadCustomThread = new CustomThread<>(new Thread(tsjThread, "tsjThreadCustomThread"), tsjThread);
//充电线程
CdzThread cdzThread = new CdzThread();
cdzThreadCustomThread = new CustomThread<>(new Thread(cdzThread, "cdzThreadCustomThread"), cdzThread);
refreshThreadMap();
}
/**
* plc读取初始化
*/
private void plcReadInit() {
//plc输送机读取线程
ConcurrentHashMap<String, DeviceSsj> ssjList = new ConcurrentHashMap<>();
//获取要取的输送机
List<DeviceSsj> deviceSsjs = deviceSsjService.selectDeviceSsjList(new DeviceSsj());
for (DeviceSsj deviceSsj : deviceSsjs) {
ssjList.put(deviceSsj.getDeviceCode(), deviceSsj);
}
PlcSsjRead plcSsjRead = new PlcSsjRead(ssjList, new PlcConnectInfo("192.168.10.100", 502));
Thread plcSsjReadThread = new Thread(plcSsjRead, "plcSsjReadCustomThread");
plcSsjReadCustomThread = new CustomThread<PlcSsjRead>(plcSsjReadThread, plcSsjRead);
//plc充电桩读取线程
ConcurrentHashMap<String, DeviceCdz> cdzList = new ConcurrentHashMap<>();
List<DeviceCdz> deviceCdzList = deviceCdzService.selectDeviceCdzList(new DeviceCdz());
for (DeviceCdz deviceCdz : deviceCdzList) {
cdzList.put(String.valueOf(deviceCdz.getId()), deviceCdz);
}
PlcCdzRead plcCdzRead = new PlcCdzRead(cdzList, new PlcConnectInfo("192.168.10.100", 502));
Thread plcCdzReadThread = new Thread(plcCdzRead, "plcCdzjReadCustomThread");
plcCdzjReadCustomThread = new CustomThread<PlcCdzRead>(plcCdzReadThread, plcCdzRead);
ConcurrentHashMap<String, DeviceTsj> tsjList = new ConcurrentHashMap<>();
//获取要取的提升机
List<DeviceTsj> tsjLists = deviceTsjServiceImpl.selectDeviceTsjList(new DeviceTsj());
for (DeviceTsj t : tsjLists) {
tsjList.put(t.getName(), t);
}
//plc提升机读取线程
PlcTsjRead plcTsjRead = new PlcTsjRead(tsjList, new PlcConnectInfo("192.168.10.100", 502));
Thread plcTsjReadThread = new Thread(plcTsjRead, "plcTsjReadCustomThread");
plcTsjReadCustomThread = new CustomThread<PlcTsjRead>(plcTsjReadThread, plcTsjRead);
ConcurrentHashMap<String, DeviceSrgv> srgvList = new ConcurrentHashMap<>();
List<DeviceSrgv> srgvs = deviceSrgvService.selectDeviceSrgvList(new DeviceSrgv());
for (DeviceSrgv srgv : srgvs) {
switch (srgv.getDeviceId()) {
case 1:
PlcSrgvRead plcSrgvRead1 = new PlcSrgvRead(srgv, new PlcConnectInfo(srgv.getIp(), srgv.getPort()));
Thread plcSrgvReadThread1 = new Thread(plcSrgvRead1, "plcSrgvReadCustomThread1");
plcSrgvReadCustomThread1 = new CustomThread<PlcSrgvRead>(plcSrgvReadThread1, plcSrgvRead1);
break;
case 2:
PlcSrgvRead plcSrgvRead2 = new PlcSrgvRead(srgv, new PlcConnectInfo(srgv.getIp(), srgv.getPort()));
Thread plcSrgvReadThread2 = new Thread(plcSrgvRead2, "plcSrgvReadCustomThread2");
plcSrgvReadCustomThread2 = new CustomThread<PlcSrgvRead>(plcSrgvReadThread2, plcSrgvRead2);
break;
}
srgvList.put(srgv.getDeviceId().toString(), srgv);
}
PlcSrgvRead.deviceSrgvMap = srgvList;
//监控线程
MonitoringThread monitoringThread = new MonitoringThread();
monitoringCustomThread = new CustomThread<MonitoringThread>(new Thread(monitoringThread, "monitoringCustomThread"), monitoringThread);
refreshThreadMap();
}
public void refreshThreadMap() {
registerThread("plcSsjReadCustomThread", plcSsjReadCustomThread, "PLC输送机读取线程", "read");
registerThread("plcCdzjReadCustomThread", plcCdzjReadCustomThread, "PLC充电桩读取线程", "read");
registerThread("plcTsjReadCustomThread", plcTsjReadCustomThread, "PLC提升机读取线程", "read");
registerThread("plcSrgvReadCustomThread1", plcSrgvReadCustomThread1, "PLC四向车1读取线程", "read");
registerThread("plcSrgvReadCustomThread2", plcSrgvReadCustomThread2, "PLC四向车2读取线程", "read");
registerThread("monitoringCustomThread", monitoringCustomThread, "监控线程", "monitor");
registerThread("ssjThreadCustomThread", ssjThreadCustomThread, "输送机调度线程", "schedule");
registerThread("sRgvThreadCustomThread", sRgvThreadCustomThread, "四向车调度线程", "schedule");
registerThread("tsjThreadCustomThread", tsjThreadCustomThread, "提升机调度线程", "schedule");
registerThread("cdzThreadCustomThread", cdzThreadCustomThread, "充电调度线程", "schedule");
}
private void registerThread(String key, CustomThread<?> thread, String desc, String type) {
if (thread != null) {
threadMap.put(key, thread);
threadDescMap.put(key, desc);
threadTypeMap.put(key, type);
}
}
/**
* 开始线程 读取
*/
public void startAllThread() {
plcSsjReadCustomThread.getThread().start();
plcSrgvReadCustomThread1.getThread().start();
plcSrgvReadCustomThread2.getThread().start();
plcTsjReadCustomThread.getThread().start();
monitoringCustomThread.getThread().start();
//充电桩读取线程
plcCdzjReadCustomThread.getThread().start();
isRun = true;
}
/**
* 开启线程2 调度
*/
public void startAllThread2() {
ssjThreadCustomThread.thread.start();
tsjThreadCustomThread.thread.start();
sRgvThreadCustomThread.thread.start();
cdzThreadCustomThread.thread.start();
isRun2 = true;
}
/**
* 停止线程读取
*/
public void stopAllThread() {
plcSsjReadCustomThread.runnable.isRun = false;
plcSrgvReadCustomThread1.runnable.isRun = false;
plcSrgvReadCustomThread2.runnable.isRun = false;
plcTsjReadCustomThread.runnable.isRun = false;
monitoringCustomThread.runnable.isRun = false;
plcReadInit();
isRun = false;
}
/**
* 停止线程调度
*/
public void stopAllThread2() {
ssjThreadCustomThread.runnable.isRun = false;
tsjThreadCustomThread.runnable.isRun = false;
sRgvThreadCustomThread.runnable.isRun = false;
scheduleInit();
isRun2 = false;
}
/**
* 获取调度状态
*/
public HashMap<String, Boolean> getScheduleStatus() {
HashMap<String, Boolean> map = new HashMap<>();
map.put("isRun", isRun);
map.put("isRun2", isRun2);
return map;
}
/**
* 获取所有受管线程状态
*/
public List<Map<String, Object>> getAllThreadStatus() {
List<Map<String, Object>> list = new ArrayList<>();
for (String key : threadDescMap.keySet()) {
CustomThread<?> ct = threadMap.get(key);
Map<String, Object> info = new HashMap<>();
info.put("key", key);
info.put("name", threadDescMap.get(key));
info.put("type", threadTypeMap.get(key));
boolean alive = false;
boolean isRun = false;
if (ct != null && ct.getRunnable() != null) {
try {
Field field = ct.getRunnable().getClass().getField("isRun");
isRun = field.getBoolean(ct.getRunnable());
} catch (Exception e) {
log.error("获取线程[{}]isRun状态异常", key);
}
}
info.put("isRun", isRun);
if (ct != null && ct.getThread() != null) {
alive = ct.getThread().isAlive();
}
info.put("alive", alive);
list.add(info);
}
return list;
}
/**
* 启动指定线程
*/
public synchronized void startThread(String key) {
CustomThread<?> ct = threadMap.get(key);
if (ct == null) {
throw new RuntimeException("未知线程: " + key);
}
if (ct.getThread() != null && ct.getThread().isAlive()) {
log.info("线程 {} 已经在运行中", key);
return;
}
// 设置Runnable isRun = true
setRunnableIsRun(ct.getRunnable(), true);
// 创建新线程
Thread newThread = new Thread((Runnable) ct.getRunnable(), key);
ct.setThread(newThread);
newThread.start();
log.info("线程 {} 已启动", key);
}
/**
* 停止指定线程
*/
public synchronized void stopThread(String key) {
CustomThread<?> ct = threadMap.get(key);
if (ct == null) {
throw new RuntimeException("未知线程: " + key);
}
// 设置Runnable isRun = false
setRunnableIsRun(ct.getRunnable(), false);
log.info("线程 {} 停止指令已下发", key);
}
private void setRunnableIsRun(Object runnable, boolean isRun) {
try {
Field field = runnable.getClass().getField("isRun"); // 使用getField获取public字段
field.setBoolean(runnable, isRun);
} catch (NoSuchFieldException e) {
// 尝试查找父类或非public字段? 目前都是public
log.error("无法找到isRun字段: {}", runnable.getClass().getName());
} catch (IllegalAccessException e) {
log.error("无法访问isRun字段: {}", runnable.getClass().getName());
}
}
}