diff --git a/src/main/java/com/gaotao/modules/dashboard/task/DashboardPushTask.java b/src/main/java/com/gaotao/modules/dashboard/task/DashboardPushTask.java index 3d8f11e..f9e28bb 100644 --- a/src/main/java/com/gaotao/modules/dashboard/task/DashboardPushTask.java +++ b/src/main/java/com/gaotao/modules/dashboard/task/DashboardPushTask.java @@ -1336,5 +1336,213 @@ public class DashboardPushTask { return "idle"; } + + /** + * 定时推送异常看板数据 + * + *

功能说明:每10秒从WMS系统获取异常数据并推送到前端

+ * + *

数据来源:

+ * + * + *

配置开关:

+ * + */ + @Scheduled(fixedRate = 10000) + public void pushExceptionBoardData() { + // 检查总开关 + if (!dashboardPushEnabled) { + log.trace("看板推送已禁用"); + return; + } + + try { + // 从WMS获取异常数据 + Map data = getExceptionDataFromWms(); + + // 如果返回null,转换为空数据 + if (data == null) { + data = createEmptyExceptionData(); + } + + // 计算数据哈希值并推送 + int currentHash = data.hashCode(); + webSocketService.pushExceptionBoardData(data); + lastDataHash.put("exception-board", currentHash); + + } catch (Exception e) { + log.error("推送异常看板数据失败,推送空数据: {}", e.getMessage(), e); + // 异常时推送空数据,避免前端显示过期数据 + try { + Map emptyData = createEmptyExceptionData(); + webSocketService.pushExceptionBoardData(emptyData); + lastDataHash.put("exception-board", emptyData.hashCode()); + } catch (Exception ex) { + log.error("推送空数据失败: {}", ex.getMessage()); + } + } + } + + /** + * 从WMS系统获取异常数据 + * + *

数据来源:

+ * + * + * @return 异常数据 + */ + private Map getExceptionDataFromWms() { + List> exceptionList = new ArrayList<>(); + + try { + // 调用WMS异常查询API + String url = wcsUrl + "query-task-error-info"; + log.debug("调用WMS异常查询API: {}", url); + + // 构造POST请求参数 - 传入设备编码1101 + String jsonBody = "\"1101\""; + String wmsResponse = HttpUtils.doPost(url, jsonBody, null); + log.debug("WMS API返回数据: {}", wmsResponse); + + // 解析JSON数据 + ObjectMapper mapper = new ObjectMapper(); + JsonNode rootNode = mapper.readTree(wmsResponse); + + // 检查返回码 + int resCode = rootNode.get("resCode").asInt(); + if (resCode != 200) { + log.warn("WMS异常查询API返回错误: code={}", resCode); + return createExceptionBoardData(exceptionList); + } + + // 解析resMsg + String resMsg = rootNode.has("resMsg") ? rootNode.get("resMsg").asText() : ""; + + // 检查是否有真实异常(排除"无异常"的情况) + if (resMsg != null && !resMsg.isEmpty() && !resMsg.equals("无异常")) { + Map exception = parseExceptionMessage(resMsg); + if (exception != null) { + exceptionList.add(exception); + } + } else { + log.debug("WMS返回无异常信息: {}", resMsg); + } + + log.info("从WMS获取到{}条异常数据", exceptionList.size()); + + } catch (Exception e) { + log.error("从WMS获取异常数据失败", e); + } + + return createExceptionBoardData(exceptionList); + } + + /** + * 解析异常消息 + * 消息格式:托盘号:W00108,异常信息:数据对比异常,多盘标签:["A552025111800000129"],少盘标签:[] + * + * @param resMsg 异常消息 + * @return 解析后的异常数据 + */ + private Map parseExceptionMessage(String resMsg) { + try { + Map exception = new HashMap<>(); + + // 提取托盘号 + String palletCode = ""; + if (resMsg.contains("托盘号:")) { + int start = resMsg.indexOf("托盘号:") + 4; + int end = resMsg.indexOf(",", start); + if (end > start) { + palletCode = resMsg.substring(start, end).trim(); + } + } + + // 提取异常信息 + String exceptionInfo = ""; + String exceptionType = "数据对比异常"; + if (resMsg.contains("异常信息:")) { + int start = resMsg.indexOf("异常信息:") + 5; + exceptionInfo = resMsg.substring(start).trim(); + + // 提取异常类型 + if (exceptionInfo.contains(",")) { + exceptionType = exceptionInfo.substring(0, exceptionInfo.indexOf(",")); + } + } + + // 提取多盘标签 + String moreTags = ""; + if (resMsg.contains("多盘标签:")) { + int start = resMsg.indexOf("多盘标签:") + 5; + int end = resMsg.indexOf("],", start); + if (end > start) { + moreTags = resMsg.substring(start, end + 1).trim(); + } + } + + // 提取少盘标签 + String lessTags = ""; + if (resMsg.contains("少盘标签:")) { + int start = resMsg.indexOf("少盘标签:") + 5; + lessTags = resMsg.substring(start).trim(); + } + + // 构造异常数据 + exception.put("palletCode", palletCode); + exception.put("exceptionType", exceptionType); + exception.put("description", exceptionInfo); + exception.put("moreTags", moreTags); + exception.put("lessTags", lessTags); + exception.put("occurTime", java.time.LocalDateTime.now().format( + java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + exception.put("status", "待处理"); + exception.put("location", "1101"); // 设备编码作为位置 + + return exception; + + } catch (Exception e) { + log.error("解析异常消息失败: {}", resMsg, e); + return null; + } + } + + /** + * 创建异常看板数据结构 + * + * @param exceptionList 异常列表 + * @return 看板数据 + */ + private Map createExceptionBoardData(List> exceptionList) { + Map data = new HashMap<>(); + data.put("exceptionList", exceptionList != null ? exceptionList : new ArrayList<>()); + data.put("totalCount", exceptionList != null ? exceptionList.size() : 0); + data.put("updateTime", java.time.LocalDateTime.now().format( + java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + return data; + } + + /** + * 创建空的异常看板数据 + * + * @return 空数据结构 + */ + private Map createEmptyExceptionData() { + Map data = new HashMap<>(); + data.put("exceptionList", new ArrayList<>()); + data.put("totalCount", 0); + data.put("updateTime", java.time.LocalDateTime.now().format( + java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + return data; + } }