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 62a91f9..c577f03 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 @@ -7,11 +7,14 @@ import com.dpkj.common.dto.LexMarkDTO; import com.dpkj.common.utils.ThirdServiceUtil; import com.dpkj.common.vo.Result; import com.dpkj.modules.keypad.service.KeypadService; +import com.dpkj.modules.keypad.util.WebSocketClient; import com.dpkj.modules.readcard.vo.IDCardReadResultVO; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.concurrent.TimeUnit; + /** * @BelongsProject: 银医通-澜沧中医院-DLL-台式机 * @BelongsPackage: com.dpkj.modules.keypad.service.impl @@ -45,34 +48,70 @@ public class KeypadServiceImpl implements KeypadService { connectParam.put("ServiceName", LexMarkConst.ENCRYPTOR); connect.setParam(connectParam.toJSONString()); IDCardReadResultVO connectResult = thirdServiceUtil.callDevice(connect, IDCardReadResultVO.class); - if (connectResult.getResult() == 0) { - // 明文输入 - LexMarkDTO plaintext = new LexMarkDTO(); - plaintext.setDevName(LexMarkConst.ENCRYPTOR); - plaintext.setActionName(MiddlewareFunctionsConst.INPUT_DATA); - plaintext.setCallID(1000); - JSONObject plaintextParam = new JSONObject(); - // 密码最小长度 - plaintextParam.put("MINLength", 0); - // 密码最大长度 - plaintextParam.put("MAXLength", 6); - // 是否自动结束 - plaintextParam.put("bAutoEnd", 1); - // 超时时间 - plaintextParam.put("TimeOut", 30000); - plaintext.setParam(plaintextParam.toJSONString()); - IDCardReadResultVO plaintextResult = thirdServiceUtil.callDevice(plaintext, IDCardReadResultVO.class); - if (plaintextResult.getResult() == 0) { - return null; + WebSocketClient webSocketClient = null; + try { + if (connectResult.getResult() == 0) { + // 明文输入 + LexMarkDTO plaintext = new LexMarkDTO(); + plaintext.setDevName(LexMarkConst.ENCRYPTOR); + plaintext.setActionName(MiddlewareFunctionsConst.INPUT_DATA); + plaintext.setCallID(1000); + JSONObject plaintextParam = new JSONObject(); + // 密码最小长度 + plaintextParam.put("MINLength", 0); + // 密码最大长度 + plaintextParam.put("MAXLength", 6); + // 是否自动结束 + plaintextParam.put("bAutoEnd", 1); + // 超时时间 + plaintextParam.put("TimeOut", 30000); + + plaintextParam.put("ActiveKeys", 81919); + plaintextParam.put("TerminateKeys", 1024); + plaintext.setParam(plaintextParam.toJSONString()); + // 创建 WebSocket 客户端 + webSocketClient = new WebSocketClient("ws://127.0.0.1:12347"); + + IDCardReadResultVO plaintextResult = thirdServiceUtil.callDevice(plaintext, IDCardReadResultVO.class); + if (plaintextResult.getResult() == 0) { + try { + // 适当延长等待时间 + while (true) { + TimeUnit.SECONDS.sleep(1); + String password = webSocketClient.getPassword(); + if (password.length() >= plaintextParam.getIntValue("MAXLength")) { + break; + } + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + String password = webSocketClient.getPassword(); + return Result.ok(password); + } else { + return Result.error("密码输入异常!详情:" + connectResult.getDesc()); + } } else { - return Result.error("密码输入异常!详情:" + connectResult.getDesc()); + return Result.error("数字键盘设备连接失败!详情:" + connectResult.getDesc()); + } + } finally { + if (webSocketClient != null) { + webSocketClient.close(); + } + // 关闭身份证读卡设备 + LexMarkDTO close = new LexMarkDTO(); + close.setDevName(LexMarkConst.ENCRYPTOR); + close.setCallID(0); + close.setActionName(MiddlewareFunctionsConst.CLOSE_CONNECTION); + IDCardReadResultVO closeResult = thirdServiceUtil.callDevice(close, IDCardReadResultVO.class); + if (closeResult.getResult() != 0) { + log.info("数字键盘设备关闭异常!"); } - } else { - return Result.error("数字键盘设备连接失败!详情:" + connectResult.getDesc()); } } + /** * 取消输入 (明文或者密文) * @return diff --git a/src/main/java/com/dpkj/modules/keypad/util/WebSocketClient.java b/src/main/java/com/dpkj/modules/keypad/util/WebSocketClient.java new file mode 100644 index 0000000..e715261 --- /dev/null +++ b/src/main/java/com/dpkj/modules/keypad/util/WebSocketClient.java @@ -0,0 +1,82 @@ +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; + +@ClientEndpoint +@Slf4j +public class WebSocketClient { + + private Session session; + private String password = ""; + + public WebSocketClient(String uri) { + try { + WebSocketContainer container = ContainerProvider.getWebSocketContainer(); + session = container.connectToServer(this, new URI(uri)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @OnOpen + public void onOpen(Session session) { + this.session = session; + } + + @OnMessage + public void onMessage(String message) { + + // 解析外层 JSON 对象 + JSONObject outerObj = JSONObject.parseObject(message); + + // 获取 "param" 字段对应的 JSON 对象 + JSONObject paramObj = outerObj.getJSONObject("param"); + + // 从 "param" 对象中获取 "Key" 的值 + Integer keyValue = paramObj.getInteger("Key"); + + if (keyValue != null) { + Integer i = parseAsciiToDigit(keyValue); + if (i != -1) { + password += i.toString(); + } + } else { + log.info("按键事件返回值,未找到 Key 字段或其值为空。"); + } + } + + @OnClose + public void onClose(Session session, CloseReason closeReason) { + + } + + public String getPassword() { + return password; + } + + public void close() { + try { + if (session != null && session.isOpen()) { + session.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + 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; + } + } +}