#
Junjie
14 小时以前 0c5a58771fcf86ad0b562829fbfa440da9392703
src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java
@@ -5,12 +5,13 @@
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasCrnp;
import com.zy.asrs.entity.BasCrnpOpt;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.entity.DeviceDataLog;
import com.zy.asrs.service.BasCrnpService;
import com.zy.asrs.service.BasCrnpOptService;
import com.zy.asrs.utils.Utils;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.CrnTaskModeType;
@@ -29,6 +30,10 @@
import java.text.MessageFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/**
 * 堆垛机线程
@@ -41,7 +46,11 @@
    private RedisUtil redisUtil;
    private ZyCrnConnectDriver zyCrnConnectDriver;
    private CrnProtocol crnProtocol;
    private int deviceLogCollectTime = 200;
    private boolean resetFlag = false;
    private volatile boolean closed = false;
    private ScheduledExecutorService readExecutor;
    private ScheduledExecutorService processExecutor;
    public ZySiemensCrnThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
        this.deviceConfig = deviceConfig;
@@ -53,29 +62,53 @@
    public void run() {
        this.connect();
        this.initCrn();
        while (true) {
        readExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("CrnReader-" + deviceConfig.getDeviceNo());
                t.setDaemon(true);
                return t;
            }
        });
        readExecutor.scheduleAtFixedRate(() -> {
            if (closed || Thread.currentThread().isInterrupted()) {
                return;
            }
            try {
                deviceLogCollectTime = Utils.getDeviceLogCollectTime();
                readStatus();
            } catch (Exception e) {
                log.error("CrnThread Fail", e);
            }
        }, 0, 200, TimeUnit.MILLISECONDS);
        processExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("CrnWriter-" + deviceConfig.getDeviceNo());
                t.setDaemon(true);
                return t;
            }
        });
        processExecutor.scheduleAtFixedRate(() -> {
            if (closed || Thread.currentThread().isInterrupted()) {
                return;
            }
            try {
                int step = 1;
                Task task = MessageQueue.poll(SlaveType.Crn, deviceConfig.getDeviceNo());
                if (task != null) {
                    step = task.getStep();
                }
                switch (step) {
                    // 读数据
                    case 1:
                        readStatus();
                        break;
                    case 2:
                        sendCommand((CrnCommand) task.getData());
                        break;
                    default:
                        break;
                if (step == 2 && task != null) {
                    sendCommand((CrnCommand) task.getData());
                }
                Thread.sleep(200);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        }, 0, 200, TimeUnit.MILLISECONDS);
    }
    /**
@@ -108,7 +141,7 @@
    @Override
    public boolean connect() {
        zyCrnConnectDriver = new ZyCrnConnectDriver(deviceConfig);
        new Thread(zyCrnConnectDriver).start();
        zyCrnConnectDriver.start();
        DeviceConnectPool.put(SlaveType.Crn, deviceConfig.getDeviceNo(), zyCrnConnectDriver);
        return true;
    }
@@ -120,7 +153,6 @@
        ZyCrnStatusEntity crnStatus = zyCrnConnectDriver.getStatus();
        if (crnStatus == null) {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
            News.error("SiemensCrn"+" - 5"+" - 读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
            return;
        }
@@ -170,7 +202,7 @@
            crnProtocol.setLastCommandTime(System.currentTimeMillis());
        }
        if (System.currentTimeMillis() - crnProtocol.getDeviceDataLog() > 200) {
        if (System.currentTimeMillis() - crnProtocol.getDeviceDataLog() > deviceLogCollectTime) {
            //保存数据记录
            DeviceDataLog deviceDataLog = new DeviceDataLog();
            deviceDataLog.setOriginData(JSON.toJSONString(crnStatus));
@@ -184,7 +216,12 @@
            crnProtocol.setDeviceDataLog(System.currentTimeMillis());
        }
        BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class);
        BasCrnpService basCrnpService = null;
        try {
            basCrnpService = SpringUtils.getBean(BasCrnpService.class);
        }catch (Exception e){
        }
        if (basCrnpService != null) {
            BasCrnp basCrnp = basCrnpService.selectOne(new EntityWrapper<BasCrnp>().eq("crn_no", deviceConfig.getDeviceNo()));
            if(basCrnp == null) {
@@ -193,6 +230,8 @@
                basCrnp.setStatus(1);
                basCrnp.setInEnable("N");
                basCrnp.setOutEnable("N");
                basCrnp.setMaxInTask(5);
                basCrnp.setMaxOutTask(5);
                basCrnp.setCreateTime(new Date());
                basCrnpService.insert(basCrnp);
            }
@@ -201,7 +240,18 @@
    @Override
    public void close() {
        zyCrnConnectDriver.close();
        closed = true;
        ScheduledExecutorService ex = readExecutor;
        if (ex != null) {
            try { ex.shutdownNow(); } catch (Exception ignore) {}
        }
        ScheduledExecutorService px = processExecutor;
        if (px != null) {
            try { px.shutdownNow(); } catch (Exception ignore) {}
        }
        if (zyCrnConnectDriver != null) {
            zyCrnConnectDriver.close();
        }
    }
    @Override
@@ -259,6 +309,33 @@
    @Override
    public synchronized CommandResponse sendCommand(CrnCommand command) {
        this.crnProtocol.setLastCommandTime(System.currentTimeMillis());
        return zyCrnConnectDriver.sendCommand(command);
        CommandResponse response = null;
        try {
            response = zyCrnConnectDriver.sendCommand(command);
            return response;
        } finally {
            String sourceLocNo = Utils.getLocNo(command.getSourcePosX(), command.getSourcePosY(), command.getSourcePosZ());
            String targetLocNo = Utils.getLocNo(command.getDestinationPosX(), command.getDestinationPosY(), command.getDestinationPosZ());
            BasCrnpOptService bean = SpringUtils.getBean(BasCrnpOptService.class);
            ZyCrnStatusEntity statusEntity = zyCrnConnectDriver.getStatus();
            BasCrnpOpt basCrnpOpt = new BasCrnpOpt(
                    command.getTaskNo().intValue(),
                    command.getCrnNo(),
                    new Date(),
                    String.valueOf(command.getTaskMode()),
                    sourceLocNo,
                    targetLocNo,
                    null,
                    null,
                    null,
                    JSON.toJSONString(command),
                    JSON.toJSONString(statusEntity),
                    1,
                    JSON.toJSONString(response)
            );
            if (bean != null) {
                bean.insert(basCrnpOpt);
            }
        }
    }
}