pang.jiabao
17 小时以前 15161de49e7f35e2fc926a20577905621f30554c
src/main/java/com/zy/service/impl/RcsServiceImpl.java
@@ -1,5 +1,6 @@
package com.zy.service.impl;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@@ -13,10 +14,8 @@
import com.zy.core.model.Task;
import com.zy.core.model.protocol.StaProtocol;
import com.zy.core.properties.SlaveProperties;
import com.zy.entity.RcsReporterTask;
import com.zy.entity.RcsReturn;
import com.zy.entity.WrkDetl;
import com.zy.entity.WrkMast;
import com.zy.core.thread.SiemensDevpThread;
import com.zy.entity.*;
import com.zy.enums.RcsRetMethodEnum;
import com.zy.service.ApiLogService;
import com.zy.service.RcsService;
@@ -29,11 +28,21 @@
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static com.zy.enums.RcsRetMethodEnum.APPLY_FROM_AGV;
@Slf4j
@Service
@@ -41,6 +50,9 @@
    @Value("${dj.url}")
    public String DJ_URL;
    @Value("${hik.url}")
    private String HIK_URL;
    @Resource
    private WrkMastService wrkMastService;
@@ -74,19 +86,86 @@
            if (singleRobotCode.equals("14") || singleRobotCode.equals("15")) { // 两台CTU库机器人编号
                WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("task_no", robotTaskCode));
                if (wrkMast != null) {
                    if (Objects.requireNonNull(RcsRetMethodEnum.getEnum(method)) == RcsRetMethodEnum.TASK_START) {
                        // 开始
                        wrkMast.setModiTime(now);
                        wrkMastService.updateById(wrkMast);
                    } else if (Objects.requireNonNull(RcsRetMethodEnum.getEnum(method)) == RcsRetMethodEnum.PICK_COMPLETE) {
                        // 取货完成,清除输送线任务号
                        SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                        StaProtocol staProtocol = siemensDevpThread.getStation().get(106).clone();
                        // 取货完成清除输送线任务号
                        if (Objects.equals(staProtocol.getWorkNo(), wrkMast.getWrkNo())) {
                            staProtocol.setWorkNo(0);
                            staProtocol.setStaNo((short) 0);
                            boolean result = MessageQueue.offer(SlaveType.Devp, 1, new Task(2, staProtocol));
                            News.info("CTU取货完成,给站点写0工作号,下发任务:{},站点:{},agv任务号:{}", result, 106, robotTaskCode);
                            if (!result) {
                                rcsReturn.setCode("Err_Internal");
                                rcsReturn.setMessage("清除输送线任务号命令下发失败");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                                return rcsReturn;
                            }
                        } else {
                            News.warn("CTU取货完成任务号{},清除输送线任务号:{},任务号不同", wrkMast.getWrkNo(), staProtocol.getWorkNo());
                        }
                    } else if (method.equals(APPLY_FROM_AGV.getCode())) {
                        // 放货申请
                        SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                        StaProtocol staProtocol = siemensDevpThread.getStation().get(101).clone();
                        if (staProtocol == null) {
                            rcsReturn.setCode("Err_Internal");
                            rcsReturn.setMessage("101站点线程为空");
                            JSONObject data = new JSONObject();
                            data.put("robotTaskCode", robotTaskCode);
                            rcsReturn.setData(data);
                            return rcsReturn;
                        }
                        if (staProtocol.isOutEnable() && staProtocol.isAutoing() && !staProtocol.isLoading() && staProtocol.getWorkNo() == 0) { // 可出
                            RcsTaskContinue rcsTaskContinue = new RcsTaskContinue();
                            rcsTaskContinue.setRobotTaskCode(robotTaskCode);
                            rcsTaskContinue.setTriggerType("TASK");
                            rcsTaskContinue.setTriggerCode(robotTaskCode);
                            String url = HIK_URL + "api/robot/controller/task/extend/continue";
                            String response = sendPost(url, JSONObject.toJSONString(rcsTaskContinue));
                            if (!StringUtils.isEmpty(response) && response.contains("code")) {
                                RcsReturn rcsReturn1 = JSONObject.parseObject(response, RcsReturn.class);
                                if (!"SUCCESS".equals(rcsReturn1.getCode())) {
                                    // 返回RCS
                                    rcsReturn.setCode("Err_Internal");
                                    rcsReturn.setMessage("返回继续执行失败");
                                    JSONObject data = new JSONObject();
                                    data.put("robotTaskCode", robotTaskCode);
                                    rcsReturn.setData(data);
                                    return rcsReturn;
                                }
                            }
                        } else {
                            News.warn("站点{}不满足放货条件,自动:{},无物:{},可出:{},任务号:{}",
                                    101, staProtocol.isAutoing(), staProtocol.isLoading(), staProtocol.isOutEnable(), staProtocol.getWorkNo());
                            rcsReturn.setCode("Err_Internal");
                            rcsReturn.setMessage("站点不满足放货条件");
                            JSONObject data = new JSONObject();
                            data.put("robotTaskCode", robotTaskCode);
                            rcsReturn.setData(data);
                            return rcsReturn;
                        }
                    } else if (Objects.requireNonNull(RcsRetMethodEnum.getEnum(method)) == RcsRetMethodEnum.TASK_END) {
                        // 任务完成
                    Integer ioType = wrkMast.getIoType();
                    if (Objects.requireNonNull(RcsRetMethodEnum.getEnum(method)) == RcsRetMethodEnum.TASK_END) {
                        if ((ioType == 101 || ioType == 110 || ioType == 103) && wrkMast.getWrkSts() == 12) {
                        if (ioType == 1 || ioType == 10 || ioType == 53 || ioType == 57) {
                            wrkMast.setWrkSts(4L);
                            wrkMast.setCrnEndTime(now);
                            wrkMast.setModiTime(now);
                            wrkMastService.updateById(wrkMast);
                        } else if ((ioType == 101 || ioType == 110 || ioType == 103 || ioType == 107) && wrkMast.getWrkSts() == 12) {
                            // 给输送线下发命令
                            for (DevpSlave devp : slaveProperties.getDevp()) {
                                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                                Map<Integer, StaProtocol> station = devpThread.getStation();
                                StaProtocol staProtocol = station.get(wrkMast.getSourceStaNo());
                                if (staProtocol == null) {
                                    continue;
                                } else {
                                    staProtocol = staProtocol.clone();
                                }
                                StaProtocol staProtocol = devpThread.getStation().get(wrkMast.getSourceStaNo()).clone();
                                staProtocol.setWorkNo(wrkMast.getWrkNo());
                                staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
                                boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
@@ -188,4 +267,123 @@
        return rcsReturn;
    }
    @Override
    public RcsReturn reporterEqpt(ReporterEqpt param) {
        String robotTaskCode = param.getTaskCode();
        String method = param.getMethod();
        RcsReturn rcsReturn = new RcsReturn();
        if (method.equals(APPLY_FROM_AGV.getCode())) {
            SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
            StaProtocol staProtocol = siemensDevpThread.getStation().get(101).clone();
            if (staProtocol == null) {
                rcsReturn.setCode("Err_Internal");
                rcsReturn.setMessage("101站点线程为空");
                JSONObject data = new JSONObject();
                data.put("robotTaskCode", robotTaskCode);
                rcsReturn.setData(data);
                return rcsReturn;
            }
            if (staProtocol.isOutEnable() && staProtocol.isAutoing() && !staProtocol.isLoading() && staProtocol.getWorkNo() == 0) { // 可出
                EqptNotify eqptNotify = new EqptNotify(param.getEqptCode(), robotTaskCode, "6", "101");
                String url = HIK_URL + "spi/wcs/robot/eqpt/notify";
                String response = sendPost(url, JSONObject.toJSONString(eqptNotify));
                if (!StringUtils.isEmpty(response) && response.contains("code")) {
                    RcsReturn rcsReturn1 = JSONObject.parseObject(response, RcsReturn.class);
                    if (!"0".equals(rcsReturn1.getCode())) {
                        // 返回RCS
                        rcsReturn.setCode("Err_Internal");
                        rcsReturn.setMessage("通知RCS继续放货失败");
                        JSONObject data = new JSONObject();
                        data.put("robotTaskCode", robotTaskCode);
                        rcsReturn.setData(data);
                        return rcsReturn;
                    }
                } else {
                    rcsReturn.setCode("Err_Internal");
                    rcsReturn.setMessage("请求url" + url + "失败");
                    JSONObject data = new JSONObject();
                    data.put("robotTaskCode", robotTaskCode);
                    rcsReturn.setData(data);
                    return rcsReturn;
                }
            } else {
                News.warn("站点{}不满足放货条件,自动:{},无物:{},可出:{},任务号:{}",
                        101, staProtocol.isAutoing(), staProtocol.isLoading(), staProtocol.isOutEnable(), staProtocol.getWorkNo());
                rcsReturn.setCode("Err_Internal");
                rcsReturn.setMessage("站点不满足放货条件");
                JSONObject data = new JSONObject();
                data.put("robotTaskCode", robotTaskCode);
                rcsReturn.setData(data);
                return rcsReturn;
            }
        }
        rcsReturn.setCode("SUCCESS");
        rcsReturn.setMessage("成功!");
        JSONObject data = new JSONObject();
        data.put("extra", "null");
        rcsReturn.setData(data);
        log.info("agv放货请求返回:{}", JSONObject.toJSONString(rcsReturn));
        return rcsReturn;
    }
    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url   发送请求的 URL
     * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     */
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        try {
            log.info("sendPost - {} - {}", url, param);
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Content-Length", "<calculated when request is sent>");
            conn.setRequestProperty("Host", "<calculated when request is sent>");
            conn.setRequestProperty("Accept", "*/*");
            conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
            conn.setRequestProperty("Connection", "keep-alive");
            conn.setRequestProperty("X-lr-request-id", String.valueOf(new Date().getTime()));
            conn.setRequestProperty("X-lr-version", "4.3");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            log.info("recv - {}", result);
        } catch (ConnectException e) {
            log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
        } catch (SocketTimeoutException e) {
            log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
        } catch (IOException e) {
            log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
        } catch (Exception e) {
            log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
            }
        }
        return result.toString();
    }
}