|
@@ -0,0 +1,98 @@
|
|
|
+package com.gzlh.camera.client;
|
|
|
+
|
|
|
+import com.gzlh.camera.properties.CameraPropertiesConfig;
|
|
|
+import io.netty.bootstrap.Bootstrap;
|
|
|
+import io.netty.channel.*;
|
|
|
+import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
+import io.netty.channel.socket.SocketChannel;
|
|
|
+import io.netty.channel.socket.nio.NioSocketChannel;
|
|
|
+import io.netty.handler.codec.string.StringDecoder;
|
|
|
+import io.netty.handler.codec.string.StringEncoder;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@Slf4j
|
|
|
+public class CameraNettyConfig {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private CameraPropertiesConfig cameraConfig;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CameraClientHandler cameraClientHandler;
|
|
|
+
|
|
|
+ private Channel channel;
|
|
|
+
|
|
|
+ @Bean("cameraBootstrap")
|
|
|
+ public Bootstrap bootstrap() {
|
|
|
+ String host = cameraConfig.getHost();
|
|
|
+ int port = cameraConfig.getPort();
|
|
|
+ if (cameraConfig.isEnable()) {
|
|
|
+ log.info("初始化 led:{},{}", host, port);
|
|
|
+ }
|
|
|
+ EventLoopGroup group = new NioEventLoopGroup();
|
|
|
+ return new Bootstrap()
|
|
|
+ .group(group)
|
|
|
+ .channel(NioSocketChannel.class)
|
|
|
+ .remoteAddress(host, port)
|
|
|
+ .option(ChannelOption.SO_KEEPALIVE, true)
|
|
|
+ .handler(new ChannelInitializer<SocketChannel>() {
|
|
|
+ @Override
|
|
|
+ protected void initChannel(SocketChannel ch) {
|
|
|
+ try {
|
|
|
+ ChannelPipeline pipeline = ch.pipeline();
|
|
|
+ pipeline.addLast("decoder", new StringDecoder());
|
|
|
+ pipeline.addLast("encoder", new StringEncoder());
|
|
|
+ pipeline.addLast("handler", cameraClientHandler);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("error connect:{}", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean("cameraClientHandler")
|
|
|
+ public CameraClientHandler cameraClientHandler() {
|
|
|
+ return new CameraClientHandler(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void connect() {
|
|
|
+ String host = cameraConfig.getHost();
|
|
|
+ int port = cameraConfig.getPort();
|
|
|
+ ChannelFuture future = bootstrap().connect();
|
|
|
+ future.addListener((ChannelFutureListener) future1 -> {
|
|
|
+ if (future1.isSuccess()) {
|
|
|
+ channel = future1.channel();
|
|
|
+ log.info("Camera 串口服务器连接成功,{},{}", host, port);
|
|
|
+ } else {
|
|
|
+ log.error("-------------Camera 连接服务器失败,{},{}-----------,进行重连", host, port);
|
|
|
+ future1.channel().eventLoop().schedule(this::connect, 5, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ try {
|
|
|
+ future.channel().closeFuture().sync();
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void send(String message) {
|
|
|
+ if (channel != null && channel.isActive()) {
|
|
|
+ channel.writeAndFlush(message);
|
|
|
+ } else {
|
|
|
+ log.error("未建立连接,无法发送消息");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void close() {
|
|
|
+ if (channel != null) {
|
|
|
+ channel.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|