package com.zy.common.task; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.core.common.Cools; import com.zy.core.cache.MessageQueue; import com.zy.core.cache.SlaveConnection; import com.zy.core.enums.SlaveType; import com.zy.core.model.Task; import com.zy.core.model.protocol.StaProtocol; import com.zy.core.thread.SiemensDevpThread; import com.zy.entity.BasDevp; import com.zy.entity.WrkDetl; import com.zy.entity.WrkMast; import com.zy.enums.AgvApi; import com.zy.service.BasDevpService; import com.zy.service.WrkDetlService; import com.zy.service.WrkMastService; import com.zy.utils.HttpHandler; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; /** * @author pang.jiabao * @description 冠鸿龙南专用定时任务 * @createDate 2024/7/10 9:20 */ @Slf4j @Component public class GhlnWcsScheduler { @Autowired private BasDevpService basDevpService; @Autowired private WrkMastService wrkMastService; @Resource private WrkDetlService wrkDetlService; /** * wcs开门完成,通知agv取货放货 */ @Scheduled(cron = "0/5 * * * * ?") private void wcsOpenComplete() { SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1); StaProtocol staProtocol305 = devpThread.getStation().get(305); if (staProtocol305!= null && staProtocol305.getIfOpenDoor()) { BasDevp basDevp = basDevpService.selectOne(new EntityWrapper().eq("dev_no", 305)); if (basDevp.getWrkNo() != 0) { WrkMast wrkMast = wrkMastService.selectById(basDevp.getWrkNo()); if (wrkMast == null || wrkMast.getWhsType() == null || wrkMast.getWhsType() == 3){ return; } if (wrkMast.getWhsType() == 4) { return; } WrkDetl wrkDetl = wrkDetlService.selectOne(new EntityWrapper().eq("wrk_no", wrkMast.getWrkNo())); // 判断开门完成,则通知agv可以放货 // 调用结果 boolean success = false; // 构造请求头 Map headers = new HashMap<>(); headers.put("Content-Type", "application/json;charset=UTF-8"); // 构造请求体 JSONObject jsonObject = new JSONObject(); jsonObject.put("matnr",wrkDetl.getMatnr()); jsonObject.put("batch",wrkDetl.getBatch()); jsonObject.put("flag",wrkMast.getIoType() > 100 ? "0" : "1"); String body = jsonObject.toJSONString(); String response = ""; try { response = new HttpHandler.Builder() .setUri(AgvApi.OPEN_COMPLETE_PUSH.getURI()) .setPath(AgvApi.OPEN_COMPLETE_PUSH.getPath()) .setHeaders(headers) .setJson(body) .build() .doPost(); if (!Cools.isEmpty(response)) { JSONObject jsonObject1 = JSONObject.parseObject(response); if ((Integer) jsonObject1.get("code") == 200) { success = true; wrkMast.setWhsType(3); wrkMastService.updateById(wrkMast); } } else { log.error("wcs开门完成通知agv接口异常接口失败!!!url:{};request:{};response:{}", "", body, response); } } catch (Exception e) { log.error("wcs开门完成通知agv接口异常:{}", e.getMessage()); } finally { log.error("wcs开门完成通知agv接口:{}", success); } } } } /** * 读已关门 */ @Scheduled(cron = "0/5 * * * * ?") private void wcsCloseComplete() { SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1); StaProtocol staProtocol = devpThread.getStation().get(305); if (staProtocol == null) { return; } else { staProtocol = staProtocol.clone(); } BasDevp basDevp = basDevpService.selectOne(new EntityWrapper().eq("dev_no", 305)); if (basDevp.getWrkNo() != 0 && basDevp.getWrkNo() != 9999) { WrkMast wrkMast = wrkMastService.selectById(basDevp.getWrkNo()); if (wrkMast == null || wrkMast.getWhsType() == null ||wrkMast.getWhsType() != 4) { return; } if (wrkMast.getIoType() == 1 && wrkMast.getWrkSts() == 1) { staProtocol.setWorkNo(wrkMast.getWrkNo()); staProtocol.setStaNo(wrkMast.getStaNo().shortValue()); if(!MessageQueue.offer(SlaveType.Devp, 1, new Task(2, staProtocol))){ log.error("入库关门完成失败"); } wrkMast.setWrkSts(2L); wrkMastService.updateById(wrkMast); basDevp.setWrkNo(staProtocol.getWorkNo()); basDevpService.updateById(basDevp); } else if (wrkMast.getIoType() == 101) { staProtocol.setWorkNo(9999); staProtocol.setStaNo(wrkMast.getSourceStaNo().shortValue()); if(!MessageQueue.offer(SlaveType.Devp, 1, new Task(2, staProtocol))){ log.error("出库关门完成失败"); } basDevp.setWrkNo(staProtocol.getWorkNo()); basDevpService.updateById(basDevp); } } } }