package com.zy.asrs.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.core.common.Cools; import com.core.exception.CoolException; import com.zy.asrs.entity.AgvBasDevp; import com.zy.asrs.entity.AgvLocMast; import com.zy.asrs.entity.AgvWrkMast; import com.zy.asrs.entity.param.AgvTaskCreateParam; import com.zy.asrs.entity.param.AgvTaskParam; import com.zy.asrs.mapper.AgvWrkMastMapper; import com.zy.asrs.service.*; import com.zy.common.service.AgvCommonService; import com.zy.common.utils.HttpHandler; 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 java.io.IOException; import java.util.*; @Service @Slf4j public class AgvWrkMastServiceImp extends ServiceImpl implements AgvWrkMastService { @Value("${agv.url}") private String url; @Value("${agv.taskCreatePath}") private String taskCreatePath; @Value("${agv.taskCancelPath}") private String taskCancelPath; @Value("${agv.containerMoveInPath}") private String containerMoveInPath; @Value("${agv.containerMoveOutPath}") private String containerMoveOutPath; @Value("${agv.containerArrivedPath}") private String containerArrivedPath; @Autowired AgvWrkMastMapper agvWrkMastMapper; @Autowired AgvWrkDetlService agvWrkDetlService; @Autowired AgvLocMastService agvLocMastService; @Autowired ApiLogService apiLogService; @Autowired AgvBasDevpService agvBasDevpService; @Autowired private AgvCommonService agvCommonService; @Autowired private AgvWrkMastService agvWrkMastService; public void updateWrkStsByWrkNo(int wrkNo, long wrkSts) { AgvWrkMast agvWrkMast = this.selectById(wrkNo); //判断要修改的工作档状态是否合理,如果不合理则抛出异常 checkWrkSts(agvWrkMast,wrkSts); agvWrkMast.setWrkSts(wrkSts); this.updateById(agvWrkMast); } // TODO 判断规则 private boolean checkWrkSts(AgvWrkMast agvWrkMast,long wrkSts){ return true; } public int startWrk(AgvWrkMast agvWrkMast) throws IOException { AgvTaskCreateParam agvTaskCreateParam = new AgvTaskCreateParam(); //agvTaskCreateParam.setTaskType(taskType); getRequestParam(agvTaskCreateParam,agvWrkMast); return doHttpRequest(agvTaskCreateParam,"搬运任务下发",url, taskCreatePath,null,"127.0.0.1"); //return containerMoveParam; } public int startAllcationIn(AgvWrkMast agvWrkMast) throws IOException { AgvBasDevp basDevp = agvBasDevpService.selectByDevNo(agvWrkMast.getSourceLocNo()); //检索库位,选择合适的库位 AgvLocMast locMast = agvCommonService.getLocNo(basDevp.getLocType1(),basDevp.getFloor()); agvWrkMast.setLocNo(locMast.getLocNo()); agvWrkMast.setWrkSts(201L); agvWrkMastService.updateById(agvWrkMast); return 0; } //容器入场 public int containerMoveIn(List agvWrkMastList) throws IOException { //调用容器入场时所需要参数 Map>> containerMoveParam = new HashMap<>(); List> positionCodeMapList = new ArrayList<>(); containerMoveParam.put("containerMoveIns",positionCodeMapList); getContainerMoveParam(agvWrkMastList,positionCodeMapList); if(Cools.isEmpty(positionCodeMapList)){ return 0; } return doHttpRequest(containerMoveParam,"容器入场任务下发",url, containerMoveInPath,null,"127.0.0.1"); } //容器离场 public int containerMoveOut(List agvWrkMastList){ Map>> containerMoveParam = new HashMap<>(); List> positionCodeMapList = new ArrayList<>(); containerMoveParam.put("containerMoveOuts",positionCodeMapList); for(AgvWrkMast agvWrkMast : agvWrkMastList){ Map positionCodeMap = new HashMap<>(); positionCodeMap.put("positionCode",agvWrkMast.getLocNo()); positionCodeMapList.add(positionCodeMap); } return doHttpRequest(containerMoveParam,"容器离场任务下发",url, containerMoveOutPath,null,"127.0.0.1"); } public boolean insertByIncrease(AgvWrkMast agvWrkMast) { int index = this.baseMapper.insertByIncrease(agvWrkMast); return index > 0; } @Override public boolean deleteByWrkNo(int wrkNo) { return this.delete(new EntityWrapper().eq("wrk_no", wrkNo)); } //容器到达通知 public int containerArrived(AgvWrkMast agvWrkMast) { Map containerArrivedParam = new HashMap<>(); containerArrivedParam.put("slotCode",agvWrkMast.getSourceLocNo()); containerArrivedParam.put("containerCode",agvWrkMast.getBarcode()); return doHttpRequest(containerArrivedParam,"容器达到通知",url, containerArrivedPath,null,"127.0.0.1"); } @Override public List selectReadyAgvWrkMast() { List stationCodes = agvBasDevpService.selectAllStationCode(); Wrapper wrapper = new EntityWrapper().eq("wrk_sts", 21L).like(false, "loc_no", "@"); wrapper.in("loc_no",stationCodes); return this.selectList(wrapper); //return this.baseMapper.selectReadyAgvWrkMast(); } @Override public AgvWrkMast selectByContainerCode(String containerCode) { return this.selectOne(new EntityWrapper().eq("barcode",containerCode)); } @Override public int callCancelTask(int wrkNo) { AgvTaskCreateParam agvTaskCreateParam = new AgvTaskCreateParam(); agvTaskCreateParam.setTaskCode(wrkNo + ""); return doHttpRequest(agvTaskCreateParam,"取消任务下发",url, taskCancelPath,null,"127.0.0.1"); } private void getContainerMoveParam(List agvWrkMastList,List> positionCodeMapList){ //往容器入场参数中放入源站点位置 for(AgvWrkMast agvWrkMast : agvWrkMastList){ //判断是否为输送线入库,是则不需要容器进场请求 if("Y".equals(agvWrkMast.getMk())){ continue; } Map positionCodeMap = new HashMap<>(); positionCodeMap.put("positionCode",agvWrkMast.getSourceLocNo()); positionCodeMapList.add(positionCodeMap); } } private void getRequestParam(AgvTaskCreateParam agvTaskCreateParam,AgvWrkMast agvWrkMast){ List agvTaskParamList = Arrays.asList( //起始位 new AgvTaskParam(agvWrkMast.getSourceLocNo()), //目标位 new AgvTaskParam(agvWrkMast.getLocNo())); // //起始位 // agvTaskParamList.add(new AgvTaskParam(agvWrkMast.getSourceLocNo())); // //目标位 // agvTaskParamList.add(new AgvTaskParam(agvWrkMast.getLocNo())); if(agvWrkMast.getIoType() == 53 || agvWrkMast.getIoType() == 57 ){ agvWrkMast.setWrkNo(-agvWrkMast.getWrkNo()); } agvTaskCreateParam.setTaskCode(agvWrkMast.getWrkNo().toString()); if (agvWrkMast.getIoType() == 108 || (agvWrkMast.getIoType() == 109 && agvWrkMast.getWrkSts() == 201) || agvWrkMast.getIoType() == 12) { agvTaskCreateParam.setTaskTyp("F06"); } else { agvTaskCreateParam.setTaskTyp("F01"); } agvTaskCreateParam.setPositionCodePath(agvTaskParamList); } private int doHttpRequest(Object requestParam, String namespace, String url, String path, String appkey, String ip){ String response = ""; boolean success = false; try { response = new HttpHandler.Builder() .setUri(url) .setPath(path) .setJson(JSONObject.toJSONString(requestParam)) .build() .doPost(); JSONObject jsonObject = JSON.parseObject(response); int code = Integer.parseInt(jsonObject.get("code").toString()); if(code != 0){ throw new CoolException("调用AGV响应错误"); } success = true; return code; }catch (Exception e){ log.error(e.getMessage()); throw new CoolException("调用AGV响应错误"); }finally { apiLogService.save( namespace, url + path, appkey, ip, JSON.toJSONString(JSONObject.toJSONString(requestParam)), response, success ); } } }