|
@@ -1,7 +1,10 @@
|
|
|
package com.gzlh.device.led.client;
|
|
|
|
|
|
+import com.gzlh.config.netty.NettyDecoder;
|
|
|
import com.gzlh.device.led.properties.LedPropertiesConfig;
|
|
|
import io.netty.bootstrap.Bootstrap;
|
|
|
+import io.netty.buffer.ByteBuf;
|
|
|
+import io.netty.buffer.Unpooled;
|
|
|
import io.netty.channel.*;
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
import io.netty.channel.socket.SocketChannel;
|
|
@@ -14,6 +17,7 @@ import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.nio.ByteBuffer;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
@@ -48,8 +52,8 @@ public class LedNettyConfig {
|
|
|
protected void initChannel(SocketChannel ch) {
|
|
|
try {
|
|
|
ChannelPipeline pipeline = ch.pipeline();
|
|
|
- pipeline.addLast("decoder", new StringDecoder(StandardCharsets.US_ASCII));
|
|
|
- pipeline.addLast("encoder", new StringEncoder(StandardCharsets.US_ASCII));
|
|
|
+ pipeline.addLast( new NettyDecoder());
|
|
|
+ pipeline.addLast( new StringEncoder(StandardCharsets.UTF_8));
|
|
|
pipeline.addLast("handler", ledClientHandler);
|
|
|
} catch (Exception e) {
|
|
|
log.info("error connect:{}", e.getMessage());
|
|
@@ -85,7 +89,7 @@ public class LedNettyConfig {
|
|
|
|
|
|
public void send(String message) {
|
|
|
if (channel != null && channel.isActive()) {
|
|
|
- channel.writeAndFlush(message);
|
|
|
+ channel.writeAndFlush("010f00000010020900e470");
|
|
|
} else {
|
|
|
log.error("未建立连接,无法发送消息");
|
|
|
}
|
|
@@ -96,5 +100,15 @@ public class LedNettyConfig {
|
|
|
channel.close();
|
|
|
}
|
|
|
}
|
|
|
+ private byte[] hexStringToByteArray(String hexString) {
|
|
|
+
|
|
|
+ int len = hexString.length();
|
|
|
+ byte[] data = new byte[len / 2];
|
|
|
+ for (int i = 0; i < len; i += 2) {
|
|
|
+ data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
|
|
|
+ + Character.digit(hexString.charAt(i+1), 16));
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
}
|
|
|
|