فهرست منبع

增加环控输入处理服务

zhuoning 2 سال پیش
والد
کامیت
90e3b37da0

+ 66 - 0
huimv-farm-device/huimv-farm-environ/pom.xml

@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.6.1</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>huimv-farm-environ-input</artifactId>
+    <packaging>jar</packaging>
+
+    <properties>
+        <java.version>1.8</java.version>
+    </properties>
+    <dependencies>
+        <!-- eartag2-common -->
+<!--        <dependency>-->
+<!--            <groupId>com.huimv</groupId>-->
+<!--            <artifactId>huimv-eartag2-common</artifactId>-->
+<!--            <version>0.0.2-SNAPSHOT</version>-->
+<!--        </dependency>-->
+        <!-- 排除Tomcat容器 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+            <!-- 移除掉默认支持的 Tomcat -->
+            <exclusions>
+                <exclusion>
+                    <groupId>org.springframework.boot</groupId>
+                    <artifactId>spring-boot-starter-tomcat</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <!-- 添加 Undertow 容器 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-undertow</artifactId>
+        </dependency>
+        <!-- netty -->
+        <dependency>
+            <groupId>io.netty</groupId>
+            <artifactId>netty-all</artifactId>
+            <version>4.1.45.Final</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <finalName>huimv-farm-environ-input-0.0.1-SNAPSHOT</finalName>
+        <plugins>
+            <!--  -->
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+            </plugin>
+            <!--  -->
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-resources-plugin</artifactId>
+                <version>2.6</version>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 74 - 0
huimv-farm-device/huimv-farm-environ/src/main/java/com/huimv/environ/server/EartagServer2.java

@@ -0,0 +1,74 @@
+package com.huimv.environ.server;
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class EartagServer2 {
+    @Autowired
+    private EartagServerHandler2 serverHandler;
+    //监听端口
+    private int port = 8017;
+    //创建构造方法
+    public EartagServer2(){
+    }
+
+    public static void main(String[] args) throws InterruptedException {
+        new EartagServer2().run();
+    }
+/**
+ *
+ * 功能描述: 启动方法前台多个服务  处理多个线程
+ *
+ * @param:
+ * @return:
+ * @auther: LiGang
+ * @date: 2019/3/26 11:31
+ */
+    /**
+     * 启动流程
+     */
+    public void run() throws InterruptedException {
+        //配置服务端线程组
+        EventLoopGroup bossGroup=new NioEventLoopGroup();
+        EventLoopGroup workGroup=new NioEventLoopGroup();
+
+        try{
+            //引导整个server的启动
+            ServerBootstrap serverBootstrap = new ServerBootstrap();
+            serverBootstrap.group(bossGroup,workGroup)
+                    .channel(NioServerSocketChannel.class)    //指定处理的连接类型
+                    .childHandler(new ChannelInitializer<SocketChannel>() {
+                        @Override
+                        protected void initChannel(SocketChannel socketChannel) throws Exception {
+                            socketChannel.pipeline().addLast(serverHandler);
+                        }
+                    });
+            System.out.println("# 设备数据接收服务器已经启动[监听端口:"+port+"]#");
+            System.out.println("# 准备接收数据:");
+            //绑定端口,同步等待成功
+            ChannelFuture cf = serverBootstrap.bind(port).sync();
+            // 等待服务端监听端口关闭
+            cf.channel().closeFuture().sync();
+        }finally {
+            //优雅的退出
+            bossGroup.shutdownGracefully();
+            workGroup.shutdownGracefully();
+        }
+    }
+}

+ 98 - 0
huimv-farm-device/huimv-farm-environ/src/main/java/com/huimv/environ/server/EartagServerHandler2.java

