From fd2baf7d338ed08b8cea1d5ab8b3fac922f62b06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BF=97=E6=88=90?= <15187855430@163.com> Date: Mon, 17 Feb 2025 14:09:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=86=E7=A0=81=E8=BE=93=E5=85=A5=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/KeypadServiceImpl.java | 6 +- .../modules/keypad/util/WebSocketClient.java | 65 ++++++++++++++----- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/dpkj/modules/keypad/service/impl/KeypadServiceImpl.java b/src/main/java/com/dpkj/modules/keypad/service/impl/KeypadServiceImpl.java index c577f03..8776bf3 100644 --- a/src/main/java/com/dpkj/modules/keypad/service/impl/KeypadServiceImpl.java +++ b/src/main/java/com/dpkj/modules/keypad/service/impl/KeypadServiceImpl.java @@ -79,8 +79,8 @@ public class KeypadServiceImpl implements KeypadService { // 适当延长等待时间 while (true) { TimeUnit.SECONDS.sleep(1); - String password = webSocketClient.getPassword(); - if (password.length() >= plaintextParam.getIntValue("MAXLength")) { + Boolean confirm = webSocketClient.getConfirm(); + if (confirm) { break; } } @@ -88,7 +88,7 @@ public class KeypadServiceImpl implements KeypadService { e.printStackTrace(); } String password = webSocketClient.getPassword(); - return Result.ok(password); + return Result.ok("密码输入完成!", password); } else { return Result.error("密码输入异常!详情:" + connectResult.getDesc()); } diff --git a/src/main/java/com/dpkj/modules/keypad/util/WebSocketClient.java b/src/main/java/com/dpkj/modules/keypad/util/WebSocketClient.java index e715261..c75c9fb 100644 --- a/src/main/java/com/dpkj/modules/keypad/util/WebSocketClient.java +++ b/src/main/java/com/dpkj/modules/keypad/util/WebSocketClient.java @@ -1,18 +1,21 @@ package com.dpkj.modules.keypad.util; import com.alibaba.fastjson.JSONObject; -import lombok.extern.slf4j.Slf4j; import javax.websocket.*; import java.io.IOException; import java.net.URI; +import java.util.HashMap; +import java.util.Map; @ClientEndpoint -@Slf4j public class WebSocketClient { private Session session; + // 密码 private String password = ""; + // 是否确认 + private boolean confirm = false; public WebSocketClient(String uri) { try { @@ -25,11 +28,13 @@ public class WebSocketClient { @OnOpen public void onOpen(Session session) { + System.out.println("Connected to WebSocket server"); this.session = session; } @OnMessage public void onMessage(String message) { + System.out.println("Received message: " + message); // 解析外层 JSON 对象 JSONObject outerObj = JSONObject.parseObject(message); @@ -41,24 +46,38 @@ public class WebSocketClient { Integer keyValue = paramObj.getInteger("Key"); if (keyValue != null) { - Integer i = parseAsciiToDigit(keyValue); - if (i != -1) { - password += i.toString(); + // 更正 + if (keyValue == 8) { + password = password.substring(0, password.length() - 1); + // 取消 + } else if (keyValue == 27) { + password = ""; + // 确认 + } else if (keyValue == 13) { + confirm = true; + } else if (keyValue == 2) { + password += "00"; + } else { + password += KEYPAD_MAPPING.get("0x" + Integer.toHexString(keyValue)); } } else { - log.info("按键事件返回值,未找到 Key 字段或其值为空。"); + System.out.println("未找到 Key 字段或其值为空。"); } } @OnClose public void onClose(Session session, CloseReason closeReason) { - + System.out.println("Connection closed: " + closeReason.getReasonPhrase()); } public String getPassword() { return password; } + public Boolean getConfirm() { + return confirm; + } + public void close() { try { if (session != null && session.isOpen()) { @@ -69,14 +88,28 @@ public class WebSocketClient { } } - public static Integer parseAsciiToDigit(Integer asciiValue) { - // 检查输入的 ASCII 值是否在 '0' 到 '9' 的范围内 - if (asciiValue >= 48 && asciiValue <= 57) { - // 将 ASCII 值转换为对应的数字 - return asciiValue - 48; - } else { - // 如果输入的 ASCII 值不在 '0' 到 '9' 的范围内,返回 -1 表示无效输入 - return -1; - } + private static final Map KEYPAD_MAPPING = new HashMap<>(); + + static { + // 初始化映射关系 + KEYPAD_MAPPING.put("0x30", "0"); + KEYPAD_MAPPING.put("0x31", "1"); + KEYPAD_MAPPING.put("0x32", "2"); + KEYPAD_MAPPING.put("0x33", "3"); + KEYPAD_MAPPING.put("0x34", "4"); + KEYPAD_MAPPING.put("0x35", "5"); + KEYPAD_MAPPING.put("0x36", "6"); + KEYPAD_MAPPING.put("0x37", "7"); + KEYPAD_MAPPING.put("0x38", "8"); + KEYPAD_MAPPING.put("0x39", "9"); + KEYPAD_MAPPING.put("0x08", "BACKSPACE"); // 更正按键 + KEYPAD_MAPPING.put("0x10", "CLEAR"); + KEYPAD_MAPPING.put("0x0D", "ENTER"); // 确认按键 + KEYPAD_MAPPING.put("0x1B", "CANCEL"); // 取消 + KEYPAD_MAPPING.put("0xA1", "HELP"); // 帮助 + KEYPAD_MAPPING.put("0x2E", "DECPOINT"); + KEYPAD_MAPPING.put("0x02", "00"); } + + }