523096025 1 год назад
Родитель
Сommit
5f6e4de549
19 измененных файлов с 721 добавлено и 67 удалено
  1. 6 0
      huimv-admin/pom.xml
  2. 11 0
      huimv-admin/src/main/resources/mapper/BaseScreenCameraAccountMapper.xml
  3. 38 0
      huimv-admin/src/test/java/com/huimv/guowei/admin/AdminApplicationTests.java
  4. 22 0
      huimv-admin/src/test/java/com/huimv/guowei/admin/MyControllerTest.java
  5. 20 0
      input/src/main/java/com/huimv/input/controller/EggProductionController.java
  6. 3 0
      input/src/main/java/com/huimv/input/entity/BaseDuckInfo.java
  7. 52 0
      input/src/main/java/com/huimv/input/entity/EggProduction.java
  8. 3 1
      input/src/main/java/com/huimv/input/entity/EnvRegularCallEgg.java
  9. 5 0
      input/src/main/java/com/huimv/input/entity/EnvRegularCallFeeding.java
  10. 1 0
      input/src/main/java/com/huimv/input/entity/RawData.java
  11. 16 0
      input/src/main/java/com/huimv/input/mapper/EggProductionMapper.java
  12. 21 2
      input/src/main/java/com/huimv/input/server/EnvInputServer.java
  13. 367 64
      input/src/main/java/com/huimv/input/server/EnvInputServerHandler.java
  14. 32 0
      input/src/main/java/com/huimv/input/server/HexFrameDecoder.java
  15. 41 0
      input/src/main/java/com/huimv/input/server/MyFixedLengthDecoder.java
  16. 16 0
      input/src/main/java/com/huimv/input/service/IEggProductionService.java
  17. 20 0
      input/src/main/java/com/huimv/input/service/impl/EggProductionServiceImpl.java
  18. 14 0
      input/src/main/resources/mapper/EggProductionMapper.xml
  19. 33 0
      input/src/test/java/com/huimv/input/InputApplicationTests.java

+ 6 - 0
huimv-admin/pom.xml

@@ -23,6 +23,12 @@
         </dependency>
 
         <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <optional>true</optional>

+ 11 - 0
huimv-admin/src/main/resources/mapper/BaseScreenCameraAccountMapper.xml

@@ -2,4 +2,15 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.huimv.guowei.admin.mapper.BaseScreenCameraAccountMapper">
 
+<<<<<<< Updated upstream
+=======
+    <!-- 通用查询映射结果 -->
+    <resultMap id="BaseResultMap" type="com.huimv.guowei.admin.entity.BaseScreenCameraAccount">
+        <id column="id" property="id" />
+        <result column="farm_id" property="farmId" />
+        <result column="camera_id" property="cameraId" />
+        <result column="account_id" property="accountId" />
+    </resultMap>
+
+>>>>>>> Stashed changes
 </mapper>

+ 38 - 0
huimv-admin/src/test/java/com/huimv/guowei/admin/AdminApplicationTests.java

@@ -1,13 +1,51 @@
 package com.huimv.guowei.admin;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.huimv.guowei.admin.entity.RawData;
+import com.huimv.guowei.admin.mapper.RawDataMapper;
 import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+import java.util.List;
+
 @SpringBootTest
 class AdminApplicationTests {
+    @Autowired
+    private RawDataMapper rawDataMapper;
 
     @Test
     void contextLoads() {
+
+        List<RawData> rawData = rawDataMapper.selectList(new QueryWrapper<RawData>().between("create_time", "2023-12-07 18:00:00", "2023-12-08 05:05:00")
+                .eq("device_code", "4849720"));
+
+
+        for (RawData rawDatum : rawData) {
+
+            String str =rawDatum.getData();
+
+            String[] s = str.split(" ");
+            Date date = rawDatum.getCreateTime();
+
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+            long time = date.getTime();
+            for (int i = 0; i < 60; i++) {
+
+                long l = time - ((60 - i) * 5 * 60); // 修正此行
+                Date thenTime = new Date(l * 1000); // 将秒转换为毫秒
+                String str1 = s[i * 2 + 11] + s[i * 2 + 10];
+                BigDecimal weight = new BigDecimal(Integer.parseInt(str1, 16));
+                System.out.println("" +  sdf.format(thenTime)+"  重量:" +weight  );
+            }
+        }
+
     }
 
 }

+ 22 - 0
huimv-admin/src/test/java/com/huimv/guowei/admin/MyControllerTest.java

@@ -0,0 +1,22 @@
+/*
+package com.huimv.guowei.admin;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.web.servlet.MockMvc;
+
+@SpringBootTest
+@AutoConfigureMockMvc
+public class MyControllerTest {
+
+    @Autowired
+    private MockMvc mockMvc;
+
+    @Test
+    public void testControllerEndpoint() throws Exception {
+        // 发起 GET 请求,并期望返回状态码 200
+        mockMvc.perform(get("/api/my-endpoint"))
+                .andExpect(status().isOk());
+    }
+}*/

