sige 1 year ago
parent
commit
3c681a91dd
  1. 40
      src/src/main/java/com/my/graphiteDigesterBg/diframe/connection/DiConWebsocket.java

40
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) { synchronized public void call(DiCommandRequest request) {
this.requests.add(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 ) { synchronized ( request ) {
try { try {
LOG.info("wait for response : mid = {}; hash={}", request.id, request.hashCode());
request.wait(); request.wait();
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -119,6 +137,8 @@ public class DiConWebsocket implements DiDeviceConnection {
// handle on data // handle on data
public void handleOnText(String text) { public void handleOnText(String text) {
LOG.info("HandleOnText : {}", text);
ByteBuffer message = DiByteBuffer.fromHex(text); ByteBuffer message = DiByteBuffer.fromHex(text);
message.order(ByteOrder.LITTLE_ENDIAN); message.order(ByteOrder.LITTLE_ENDIAN);
byte messageType = message.get(5); byte messageType = message.get(5);
@ -133,6 +153,7 @@ public class DiConWebsocket implements DiDeviceConnection {
// handle on data timeout for binary mode ack message // handle on data timeout for binary mode ack message
private void handleOnTextAckMessage(ByteBuffer message) { private void handleOnTextAckMessage(ByteBuffer message) {
LOG.info("HandleOnTextAckMessage : start ...");
short messageId = message.getShort(0); short messageId = message.getShort(0);
DiCommandRequest request = null; DiCommandRequest request = null;
for ( DiCommandRequest requestItem : this.requests ) { for ( DiCommandRequest requestItem : this.requests ) {
@ -142,6 +163,7 @@ public class DiConWebsocket implements DiDeviceConnection {
} }
} }
if ( null == request ) { if ( null == request ) {
LOG.info("HandleOnTextAckMessage : request not found. messageId={}", messageId);
return ; // 可能是超时了 已经被处理掉了 ~~~ return ; // 可能是超时了 已经被处理掉了 ~~~
} }
request.response = message; request.response = message;
@ -151,7 +173,9 @@ public class DiConWebsocket implements DiDeviceConnection {
request.timeoutTimer = null; request.timeoutTimer = null;
} }
synchronized ( request ) { synchronized ( request ) {
LOG.info("notify response : mid = {}; hash={}", request.id, request.hashCode());
request.notify(); request.notify();
} }
LOG.info("HandleOnTextAckMessage : End");
} }
} }
Loading…
Cancel
Save