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.PlcDdj1Read; import com.ruoyi.schedule.plcRead.ddj.PlcDdj2Read; import com.ruoyi.schedule.plcRead.ssj.PlcSsjRead; import com.ruoyi.common.domain.CustomThread; import com.ruoyi.schedule.scheduleTask.ddj.DdjThread; 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.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; import java.util.HashMap; import java.util.List; import java.util.concurrent.ConcurrentHashMap; @Component @Slf4j public class ScheduleThread { public CustomThread plcSsjReadCustomThread; public CustomThread plcDdj1ReadCustomThread; public CustomThread plcDdj2ReadCustomThread; public CustomThread floor1BothWayInSsjCustomThread; public CustomThread floor1BothWayOutSsjCustomThread; public CustomThread floor1SingleSsjCustomThread; public CustomThread floor2InSsjCustomThread; public CustomThread floor2OutSsjCustomThread; public CustomThread lineSsjCustomThread; public CustomThread monitoringCustomThread; public CustomThread 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("调度服务执行成功"); //startAllThread(); //startAllThread2(); } /** * 调度初始化 */ private void scheduleInit() { MonitoringThread monitoringThread = new MonitoringThread(); monitoringCustomThread = new CustomThread(new Thread(monitoringThread), monitoringThread); Floor1BothWayInSsjThread floor1BothWayInSsjThread = new Floor1BothWayInSsjThread(); floor1BothWayInSsjCustomThread = new CustomThread(new Thread(floor1BothWayInSsjThread), floor1BothWayInSsjThread); Floor1BothWayOutSsjThread floor1BothWayOutSsjThread = new Floor1BothWayOutSsjThread(); floor1BothWayOutSsjCustomThread = new CustomThread(new Thread(floor1BothWayOutSsjThread), floor1BothWayOutSsjThread); Floor1SingleSsjThread floor1SingleSsjThread = new Floor1SingleSsjThread(); floor1SingleSsjCustomThread = new CustomThread(new Thread(floor1SingleSsjThread), floor1SingleSsjThread); Floor2InOutSsjThread floor2InSsjThread = new Floor2InOutSsjThread(); floor2InSsjCustomThread = new CustomThread(new Thread(floor2InSsjThread), floor2InSsjThread); LineSsjThread lineSsjThread = new LineSsjThread(); lineSsjCustomThread = new CustomThread(new Thread(lineSsjThread), lineSsjThread); Floor2RoadWaySsjThread floor2OutSsjThread = new Floor2RoadWaySsjThread(); floor2OutSsjCustomThread = new CustomThread(new Thread(floor2OutSsjThread), floor2OutSsjThread); DdjThread ddj1Thread = new DdjThread(); ddj1CustomThread = new CustomThread(new Thread(ddj1Thread), ddj1Thread); } /** * plc读取初始化 */ private void plcReadInit() { ConcurrentHashMap ssjList = new ConcurrentHashMap<>(); //获取要去取的输送机 List 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(plcSsjReadThread, plcSsjRead); PlcSsjRead.deviceSsjMap = ssjList; //获取堆垛机 ConcurrentHashMap ddj1List = new ConcurrentHashMap<>(); ConcurrentHashMap ddj2List = new ConcurrentHashMap<>(); List msgDdjs = msgDdjService.selectMsgDdjList(new MsgDdj()); for (MsgDdj msgDdj : msgDdjs) { msgDdj.setPlcDbReadAddress(msgDdj.getPlcDbReadAddress()); switch (msgDdj.getId()){ case 1: PlcDdj1Read plcDdj1Read = new PlcDdj1Read(msgDdj, new PlcConnectInfo(msgDdj.getIp(), Convert.toInt(msgDdj.getPort()))); Thread plcDdj1ReadThread = new Thread(plcDdj1Read); plcDdj1ReadCustomThread = new CustomThread(plcDdj1ReadThread, plcDdj1Read); ddj1List.put("ddj1", msgDdj); PlcDdj1Read.deviceDdjMap = ddj1List; break; case 2: PlcDdj2Read plcDdj2Read = new PlcDdj2Read(msgDdj, new PlcConnectInfo(msgDdj.getIp(), Convert.toInt(msgDdj.getPort()))); Thread plcDdj2ReadThread = new Thread(plcDdj2Read); plcDdj2ReadCustomThread = new CustomThread(plcDdj2ReadThread, plcDdj2Read); ddj2List.put("ddj2", msgDdj); PlcDdj2Read.deviceDdjMap = ddj2List; break; } } } /** * 开始线程 */ 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 getScheduleStatus() { HashMap map = new HashMap<>(); map.put("isRun",isRun); map.put("isRun2",isRun2); return map; } public static void main(String[] args) { // Socket socket = new Socket(); // OutputStream out = null; // InputStream in = null; // try { //// socket.connect(new InetSocketAddress("192.168.56.32", 2002), 1000); //// in = socket.getInputStream(); //// out = socket.getOutputStream(); // // } catch (IOException e) { // throw new RuntimeException(e); // } // String code = sendWeight("192.168.56.33", 2002); // System.err.println(code); String pp = "MODEL:EX-1203 COLOR:9005 SERIAL:QT25C1879 V:220 Hz:50 PH:1 N.W.:207kg G.W.:238kg @:9781C52TQ$S0T0T0T3T0U7T0$7005280MA0A@"; String ss = "MODEL:TR-360 COLOR:7016 SERIAL:QT25C1899 V:380 Hz:50 PH:3 N.W.:231kg G.W.:261kg @:9981C52TQ$S0T0U5RDS2S3S6T0T3T0U7T0$4105230IF0A@"; System.err.println(pp); System.err.println(pp.length()); System.err.println(ss.length()); } public static String sendWeight(String ip, int por) { String zhi=""; Socket socket = null; OutputStream os = null; InputStream is = null; try { //新建客户端并请求服务端的ip为localhost 端口为8088 socket = new Socket(ip,por); // System.out.println("socket end ="+System.currentTimeMillis()); //休眠3秒 Thread.sleep(1000); char[] charArray = new char[500]; //获取客户端发送的数据的输入流 is = socket.getInputStream(); //读取输入流 InputStreamReader inputStreamReader = new InputStreamReader(is); // System.out.println("read begin =" + System.currentTimeMillis()); int readLength = inputStreamReader.read(charArray); System.out.println("readLength:"+readLength); if (readLength != -1) { String newString = new String(charArray, 0, readLength); // System.out.println("newString:"+newString+"长度"+newString.length()); // newString=newString.replaceAll("\r|\n", ""); // if(newString.length()==8&&!newString.equals("=0000000")){ // newString=newString.substring(1,8); // newString=newString.replaceAll("^(0+)", ""); // zhi=newString; // } zhi = newString; readLength = inputStreamReader.read(charArray); } // System.out.println("read end =" + System.currentTimeMillis()); inputStreamReader.close(); is.close(); socket.close(); } catch (Exception e) { e.printStackTrace(); try { socket.close(); is.close(); os.close(); } catch (IOException e1) { e1.printStackTrace(); } } return zhi; } }