+ 20 - 0
input/src/main/java/com/huimv/input/controller/EggProductionController.java

@@ -0,0 +1,20 @@
+package com.huimv.input.controller;
+
+
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * <p>
+ *  前端控制器
+ * </p>
+ *
+ * @author author
+ * @since 2023-11-30
+ */
+@RestController
+@RequestMapping("/egg-production")
+public class EggProductionController {
+
+}

+ 3 - 0
input/src/main/java/com/huimv/input/entity/BaseDuckInfo.java

@@ -127,6 +127,9 @@ public class BaseDuckInfo implements Serializable {
      * 上下级位置id
      */
     private String unitList;
+    private String chiNum;
+    private String jiaoNum;
+    private String batchNum;
 
 
 }

+ 52 - 0
input/src/main/java/com/huimv/input/entity/EggProduction.java

@@ -0,0 +1,52 @@
+package com.huimv.input.entity;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import java.time.LocalTime;
+import java.io.Serializable;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author author
+ * @since 2023-11-30
+ */
+@Data
+@EqualsAndHashCode(callSuper = false)
+@Accessors(chain = true)
+@TableName("egg_production")
+public class EggProduction implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    /**
+     * 开始时间
+     */
+    private LocalTime startTime;
+
+    /**
+     * 结束时间
+     */
+    private LocalTime endTime;
+
+    /**
+     * 0:捡蛋 1:上料
+     */
+    private Integer eventType;
+
+    /**
+     * 牧场编号
+     */
+    private Integer farmId;
+
+
+}

+ 3 - 1
input/src/main/java/com/huimv/input/entity/EnvRegularCallEgg.java

@@ -87,6 +87,8 @@ public class EnvRegularCallEgg implements Serializable {
      * 位置名称
      */
     private String unitName;
-
+    private String chiNum;
+    private String jiaoNum;
+    private String batchNum;
 
 }

+ 5 - 0
input/src/main/java/com/huimv/input/entity/EnvRegularCallFeeding.java

@@ -78,5 +78,10 @@ public class EnvRegularCallFeeding implements Serializable {
     private String unitName;
     private Integer eventType;
 
+    private String chiNum;
+    private String jiaoNum;
+    private String batchNum;
+
+
 
 }

+ 1 - 0
input/src/main/java/com/huimv/input/entity/RawData.java

