| | |
| | | package com.zy.core.thread.impl; |
| | | |
| | | import HslCommunication.Profinet.Siemens.SiemensPLCS; |
| | | import HslCommunication.Profinet.Siemens.SiemensS7Net; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | 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.DeviceDataLogService; |
| | | 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.ThreadHandler; |
| | | 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.model.CommandResponse; |
| | | import com.zy.core.model.Task; |
| | | import com.zy.core.model.command.CrnCommand; |
| | | import com.zy.core.model.protocol.CrnProtocol; |
| | | import com.zy.core.network.DeviceConnectPool; |
| | | import com.zy.core.network.ZyCrnConnectThread; |
| | | import com.zy.core.network.ZyCrnConnectDriver; |
| | | import com.zy.core.network.entity.ZyCrnStatusEntity; |
| | | import com.zy.core.thread.CrnThread; |
| | | import lombok.Data; |
| | |
| | | |
| | | /** |
| | | * 堆垛机线程 |
| | | * Created by vincent on 2020/8/4 |
| | | */ |
| | | @Data |
| | | @Slf4j |
| | |
| | | |
| | | private DeviceConfig deviceConfig; |
| | | private RedisUtil redisUtil; |
| | | private ZyCrnConnectThread zyCrnConnectThread; |
| | | private ZyCrnConnectDriver zyCrnConnectDriver; |
| | | private CrnProtocol crnProtocol; |
| | | private int deviceLogCollectTime = 200; |
| | | private boolean resetFlag = false; |
| | | |
| | | public ZySiemensCrnThread(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | |
| | | this.initCrn(); |
| | | while (true) { |
| | | try { |
| | | deviceLogCollectTime = Utils.getDeviceLogCollectTime(); |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.Crn, deviceConfig.getDeviceNo()); |
| | | if (task != null) { |
| | |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | SiemensS7Net siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, deviceConfig.getIp()); |
| | | zyCrnConnectThread = new ZyCrnConnectThread(siemensS7Net, deviceConfig); |
| | | new Thread(zyCrnConnectThread).start(); |
| | | DeviceConnectPool.put(SlaveType.Crn, deviceConfig.getDeviceNo(), zyCrnConnectThread); |
| | | zyCrnConnectDriver = new ZyCrnConnectDriver(deviceConfig); |
| | | new Thread(zyCrnConnectDriver).start(); |
| | | DeviceConnectPool.put(SlaveType.Crn, deviceConfig.getDeviceNo(), zyCrnConnectDriver); |
| | | return true; |
| | | } |
| | | |
| | |
| | | * 读取状态 |
| | | */ |
| | | private void readStatus(){ |
| | | ZyCrnStatusEntity crnStatus = zyCrnConnectThread.getStatus(); |
| | | 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()); |
| | |
| | | crnProtocol.setLastCommandTime(System.currentTimeMillis()); |
| | | } |
| | | |
| | | if (System.currentTimeMillis() - crnProtocol.getDeviceDataLog() > 1000 * 1) { |
| | | //采集时间超过1s,保存一次数据记录 |
| | | if (System.currentTimeMillis() - crnProtocol.getDeviceDataLog() > deviceLogCollectTime) { |
| | | //保存数据记录 |
| | | DeviceDataLogService deviceDataLogService = SpringUtils.getBean(DeviceDataLogService.class); |
| | | DeviceDataLog deviceDataLog = new DeviceDataLog(); |
| | | deviceDataLog.setOriginData(JSON.toJSONString(crnStatus)); |
| | | deviceDataLog.setWcsData(JSON.toJSONString(crnProtocol)); |
| | | deviceDataLog.setType(String.valueOf(SlaveType.Crn)); |
| | | deviceDataLog.setDeviceNo(crnProtocol.getCrnNo()); |
| | | deviceDataLog.setCreateTime(new Date()); |
| | | deviceDataLogService.insert(deviceDataLog); |
| | | |
| | | redisUtil.set(RedisKeyType.DEVICE_LOG_KEY.key + System.currentTimeMillis(), deviceDataLog, 60 * 60 * 24); |
| | | //更新采集时间 |
| | | crnProtocol.setDeviceDataLog(System.currentTimeMillis()); |
| | | } |
| | |
| | | basCrnp = new BasCrnp(); |
| | | basCrnp.setCrnNo(deviceConfig.getDeviceNo()); |
| | | basCrnp.setStatus(1); |
| | | basCrnp.setInEnable("N"); |
| | | basCrnp.setOutEnable("N"); |
| | | basCrnp.setCreateTime(new Date()); |
| | | basCrnpService.insert(basCrnp); |
| | | } |
| | |
| | | |
| | | @Override |
| | | public void close() { |
| | | zyCrnConnectThread.close(); |
| | | zyCrnConnectDriver.close(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public CrnCommand getMoveCommand(String sourceLocNo, String targetLocNo, Integer taskNo, Integer crnNo) { |
| | | public CrnCommand getMoveCommand(String targetLocNo, Integer taskNo, Integer crnNo) { |
| | | CrnCommand crnCommand = new CrnCommand(); |
| | | crnCommand.setCrnNo(crnNo); // 堆垛机编号 |
| | | crnCommand.setTaskNo(taskNo.shortValue()); // 工作号 |
| | | crnCommand.setAckFinish((short) 0); // 任务完成确认位 |
| | | crnCommand.setTaskMode(CrnTaskModeType.CRN_MOVE.id.shortValue()); // 任务模式: 堆垛机移动 |
| | | crnCommand.setSourcePosX((short) Utils.getRow(sourceLocNo)); // 源库位排 |
| | | crnCommand.setSourcePosY((short) Utils.getBay(sourceLocNo)); // 源库位列 |
| | | crnCommand.setSourcePosZ((short) Utils.getLev(sourceLocNo)); // 源库位层 |
| | | crnCommand.setDestinationPosX((short) 0); // 目标库位排 |
| | | crnCommand.setDestinationPosY((short) 0); // 目标库位列 |
| | | crnCommand.setDestinationPosZ((short) 0); // 目标库位层 |
| | | crnCommand.setDestinationPosX((short) Utils.getRow(targetLocNo)); // 目标库位排 |
| | | crnCommand.setDestinationPosY((short) Utils.getBay(targetLocNo)); // 目标库位列 |
| | | crnCommand.setDestinationPosZ((short) Utils.getLev(targetLocNo)); // 目标库位层 |
| | | crnCommand.setCommand((short) 1); // 任务确认 |
| | | return crnCommand; |
| | | } |
| | |
| | | @Override |
| | | public synchronized CommandResponse sendCommand(CrnCommand command) { |
| | | this.crnProtocol.setLastCommandTime(System.currentTimeMillis()); |
| | | return zyCrnConnectThread.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); |
| | | } |
| | | } |
| | | } |
| | | } |