| | |
| | | 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; |
| | | import com.zy.asrs.entity.BasDualCrnp; |
| | | import com.zy.asrs.entity.BasDualCrnpOpt; |
| | | import com.zy.asrs.entity.DeviceConfig; |
| | | import com.zy.asrs.entity.DeviceDataLog; |
| | | import com.zy.asrs.entity.*; |
| | | import com.zy.asrs.service.BasDualCrnpOptService; |
| | | import com.zy.asrs.service.BasDualCrnpService; |
| | | import com.zy.asrs.service.WrkMastService; |
| | | 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; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.cache.SlaveConnection; |
| | | import com.zy.core.enums.*; |
| | | import com.zy.core.model.CommandResponse; |
| | | import com.zy.core.model.StationObjModel; |
| | | import com.zy.core.model.Task; |
| | | import com.zy.core.model.command.DualCrnCommand; |
| | | import com.zy.core.model.protocol.DualCrnProtocol; |
| | | import com.zy.core.model.protocol.StationProtocol; |
| | | import com.zy.core.network.DeviceConnectPool; |
| | | import com.zy.core.network.ZyDualCrnConnectDriver; |
| | | import com.zy.core.network.entity.ZyDualCrnStatusEntity; |
| | | import com.zy.core.thread.DualCrnThread; |
| | | import com.zy.core.thread.StationThread; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | 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; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 双工位堆垛机线程 |
| | |
| | | private ZyDualCrnConnectDriver zyDualCrnConnectDriver; |
| | | private DualCrnProtocol crnProtocol; |
| | | private int deviceLogCollectTime = 200; |
| | | private boolean resetFlag = false; |
| | | private volatile boolean closed = false; |
| | | private ScheduledExecutorService readExecutor; |
| | | private ScheduledExecutorService processExecutor; |
| | | |
| | | public ZySiemensDualCrnThread(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | | this.deviceConfig = deviceConfig; |
| | |
| | | public void run() { |
| | | this.connect(); |
| | | this.initCrn(); |
| | | readExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { |
| | | @Override |
| | | public Thread newThread(Runnable r) { |
| | | Thread t = new Thread(r); |
| | | t.setName("DualCrnReader-" + 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("DualCrnThread Fail", e); |
| | | } |
| | | }, 0, 200, TimeUnit.MILLISECONDS); |
| | | |
| | | processExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { |
| | | @Override |
| | | public Thread newThread(Runnable r) { |
| | | Thread t = new Thread(r); |
| | | t.setName("DualCrnWriter-" + deviceConfig.getDeviceNo()); |
| | | t.setDaemon(true); |
| | | return t; |
| | | Thread readThread = new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | | deviceLogCollectTime = Utils.getDeviceLogCollectTime(); |
| | | readStatus(); |
| | | Thread.sleep(100); |
| | | } catch (Exception e) { |
| | | log.error("DualCrnThread Fail", e); |
| | | } |
| | | } |
| | | }); |
| | | processExecutor.scheduleAtFixedRate(() -> { |
| | | if (closed || Thread.currentThread().isInterrupted()) { |
| | | return; |
| | | } |
| | | try { |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.DualCrn, deviceConfig.getDeviceNo()); |
| | | if (task != null) { |
| | | step = task.getStep(); |
| | | readThread.start(); |
| | | |
| | | Thread processThread = new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.DualCrn, deviceConfig.getDeviceNo()); |
| | | if (task != null) { |
| | | step = task.getStep(); |
| | | } |
| | | |
| | | 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()); |
| | | } |
| | | |
| | | Thread.sleep(100); |
| | | } catch (Exception e) { |
| | | log.error("DualCrnProcess Fail", e); |
| | | } |
| | | if (step == 2 && task != null) { |
| | | sendCommand((DualCrnCommand) task.getData()); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | }, 0, 200, TimeUnit.MILLISECONDS); |
| | | }); |
| | | processThread.start(); |
| | | |
| | | Thread commandThread = new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | | if(crnProtocol.getMode() != DualCrnModeType.AUTO.id) { |
| | | continue; |
| | | } |
| | | |
| | | if(crnProtocol.getAlarm() != 0) { |
| | | continue; |
| | | } |
| | | |
| | | if (crnProtocol.getLoaded() == 1 && crnProtocol.getLoadedTwo() == 1) { |
| | | Object wait = redisUtil.get(RedisKeyType.DUAL_CRN_PICK_WAIT_NEXT_TASK.key + crnProtocol.getCrnNo()); |
| | | if (wait != null) { |
| | | redisUtil.del(RedisKeyType.DUAL_CRN_PICK_WAIT_NEXT_TASK.key + crnProtocol.getCrnNo()); |
| | | } |
| | | } |
| | | |
| | | 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); |
| | | continue; |
| | | } |
| | | |
| | | JSONObject commandMap = JSON.parseObject(commandObj.toString()); |
| | | Integer idx = commandMap.getInteger("idx"); |
| | | List<DualCrnCommand> commandList = commandMap.getJSONArray("commands").toJavaList(DualCrnCommand.class); |
| | | if (idx >= commandList.size()) { |
| | | continue; |
| | | } |
| | | DualCrnCommand dualCommand = commandList.get(idx); |
| | | |
| | | if (dualCommand.getTaskMode() == DualCrnTaskModeType.PUT.id.shortValue()) { |
| | | //等待下一个任务 |
| | | Object wait = redisUtil.get(RedisKeyType.DUAL_CRN_PICK_WAIT_NEXT_TASK.key + crnProtocol.getCrnNo()); |
| | | if (wait != null) { |
| | | continue; |
| | | } |
| | | |
| | | Object outTaskStationInfoObj = redisUtil.get(RedisKeyType.DUAL_CRN_OUT_TASK_STATION_INFO.key + taskNo); |
| | | if (outTaskStationInfoObj != null) { |
| | | //检测出口站是否可执行放货动作 |
| | | StationObjModel stationObjModel = JSON.parseObject(outTaskStationInfoObj.toString(), StationObjModel.class); |
| | | StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo()); |
| | | if (stationThread == null) { |
| | | continue; |
| | | } |
| | | |
| | | Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap(); |
| | | StationProtocol stationProtocol = statusMap.get(stationObjModel.getStationId()); |
| | | if (stationProtocol == null) { |
| | | continue; |
| | | } |
| | | |
| | | if (!stationProtocol.isAutoing()) { |
| | | continue; |
| | | } |
| | | |
| | | if (stationProtocol.isLoading()) { |
| | | continue; |
| | | } |
| | | |
| | | if (stationProtocol.getTaskNo() > 0) { |
| | | continue; |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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); |
| | | continue; |
| | | } |
| | | |
| | | JSONObject commandMap = JSON.parseObject(commandObj.toString()); |
| | | Integer idx = commandMap.getInteger("idx"); |
| | | List<DualCrnCommand> commandList = commandMap.getJSONArray("commands").toJavaList(DualCrnCommand.class); |
| | | if (idx >= commandList.size()) { |
| | | continue; |
| | | } |
| | | DualCrnCommand dualCommand = commandList.get(idx); |
| | | |
| | | if (dualCommand.getTaskMode() == DualCrnTaskModeType.PUT.id.shortValue()) { |
| | | //等待下一个任务 |
| | | Object wait = redisUtil.get(RedisKeyType.DUAL_CRN_PICK_WAIT_NEXT_TASK.key + crnProtocol.getCrnNo()); |
| | | if (wait != null) { |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | idx++; |
| | | commandMap.put("idx", idx); |
| | | sendCommand(dualCommand); |
| | | redisUtil.set(RedisKeyType.DUAL_CRN_COMMAND_.key + taskNo, commandMap.toJSONString(), 60 * 60 * 24); |
| | | } |
| | | |
| | | Thread.sleep(100); |
| | | } catch (Exception e) { |
| | | log.error("DualCrnCommand Fail", e); |
| | | } |
| | | } |
| | | }); |
| | | commandThread.start(); |
| | | } |
| | | |
| | | /** |
| | |
| | | crnProtocol.setForkPos(-1); |
| | | crnProtocol.setLoaded(0); |
| | | crnProtocol.setWalkPos(0); |
| | | crnProtocol.setLiftPos(0); |
| | | |
| | | //工位2 |
| | | crnProtocol.setTaskNoTwo(0); |
| | |
| | | crnProtocol.setForkPosTwo(-1); |
| | | crnProtocol.setLoadedTwo(0); |
| | | crnProtocol.setWalkPosTwo(0); |
| | | crnProtocol.setLiftPosTwo(0); |
| | | |
| | | crnProtocol.setAlarm(0); |
| | | crnProtocol.setXSpeed(0); |
| | |
| | | OutputQueue.DUAL_CRN.offer(MessageFormat.format("【{0}】读取双工位堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort())); |
| | | return; |
| | | } |
| | | |
| | | crnProtocol.setMode(crnStatus.getMode()); |
| | | |
| | | //工位1 |
| | | crnProtocol.setTaskNo(crnStatus.getTaskNo()); |
| | | crnProtocol.setStatus(crnStatus.getStatus()); |
| | | crnProtocol.setBay(crnStatus.getBay()); |
| | | crnProtocol.setLevel(crnStatus.getLevel()); |
| | | crnProtocol.setForkPos(crnStatus.getForkPos()); |
| | | crnProtocol.setLoaded(crnStatus.getLoaded()); |
| | | crnProtocol.setWalkPos(crnStatus.getWalkPos()); |
| | | crnProtocol.setTaskReceive(crnStatus.getTaskReceive()); |
| | | |
| | | //工位2 |
| | | crnProtocol.setTaskNoTwo(crnStatus.getTaskNoTwo()); |
| | | crnProtocol.setStatusTwo(crnStatus.getStatusTwo()); |
| | | crnProtocol.setBayTwo(crnStatus.getBayTwo()); |
| | | crnProtocol.setLevelTwo(crnStatus.getLevelTwo()); |
| | | crnProtocol.setForkPosTwo(crnStatus.getForkPosTwo()); |
| | | crnProtocol.setLoadedTwo(crnStatus.getLoadedTwo()); |
| | | crnProtocol.setWalkPosTwo(crnStatus.getWalkPosTwo()); |
| | | crnProtocol.setTaskReceiveTwo(crnStatus.getTaskReceiveTwo()); |
| | | |
| | | crnProtocol.setBay(crnStatus.getBay()); |
| | | crnProtocol.setLevel(crnStatus.getLevel()); |
| | | crnProtocol.setWalkPos(crnStatus.getWalkPos()); |
| | | crnProtocol.setLiftPos(crnStatus.getLiftPos()); |
| | | crnProtocol.setAlarm(crnStatus.getAlarm()); |
| | | crnProtocol.setTemp1(crnStatus.getTemp1()); |
| | | crnProtocol.setTemp2(crnStatus.getTemp2()); |
| | |
| | | |
| | | @Override |
| | | public void 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 (zyDualCrnConnectDriver != null) { |
| | | zyDualCrnConnectDriver.close(); |
| | | } |
| | |
| | | public DualCrnCommand getPickAndPutCommand(String sourceLocNo, String targetLocNo, Integer taskNo, Integer crnNo, Integer station) { |
| | | DualCrnCommand crnCommand = new DualCrnCommand(); |
| | | crnCommand.setCrnNo(crnNo); // 堆垛机编号 |
| | | crnCommand.setTaskNo(taskNo.shortValue()); // 工作号 |
| | | crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE.id.shortValue()); // 任务模式: 库位移转 |
| | | crnCommand.setSourcePosX((short) Utils.getRow(sourceLocNo)); // 源库位排 |
| | | crnCommand.setSourcePosY((short) Utils.getBay(sourceLocNo)); // 源库位列 |
| | | crnCommand.setSourcePosZ((short) Utils.getLev(sourceLocNo)); // 源库位层 |
| | | crnCommand.setDestinationPosX((short) Utils.getRow(targetLocNo)); // 目标库位排 |
| | | crnCommand.setDestinationPosY((short) Utils.getBay(targetLocNo)); // 目标库位列 |
| | | crnCommand.setDestinationPosZ((short) Utils.getLev(targetLocNo)); // 目标库位层 |
| | | crnCommand.setStation(station.shortValue());//工位 |
| | | crnCommand.setCommand((short) 1); // 任务确认 |
| | | crnCommand.setTaskNo(taskNo); // 工作号 |
| | | crnCommand.setTaskMode(DualCrnTaskModeType.TRANSFER.id); // 任务模式: 取放货 |
| | | crnCommand.setSourcePosX(Utils.getRow(sourceLocNo)); // 源库位排 |
| | | crnCommand.setSourcePosY(Utils.getBay(sourceLocNo)); // 源库位列 |
| | | crnCommand.setSourcePosZ(Utils.getLev(sourceLocNo)); // 源库位层 |
| | | crnCommand.setDestinationPosX(Utils.getRow(targetLocNo)); // 目标库位排 |
| | | crnCommand.setDestinationPosY(Utils.getBay(targetLocNo)); // 目标库位列 |
| | | crnCommand.setDestinationPosZ(Utils.getLev(targetLocNo)); // 目标库位层 |
| | | crnCommand.setStation(station);//工位 |
| | | crnCommand.setCommand(1); // 任务确认 |
| | | return crnCommand; |
| | | } |
| | | |
| | | @Override |
| | | public DualCrnCommand getPickCommand(String targetLocNo, Integer taskNo, Integer crnNo, Integer station) { |
| | | DualCrnCommand crnCommand = new DualCrnCommand(); |
| | | crnCommand.setCrnNo(crnNo); // 堆垛机编号 |
| | | crnCommand.setTaskNo(taskNo); // 工作号 |
| | | crnCommand.setTaskMode(DualCrnTaskModeType.PICK.id); // 任务模式: 取货 |
| | | crnCommand.setDestinationPosX(Utils.getRow(targetLocNo)); // 目标库位排 |
| | | crnCommand.setDestinationPosY(Utils.getBay(targetLocNo)); // 目标库位列 |
| | | crnCommand.setDestinationPosZ(Utils.getLev(targetLocNo)); // 目标库位层 |
| | | crnCommand.setStation(station);//工位 |
| | | crnCommand.setCommand(1); // 任务确认 |
| | | return crnCommand; |
| | | } |
| | | |
| | | @Override |
| | | public DualCrnCommand getPutCommand(String targetLocNo, Integer taskNo, Integer crnNo, Integer station) { |
| | | DualCrnCommand crnCommand = new DualCrnCommand(); |
| | | crnCommand.setCrnNo(crnNo); // 堆垛机编号 |
| | | crnCommand.setTaskNo(taskNo); // 工作号 |
| | | crnCommand.setTaskMode(DualCrnTaskModeType.PUT.id); // 任务模式: 放货 |
| | | crnCommand.setDestinationPosX(Utils.getRow(targetLocNo)); // 目标库位排 |
| | | crnCommand.setDestinationPosY(Utils.getBay(targetLocNo)); // 目标库位列 |
| | | crnCommand.setDestinationPosZ(Utils.getLev(targetLocNo)); // 目标库位层 |
| | | crnCommand.setStation(station);//工位 |
| | | crnCommand.setCommand(1); // 任务确认 |
| | | return crnCommand; |
| | | } |
| | | |
| | |
| | | public DualCrnCommand getMoveCommand(String targetLocNo, Integer taskNo, Integer crnNo) { |
| | | DualCrnCommand crnCommand = new DualCrnCommand(); |
| | | crnCommand.setCrnNo(crnNo); // 堆垛机编号 |
| | | crnCommand.setTaskNo(taskNo.shortValue()); // 工作号 |
| | | crnCommand.setAckFinish((short) 0); // 任务完成确认位 |
| | | crnCommand.setTaskMode(CrnTaskModeType.CRN_MOVE.id.shortValue()); // 任务模式: 堆垛机移动 |
| | | crnCommand.setDestinationPosX((short) Utils.getRow(targetLocNo)); // 目标库位排 |
| | | crnCommand.setDestinationPosY((short) Utils.getBay(targetLocNo)); // 目标库位列 |
| | | crnCommand.setDestinationPosZ((short) Utils.getLev(targetLocNo)); // 目标库位层 |
| | | crnCommand.setCommand((short) 1); // 任务确认 |
| | | crnCommand.setTaskNo(taskNo); // 工作号 |
| | | crnCommand.setTaskMode(DualCrnTaskModeType.MOVE.id); // 任务模式: 堆垛机移动 |
| | | crnCommand.setDestinationPosX(Utils.getRow(targetLocNo)); // 目标库位排 |
| | | crnCommand.setDestinationPosY(Utils.getBay(targetLocNo)); // 目标库位列 |
| | | crnCommand.setDestinationPosZ(Utils.getLev(targetLocNo)); // 目标库位层 |
| | | crnCommand.setCommand(1); // 任务确认 |
| | | return crnCommand; |
| | | } |
| | | |
| | |
| | | public DualCrnCommand getResetCommand(Integer crnNo, Integer station) { |
| | | DualCrnCommand crnCommand = new DualCrnCommand(); |
| | | crnCommand.setCrnNo(crnNo); // 堆垛机编号 |
| | | crnCommand.setTaskNo((short) 0); // 工作号 |
| | | crnCommand.setAckFinish((short) 1); // 任务完成确认位 |
| | | crnCommand.setTaskMode(CrnTaskModeType.NONE.id.shortValue()); // 任务模式 |
| | | crnCommand.setSourcePosX((short)0); // 源库位排 |
| | | crnCommand.setSourcePosY((short)0); // 源库位列 |
| | | crnCommand.setSourcePosZ((short)0); // 源库位层 |
| | | crnCommand.setDestinationPosX((short)0); // 目标库位排 |
| | | crnCommand.setDestinationPosY((short)0); // 目标库位列 |
| | | crnCommand.setDestinationPosZ((short)0); // 目标库位层 |
| | | crnCommand.setStation(station.shortValue());//工位 |
| | | crnCommand.setCommand((short) 1); // 任务确认 |
| | | crnCommand.setTaskNo(0); // 工作号 |
| | | crnCommand.setTaskMode(DualCrnTaskModeType.CONFIRM.id); // 任务模式: 确认 |
| | | crnCommand.setSourcePosX(0); // 源库位排 |
| | | crnCommand.setSourcePosY(0); // 源库位列 |
| | | crnCommand.setSourcePosZ(0); // 源库位层 |
| | | crnCommand.setDestinationPosX(0); // 目标库位排 |
| | | crnCommand.setDestinationPosY(0); // 目标库位列 |
| | | crnCommand.setDestinationPosZ(0); // 目标库位层 |
| | | crnCommand.setStation(station);//工位 |
| | | crnCommand.setCommand(1); // 任务确认 |
| | | return crnCommand; |
| | | } |
| | | |