From 3c681a91dd63f277f61fb81fdbcfff2db6014946 Mon Sep 17 00:00:00 2001 From: sige Date: Wed, 27 Mar 2024 18:36:35 +0800 Subject: [PATCH] ~ --- .../diframe/connection/DiConWebsocket.java | 40 +++++++++++++++++----- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/src/main/java/com/my/graphiteDigesterBg/diframe/connection/DiConWebsocket.java b/src/src/main/java/com/my/graphiteDigesterBg/diframe/connection/DiConWebsocket.java index 2f4bbc7..08c82b3 100644 --- a/src/src/main/java/com/my/graphiteDigesterBg/diframe/connection/DiConWebsocket.java +++ b/src/src/main/java/com/my/graphiteDigesterBg/diframe/connection/DiConWebsocket.java @@ -55,17 +55,35 @@ public class DiConWebsocket implements DiDeviceConnection { synchronized public void call(DiCommandRequest request) { this.requests.add(request); - // build cmd from request.parameter - Charset charset = StandardCharsets.UTF_8; - CharBuffer charBuffer = charset.decode(request.parameter); - String cmd = charBuffer.toString(); + var sendTimerTask = new TimerTask() { + @Override + public void run() { + // build cmd from request.parameter + Charset charset = StandardCharsets.UTF_8; + CharBuffer charBuffer = charset.decode(request.parameter); + String cmd = charBuffer.toString(); + + LOG.info("Command => {} : [{}]", request.parameterText, cmd); + cmd = cmd.replace(" ",""); + DiConWebsocket.this.setupRequestTimeoutTimer(request); + + var client = DiConWebsocket.this.client; + if ( !client.isOpen() ) { + try { + client.wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + client.send(cmd); + } + }; + var sendTimer = new Timer(); + sendTimer.schedule(sendTimerTask, 10); - LOG.info("Command => {} : [{}]", request.parameterText, cmd); - cmd = cmd.replace(" ",""); - this.client.send(cmd); - this.setupRequestTimeoutTimer(request); synchronized ( request ) { try { + LOG.info("wait for response : mid = {}; hash={}", request.id, request.hashCode()); request.wait(); } catch (InterruptedException e) { throw new RuntimeException(e); @@ -119,6 +137,8 @@ public class DiConWebsocket implements DiDeviceConnection { // handle on data public void handleOnText(String text) { + LOG.info("HandleOnText : {}", text); + ByteBuffer message = DiByteBuffer.fromHex(text); message.order(ByteOrder.LITTLE_ENDIAN); byte messageType = message.get(5); @@ -133,6 +153,7 @@ public class DiConWebsocket implements DiDeviceConnection { // handle on data timeout for binary mode ack message private void handleOnTextAckMessage(ByteBuffer message) { + LOG.info("HandleOnTextAckMessage : start ..."); short messageId = message.getShort(0); DiCommandRequest request = null; for ( DiCommandRequest requestItem : this.requests ) { @@ -142,6 +163,7 @@ public class DiConWebsocket implements DiDeviceConnection { } } if ( null == request ) { + LOG.info("HandleOnTextAckMessage : request not found. messageId={}", messageId); return ; // 可能是超时了, 已经被处理掉了 ~~~ } request.response = message; @@ -151,7 +173,9 @@ public class DiConWebsocket implements DiDeviceConnection { request.timeoutTimer = null; } synchronized ( request ) { + LOG.info("notify response : mid = {}; hash={}", request.id, request.hashCode()); request.notify(); } + LOG.info("HandleOnTextAckMessage : End"); } }