@@ -39,6 +39,7 @@ public class RawData implements Serializable {
     private Date createTime;
     private BigDecimal lastWeight;
     private String battery;
+    private String lastTime;
 
 
 

+ 16 - 0
input/src/main/java/com/huimv/input/mapper/EggProductionMapper.java

@@ -0,0 +1,16 @@
+package com.huimv.input.mapper;
+
+import com.huimv.input.entity.EggProduction;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author author
+ * @since 2023-11-30
+ */
+public interface EggProductionMapper extends BaseMapper<EggProduction> {
+
+}

+ 21 - 2
input/src/main/java/com/huimv/input/server/EnvInputServer.java

@@ -1,17 +1,22 @@
 package com.huimv.input.server;
 
 import io.netty.bootstrap.ServerBootstrap;
+import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelPipeline;
 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 io.netty.handler.codec.DelimiterBasedFrameDecoder;
+import io.netty.handler.codec.Delimiters;
+import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 @Component
-public class EnvInputServer {
+public class EnvInputServer  {
     @Autowired
     private EnvInputServerHandler serverHandler;
     //监听端口
@@ -32,6 +37,9 @@ public class EnvInputServer {
  * @auther: LiGang
  * @date: 2019/3/26 11:31
  */
+
+
+
     /**
      * 启动流程
      */
@@ -49,7 +57,14 @@ public class EnvInputServer {
                         @Override
                         protected void initChannel(SocketChannel socketChannel) throws Exception {
                             System.out.println("连接建立");
-                            socketChannel.pipeline().addLast(serverHandler);
+                            ChannelPipeline pipeline = socketChannel.pipeline();
+
+//                            socketChannel.pipeline().addLast(new MyFixedLengthDecoder());
+
+//                            pipeline.addLast(new MyFixedLengthDecoder(137));  // 添加处理长度为137的消息的解码器
+//                            pipeline.addLast(new MyFixedLengthDecoder(8));    // 添加处理长度为8的消息的解码器
+//                            pipeline.addLast(new MyFixedLengthDecoder(6)); // 添加处理长度为6的消息的解码器
+                            pipeline.addLast(serverHandler);
                         }
                     });
             System.out.println("# 耳标及采集器设备数据接收服务器已经启动,监听端口(Port):"+port);
@@ -64,4 +79,8 @@ public class EnvInputServer {
             workGroup.shutdownGracefully();
         }
     }
+
 }
+
+
+

+ 367 - 64
input/src/main/java/com/huimv/input/server/EnvInputServerHandler.java

@@ -13,6 +13,7 @@ import com.huimv.input.utils.ModBusUtils;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.*;
+import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -21,9 +22,13 @@ import org.springframework.transaction.annotation.Transactional;
 import javax.lang.model.element.VariableElement;
 import java.io.IOException;
 import java.math.BigDecimal;
+import java.nio.channels.SocketChannel;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
 import java.time.LocalTime;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -41,7 +46,7 @@ import java.util.stream.Collectors;
 @ChannelHandler.Sharable
 @Component
 @Slf4j
-public class EnvInputServerHandler extends ChannelInboundHandlerAdapter {
+public class EnvInputServerHandler extends ChannelInboundHandlerAdapter   {
     private StringBuilder askTextSb = null;
 
     @Autowired
@@ -64,7 +69,10 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter {
     @Autowired
     private DeviceFarmConfigMapper farmConfigMapper;
 
+    @Autowired
+    private EggProductionMapper eggProductionMapper;
 
+    private static String  str = "";
 
     //
     public void appendClientAsk(String text) {
@@ -75,74 +83,111 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter {
     }
 
 
+
+    public static void main(String[] args) {
+
+        String str =
+
+                "55 86 38 00 4A 00 01 52 00 78 BC 56 B9 56 BC 56 BC 56 BA 56 BB 56 BB 56 BB 56 BA 56 BC 56 BA 56 BB 56 B9 56 BA 56 BC 56 BE 56 BB 56 BB 56 BB 56 BA 56 BA 56 BB 56 B9 56 B7 56 B8 56 B7 56 CC 56 CB 56 C9 56 CB 56 CB 56 CC 56 CB 56 CB 56 CB 56 CB 56 CA 56 CA 56 C9 56 CA 56 CB 56 CB 56 C9 56 C9 56 C8 56 CB 56 C9 56 C9 56 C9 56 C8 56 C8 56 CA 56 C9 56 C9 56 C8 56 C9 56 C9 56 C9 56 CB 56 CA 56 17 0C 07 15 0F 03 AA ";
+        String[] s = str.split(" ");
+        String  dateTimeString = "2023-12-07 23:22:54";
+        // 创建 DateTimeFormatter 对象,指定日期时间格式
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+
+        // 将字符串解析为 LocalDateTime 对象
+        LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
+
+        // 获取时间戳(以秒为单位)
+        long timestamp = dateTime.toEpochSecond(ZoneOffset.of("+8"));
+
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        System.out.println(timestamp);
+
+        for (int i = 0; i < 60; i++) {
+
+            long l = timestamp - ((60 - i) * 5 * 60); // 修正此行
+            Date thenTime = new Date(l * 1000); // 将秒转换为毫秒
+            String str1 = s[i * 2 + 11] + s[i * 2 + 10];
+            BigDecimal weight = new BigDecimal(Integer.parseInt(str1, 16));
+            System.out.println("" +  sdf.format(thenTime)+"  重量:" +weight  );
+        }
+    }
+
+
     @Override
     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
-        ByteBuf byteBuf = (ByteBuf) msg;
-        byte[] bytes = new byte[byteBuf.readableBytes()];
-        byteBuf.readBytes(bytes);
-        String str = ModBusUtils.bytes2HexString(bytes);
-        int length = str.length();
-
-        System.out.println("收到的数据:"+str+"   "+ length);
-        if(length>100){
-            saveWeight(str,bytes);
-        }else if (length== 24 ){
-            String[] s = str.split(" ");
-            String hexId = s[5] + s[4] + s[3] + s[2] ;
-            System.out.println("十六进制id"+hexId);
-            int decId = Integer.parseInt(hexId, 16);
-            System.out.println("十进制id"+decId);
-            EnvDevice envDevice = envDeviceMapper.selectOne(new QueryWrapper<EnvDevice>().eq("device_code", decId));
-
-            if(ObjectUtil.isNotEmpty(envDevice)){
-                List<DeviceFarmConfig> farm_id = farmConfigMapper.selectList(new QueryWrapper<DeviceFarmConfig>().eq("farm_id", envDevice.getFarmId()));
-                if (ObjectUtil.isNotEmpty(farm_id)){
-                    String timestr = farm_id.stream()
-                            .map(config -> String.format("%02X%02X", config.getStartHour(), config.getEndHour()))
-                            .collect(Collectors.joining());
-                    ctx.writeAndFlush(Unpooled.copiedBuffer(hexStringToByteArray("550B02"+getDateX()+timestr+"AA")));
-                }
-                if (envDevice.getDeviceStatus() != 1){
-                    envDevice.setDeviceStatus(1);
-                    envDeviceMapper.updateById(envDevice);
+//        if (msg instanceof ByteBuf) {
+            ByteBuf byteBuf = (ByteBuf) msg;
+            byte[] bytes = new byte[byteBuf.readableBytes()];
+            byteBuf.readBytes(bytes);
+             str += ModBusUtils.bytes2HexString(bytes);
+            int length = str.length();
+        if (length != 18 && length != 24 && length <411){
+            return;
+        }
+        if (length > 411){
+            str = "";
+            return;
+        }
+
+            System.out.println("收到的数据:" + str + "   " + length  );
+            if (length > 300) {
+                saveWeight(str, bytes);
+            } else if (length == 24) {
+                String[] s = str.split(" ");
+                String hexId = s[5] + s[4] + s[3] + s[2];
+                System.out.println("十六进制id" + hexId);
+                int decId = Integer.parseInt(hexId, 16);
+                System.out.println("十进制id" + decId);
+                EnvDevice envDevice = envDeviceMapper.selectOne(new QueryWrapper<EnvDevice>().eq("device_code", decId));
+
+                if (ObjectUtil.isNotEmpty(envDevice)) {
+                    List<DeviceFarmConfig> farm_id = farmConfigMapper.selectList(new QueryWrapper<DeviceFarmConfig>().eq("farm_id", envDevice.getFarmId()));
+                    if (ObjectUtil.isNotEmpty(farm_id)) {
+                        String timestr = farm_id.stream()
+                                .map(config -> String.format("%02X%02X", config.getStartHour(), config.getEndHour()))
+                                .collect(Collectors.joining());
+                        ctx.writeAndFlush(Unpooled.copiedBuffer(hexStringToByteArray("550B02" + getDateX() + timestr + "AA")));
+                    }
+                    if (envDevice.getDeviceStatus() != 1) {
+                        envDevice.setDeviceStatus(1);
+                        envDeviceMapper.updateById(envDevice);
+                    }
+                } else {
+                    List<DeviceFarmConfig> farm_id = farmConfigMapper.selectList(new QueryWrapper<DeviceFarmConfig>().eq("farm_id", 21));
+                    if (ObjectUtil.isNotEmpty(farm_id)) {
+                        String timestr = farm_id.stream()
+                                .map(config -> String.format("%02X%02X", config.getStartHour(), config.getEndHour()))
+                                .collect(Collectors.joining());
+                        ctx.writeAndFlush(Unpooled.copiedBuffer(hexStringToByteArray("550B02" + getDateX() + timestr + "AA")));
+                    }
                 }
-            }else {
-                List<DeviceFarmConfig> farm_id = farmConfigMapper.selectList(new QueryWrapper<DeviceFarmConfig>().eq("farm_id",24));
-                if (ObjectUtil.isNotEmpty(farm_id)){
+
+            } else {
+                List<DeviceFarmConfig> farm_id = farmConfigMapper.selectList(new QueryWrapper<DeviceFarmConfig>().like("device_id", str));
+                if (ObjectUtil.isNotEmpty(farm_id)) {
                     String timestr = farm_id.stream()
                             .map(config -> String.format("%02X%02X", config.getStartHour(), config.getEndHour()))
                             .collect(Collectors.joining());
-                    ctx.writeAndFlush(Unpooled.copiedBuffer(hexStringToByteArray("550B02"+getDateX()+timestr+"AA")));
-                }
-            }
-
-        }/*else if (length== 18 ) {*/
-            List<DeviceFarmConfig> farm_id = farmConfigMapper.selectList(new QueryWrapper<DeviceFarmConfig>().like("device_id",str ));
-            if (ObjectUtil.isNotEmpty(farm_id)) {
-                String timestr = farm_id.stream()
-                        .map(config -> String.format("%02X%02X", config.getStartHour(), config.getEndHour()))
-                        .collect(Collectors.joining());
-               String str1 =  "550B02" + getDateX() + timestr + "AA";
-                for (int i = 0; i < 10; i++) {
+                    String str1 = "550B02" + getDateX() + timestr + "AA";
+//                for (int i = 0; i < 10; i++) {
                     ctx.writeAndFlush(Unpooled.copiedBuffer(hexStringToByteArray(str1)));
-                    System.out.println(i+" : 下发数据:"+str1);
-                    Thread.sleep(3000);
+//                    System.out.println(i+" : 下发数据:"+str1);
+//                    Thread.sleep(3000);
                 }
 
             }
 
 //        }
-
-        System.out.println("保存成功");
+       str = "";
+            System.out.println("保存成功");
+//        }else {
+//            System.err.println("Unexpected message type: " + msg.getClass().getName());
+//        }
     }
 
-    public static void main(String[] args) {
-        System.out.println(String.format("%02X%02X", 8, 10));
 
-    }
-    private  boolean isTimeInRange(LocalTime currentTime, LocalTime startTime, LocalTime endTime) {
-        return !currentTime.isBefore(startTime) && !currentTime.isAfter(endTime);
-    }
+
     private static byte[] hexStringToByteArray(String hexString) {
         int len = hexString.length();
         byte[] dataBytes = new byte[len / 2];
@@ -206,13 +251,32 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter {
         rawData.setCreateTime(new Date(l));
         rawData.setLastWeight(new BigDecimal(Integer.parseInt(s[sLength -8]+s[sLength-9], 16)));
         rawData.setBattery(batStr);
+        int year = Integer.parseInt(s[s.length - 7], 16);
+        int month = Integer.parseInt(s[s.length - 6], 16);
+        int day = Integer.parseInt(s[s.length - 5], 16);
+        int hour = Integer.parseInt(s[s.length - 4], 16);
+        int minut = Integer.parseInt(s[s.length - 3], 16);
+        int sco = Integer.parseInt(s[s.length - 2], 16);
+
+        rawData.setLastTime(
+                year +"-" +
+                        month+"-"+
+                        day+" "+
+                        hour+":" +
+                        minut+":" +
+                        sco);
         rawDataMapper.insert(rawData);
+        if(year>30 || year<10){
+            return;
+        }
+
         //获取设备
         EnvDevice envDevice = envDeviceMapper.selectOne(new QueryWrapper<EnvDevice>().eq("device_code", decId).eq("device_type", 3));
         if (ObjectUtil.isEmpty(envDevice)){
             System.out.println("固定电子称不存在,数据抛弃");
             return;
         }
+
         //获取鸭子
         Integer unitId = envDevice.getUnitId();
         BaseDuckInfo baseDuckInfo = baseDuckInfoMapper.selectOne(new QueryWrapper<BaseDuckInfo>().eq("unit_id", unitId).eq("is_cage", 0));
@@ -220,18 +284,257 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter {
             System.out.println("该位置不存在鸭子,数据抛弃");
             return;
         }
+        List<EggProduction> farm_id = eggProductionMapper.selectList(new QueryWrapper<EggProduction>().eq("farm_id", envDevice.getFarmId()));
+
+
+        LocalTime jianStartTime = LocalTime.of(7, 30);    // 开始时间
+        LocalTime jianEndTime = LocalTime.of(8, 30);     // 结束时间
+
+        LocalTime liaoStartTime = LocalTime.of(6, 0);    // 开始时间
+        LocalTime liaoEndTime = LocalTime.of(7, 29);     // 结束时间
+
+
+
+        if(ObjectUtil.isNotEmpty(farm_id)){
+            for (EggProduction eggProduction : farm_id) {
+                if (eggProduction.getEventType() == 0){
+                    //检蛋
+                     jianStartTime = eggProduction.getStartTime();
+                     jianEndTime = eggProduction.getEndTime();
+                }else {
+                    //上料
+                     liaoStartTime = eggProduction.getStartTime();
+                     liaoEndTime = eggProduction.getEndTime();
+                }
+            }
+        }
+
+        BigDecimal bigDecimal3 = new BigDecimal(3);
+        BigDecimal bigDecimala3 = new BigDecimal(-3);
+        BigDecimal bigDecimal0 = new BigDecimal(0);
+
+        BigDecimal bigDecimal800 = new BigDecimal(-800);
+        BigDecimal bigDecimal2 = new BigDecimal(2);
+        Boolean flag = true;
+        LocalTime currentTime = LocalTime.now();
+        //检蛋
+        if(currentTime.isAfter(jianStartTime) && currentTime.isBefore(jianEndTime)){
+            for (int i = 0; i < 60; i++) {
+                String str1 = s[i * 2 + 11] + s[i * 2 + 10];
+                BigDecimal weight = new BigDecimal(Integer.parseInt(str1, 16));
+
+                BigDecimal subtract = lastWeight.subtract(weight);
+                int i1 = subtract.intValue();
+                if(i1<-400) {
+                    EnvRegularCallEgg envRegularCallEgg = new EnvRegularCallEgg();
+                    envRegularCallEgg.setBattery(bat);
+                    envRegularCallEgg.setCallDate(new Date());
+                    envRegularCallEgg.setCallCode(decId + "");
+                    envRegularCallEgg.setCallName(envDevice.getDeviceName());
+                    envRegularCallEgg.setDuckId(baseDuckInfo.getId());
+                    envRegularCallEgg.setDuckNum(baseDuckInfo.getDuckNum());
+                    envRegularCallEgg.setFarmId(baseDuckInfo.getFarmId());
+                    envRegularCallEgg.setUnitId(baseDuckInfo.getUnitId());
+                    envRegularCallEgg.setUnitName(baseDuckInfo.getUnitName());
+                    envRegularCallEgg.setChiNum(baseDuckInfo.getChiNum());
+                    envRegularCallEgg.setJiaoNum(baseDuckInfo.getJiaoNum());
+                    envRegularCallEgg.setBatchNum(baseDuckInfo.getBatchNum());
+                    if (  i1 >-800) {
+                        envRegularCallEgg.setEggNum(1);
+                    } if (i1 <-800) {
+                        envRegularCallEgg.setEggNum(2);
+                     }
+                    if (weight.compareTo(bigDecimal0) == 0) {
+                        envRegularCallEgg.setDuckFeedingOriginal(bigDecimal0);
+                    } else {
+                        envRegularCallEgg.setDuckFeedingOriginal(weight.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP));
+                    }
+                    if (subtract.compareTo(bigDecimal0) == 0) {
+                        envRegularCallEgg.setDuckWeight(bigDecimal0);
+                    } else {
+                        envRegularCallEgg.setDuckWeight(subtract.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP));
+                    }
+                    envRegularCallEggMapper.insert(envRegularCallEgg);
+                    lastWeight = weight;
+                }
+
+
+            }
+        }
+
+        //上料
+     else  if(currentTime.isAfter(liaoStartTime) && currentTime.isBefore(liaoEndTime)){
+            for (int i = 0; i < 60; i++) {
+                String str1 = s[i * 2 + 11] + s[i * 2 + 10];
+                BigDecimal weight = new BigDecimal(Integer.parseInt(str1,16));
+                if (flag){
+                    //当时时间
+                    String str2 = "";
+                    BigDecimal weight2 ;
+                    if(i<59){
+                        str2 =  s[(i+1) * 2 + 11] + s[(i+1)  * 2 + 10];
+                        weight2 =  new BigDecimal(Integer.parseInt(str2,16));
+                        BigDecimal subtract2 = weight2.subtract(lastWeight);
+                        if (subtract2.compareTo(bigDecimal800) < 0){
+                            System.out.println("连续两次减重之和小于-80,舍弃");
+                            flag = false;
+                            lastWeight  = weight;
+                            continue;
+                        }
+                    }
+
+                    BigDecimal subtract = lastWeight.subtract(weight);
+
+
+                    if (subtract.compareTo(bigDecimal3)<0&& subtract.compareTo(bigDecimal0)>0){
+                        System.out.println("波动小于0.3g,舍弃");
+                        continue;
+                    }
+                    if (subtract.compareTo(bigDecimala3)>0 && subtract.compareTo(bigDecimal0)<0){
+                        System.out.println("波动小于0.3g,舍弃");
+                        continue;
+                    }
+
+                    if (subtract.compareTo(bigDecimal0)<0){
+                        System.out.println("波动小于0.3g,舍弃");
+                        continue;
+                    }
+
+                    int compare = subtract.compareTo(new BigDecimal(1500));
+                    System.out.println(subtract +"------>"+weight+"----------->"+lastWeight);
+                    EnvRegularCallFeeding envRegularCallFeeding = new EnvRegularCallFeeding();
+                    envRegularCallFeeding.setBattery(batStr);
+                    envRegularCallFeeding.setCallDate(new Date());
+                    envRegularCallFeeding.setCallCode(decId+"");
+                    envRegularCallFeeding.setCallName(envDevice.getDeviceName());
+                    envRegularCallFeeding.setDuckId(baseDuckInfo.getId());
+                    envRegularCallFeeding.setDuckNum(baseDuckInfo.getDuckNum());
+                    envRegularCallFeeding.setFarmId(baseDuckInfo.getFarmId());
+                    envRegularCallFeeding.setUnitId(baseDuckInfo.getUnitId());
+                    envRegularCallFeeding.setUnitName(baseDuckInfo.getUnitName());
+                    if (weight.compareTo(bigDecimal0 )==0){
+                        envRegularCallFeeding.setDuckFeedingOriginal(bigDecimal0);
+                    }else {
+                        envRegularCallFeeding.setDuckFeedingOriginal(weight.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
+                    }
+                    if (subtract.compareTo(bigDecimal0 )==0){
+                        envRegularCallFeeding.setDuckWeight(bigDecimal0);
+                    }else {
+                        envRegularCallFeeding.setDuckWeight(subtract.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
+                    }
+
+                    //加料
+                    if (compare>0){
+                        EnvRegularCallFeedingCopy envRegularCallFeedingCopy = new EnvRegularCallFeedingCopy();
+                        envRegularCallFeedingCopy.setBattery(new BigDecimal(Integer.parseInt(bat, 16)).divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP).toString());
+                        envRegularCallFeedingCopy.setCallDate(new Date());
+                        envRegularCallFeedingCopy.setCallCode(decId+"");
+                        envRegularCallFeedingCopy.setCallName(envDevice.getDeviceName());
+                        envRegularCallFeedingCopy.setDuckId(baseDuckInfo.getId());
+                        envRegularCallFeedingCopy.setDuckNum(baseDuckInfo.getDuckNum());
+                        envRegularCallFeedingCopy.setFarmId(baseDuckInfo.getFarmId());
+                        envRegularCallFeedingCopy.setUnitId(baseDuckInfo.getUnitId());
+                        envRegularCallFeedingCopy.setUnitName(baseDuckInfo.getUnitName());
+                        if (weight.compareTo(bigDecimal0)==0){
+                            envRegularCallFeedingCopy.setDuckFeedingOriginal(bigDecimal0);
+                        }else {
+                            envRegularCallFeedingCopy.setDuckFeedingOriginal(weight.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
+                        }
+                        if (subtract.compareTo(bigDecimal0 )==0){
+                            envRegularCallFeedingCopy.setDuckWeight(bigDecimal0);
+                        }else {
+                            envRegularCallFeedingCopy.setDuckWeight(subtract.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
+                        }
+
+                        envRegularCallFeedingCopy.setEventType(1);
+                        envRegularCallFeedingCopyMapper.insert(envRegularCallFeedingCopy);
+                        lastWeight =weight;
+                        continue;
+                    }
+
+
+                    int compare0 = subtract.compareTo(bigDecimal0);
+                    //小于零大于负八十
+                    int compare80 = subtract.compareTo(bigDecimal800);
+                    //吃料
+                    if (compare0<0 && compare80 >=0){
+                        if (subtract.compareTo(bigDecimal0 )==0){
+                            envRegularCallFeeding.setDuckWeight(bigDecimal0);
+                        }else {
+                            envRegularCallFeeding.setDuckWeight(subtract.subtract(subtract.multiply(bigDecimal2)).divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP));
+                        }
+                        envRegularCallFeeding.setEventType(0);
+                        envRegularCallFeedingMapper.insert(envRegularCallFeeding);
+                        lastWeight =weight;
+                        continue;
+                    }
+
+                    continue;
+                }
+                lastWeight  = weight;
+                flag = true;
+            }
+
+        }
+     else {
+            BigDecimal minweight = lastWeight;
+            for (int i = 0; i < 60; i++) {
+                String str1 = s[i * 2 + 11] + s[i * 2 + 10];
+                BigDecimal weight = new BigDecimal(Integer.parseInt(str1, 16));
+
+                if (weight.compareTo(lastWeight) <0) {
+                    minweight = weight;
+                }
+
+            }
+            BigDecimal subtract = lastWeight.subtract(minweight);
+            if (subtract.compareTo(bigDecimal0)<0){
+                System.out.println("波动小于0.3g,舍弃");
+                return;
+            }
+            EnvRegularCallFeeding envRegularCallFeeding = new EnvRegularCallFeeding();
+            envRegularCallFeeding.setBattery(batStr);
+            envRegularCallFeeding.setCallDate(new Date());
+            envRegularCallFeeding.setCallCode(decId+"");
+            envRegularCallFeeding.setCallName(envDevice.getDeviceName());
+            envRegularCallFeeding.setDuckId(baseDuckInfo.getId());
+            envRegularCallFeeding.setDuckNum(baseDuckInfo.getDuckNum());
+            envRegularCallFeeding.setFarmId(baseDuckInfo.getFarmId());
+            envRegularCallFeeding.setUnitId(baseDuckInfo.getUnitId());
+            envRegularCallFeeding.setUnitName(baseDuckInfo.getUnitName());
+            envRegularCallFeeding.setChiNum(baseDuckInfo.getChiNum());
+            envRegularCallFeeding.setJiaoNum(baseDuckInfo.getJiaoNum());
+            envRegularCallFeeding.setBatchNum(baseDuckInfo.getBatchNum());
+            if (minweight.compareTo(bigDecimal0 )==0){
+                envRegularCallFeeding.setDuckFeedingOriginal(bigDecimal0);
+            }else {
+                envRegularCallFeeding.setDuckFeedingOriginal(minweight.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
+            }
+            if (subtract.compareTo(bigDecimal0 )==0){
+                envRegularCallFeeding.setDuckWeight(bigDecimal0);
+            }else {
+                envRegularCallFeeding.setDuckWeight(subtract.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
+            }
 
-        try {
-            BigDecimal bigDecimal3 = new BigDecimal(3);
-            BigDecimal bigDecimala3 = new BigDecimal(-3);
-            BigDecimal bigDecimal0 = new BigDecimal(0);
+            envRegularCallFeeding.setEventType(0);
+            envRegularCallFeedingMapper.insert(envRegularCallFeeding);
 
-            BigDecimal bigDecimal800 = new BigDecimal(-800);
-            BigDecimal bigDecimal1600 = new BigDecimal(1600);
-            BigDecimal bigDecimal2 = new BigDecimal(2);
-            Boolean flag = true;
+        }
+
+
+
+
+//        try {
+//            BigDecimal bigDecimal3 = new BigDecimal(3);
+//            BigDecimal bigDecimala3 = new BigDecimal(-3);
+//            BigDecimal bigDecimal0 = new BigDecimal(0);
+//
+//            BigDecimal bigDecimal800 = new BigDecimal(-800);
+//            BigDecimal bigDecimal1600 = new BigDecimal(1600);
+//            BigDecimal bigDecimal2 = new BigDecimal(2);
+//            Boolean flag = true;
             //循环获取体重
-        for (int i = 0; i < 60; i++) {
+      /*  for (int i = 0; i < 60; i++) {
             String str1 = s[i * 2 + 11] + s[i * 2 + 10];
             BigDecimal weight = new BigDecimal(Integer.parseInt(str1,16));
             if (flag){
@@ -423,7 +726,7 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter {
         }catch (Exception e){
             System.out.println(e);
         }
-
+*/
     }
 
 

+ 32 - 0
input/src/main/java/com/huimv/input/server/HexFrameDecoder.java

@@ -0,0 +1,32 @@
+package com.huimv.input.server;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.SimpleChannelInboundHandler;
+
+public class HexFrameDecoder extends SimpleChannelInboundHandler<ByteBuf> {
+
+    private final int messageLength;
+
+    public HexFrameDecoder(int messageLength) {
+        this.messageLength = messageLength;
+    }
+
+    @Override
+    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
+        if (msg.readableBytes() < messageLength) {
+            // 不够长度,等待更多数据
+            return;
+        }
+
+        // 读取固定长度的字节
+        byte[] byteArray = new byte[messageLength];
+        msg.readBytes(byteArray);
+
+        // 在这里处理字节数组
+        // ...
+
+        // 继续处理下一个消息
+        ctx.fireChannelRead(msg);
+    }
+}

+ 41 - 0
input/src/main/java/com/huimv/input/server/MyFixedLengthDecoder.java

@@ -0,0 +1,41 @@
+package com.huimv.input.server;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+import io.netty.util.CharsetUtil;
+
+import javax.xml.bind.DatatypeConverter;
+import java.util.List;
+
+public class MyFixedLengthDecoder extends ByteToMessageDecoder {
+
+    private  int messageLength;
+
+    public MyFixedLengthDecoder(int messageLength) {
+        this.messageLength = messageLength;
+    }
+
+    @Override
+    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
+        // 确保有足够的可读字节
+        if (in.readableBytes() < messageLength) {
+            return;
+        }
+
+        // 读取固定长度的字节
+        ByteBuf frame = in.readBytes(messageLength);
+        System.out.println(messageLength);
+        // 将读取的字节转换为十六进制字符串
+        String hexString = frame.toString(CharsetUtil.UTF_8);
+
+        // 将十六进制字符串转换为字节数组
+        byte[] byteArray = DatatypeConverter.parseHexBinary(hexString);
+
+        // 将字节数组添加到输出列表
+        out.add(Unpooled.wrappedBuffer(byteArray));
+    }
+
+
+}

+ 16 - 0
input/src/main/java/com/huimv/input/service/IEggProductionService.java

@@ -0,0 +1,16 @@
+package com.huimv.input.service;
+
+import com.huimv.input.entity.EggProduction;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author author
+ * @since 2023-11-30
+ */
+public interface IEggProductionService extends IService<EggProduction> {
+
+}

+ 20 - 0
input/src/main/java/com/huimv/input/service/impl/EggProductionServiceImpl.java

@@ -0,0 +1,20 @@
+package com.huimv.input.service.impl;
+
+import com.huimv.input.entity.EggProduction;
+import com.huimv.input.mapper.EggProductionMapper;
+import com.huimv.input.service.IEggProductionService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author author
+ * @since 2023-11-30
+ */
+@Service
+public class EggProductionServiceImpl extends ServiceImpl<EggProductionMapper, EggProduction> implements IEggProductionService {
+
+}

+ 14 - 0
input/src/main/resources/mapper/EggProductionMapper.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.huimv.input.mapper.EggProductionMapper">
+
+    <!-- 通用查询映射结果 -->
+    <resultMap id="BaseResultMap" type="com.huimv.input.entity.EggProduction">
+        <id column="id" property="id" />
+        <result column="start_time" property="startTime" />
+        <result column="end_time" property="endTime" />
+        <result column="event_type" property="eventType" />
+        <result column="farm_id" property="farmId" />
+    </resultMap>
+
+</mapper>

+ 33 - 0
input/src/test/java/com/huimv/input/InputApplicationTests.java

@@ -1,13 +1,46 @@
 package com.huimv.input;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.huimv.input.entity.RawData;
+import com.huimv.input.mapper.RawDataMapper;
 import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+
 @SpringBootTest
 class InputApplicationTests {
+    @Autowired
+    private RawDataMapper rawDataMapper;
 
     @Test
     void contextLoads() {
+        List<RawData> rawData = rawDataMapper.selectList(new QueryWrapper<RawData>().between("create_time", "2023-12-07 19:00:00", "2023-12-08 05:10:00")
+                .eq("device_code", "4849698"));
+
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+        for (RawData rawDatum : rawData) {
+
+            String str =rawDatum.getData();
+
+            String[] s = str.split(" ");
+            Date date = rawDatum.getCreateTime();
+
+            long time = date.getTime();
+            for (int i = 0; i < 60; i++) {
+
+                long l = time - ((60 - i)* 1000 * 5 * 60); // 修正此行
+                Date thenTime = new Date(l ); // 将秒转换为毫秒
+                String str1 = s[i * 2 + 11] + s[i * 2 + 10];
+                BigDecimal weight = new BigDecimal(Integer.parseInt(str1, 16));
+                System.out.println("" +  sdf.format(thenTime)+"  重量:" +weight  );
+            }
+        }
     }
 
 }