package com.zy.asrs.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.core.exception.CoolException; import com.zy.asrs.controller.requestParam.StationRequestParam; import com.zy.asrs.domain.vo.StationStatus; import com.zy.common.utils.HttpHandler; import com.zy.common.utils.News; import com.zy.core.cache.MessageQueue; import com.zy.core.cache.SlaveConnection; import com.zy.core.enums.SlaveType; import com.zy.core.model.DevpSlave; import com.zy.core.model.Task; import com.zy.core.model.protocol.StaProtocol; import com.zy.core.properties.SlaveProperties; import com.zy.core.thread.SiemensDevpThread; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; /** * 立体仓库WCS系统主流程业务 * Created by vincent on 2020/8/6 */ @Slf4j @Service("ctuMainService") @Transactional @Data public class CtuMainServiceImpl { public static final long COMMAND_TIMEOUT = 5 * 1000; @Value("${ctu.url}") private String ctuUrl; @Value("${ctu.station}") private String station; @Autowired private SlaveProperties slaveProperties; /** * 出库的时候,设备上走 */ public synchronized void out(Integer mark) { // 根据输送线plc遍历 for (DevpSlave devp : slaveProperties.getDevp()) { // 遍历入库口 for (DevpSlave.Sta inSta : devp.getOutSta()) { // 获取入库站信息 SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId()); StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo()); if (staProtocol == null) { continue; } else { staProtocol = staProtocol.clone(); } // 判断是否满足条件 if (!staProtocol.isLoading()) { continue; } //&& staProtocol.isOutEnable() if (staProtocol.isAutoing() && !staProtocol.isEmptyMk() && (staProtocol.getWorkNo() == 0 || staProtocol.getWorkNo() == 9999) && staProtocol.isPakMk()) { if (station(1001)) { News.warnNoLog("" + mark + " - 0" + " - 开始执行"); // 更新站点信息 且 下发plc命令 staProtocol.setWorkNo((int) (Math.random() * 10000)); staProtocol.setStaNo((short) 1004); devpThread.setPakMk(staProtocol.getSiteId(), false); boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol)); log.info("输送线下发3:{},{}", staProtocol.getWorkNo(), 1004); if (result) { try { Thread.sleep(8000L); } catch (InterruptedException e) { throw new RuntimeException(e); } } else { News.error("" + mark + " - 2" + " - 发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId()); } } } else { News.errorNoLog("" + mark + " - 6" + " - 站点信息不符合入库条件!!!" + " 自动信号:" + staProtocol.isLoading() + "、可入信号:" + staProtocol.isInEnable() + "、空板信号:" + staProtocol.isEmptyMk() + "、工作号:" + staProtocol.getWorkNo() + "、锁定标记" + staProtocol.isPakMk() + "、入库印记:" + staProtocol.getStamp()); } } } } /** * 入库,从拣料站到入库站(CTU取货站) */ public synchronized void in(Integer mark) { // 根据输送线plc遍历 SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1); StaProtocol staProtocol = devpThread.getStation().get(1004); if (staProtocol == null) { return; } else { staProtocol = staProtocol.clone(); } // 判断是否满足条件 if (!staProtocol.isLoading()) { return; } // && staProtocol.isInEnable() if (staProtocol.getWorkNo() > 0 && staProtocol.isAutoing() && !staProtocol.isEmptyMk() && staProtocol.isPakMk()) { if (staProtocol.getStaNo() == 1004) { try { Thread.sleep(8000L); } catch (InterruptedException e) { throw new RuntimeException(e); } staProtocol.setStaNo((short) 1006); boolean result = MessageQueue.offer(SlaveType.Devp, 1, new Task(2, staProtocol)); log.info("入库输送线下发:{},{}", staProtocol.getWorkNo(), 1007); } } } public synchronized void in2(Integer mark) { // 根据输送线plc遍历 SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1); StaProtocol staProtocol = devpThread.getStation().get(1007); if (staProtocol == null) { return; } else { staProtocol = staProtocol.clone(); } // 判断是否满足条件 if (!staProtocol.isLoading()) { return; } if (staProtocol.getWorkNo() > 0 && staProtocol.isAutoing() && !staProtocol.isEmptyMk() && staProtocol.isPakMk()) { if (station(1007)) { Integer workNo = staProtocol.getWorkNo(); staProtocol.setWorkNo(0); staProtocol.setStaNo((short) 0); boolean result = MessageQueue.offer(SlaveType.Devp, 1, new Task(2, staProtocol)); log.info("1007站点清空:{},{}", staProtocol.getWorkNo(), 1006); StaProtocol staProtocol1006 = devpThread.getStation().get(1006); staProtocol1006.setWorkNo(workNo); staProtocol1006.setStaNo((short) 1007); boolean result2 = MessageQueue.offer(SlaveType.Devp, 1, new Task(2, staProtocol)); log.info("1006站点往前走一格:{},{}", staProtocol.getWorkNo(), 1007); } } } @Transactional public boolean station(Integer staNo) { StationRequestParam stationRequestParam = new StationRequestParam(); List staNos = new ArrayList<>(); staNos.add(staNo + ""); stationRequestParam.setStaNos(staNos); String response = ""; try { response = new HttpHandler.Builder() .setUri(ctuUrl) .setPath(station) .setTimeout(1200, TimeUnit.SECONDS) .setJson(JSON.toJSONString(stationRequestParam)) .build() .doPost(); JSONObject jsonObject = JSON.parseObject(response); if (jsonObject.getInteger("code").equals(200)) { log.info("RCS返回数据:{}", response); JSONArray data = jsonObject.getJSONArray("data"); List stationStatuses = JSONArray.parseArray(data.toString(), StationStatus.class); for (StationStatus object : stationStatuses) { if (object.getStaNo().equals(staNo + "")) { if (object.getConveyable()) { return true; } else { log.info("站点:{}状态不对", staNo); return false; } } } log.info("未返回站点状态:{}", staNo); } else { //log.error("请求接口失败!!!url:{};request:{};response:{}", ctuUrl + sendTask, JSON.toJSONString(openBusSubmitParam), response); throw new CoolException("调用下发任务接口报错"); } } catch (Exception e) { log.error("fail", e); } return false; } }