Junjie
2023-03-27 06bd2d6dbd31315b68e768d6b59139c753f8ff96
src/main/java/com/zy/core/thread/ShuttleThread.java
@@ -5,23 +5,34 @@
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.ModBus.ModbusTcpNet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.core.common.DateUtils;
import com.core.exception.CoolException;
import com.zy.common.model.NavigateNode;
import com.zy.common.utils.CommonUtils;
import com.zy.common.utils.NavigatePositionConvert;
import com.zy.common.utils.NavigateUtils;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.ThreadHandler;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.ShuttleRunDirection;
import com.zy.core.enums.ShuttleStatusType;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.ShuttleSlave;
import com.zy.core.model.Task;
import com.zy.core.model.command.ShuttleAssignCommand;
import com.zy.core.model.command.ShuttleCommand;
import com.zy.core.model.protocol.ShuttleProtocol;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
 * 四向穿梭车线程
@@ -33,9 +44,11 @@
    private ModbusTcpNet modbusTcpNet;
    private ShuttleSlave slave;
    private ShuttleProtocol shuttleProtocol;
    private RedisUtil redisUtil;
    public ShuttleThread(ShuttleSlave slave) {
    public ShuttleThread(ShuttleSlave slave,RedisUtil redisUtil) {
        this.slave = slave;
        this.redisUtil = redisUtil;
    }
    @Override
@@ -53,9 +66,13 @@
                    case 1:
                        readStatus();
                        break;
                    // 写入数据
                    case 2:
                        write((ShuttleCommand) task.getData());
//                    // 写入数据
//                    case 2:
//                        write((ShuttleCommand) task.getData());
//                        break;
                    //下发任务
                    case 3:
                        assignWork((ShuttleAssignCommand) task.getData());
                        break;
                    default:
                        break;
@@ -145,8 +162,16 @@
                OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
                log.info(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
                // 根据实时信息更新数据库
                //.....
                //小车处于忙碌状态,将标记置为true
                if (shuttleProtocol.getBusyStatusType() == ShuttleStatusType.BUSY) {
                    shuttleProtocol.setPakMk(true);
                }
                //四向穿梭车空闲、有任务且标记为true,需要执行任务的下一条指令
                if (shuttleProtocol.getBusyStatusType() == ShuttleStatusType.IDLE && shuttleProtocol.getTaskNo() != 0 && shuttleProtocol.getPakMk()) {
                    //执行下一步指令
                    executeWork(shuttleProtocol.getTaskNo());
                }
            }else {
                OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】{1}四向穿梭车plc状态信息失败", DateUtils.convert(new Date()), slave.getId()));
@@ -226,9 +251,97 @@
        if (null == shuttleProtocol) {
            shuttleProtocol = new ShuttleProtocol();
        }
//        shuttleProtocol.setShuttleNo(slave.getId().shortValue());
//        shuttleProtocol.setBusyStatus(ShuttleStatusType.BUSY);
//        shuttleProtocol.setCurrentCode("0");
    }
    //分配任务
    private void assignWork(ShuttleAssignCommand assignCommand) {
        //计算路径
        List<NavigateNode> calc = NavigateUtils.calc(assignCommand.getSourceLocNo(), assignCommand.getDistLocNo(), "in");
        //获取分段路径
        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(calc);
        //将此map存入redis中
        HashMap<String, Object> map = new HashMap<>();
        //命令执行步序
        map.put("commandStep", 0);
        //路径数据
        map.put("path", data);
        //工作号
        map.put("wrk_no", assignCommand.getTaskNo());
        //任务数据保存到redis
        redisUtil.set("wrk_no_" + assignCommand.getTaskNo(), JSON.toJSONString(map));
        //执行下发任务
        executeWork(assignCommand.getTaskNo());
    }
    //执行下发的指令
    private void executeWork(Short taskNo) {
        //读取redis数据
        Object o = redisUtil.get("wrk_no_" + taskNo);
        HashMap map = JSON.parseObject(o.toString(), HashMap.class);
        //当前步序
        int commandStep = Integer.parseInt(map.get("commandStep").toString());
        //当前路径数据
        Object data = map.get("path");
        ArrayList pathList = JSON.parseObject(data.toString(), ArrayList.class);
        //取第一条路径
        Object o1 = pathList.get(commandStep);
        ArrayList path = JSON.parseObject(o1.toString(), ArrayList.class);
        int size = path.size();
        //开始路径
        JSONObject startPath = JSON.parseObject(path.get(0).toString());
        System.out.println(startPath);
        //目标路径
        JSONObject endPath = JSON.parseObject(path.get(size - 1).toString());
        System.out.println(endPath);
        //下发命令
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 1);
        command.setStartCodeNum(NavigatePositionConvert.xyToPosition(startPath.getIntValue("x"), startPath.getIntValue("y")));
        command.setMiddleCodeNum((short) 0);
        command.setDistCodeNum(NavigatePositionConvert.xyToPosition(endPath.getIntValue("x"), endPath.getIntValue("y")));
        command.setStartToDistDistance(1000);
        command.setMiddleToDistDistance(1000);
        command.setRunDirection(ShuttleRunDirection.get(startPath.get("direction").toString()).id);
        command.setPalletLift((short) 1);
        command.setForceMoveDistance(1000);
        command.setChargeSwitch((short) 2);
        command.setIOControl((short) 0);
        command.setRunSpeed((short) 1000);
        command.setRadarTmp((short) 0);
        command.setCommandEnd((short) 1);
        //下发命令
        if (!write(command)) {
            News.error("四向穿梭车命令下发失败,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
        } else {
            News.info("四向穿梭车命令下发成功,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
            //将标记置为false(防止重发)
            shuttleProtocol.setPakMk(false);
            //判断数据是否执行完成
            if (commandStep < size) {
                //更新redis数据
                //步序增加
                commandStep++;
                map.put("commandStep", commandStep);
                //任务数据保存到redis
                redisUtil.set("wrk_no_" + map.get("wrk_no").toString(), JSON.toJSONString(map));
            }else {
                //已执行完成
                //保存数据到数据库做流水
                //删除redis
                redisUtil.del("wrk_no_" + map.get("wrk_no").toString());
                //。。。
                //1、命令下方需要判断小车空闲状态
                //2、
            }
        }
    }
    /******************************************************************************************/
@@ -239,26 +352,26 @@
        slave.setId(1);
        slave.setIp("192.168.4.24");
        slave.setPort(502);
        ShuttleThread thread = new ShuttleThread(slave);
        thread.connect();
        thread.readStatus();
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 0);
        command.setStartCodeNum((short) 12323);
        command.setMiddleCodeNum((short) 22323);
        command.setDistCodeNum((short) 29999);
        command.setStartToDistDistance(109999);
        command.setMiddleToDistDistance(5000);
        command.setRunDirection((short) 1);
        command.setPalletLift((short) 2);
        command.setForceMoveDistance(3000);
        command.setChargeSwitch((short) 2);
        command.setIOControl((short) 0);
        command.setRunSpeed((short) 0);
        command.setRadarTmp((short) 0);
        command.setCommandEnd((short) 1);
        thread.write(command);
//        ShuttleThread thread = new ShuttleThread(slave);
//        thread.connect();
//        thread.readStatus();
//
//        ShuttleCommand command = new ShuttleCommand();
//        command.setCommandWord((short) 0);
//        command.setStartCodeNum((short) 12323);
//        command.setMiddleCodeNum((short) 22323);
//        command.setDistCodeNum((short) 29999);
//        command.setStartToDistDistance(109999);
//        command.setMiddleToDistDistance(5000);
//        command.setRunDirection((short) 1);
//        command.setPalletLift((short) 2);
//        command.setForceMoveDistance(3000);
//        command.setChargeSwitch((short) 2);
//        command.setIOControl((short) 0);
//        command.setRunSpeed((short) 0);
//        command.setRadarTmp((short) 0);
//        command.setCommandEnd((short) 1);
//        thread.write(command);
    }
}