Files
FT577-WCS/ruoyi-admin/src/main/java/com/ruoyi/schedule/ScheduleThread.java
2025-11-21 10:29:06 +08:00

193 lines
7.7 KiB
Java

package com.ruoyi.schedule;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.device.ddj.domain.MsgDdj;
import com.ruoyi.device.ddj.service.IMsgDdjService;
import com.ruoyi.device.ssj.domain.DeviceSsj;
import com.ruoyi.device.ssj.service.IDeviceSsjService;
import com.ruoyi.common.domain.PlcConnectInfo;
import com.ruoyi.schedule.plcRead.ddj.PlcDdjRead;
import com.ruoyi.schedule.plcRead.ssj.PlcSsjRead;
import com.ruoyi.common.domain.CustomThread;
import com.ruoyi.schedule.scheduleTask.ddj.Ddj1Thread;
import com.ruoyi.schedule.scheduleTask.monit.MonitoringThread;
import com.ruoyi.schedule.scheduleTask.ssj.floor1.Floor1BothWayInSsjThread;
import com.ruoyi.schedule.scheduleTask.ssj.floor1.Floor1BothWayOutSsjThread;
import com.ruoyi.schedule.scheduleTask.ssj.floor1.Floor1SingleSsjThread;
import com.ruoyi.schedule.scheduleTask.ssj.floor2.Floor2InOutSsjThread;
import com.ruoyi.schedule.scheduleTask.ssj.floor2.Floor2RoadWaySsjThread;
import com.ruoyi.schedule.scheduleTask.ssj.floor2.LineSsjThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@Component
@Slf4j
public class ScheduleThread {
public CustomThread<PlcSsjRead> plcSsjReadCustomThread;
public CustomThread<PlcDdjRead> plcDdj1ReadCustomThread;
public CustomThread<PlcDdjRead> plcDdj2ReadCustomThread;
public CustomThread<Floor1BothWayInSsjThread> floor1BothWayInSsjCustomThread;
public CustomThread<Floor1BothWayOutSsjThread> floor1BothWayOutSsjCustomThread;
public CustomThread<Floor1SingleSsjThread> floor1SingleSsjCustomThread;
public CustomThread<Floor2InOutSsjThread> floor2InSsjCustomThread;
public CustomThread<Floor2RoadWaySsjThread> floor2OutSsjCustomThread;
public CustomThread<LineSsjThread> lineSsjCustomThread;
public CustomThread<MonitoringThread> monitoringCustomThread;
public CustomThread<Ddj1Thread> ddj1CustomThread;
@Autowired
private IDeviceSsjService deviceSsjService;
@Autowired
private IMsgDdjService msgDdjService;
private boolean isRun = false;
private boolean isRun2 = false;
/**
* 项目初始化启动
*/
@PostConstruct
public void init() {
log.info("初始化调度服务");
//读取信息初始化
plcReadInit();
//调度初始化
scheduleInit();
log.info("调度服务执行成功");
}
/**
* 调度初始化
*/
private void scheduleInit() {
MonitoringThread monitoringThread = new MonitoringThread();
monitoringCustomThread = new CustomThread<MonitoringThread>(new Thread(monitoringThread), monitoringThread);
Floor1BothWayInSsjThread floor1BothWayInSsjThread = new Floor1BothWayInSsjThread();
floor1BothWayInSsjCustomThread = new CustomThread<Floor1BothWayInSsjThread>(new Thread(floor1BothWayInSsjThread), floor1BothWayInSsjThread);
Floor1BothWayOutSsjThread floor1BothWayOutSsjThread = new Floor1BothWayOutSsjThread();
floor1BothWayOutSsjCustomThread = new CustomThread<Floor1BothWayOutSsjThread>(new Thread(floor1BothWayOutSsjThread), floor1BothWayOutSsjThread);
Floor1SingleSsjThread floor1SingleSsjThread = new Floor1SingleSsjThread();
floor1SingleSsjCustomThread = new CustomThread<Floor1SingleSsjThread>(new Thread(floor1SingleSsjThread), floor1SingleSsjThread);
Floor2InOutSsjThread floor2InSsjThread = new Floor2InOutSsjThread();
floor2InSsjCustomThread = new CustomThread<Floor2InOutSsjThread>(new Thread(floor2InSsjThread), floor2InSsjThread);
LineSsjThread lineSsjThread = new LineSsjThread();
lineSsjCustomThread = new CustomThread<LineSsjThread>(new Thread(lineSsjThread), lineSsjThread);
Floor2RoadWaySsjThread floor2OutSsjThread = new Floor2RoadWaySsjThread();
floor2OutSsjCustomThread = new CustomThread<Floor2RoadWaySsjThread>(new Thread(floor2OutSsjThread), floor2OutSsjThread);
Ddj1Thread ddj1Thread = new Ddj1Thread();
ddj1CustomThread = new CustomThread<Ddj1Thread>(new Thread(ddj1Thread), ddj1Thread);
}
/**
* plc读取初始化
*/
private void plcReadInit() {
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(deviceSsjs.get(0).getIpAddress(), Convert.toInt(deviceSsjs.get(0).getPort())));
Thread plcSsjReadThread = new Thread(plcSsjRead);
plcSsjReadCustomThread = new CustomThread<PlcSsjRead>(plcSsjReadThread, plcSsjRead);
PlcSsjRead.deviceSsjMap = ssjList;
//获取堆垛机
ConcurrentHashMap<String, MsgDdj> ddjList = new ConcurrentHashMap<>();
List<MsgDdj> msgDdjs = msgDdjService.selectMsgDdjList(new MsgDdj());
for (MsgDdj msgDdj : msgDdjs) {
msgDdj.setPlcDbReadAddress(msgDdj.getPlcDbReadAddress());
PlcDdjRead plcDdjRead = new PlcDdjRead(msgDdj, new PlcConnectInfo(msgDdj.getIp(), Convert.toInt(msgDdj.getPort())));
Thread plcDdjReadThread = new Thread(plcDdjRead);
switch (msgDdj.getId()){
case 1:
plcDdj1ReadCustomThread = new CustomThread<PlcDdjRead>(plcDdjReadThread, plcDdjRead);
break;
case 2:
plcDdj2ReadCustomThread = new CustomThread<PlcDdjRead>(plcDdjReadThread, plcDdjRead);
break;
}
ddjList.put("ddj" + msgDdj.getId(), msgDdj);
}
PlcDdjRead.deviceDdjMap = ddjList;
}
/**
* 开始线程
*/
public void startAllThread() {
plcSsjReadCustomThread.getThread().start();
plcDdj1ReadCustomThread.getThread().start();
plcDdj2ReadCustomThread.getThread().start();
isRun = true;
}
/**
* 开启线程2
*/
public void startAllThread2() {
monitoringCustomThread.thread.start();
floor1BothWayInSsjCustomThread.thread.start();
floor1BothWayOutSsjCustomThread.thread.start();
floor1SingleSsjCustomThread.thread.start();
floor2InSsjCustomThread.thread.start();
lineSsjCustomThread.thread.start();
floor2OutSsjCustomThread.thread.start();
ddj1CustomThread.thread.start();
isRun2 = true;
}
/**
* 停止线程
*/
public void stopAllThread() {
plcSsjReadCustomThread.runnable.isRun = false;
plcDdj1ReadCustomThread.runnable.isRun = false;
plcDdj2ReadCustomThread.runnable.isRun = false;
plcReadInit();
isRun = false;
}
/**
* 停止线程2
*/
public void stopAllThread2() {
monitoringCustomThread.runnable.isRun = false;
floor1BothWayInSsjCustomThread.runnable.isRun = false;
floor1BothWayOutSsjCustomThread.runnable.isRun = false;
floor1SingleSsjCustomThread.runnable.isRun = false;
floor2InSsjCustomThread.runnable.isRun = false;
lineSsjCustomThread.runnable.isRun = false;
floor2OutSsjCustomThread.runnable.isRun = false;
ddj1CustomThread.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;
}
}