#
Junjie
12 小时以前 f5e49b2c04137fdc88c453cbe5d190cf7daab079
src/main/java/com/zy/core/thread/impl/ZySiemensDualCrnThread.java
@@ -1,6 +1,8 @@
package com.zy.core.thread.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
@@ -12,11 +14,10 @@
import com.zy.asrs.service.BasDualCrnpService;
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.DualCrnTaskModeType;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.*;
import com.zy.core.model.CommandResponse;
import com.zy.core.model.Task;
import com.zy.core.model.command.DualCrnCommand;
@@ -30,6 +31,8 @@
import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
@@ -51,6 +54,7 @@
    private volatile boolean closed = false;
    private ScheduledExecutorService readExecutor;
    private ScheduledExecutorService processExecutor;
    private ScheduledExecutorService commandExecutor;
    public ZySiemensDualCrnThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
        this.deviceConfig = deviceConfig;
@@ -102,13 +106,93 @@
                if (task != null) {
                    step = task.getStep();
                }
                if (step == 2 && task != null) {
                if (step == 2) {
                    List<DualCrnCommand> commandList = (List<DualCrnCommand>) task.getData();
                    DualCrnCommand command = commandList.get(0);
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("commands", commandList);
                    map.put("idx", 1);
                    redisUtil.set(RedisKeyType.DUAL_CRN_COMMAND_.key + command.getTaskNo(), JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect), 60 * 60 * 24);
                    sendCommand(command);
                } else if (step == 3) {
                    sendCommand((DualCrnCommand) task.getData());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, 0, 200, TimeUnit.MILLISECONDS);
        commandExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("DualCrnCommand-" + deviceConfig.getDeviceNo());
                t.setDaemon(true);
                return t;
            }
        });
        commandExecutor.scheduleAtFixedRate(() -> {
            if (closed || Thread.currentThread().isInterrupted()) {
                return;
            }
            try {
                if(crnProtocol.getMode() != DualCrnModeType.AUTO.id) {
                    return;
                }
                if(crnProtocol.getAlarm() != 0) {
                    return;
                }
                //等待下一个任务
                Object wait = redisUtil.get(RedisKeyType.DUAL_CRN_PICK_WAIT_NEXT_TASK.key + crnProtocol.getCrnNo());
                if (wait != null) {
                    return;
                }
                if(crnProtocol.getTaskNo() > 0 && crnProtocol.getStatus() == DualCrnStatusType.IDLE.id) {
                    Integer taskNo = crnProtocol.getTaskNo();
                    Object commandObj = redisUtil.get(RedisKeyType.DUAL_CRN_COMMAND_.key + taskNo);
                    if (commandObj == null) {
                        News.error("双工位堆垛机,工位1空闲等待下发命令,但未找到命令。堆垛机号={},工作号={}", crnProtocol.getCrnNo(), taskNo);
                        return;
                    }
                    JSONObject commandMap = JSON.parseObject(commandObj.toString());
                    Integer idx = commandMap.getInteger("idx");
                    List<DualCrnCommand> commandList = commandMap.getJSONArray("commands").toJavaList(DualCrnCommand.class);
                    DualCrnCommand dualCommand = commandList.get(idx);
                    idx++;
                    commandMap.put("idx", idx);
                    sendCommand(dualCommand);
                    redisUtil.set(RedisKeyType.DUAL_CRN_COMMAND_.key + taskNo, commandMap.toJSONString(), 60 * 60 * 24);
                }
                if(crnProtocol.getTaskNoTwo() > 0 && crnProtocol.getStatusTwo() == DualCrnStatusType.IDLE.id) {
                    Integer taskNo = crnProtocol.getTaskNoTwo();
                    Object commandObj = redisUtil.get(RedisKeyType.DUAL_CRN_COMMAND_.key + taskNo);
                    if (commandObj == null) {
                        News.error("双工位堆垛机,工位2空闲等待下发命令,但未找到命令。堆垛机号={},工作号={}", crnProtocol.getCrnNo(), taskNo);
                        return;
                    }
                    JSONObject commandMap = JSON.parseObject(commandObj.toString());
                    Integer idx = commandMap.getInteger("idx");
                    List<DualCrnCommand> commandList = commandMap.getJSONArray("commands").toJavaList(DualCrnCommand.class);
                    DualCrnCommand dualCommand = commandList.get(idx);
                    idx++;
                    commandMap.put("idx", idx);
                    sendCommand(dualCommand);
                    redisUtil.set(RedisKeyType.DUAL_CRN_COMMAND_.key + taskNo, commandMap.toJSONString(), 60 * 60 * 24);
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("DualCrnCommandThread Fail", e);
            }
        }, 0, 200, TimeUnit.MILLISECONDS);
    }
    /**