diff --git a/pom.xml b/pom.xml
index 347b725..93f0247 100644
--- a/pom.xml
+++ b/pom.xml
@@ -44,6 +44,18 @@
log4j-api
2.17.0
+
+
+ io.netty
+ netty-all
+ 4.1.85.Final
+
+
+
+ cn.hutool
+ hutool-all
+ 5.7.16
+
com.baomidou
diff --git a/src/main/java/com/hive/bee/entity/BeeHive.java b/src/main/java/com/hive/bee/entity/BeeHive.java
new file mode 100644
index 0000000..d36ee10
--- /dev/null
+++ b/src/main/java/com/hive/bee/entity/BeeHive.java
@@ -0,0 +1,101 @@
+package com.hive.bee.entity;
+
+import lombok.*;
+
+import java.io.Serializable;
+import java.util.*;
+import java.time.LocalDateTime;
+import java.time.LocalDateTime;
+import java.time.LocalDateTime;
+import com.baomidou.mybatisplus.annotation.*;
+
+/**
+ * 蜂箱 DO
+ *
+ * @author 芋道源码
+ */
+@TableName("bee_hive")
+@KeySequence("bee_hive_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
+@Data
+@ToString(callSuper = true)
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class BeeHive implements Serializable {
+ private static long serialVersionUID = 1L;
+
+ /**
+ * 主键
+ */
+ @TableId
+ private Long id;
+ /**
+ * 蜂场id
+ *
+ * 枚举 {@link TODO mf_type 对应的类}
+ */
+ private Long peakFieldId;
+ /**
+ * 蜂箱编号
+ */
+ private String beeHiveNumber;
+ /**
+ * 蜂箱名称
+ */
+ private String beeHiveName;
+ /**
+ * mac号
+ */
+ private String beeHiveMac;
+ /**
+ * 蜂箱位置
+ */
+ private String beeHivePosition;
+ /**
+ * 最后在线时间
+ */
+ private LocalDateTime lastOnlineTime;
+ /**
+ * 在线状态
+ */
+ private Integer onlineType;
+ /**
+ * 蜂箱重量
+ */
+ private String beeHiveWeight;
+ /**
+ * 当前温度
+ */
+ private String currentTemperature;
+ /**
+ * 当前湿度
+ */
+ private String currentHumidity;
+ /**
+ * 当前噪音
+ */
+ private String currentNoise;
+ /**
+ * 是否删除
+ */
+ private boolean deleted;
+ /**
+ * 设备di
+ */
+ private long deviceId;
+ /**
+ * 蜜蜂数量
+ */
+ private String currentNumber;
+ /**
+ * 电量
+ */
+ private String electricQuantity;
+
+ /**
+ * 数据上传时间
+ */
+ @TableField(exist = false)
+ private LocalDateTime dateUploadTime;
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/hive/bee/entity/BeeHiveLog.java b/src/main/java/com/hive/bee/entity/BeeHiveLog.java
new file mode 100644
index 0000000..7fd6b02
--- /dev/null
+++ b/src/main/java/com/hive/bee/entity/BeeHiveLog.java
@@ -0,0 +1,70 @@
+package com.hive.bee.entity;
+
+import lombok.*;
+
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+import com.baomidou.mybatisplus.annotation.*;
+
+/**
+ * 蜂箱日志 DO
+ *
+ * @author 智能蜂箱
+ */
+@TableName("bee_hive_log")
+@KeySequence("bee_hive_log_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
+@Data
+@ToString(callSuper = true)
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class BeeHiveLog implements Serializable {
+ private static long serialVersionUID = 1L;
+ /**
+ * 主键
+ */
+ @TableId
+ private Long id;
+ /**
+ * 蜂场id
+ */
+ private Long peakFieldId;
+ /**
+ * 蜂箱编号
+ */
+ private String beeHiveNumber;
+ /**
+ * 蜂箱名称
+ */
+ private String beeHiveName;
+ /**
+ * 重量
+ */
+ private String beeHiveWeight;
+ /**
+ * 温度
+ */
+ private String currentTemperature;
+ /**
+ * 湿度
+ */
+ private String currentHumidity;
+ /**
+ * 噪音
+ */
+ private String currentNoise;
+ /**
+ * 是否删除
+ */
+ private boolean deleted;
+ /**
+ * 蜂箱id
+ */
+ private long beeHiveId;
+
+ /**
+ * 数据上传时间
+ */
+ private LocalDateTime dateUploadTime;
+}
\ No newline at end of file
diff --git a/src/main/java/com/hive/bee/mapper/HiveLogMapper.java b/src/main/java/com/hive/bee/mapper/HiveLogMapper.java
new file mode 100644
index 0000000..de625e8
--- /dev/null
+++ b/src/main/java/com/hive/bee/mapper/HiveLogMapper.java
@@ -0,0 +1,16 @@
+package com.hive.bee.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.hive.bee.entity.BeeHive;
+import com.hive.bee.entity.BeeHiveLog;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * 蜂箱历史数据 Mapper
+ */
+@Mapper
+public interface HiveLogMapper extends BaseMapper {
+
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/hive/bee/mapper/HiveMapper.java b/src/main/java/com/hive/bee/mapper/HiveMapper.java
new file mode 100644
index 0000000..5bb2b4f
--- /dev/null
+++ b/src/main/java/com/hive/bee/mapper/HiveMapper.java
@@ -0,0 +1,15 @@
+package com.hive.bee.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.hive.bee.entity.BeeHive;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * 蜂箱 Mapper
+ */
+@Mapper
+public interface HiveMapper extends BaseMapper {
+
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/hive/bee/mapper/xml/HiveLogMapper.xml b/src/main/java/com/hive/bee/mapper/xml/HiveLogMapper.xml
new file mode 100644
index 0000000..6f52cc1
--- /dev/null
+++ b/src/main/java/com/hive/bee/mapper/xml/HiveLogMapper.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/hive/bee/mapper/xml/HiveMapper.xml b/src/main/java/com/hive/bee/mapper/xml/HiveMapper.xml
new file mode 100644
index 0000000..bdad5a0
--- /dev/null
+++ b/src/main/java/com/hive/bee/mapper/xml/HiveMapper.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/hive/bee/service/HiveLogService.java b/src/main/java/com/hive/bee/service/HiveLogService.java
new file mode 100644
index 0000000..ddd6b3d
--- /dev/null
+++ b/src/main/java/com/hive/bee/service/HiveLogService.java
@@ -0,0 +1,14 @@
+package com.hive.bee.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.hive.bee.entity.BeeHive;
+import com.hive.bee.entity.BeeHiveLog;
+
+/**
+ * 蜂箱历史数据 Service 接口
+ */
+public interface HiveLogService extends IService {
+
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/hive/bee/service/HiveService.java b/src/main/java/com/hive/bee/service/HiveService.java
new file mode 100644
index 0000000..dc7337f
--- /dev/null
+++ b/src/main/java/com/hive/bee/service/HiveService.java
@@ -0,0 +1,13 @@
+package com.hive.bee.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.hive.bee.entity.BeeHive;
+
+/**
+ * 蜂箱 Service 接口
+ */
+public interface HiveService extends IService {
+
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/hive/bee/service/impl/HiveLogServiceImpl.java b/src/main/java/com/hive/bee/service/impl/HiveLogServiceImpl.java
new file mode 100644
index 0000000..5d6f739
--- /dev/null
+++ b/src/main/java/com/hive/bee/service/impl/HiveLogServiceImpl.java
@@ -0,0 +1,20 @@
+package com.hive.bee.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.hive.bee.entity.BeeHive;
+import com.hive.bee.entity.BeeHiveLog;
+import com.hive.bee.mapper.HiveLogMapper;
+import com.hive.bee.mapper.HiveMapper;
+import com.hive.bee.service.HiveLogService;
+import com.hive.bee.service.HiveService;
+import org.springframework.stereotype.Service;
+
+
+/**
+ * 蜂箱历史数据 Service 实现类
+ */
+@Service
+public class HiveLogServiceImpl extends ServiceImpl implements HiveLogService {
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/hive/bee/service/impl/HiveServiceImpl.java b/src/main/java/com/hive/bee/service/impl/HiveServiceImpl.java
new file mode 100644
index 0000000..7b6a6fa
--- /dev/null
+++ b/src/main/java/com/hive/bee/service/impl/HiveServiceImpl.java
@@ -0,0 +1,17 @@
+package com.hive.bee.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.hive.bee.entity.BeeHive;
+import com.hive.bee.mapper.HiveMapper;
+import com.hive.bee.service.HiveService;
+import org.springframework.stereotype.Service;
+
+
+/**
+ * 蜂箱 Service 实现类
+ */
+@Service
+public class HiveServiceImpl extends ServiceImpl implements HiveService {
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/hive/bee/vo/BeeHiveVo.java b/src/main/java/com/hive/bee/vo/BeeHiveVo.java
new file mode 100644
index 0000000..067dc14
--- /dev/null
+++ b/src/main/java/com/hive/bee/vo/BeeHiveVo.java
@@ -0,0 +1,89 @@
+package com.hive.bee.vo;
+
+import com.baomidou.mybatisplus.annotation.KeySequence;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.*;
+
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+/**
+ * 蜂箱 DO
+ *
+ * @author 芋道源码
+ */
+@TableName("bee_hive")
+@KeySequence("bee_hive_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
+@Data
+@ToString(callSuper = true)
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class BeeHiveVo implements Serializable {
+ private static long serialVersionUID = 1L;
+ /**
+ * 蜂场id
+ *
+ * 枚举 {@link TODO mf_type 对应的类}
+ */
+ private Long peakFieldId;
+ /**
+ * 蜂箱编号
+ */
+ private String beeHiveNumber;
+ /**
+ * 蜂箱名称
+ */
+ private String beeHiveName;
+ /**
+ * mac号
+ */
+ private String beeHiveMac;
+ /**
+ * 蜂箱位置
+ */
+ private String beeHivePosition;
+ /**
+ * 最后在线时间
+ */
+ private LocalDateTime lastOnlineTime;
+ /**
+ * 在线状态
+ */
+ private Integer onlineType;
+ /**
+ * 蜂箱重量
+ */
+ private String beeHiveWeight;
+ /**
+ * 当前温度
+ */
+ private String currentTemperature;
+ /**
+ * 当前湿度
+ */
+ private String currentHumidity;
+ /**
+ * 当前噪音
+ */
+ private String currentNoise;
+ /**
+ * 设备di
+ */
+ private long deviceId;
+ /**
+ * 蜜蜂数量
+ */
+ private String currentNumber;
+ /**
+ * 电量
+ */
+ private String electricQuantity;
+
+ /**
+ * 数据上传时间
+ */
+ private LocalDateTime dateUploadTime;
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/hive/communication/netty/server/ChannelManager.java b/src/main/java/com/hive/communication/netty/server/ChannelManager.java
new file mode 100644
index 0000000..51ac153
--- /dev/null
+++ b/src/main/java/com/hive/communication/netty/server/ChannelManager.java
@@ -0,0 +1,42 @@
+package com.hive.communication.netty.server;
+
+import com.hive.config.netty.NettyConfig;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+
+/**
+ * @Author: HIVE - LGH
+ * @Date: 2024/10/11 15:28
+ * @Version: V1.0
+ */
+public class ChannelManager {
+
+ // 添加Channel到group和map中
+ public static void addChannel(Channel channel, String deviceId) {
+ NettyConfig.group.add(channel);
+ NettyConfig.portToChannelMap.put(deviceId, channel);
+ }
+ // 从group和map中移除Channel
+ public static void removeChannel(Channel channel) {
+ NettyConfig.group.remove(channel);
+ NettyConfig.portToChannelMap.values().removeIf(c -> c.equals(channel));
+ }
+ // 关闭指定端口的Channel
+ public static void closeChannelByPort(int port) {
+ Channel channel = NettyConfig.portToChannelMap.get(port);
+ System.out.println(channel.isOpen());
+ if (channel != null && channel.isOpen()) {
+ ChannelFuture future = channel.close();
+ future.addListener(f -> {
+ if (f.isSuccess()) {
+ NettyConfig.portToChannelMap.remove(port);
+ }
+ });
+ }
+ }
+ // 关闭所有Channel
+ public static void closeAllChannels() {
+ NettyConfig.group.close().syncUninterruptibly();
+ NettyConfig.portToChannelMap.clear();
+ }
+}
diff --git a/src/main/java/com/hive/communication/netty/server/EchoServer.java b/src/main/java/com/hive/communication/netty/server/EchoServer.java
new file mode 100644
index 0000000..cccb259
--- /dev/null
+++ b/src/main/java/com/hive/communication/netty/server/EchoServer.java
@@ -0,0 +1,117 @@
+package com.hive.communication.netty.server;
+
+import com.hive.communication.netty.server.decoder.DelimiterBasedFrameDecoder;
+import com.hive.communication.netty.server.handler.EchoServerHandler;
+import io.netty.bootstrap.ServerBootstrap;
+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;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.codec.bytes.ByteArrayEncoder;
+import io.netty.handler.codec.string.StringEncoder;
+
+import java.nio.charset.Charset;
+import java.util.HashMap;
+import java.util.Map;
+
+public class EchoServer {
+
+ private final int port;
+
+ public EchoServer(int port) {
+ this.port = port;
+ }
+
+ private static final Map portToChannelMap = new HashMap<>();
+ private static final Map portToBootstrapMap = new HashMap<>();
+ private static final Map portToEventLoopGroupMap = new HashMap<>();
+
+ public void start() {
+ EventLoopGroup bossGroup = new NioEventLoopGroup();
+ EventLoopGroup group = new NioEventLoopGroup();
+ try {
+ ServerBootstrap sb = new ServerBootstrap();
+ sb.option(ChannelOption.SO_BACKLOG, 1024);
+ sb.group(group, bossGroup) // 绑定线程池
+ .channel(NioServerSocketChannel.class) // 指定使用的channel
+ .localAddress(port)// 绑定监听端口
+ //保持连接数
+ .option(ChannelOption.SO_BACKLOG, 128)
+ //有数据立即发送
+ .option(ChannelOption.TCP_NODELAY, true)
+ //保持连接
+ .childOption(ChannelOption.SO_KEEPALIVE, true)
+ //处理新连接
+ .childHandler(new ChannelInitializer() { // 绑定客户端连接时候触发操作
+
+ @Override
+ protected void initChannel(SocketChannel ch) throws Exception {
+ System.out.println("报告");
+ System.out.println("信息:有一客户端链接到本服务端");
+ System.out.println("IP:" + ch.localAddress().getHostName());
+ System.out.println("Port:" + ch.localAddress().getPort());
+ System.out.println("报告完毕");
+
+ ch.pipeline().addLast(new StringEncoder(Charset.forName("UTF-8")));
+ String startDelimiterStr = "99";
+ String endDelimiterStr = "0A";
+ ByteBuf startDelimiter = Unpooled.copiedBuffer(startDelimiterStr, Charset.forName("UTF-8"));
+ ByteBuf endDelimiter = Unpooled.copiedBuffer(endDelimiterStr, Charset.forName("UTF-8"));
+ ch.pipeline().addLast(new DelimiterBasedFrameDecoder(startDelimiter, endDelimiter));
+ ch.pipeline().addLast(new EchoServerHandler());
+ ch.pipeline().addLast(new ByteArrayEncoder());
+
+ }
+ });
+ ChannelFuture cf = sb.bind().sync(); // 服务器异步创建绑定
+ System.out.println(EchoServer.class + " 启动正在监听: " + cf.channel().localAddress());
+ portToChannelMap.put(port, cf.channel());
+ portToBootstrapMap.put(port, sb);
+ portToEventLoopGroupMap.put(port, group);
+ cf.channel().closeFuture().sync(); // 关闭服务器通道
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ //释放线程池资源
+ group.shutdownGracefully();
+ bossGroup.shutdownGracefully();
+ }
+ }
+
+ public void stopServer(int port) {
+ Channel channel = portToChannelMap.get(port);
+ if (channel != null && channel.isOpen()) {
+ ChannelFuture future = channel.close();
+ future.addListener(f -> {
+ if (f.isSuccess()) {
+ portToChannelMap.remove(port);
+ // 确保在Channel关闭后处理ServerBootstrap和EventLoopGroup
+ handleServerBootstrapAndEventLoopGroup(port);
+ } else {
+ // 处理关闭Channel失败的情况
+ }
+ });
+ } else {
+ // 处理Channel不存在或已关闭的情况
+ handleServerBootstrapAndEventLoopGroup(port);
+ }
+ }
+
+ private void handleServerBootstrapAndEventLoopGroup(int port) {
+ ServerBootstrap bootstrap = portToBootstrapMap.get(port);
+ if (bootstrap != null) {
+ EventLoopGroup bossGroup = portToEventLoopGroupMap.get(port);
+ EventLoopGroup workerGroup = bootstrap.childGroup();
+ bossGroup.shutdownGracefully();
+ workerGroup.shutdownGracefully();
+ portToBootstrapMap.remove(port);
+ portToEventLoopGroupMap.remove(port);
+ } else {
+ System.out.println("找不到端口" + port);
+ }
+ }
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/hive/communication/netty/server/NettyServerRunner.java b/src/main/java/com/hive/communication/netty/server/NettyServerRunner.java
new file mode 100644
index 0000000..6eebceb
--- /dev/null
+++ b/src/main/java/com/hive/communication/netty/server/NettyServerRunner.java
@@ -0,0 +1,15 @@
+package com.hive.communication.netty.server;
+
+import com.hive.communication.util.NettyServerUtil;
+import com.hive.config.netty.NettyHiveServerConfig;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.stereotype.Component;
+
+@Component
+public class NettyServerRunner implements CommandLineRunner {
+
+ @Override
+ public void run(String... args) throws Exception {
+ new EchoServer(NettyServerUtil.getPort()).start();
+ }
+}
diff --git a/src/main/java/com/hive/communication/netty/server/dataprocessing/BeeHiveAdd.java b/src/main/java/com/hive/communication/netty/server/dataprocessing/BeeHiveAdd.java
new file mode 100644
index 0000000..fbbd17c
--- /dev/null
+++ b/src/main/java/com/hive/communication/netty/server/dataprocessing/BeeHiveAdd.java
@@ -0,0 +1,37 @@
+package com.hive.communication.netty.server.dataprocessing;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.hive.bee.entity.BeeHive;
+import com.hive.bee.entity.BeeHiveLog;
+import com.hive.bee.service.HiveLogService;
+import com.hive.bee.service.HiveService;
+import com.hive.bee.vo.BeeHiveVo;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import static com.hive.util.ConverterUtil.copySameFields;
+
+@Slf4j
+@Component
+public class BeeHiveAdd {
+ @Autowired
+ private HiveService hiveService;
+ @Autowired
+ private HiveLogService hiveLogService;
+ public void addAndEdit(BeeHive beeHive){
+ hiveService.getBaseMapper().updateById(beeHive);
+ BeeHiveLog beeHiveLog = new BeeHiveLog();
+ BeeHiveVo beeHiveVo = new BeeHiveVo();
+ //用来去除不需要的字段
+ copySameFields(beeHive, beeHiveVo);
+ copySameFields(beeHiveVo, beeHiveLog);
+ beeHiveLog.setBeeHiveId(beeHive.getId());
+ hiveLogService.save(beeHiveLog);
+ }
+ public BeeHive getOneBeeHive(long deviceId){
+ QueryWrapper queryWrapper = new QueryWrapper<>();
+ queryWrapper.lambda().eq(BeeHive::isDeleted,true).eq(BeeHive::getDeviceId,deviceId);
+ return hiveService.getBaseMapper().selectOne(queryWrapper);
+ }
+}
diff --git a/src/main/java/com/hive/communication/netty/server/dataprocessing/HandlerDateProcessing.java b/src/main/java/com/hive/communication/netty/server/dataprocessing/HandlerDateProcessing.java
new file mode 100644
index 0000000..d8ccd52
--- /dev/null
+++ b/src/main/java/com/hive/communication/netty/server/dataprocessing/HandlerDateProcessing.java
@@ -0,0 +1,157 @@
+package com.hive.communication.netty.server.dataprocessing;
+
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.hive.bee.entity.BeeHive;
+import com.hive.communication.util.HexConversion;
+import io.netty.channel.ChannelHandlerContext;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static com.hive.communication.netty.server.handler.EchoServerHandler.sendActive;
+import static com.hive.util.DateConvetUtil.isDifferenceMoreThan10Minutes;
+
+/**
+ * 数据处理
+ *
+ * @Author: hive - LGH
+ * @Date: 2024/10/11 14:46
+ * @Version: V1.0
+ */
+@Slf4j
+@Component
+public class HandlerDateProcessing {
+
+
+ /**
+ * 处理响应数据
+ */
+ private final AtomicBoolean isScheduled = new AtomicBoolean(false);
+
+ // private boolean isScheduled = false;
+ public void handleResponse(ChannelHandlerContext ctx, StringBuffer sb) throws Exception {
+ boolean flag = false;
+ log.info("收到蜂箱数据" + sb);
+ BeeHive beeHive = dataAnalysis(sb.toString());
+ if (ObjectUtils.isNotNull(beeHive)) {
+ BeeHiveAdd beeHiveAdd = new BeeHiveAdd();
+ beeHiveAdd.addAndEdit(beeHive);
+ flag = true;
+ }
+ // 处理响应逻辑
+ // 读取数据并获取设备编号
+ // String deviceId = deviceChannels.get(ctx.channel().id());
+ // String deviceId = ctx.channel().attr(DEVICE_ID).get();
+ //数据长度不够则解析异常,需要设备再次上报
+ /* if (!flag) {
+ //解析 数据不符合要求,需要设备再次上报
+ } else {
+
+ //变更注册回复协议
+ }*/
+ // 创建一个CompletableFuture,将在2s后完成
+ if (flag) {
+ CompletableFuture future = new CompletableFuture<>();
+ //isScheduled.compareAndSet 确保schedule()方法只被调用一次
+ if (isScheduled.compareAndSet(false, true)) {
+ ctx.channel().eventLoop().schedule(() -> {
+ try {
+ // 正常完成Future
+ future.complete(null);
+ } catch (Exception e) {
+ // 异常完成Future
+ future.completeExceptionally(e);
+ } finally {
+ isScheduled.set(false); // 无论如何都将标志重置为false
+ }
+ // }, 4, TimeUnit.HOURS);
+ }, 2, TimeUnit.SECONDS); // 将延迟时间设置为2s
+ // isScheduled = true; // 设置标志变量为true,表示延迟逻辑已被调度
+ future.thenAccept(v -> sendActive(ctx, true, beeHive, true));
+ //ctx.writeAndFlush(messageBuffer)
+
+ } else {
+ //ctx.writeAndFlush();
+ }
+ }
+ }
+
+ public BeeHive dataAnalysis(String data) {
+ BeeHiveAdd beeHiveAdd = new BeeHiveAdd();
+ BeeHive beeHive = new BeeHive();
+ for (int i = 0; i < 7; i++) {
+ Integer startIndex = 0;
+ Integer endIndex = 0;
+ switch (i) {
+ case 0:
+ //蜂箱id
+ startIndex += 16;
+ endIndex += 32;
+ long deviceId = HexConversion.hexToBigLong(getSubData(data, startIndex, endIndex));
+ beeHive = beeHiveAdd.getOneBeeHive(deviceId);
+ if(ObjectUtils.isNull(beeHive)){
+ return null;
+ }
+ beeHive.setDeviceId(deviceId);
+ break;
+ case 1:
+ //时间戳
+ startIndex += 6;
+ endIndex += 14;
+ long date = HexConversion.hexToBigInt(getSubData(data, startIndex, endIndex));
+ //时间戳大于10分钟则为有效数据
+ if (!isDifferenceMoreThan10Minutes(Instant.now().getEpochSecond(), date)) {
+ return null;
+ }
+ beeHive.setDateUploadTime(LocalDateTime.ofInstant(Instant.ofEpochSecond(date), ZoneId.systemDefault()));
+ break;
+ case 2:
+ //当前温度值2
+ startIndex += 36;
+ endIndex += 40;
+ beeHive.setCurrentTemperature(String.valueOf(HexConversion.hexadecimal16Conversion(getSubData(data, startIndex, endIndex)) / 10));
+ //startIndex=4 ,endIndex=10
+ break;
+ case 3:
+ //当前湿度值2
+ startIndex += 40;
+ endIndex += 44;
+ beeHive.setCurrentHumidity(String.valueOf(HexConversion.hexadecimal16Conversion(getSubData(data, startIndex, endIndex))));
+ //startIndex=4 ,endIndex=10
+ break;
+ case 4:
+ //蜂箱重量4
+ startIndex += 44;
+ endIndex += 52;
+ beeHive.setBeeHiveWeight(String.valueOf(HexConversion.hexDouble2BigDecimal2(getSubData(data, startIndex, endIndex))));
+ //startIndex=4 ,endIndex=10
+ break;
+ case 5:
+ //蜂进出数量4
+ startIndex += 52;
+ endIndex += 60;
+ beeHive.setCurrentNumber(String.valueOf(HexConversion.hexadecimal16Conversion(getSubData(data, startIndex, endIndex))));
+ //startIndex=4 ,endIndex=10
+ break;
+ case 6:
+ //声音异常0表示无异常FF表示异常1
+ startIndex += 60;
+ endIndex += 62;
+ beeHive.setCurrentNoise(String.valueOf(getSubData(data, startIndex, endIndex)));
+ break;
+ }
+ }
+ return beeHive;
+ }
+
+ public String getSubData(String data, Integer startIndex, Integer endIndex) {
+ return data.substring(startIndex, endIndex);
+ }
+
+}
diff --git a/src/main/java/com/hive/communication/netty/server/decoder/CustomDecoder.java b/src/main/java/com/hive/communication/netty/server/decoder/CustomDecoder.java
new file mode 100644
index 0000000..720e7ee
--- /dev/null
+++ b/src/main/java/com/hive/communication/netty/server/decoder/CustomDecoder.java
@@ -0,0 +1,69 @@
+package com.hive.communication.netty.server.decoder;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+
+import java.util.List;
+
+/**
+ * @Author: HIVE - LGH
+ * @Date: 2024/10/11 13:27
+ * @Version: V1.0
+ */
+public class CustomDecoder extends ByteToMessageDecoder {
+ private static final byte HEARTBEAT_FLAG = (byte) 0xfe; // 心跳消息的标识
+ private int length1;
+ private int length2;
+ private byte specialFlag;
+
+ public CustomDecoder(int length1, int length2, byte specialFlag) {
+ this.length1 = length1;
+ this.length2 = length2;
+ this.specialFlag = specialFlag;
+ }
+
+ @Override
+ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List