@@ -0,0 +1,98 @@
+package com.huimv.environ.server;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.util.CharsetUtil;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@ChannelHandler.Sharable
+@Component
+public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
+    private StringBuilder askTextSb = null;
+    private int num = 0;
+
+    //
+    public void appendClientAsk(String text) {
+        if (this.askTextSb == null) {
+            askTextSb = new StringBuilder();
+        }
+        askTextSb.append(text);
+    }
+
+    @Override
+    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+        ByteBuf data = (ByteBuf) msg;
+        String clientAskText = data.toString(CharsetUtil.UTF_8);
+//        System.out.println((++num)+"次, 客户端消息clientAskText>>"+clientAskText);
+        System.out.println("############################### 客户端消-接收消息 clientAskText>>" + clientAskText);
+        //保存实例内的客户端请求
+//        appendClientAsk(clientAskText);
+
+        // 处理客户端消息
+        handleClientAskMessage(clientAskText, ctx);
+
+        //临时写入耳标数据到文件
+//        writeTxt(clientAskText,"all");
+    }
+
+    private void handleClientAskMessage(String clientAskText, ChannelHandlerContext ctx) {
+        System.out.println("clientAskText=" + clientAskText);
+        String answerText = "";
+        if (clientAskText.length() == 12) {
+            answerText = "loginok";
+        } else {
+            answerText = "rok";
+            //添加数据入库
+            saveEnvDeviceData(clientAskText);
+        }
+        System.out.println("应答数据:"+answerText);
+        answerCmd(answerText, ctx);
+    }
+
+    private void saveEnvDeviceData(String clientAskText) {
+        String[] askDataArray = clientAskText.split("\\,");
+        System.out.println("askDataArray.length=" + askDataArray.length);
+
+    }
+
+    // 应答
+    public void answerCmd(String answerText, ChannelHandlerContext ctx)
+    {
+        ctx.writeAndFlush(Unpooled.copiedBuffer(answerText.getBytes()));
+    }
+
+    @Override
+    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
+//        if (askTextSb.toString().indexOf("end") != -1) {
+//            // 处理客户端消息
+//            handleClientEartagMessage(askTextSb.toString(), ctx);
+//            //清空重置;
+////            askTextSb.delete(0, askTextSb.length());
+//            askTextSb = new StringBuilder();
+//            num = 0;
+//        }
+    }
+
+    @Override
+    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+        if (cause.getMessage().indexOf("Connection reset") != -1) {
+            System.out.println("相关采集器设备正在重启:" + cause.toString());
+        }
+        ctx.close();
+    }
+
+
+
+
+}

+ 21 - 0
huimv-farm-device/huimv-farm-environ/src/main/java/com/huimv/environ/server/HuimvEnvironInputApplication.java

@@ -0,0 +1,21 @@
+package com.huimv.environ.server;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.ApplicationContext;
+
+/**
+ * @Project : huimv-farm-device
+ * @Package : ${}
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2022/11/30
+ **/
+@SpringBootApplication
+public class HuimvEnvironInputApplication {
+    public static void main(String[] args) throws InterruptedException {
+        ApplicationContext applicationContext = SpringApplication.run(HuimvEnvironInputApplication.class, args);
+        applicationContext.getBean(EartagServer2.class).run();
+    }
+}

+ 13 - 0
huimv-farm-device/huimv-farm-environ/src/main/resources/application-dev.yml

@@ -0,0 +1,13 @@
+server:
+  port: 3300
+
+spring:
+  application:
+    name: huimv-farm-environ-input
+
+management:
+  endpoints:
+    web:
+      exposure:
+        include: "*"   # * 在yaml 文件属于关键字,所以需要加引号
+

+ 31 - 0
huimv-farm-device/huimv-farm-environ/src/main/resources/application.properties

@@ -0,0 +1,31 @@
+spring.profiles.active=dev
+
+#�����������
+management.endpoints.web.exposure.include=*
+
+#########################################################
+###             ����undertowȡ��tomcat                ###
+#########################################################
+# �Ƿ�� undertow ��־��Ĭ��Ϊ false
+server.undertow.accesslog.enabled=false
+# ���÷�����־����Ŀ¼
+server.undertow.accesslog.dir=logs
+# ָ���������̵߳� I/0 �߳�����Ĭ��Ϊ 2 ���� CPU �ĸ���
+server.undertow.io-threads=
+# ָ���������̸߳�����Ĭ��Ϊ I/O �̸߳����� 8 ��
+server.undertow.worker-threads=
+# ���� HTTP POST ���ݵ���󳤶ȣ�Ĭ�ϲ�������
+server.undertow.max-http-post-size=0
+
+#########################################################
+###   Actuator Monitor  --   Actuator configuration   ###
+#########################################################
+management.security.enabled=false
+
+# ���ݴ�������
+data.input.flow=2
+
+# �Ƿ�д���������(1:д�� 0:��д��)
+data.test.input=0
+
+