Commit ddb4de36823a9128150c9a2e9e050beccd35b585

Authored by 芯火源
1 parent 70de8ee1

refactor: 上、下行内容乱码问题

  1 +package org.thingsboard.server.common.data.yunteng.enums;
  2 +
  3 +/**
  4 + * 编码类型
  5 + */
  6 +public enum TcpDataTypeEnum {
  7 + HEX,
  8 + JSON,
  9 + ASCII
  10 +}
@@ -156,7 +156,7 @@ public class TcpTransportHandler extends ChannelInboundHandlerAdapter implements @@ -156,7 +156,7 @@ public class TcpTransportHandler extends ChannelInboundHandlerAdapter implements
156 if (msg instanceof ByteBuf) { 156 if (msg instanceof ByteBuf) {
157 ByteBuf message = (ByteBuf) msg; 157 ByteBuf message = (ByteBuf) msg;
158 byte[] byteMsg = ByteUtils.buf2Bytes(message); 158 byte[] byteMsg = ByteUtils.buf2Bytes(message);
159 - String msgStr = ByteUtils.bytesToHex(byteMsg); 159 + String msgStr = ByteUtils.bytesToStr(byteMsg);
160 log.error("会话【{}】收到设备【{}】来自【{}】数据【{}】", sessionId, deviceSessionCtx.getDeviceId(), address, msgStr); 160 log.error("会话【{}】收到设备【{}】来自【{}】数据【{}】", sessionId, deviceSessionCtx.getDeviceId(), address, msgStr);
161 deviceSessionCtx.setChannel(ctx); 161 deviceSessionCtx.setChannel(ctx);
162 if (deviceSessionCtx.getDeviceInfo() == null || deviceSessionCtx.getDeviceProfile() == null) { 162 if (deviceSessionCtx.getDeviceInfo() == null || deviceSessionCtx.getDeviceProfile() == null) {
@@ -589,15 +589,10 @@ public class TcpTransportHandler extends ChannelInboundHandlerAdapter implements @@ -589,15 +589,10 @@ public class TcpTransportHandler extends ChannelInboundHandlerAdapter implements
589 * @return 589 * @return
590 */ 590 */
591 private ChannelFuture pushDeviceMsg(ChannelHandlerContext ctx, String message) { 591 private ChannelFuture pushDeviceMsg(ChannelHandlerContext ctx, String message) {
592 - try {  
593 - byte[] payloadInBytes = message.getBytes(ByteUtils.UTF_8);  
594 - ByteBuf payload = Unpooled.copiedBuffer(payloadInBytes); 592 + byte[] payloadInBytes = ByteUtils.strToBytes(message);
  593 + ByteBuf payload = Unpooled.copiedBuffer(payloadInBytes);
595 594
596 - return ctx.writeAndFlush(payload);  
597 - } catch (UnsupportedEncodingException e) {  
598 - log.error(e.getMessage(), e);  
599 - throw new RuntimeException(e);  
600 - } 595 + return ctx.writeAndFlush(payload);
601 } 596 }
602 597
603 598
@@ -126,8 +126,15 @@ public class JsonTcpAdaptor implements TcpTransportAdaptor { @@ -126,8 +126,15 @@ public class JsonTcpAdaptor implements TcpTransportAdaptor {
126 126
127 @Override 127 @Override
128 public Optional<TcpDownEntry> convertToPublish(TcpDeviceWareSessionContext ctx, TransportProtos.ToDeviceRpcRequestMsg rpcRequest) throws ExecutionException, InterruptedException { 128 public Optional<TcpDownEntry> convertToPublish(TcpDeviceWareSessionContext ctx, TransportProtos.ToDeviceRpcRequestMsg rpcRequest) throws ExecutionException, InterruptedException {
129 - ListenableFuture<Object> result = ctx.getContext().getJsEngine().invokeFunction(ctx.getRpcScriptId(), rpcRequest.getParams());  
130 - return Optional.of(Futures.transform(result, t -> JacksonUtil.fromString(t.toString(), TcpDownEntry.class), MoreExecutors.directExecutor()).get()); 129 +// ListenableFuture<Object> result = ctx.getContext().getJsEngine().invokeFunction(ctx.getRpcScriptId(), rpcRequest.getParams());
  130 +// return Optional.of(Futures.transform(result, t -> JacksonUtil.fromString(t.toString(), TcpDownEntry.class), MoreExecutors.directExecutor()).get());
  131 + String payload = rpcRequest.getParams();//methodThingskit
  132 + if (!payload.startsWith("{") && !payload.endsWith("}")) {
  133 + payload = payload.replace("\"","");;
  134 + }
  135 + TcpDownEntry data = new TcpDownEntry();
  136 + data.setDatas(payload);
  137 + return Optional.of(data);
131 } 138 }
132 139
133 @Override 140 @Override
@@ -2,6 +2,7 @@ package org.thingsboard.server.transport.tcp.adaptors; @@ -2,6 +2,7 @@ package org.thingsboard.server.transport.tcp.adaptors;
2 2
3 import lombok.AllArgsConstructor; 3 import lombok.AllArgsConstructor;
4 import lombok.Data; 4 import lombok.Data;
  5 +import org.thingsboard.server.common.data.yunteng.enums.TcpDataTypeEnum;
5 6
6 import java.io.Serializable; 7 import java.io.Serializable;
7 import java.util.UUID; 8 import java.util.UUID;
@@ -10,7 +11,6 @@ import java.util.UUID; @@ -10,7 +11,6 @@ import java.util.UUID;
10 * 下行脚本 11 * 下行脚本
11 */ 12 */
12 @Data 13 @Data
13 -@AllArgsConstructor  
14 public class TcpDownEntry implements Serializable { 14 public class TcpDownEntry implements Serializable {
15 15
16 /** 16 /**
@@ -25,4 +25,7 @@ public class TcpDownEntry implements Serializable { @@ -25,4 +25,7 @@ public class TcpDownEntry implements Serializable {
25 * 下发给设备的最终内容 25 * 下发给设备的最终内容
26 */ 26 */
27 private String datas; 27 private String datas;
  28 +
  29 + /**下发给设备的字符串编码*/
  30 + private TcpDataTypeEnum dataType;
28 } 31 }