| | |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.Collection; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 定时:GET RCS /station/getTaskNo;POST RCS /station/getError;任务号存在时调 WMS queryTask 合并料箱与物料明细进 Redis |
| | | * 定时:GET RCS /cv/station/getTaskNo;POST RCS /cv/station/getError;任务号存在时调 WMS queryTask 合并料箱与物料明细进 Redis |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class TvRcsStationPollService { |
| | | private static final int MAX_LOG_LEN = 2000; |
| | | |
| | | @Resource |
| | | private RestTemplate restTemplate; |
| | |
| | | String stationId = pollProperties.getTaskNoStationId(); |
| | | if (StringUtils.hasText(pollProperties.getTaskNoPollUrl())) { |
| | | try { |
| | | String raw = restTemplate.getForObject(pollProperties.getTaskNoPollUrl(), String.class); |
| | | String raw = pollTaskNoRaw(); |
| | | String taskNo = applyTaskNoResponse(raw, stationId); |
| | | enrichSnapshotFromWms(stationId, taskNo); |
| | | } catch (RestClientException e) { |
| | |
| | | try { |
| | | Map<String, Object> res = wmsServerFeignClient.openAsrsQueryTask(req); |
| | | if (res == null) { |
| | | wcsStationStatusService.upsertFromRcsPoll(stationId, taskNo, null); |
| | | // wcsStationStatusService.upsertFromRcsPoll(stationId, taskNo, null); |
| | | wcsStationStatusService.clearStationSnapshot(stationId); |
| | | return; |
| | | } |
| | | Object codeObj = res.get("code"); |
| | | int code = parseCode(codeObj); |
| | | if (code != 200) { |
| | | log.debug("WMS queryTask 非成功 code={} msg={}", code, res.get("msg")); |
| | | wcsStationStatusService.upsertFromRcsPoll(stationId, taskNo, null); |
| | | // wcsStationStatusService.upsertFromRcsPoll(stationId, taskNo, null); |
| | | wcsStationStatusService.clearStationSnapshot(stationId); |
| | | return; |
| | | } |
| | | Object data = res.get("data"); |
| | | @SuppressWarnings("unchecked") |
| | | Map<String, Object> dataMap = data instanceof Map ? (Map<String, Object>) data : null; |
| | | if (dataMap == null || !hasWrkDetls(dataMap)) { |
| | | // queryTask 空数据时清空站点缓存,避免电视机显示历史物料 |
| | | wcsStationStatusService.clearStationSnapshot(stationId); |
| | | return; |
| | | } |
| | | wcsStationStatusService.upsertFromRcsPoll(stationId, taskNo, dataMap); |
| | | } catch (Exception e) { |
| | | log.warn("WMS queryTask 调用失败: {}", e.getMessage()); |
| | | wcsStationStatusService.upsertFromRcsPoll(stationId, taskNo, null); |
| | | // wcsStationStatusService.upsertFromRcsPoll(stationId, taskNo, null); |
| | | wcsStationStatusService.clearStationSnapshot(stationId); |
| | | } |
| | | } |
| | | |
| | | private static boolean hasWrkDetls(Map<String, Object> dataMap) { |
| | | if (dataMap == null) { |
| | | return false; |
| | | } |
| | | Object wrkDetls = dataMap.get("wrkDetls"); |
| | | if (wrkDetls == null) { |
| | | return false; |
| | | } |
| | | if (wrkDetls instanceof Collection) { |
| | | return !((Collection<?>) wrkDetls).isEmpty(); |
| | | } |
| | | if (wrkDetls.getClass().isArray()) { |
| | | return java.lang.reflect.Array.getLength(wrkDetls) > 0; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | private static int parseCode(Object codeObj) { |
| | |
| | | } |
| | | |
| | | private String pollErrorRaw() throws Exception { |
| | | String url = pollProperties.getErrorPollUrl(); |
| | | if (pollProperties.isErrorPollUsePost()) { |
| | | ObjectNode body = objectMapper.createObjectNode(); |
| | | body.put("staNo", pollProperties.getErrorStationId()); |
| | | String reqBody = objectMapper.writeValueAsString(createStationBody(pollProperties.getErrorStationId())); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | | HttpEntity<String> entity = new HttpEntity<>(objectMapper.writeValueAsString(body), headers); |
| | | HttpEntity<String> entity = new HttpEntity<>(reqBody, headers); |
| | | log.info("RCS站点轮询请求 POST /cv/station/getError url={} body={}", url, trimForLog(reqBody)); |
| | | ResponseEntity<String> resp = restTemplate.exchange( |
| | | pollProperties.getErrorPollUrl(), HttpMethod.POST, entity, String.class); |
| | | return resp.getBody(); |
| | | url, HttpMethod.POST, entity, String.class); |
| | | String respBody = resp.getBody(); |
| | | log.info("RCS站点轮询响应 POST /cv/station/getError body={}", trimForLog(respBody)); |
| | | return respBody; |
| | | } |
| | | return restTemplate.getForObject(pollProperties.getErrorPollUrl(), String.class); |
| | | log.info("RCS站点轮询请求 GET /cv/station/getError url={}", url); |
| | | String raw = restTemplate.getForObject(url, String.class); |
| | | log.info("RCS站点轮询响应 GET /cv/station/getError body={}", trimForLog(raw)); |
| | | return raw; |
| | | } |
| | | |
| | | private String pollTaskNoRaw() throws Exception { |
| | | String url = pollProperties.getTaskNoPollUrl(); |
| | | if (pollProperties.isTaskNoPollUsePost()) { |
| | | String reqBody = objectMapper.writeValueAsString(createStationBody(pollProperties.getTaskNoStationId())); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | | HttpEntity<String> entity = new HttpEntity<>(reqBody, headers); |
| | | // log.info("RCS站点轮询请求 GET /cv/station/getTaskNo url={}", url); |
| | | log.info("RCS站点轮询请求 POST /cv/station/getTaskNo url={} body={}", url, trimForLog(reqBody)); |
| | | ResponseEntity<String> resp = restTemplate.exchange( |
| | | url, HttpMethod.POST, entity, String.class); |
| | | String respBody = resp.getBody(); |
| | | // log.info("RCS站点轮询响应 GET /cv/station/getTaskNo body={}", trimForLog(raw)); |
| | | log.info("RCS站点轮询响应 POST /cv/station/getTaskNo body={}", trimForLog(respBody)); |
| | | return respBody; |
| | | } |
| | | log.info("RCS站点轮询请求 GET /cv/station/getTaskNo url={}", url); |
| | | String raw = restTemplate.getForObject(url, String.class); |
| | | log.info("RCS站点轮询响应 GET /cv/station/getTaskNo body={}", trimForLog(raw)); |
| | | return raw; |
| | | } |
| | | |
| | | private ObjectNode createStationBody(String stationId) { |
| | | ObjectNode body = objectMapper.createObjectNode(); |
| | | if (StringUtils.hasText(stationId)) { |
| | | body.put("stationId", stationId); |
| | | body.put("staNo", stationId); |
| | | } |
| | | return body; |
| | | } |
| | | |
| | | /** 写入 tvRcs 任务号 Hash;返回解析出的 taskNo(用于 WMS 合并) */ |
| | |
| | | } |
| | | return arr; |
| | | } |
| | | |
| | | private static String trimForLog(String raw) { |
| | | if (raw == null) { |
| | | return "null"; |
| | | } |
| | | String text = raw.trim(); |
| | | if (text.length() <= MAX_LOG_LEN) { |
| | | return text; |
| | | } |
| | | return text.substring(0, MAX_LOG_LEN) + "...(truncated)"; |
| | | } |
| | | } |