diff --git a/.idea/compiler.xml b/.idea/compiler.xml index dde952b..3ec2db6 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -2,16 +2,19 @@ + + diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml index c7ea920..26a2c9e 100644 --- a/.idea/jarRepositories.xml +++ b/.idea/jarRepositories.xml @@ -1,6 +1,11 @@ + + diff --git a/src/main/java/com/hive/SharpCoreApplication.java b/src/main/java/com/hive/SharpCoreApplication.java index 6783490..174f007 100644 --- a/src/main/java/com/hive/SharpCoreApplication.java +++ b/src/main/java/com/hive/SharpCoreApplication.java @@ -2,16 +2,18 @@ package com.hive; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.context.annotation.Bean; @SpringBootApplication -@MapperScan("com.hive.mapper") +@MapperScan("com.hive.*.mapper") public class SharpCoreApplication { public static void main(String[] args) { - SpringApplication.run(SharpCoreApplication.class,args); + SpringApplication.run(SharpCoreApplication.class, args); } + + } + diff --git a/src/main/java/com/hive/bee/entity/BeeHive.java b/src/main/java/com/hive/bee/entity/BeeHive.java index d36ee10..4d66e44 100644 --- a/src/main/java/com/hive/bee/entity/BeeHive.java +++ b/src/main/java/com/hive/bee/entity/BeeHive.java @@ -3,10 +3,8 @@ 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.*; /** @@ -82,7 +80,7 @@ public class BeeHive implements Serializable { /** * 设备di */ - private long deviceId; + private String deviceId; /** * 蜜蜂数量 */ @@ -95,7 +93,6 @@ public class BeeHive implements Serializable { /** * 数据上传时间 */ - @TableField(exist = false) private LocalDateTime dateUploadTime; } \ 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 index dc7337f..4e92724 100644 --- a/src/main/java/com/hive/bee/service/HiveService.java +++ b/src/main/java/com/hive/bee/service/HiveService.java @@ -2,12 +2,15 @@ package com.hive.bee.service; import com.baomidou.mybatisplus.extension.service.IService; import com.hive.bee.entity.BeeHive; +import org.springframework.stereotype.Service; /** * 蜂箱 Service 接口 */ -public interface HiveService extends IService { +public interface HiveService extends IService { + void addAndEdit(BeeHive beeHive); + BeeHive getOneBeeHive(String deviceId); } \ 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 index 7b6a6fa..74f5172 100644 --- a/src/main/java/com/hive/bee/service/impl/HiveServiceImpl.java +++ b/src/main/java/com/hive/bee/service/impl/HiveServiceImpl.java @@ -1,17 +1,43 @@ package com.hive.bee.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 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.HiveMapper; +import com.hive.bee.service.HiveLogService; import com.hive.bee.service.HiveService; +import com.hive.bee.vo.BeeHiveVo; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import static com.hive.util.ConverterUtil.copySameFields; + /** * 蜂箱 Service 实现类 */ @Service public class HiveServiceImpl extends ServiceImpl implements HiveService { + @Autowired + private HiveLogService hiveLogService; + @Override + public void addAndEdit(BeeHive beeHive) { + this.updateById(beeHive); + BeeHiveLog beeHiveLog = new BeeHiveLog(); + BeeHiveVo beeHiveVo = new BeeHiveVo(); + //用来去除不需要的字段 + copySameFields(beeHive, beeHiveVo); + copySameFields(beeHiveVo, beeHiveLog); + beeHiveLog.setBeeHiveId(beeHive.getId()); + hiveLogService.save(beeHiveLog); + } + @Override + public BeeHive getOneBeeHive(String deviceId) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda().eq(BeeHive::isDeleted,true).eq(BeeHive::getDeviceId,deviceId); + return this.getOne(queryWrapper); + } } \ No newline at end of file diff --git a/src/main/java/com/hive/communication/netty/server/EchoServer.java b/src/main/java/com/hive/communication/netty/server/EchoServer.java index cccb259..0ab712a 100644 --- a/src/main/java/com/hive/communication/netty/server/EchoServer.java +++ b/src/main/java/com/hive/communication/netty/server/EchoServer.java @@ -59,7 +59,7 @@ public class EchoServer { 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 DelimiterBasedFrameDecoder(startDelimiter, endDelimiter)); ch.pipeline().addLast(new EchoServerHandler()); ch.pipeline().addLast(new ByteArrayEncoder()); 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 index fbbd17c..aea08c9 100644 --- a/src/main/java/com/hive/communication/netty/server/dataprocessing/BeeHiveAdd.java +++ b/src/main/java/com/hive/communication/netty/server/dataprocessing/BeeHiveAdd.java @@ -20,7 +20,7 @@ public class BeeHiveAdd { @Autowired private HiveLogService hiveLogService; public void addAndEdit(BeeHive beeHive){ - hiveService.getBaseMapper().updateById(beeHive); + hiveService.updateById(beeHive); BeeHiveLog beeHiveLog = new BeeHiveLog(); BeeHiveVo beeHiveVo = new BeeHiveVo(); //用来去除不需要的字段 @@ -29,9 +29,9 @@ public class BeeHiveAdd { beeHiveLog.setBeeHiveId(beeHive.getId()); hiveLogService.save(beeHiveLog); } - public BeeHive getOneBeeHive(long deviceId){ + public BeeHive getOneBeeHive(String deviceId){ QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.lambda().eq(BeeHive::isDeleted,true).eq(BeeHive::getDeviceId,deviceId); - return hiveService.getBaseMapper().selectOne(queryWrapper); + queryWrapper.lambda().eq(BeeHive::isDeleted,0).eq(BeeHive::getDeviceId,deviceId); + return hiveService.getOne(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 index d8ccd52..a24ccc1 100644 --- a/src/main/java/com/hive/communication/netty/server/dataprocessing/HandlerDateProcessing.java +++ b/src/main/java/com/hive/communication/netty/server/dataprocessing/HandlerDateProcessing.java @@ -1,5 +1,7 @@ package com.hive.communication.netty.server.dataprocessing; +import cn.hutool.core.util.HexUtil; +import cn.hutool.extra.spring.SpringUtil; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.hive.bee.entity.BeeHive; import com.hive.communication.util.HexConversion; @@ -7,6 +9,7 @@ import io.netty.channel.ChannelHandlerContext; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; +import java.math.BigDecimal; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; @@ -40,7 +43,7 @@ public class HandlerDateProcessing { log.info("收到蜂箱数据" + sb); BeeHive beeHive = dataAnalysis(sb.toString()); if (ObjectUtils.isNotNull(beeHive)) { - BeeHiveAdd beeHiveAdd = new BeeHiveAdd(); + BeeHiveAdd beeHiveAdd = SpringUtil.getBean(BeeHiveAdd.class); beeHiveAdd.addAndEdit(beeHive); flag = true; } @@ -83,8 +86,11 @@ public class HandlerDateProcessing { } public BeeHive dataAnalysis(String data) { - BeeHiveAdd beeHiveAdd = new BeeHiveAdd(); + BeeHiveAdd beeHiveAdd = SpringUtil.getBean(BeeHiveAdd.class); BeeHive beeHive = new BeeHive(); + if (data.length() != 61) { + return beeHive; + } for (int i = 0; i < 7; i++) { Integer startIndex = 0; Integer endIndex = 0; @@ -93,12 +99,12 @@ public class HandlerDateProcessing { //蜂箱id startIndex += 16; endIndex += 32; - long deviceId = HexConversion.hexToBigLong(getSubData(data, startIndex, endIndex)); - beeHive = beeHiveAdd.getOneBeeHive(deviceId); - if(ObjectUtils.isNull(beeHive)){ + String subData = getSubData(data, startIndex, endIndex); + beeHive = beeHiveAdd.getOneBeeHive(subData); + if (ObjectUtils.isNull(beeHive)) { return null; } - beeHive.setDeviceId(deviceId); + beeHive.setDeviceId(subData); break; case 1: //时间戳 @@ -106,16 +112,17 @@ public class HandlerDateProcessing { endIndex += 14; long date = HexConversion.hexToBigInt(getSubData(data, startIndex, endIndex)); //时间戳大于10分钟则为有效数据 - if (!isDifferenceMoreThan10Minutes(Instant.now().getEpochSecond(), date)) { - return null; - } +// 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)); + int i2 = HexUtil.hexToInt(getSubData(data, startIndex, endIndex)); + beeHive.setCurrentTemperature(String.valueOf(new BigDecimal(i2).multiply(new BigDecimal("0.1")))); //startIndex=4 ,endIndex=10 break; case 3: @@ -129,14 +136,15 @@ public class HandlerDateProcessing { //蜂箱重量4 startIndex += 44; endIndex += 52; - beeHive.setBeeHiveWeight(String.valueOf(HexConversion.hexDouble2BigDecimal2(getSubData(data, startIndex, endIndex)))); + int i1 = HexUtil.hexToInt(getSubData(data, startIndex, endIndex)); + beeHive.setBeeHiveWeight(String.valueOf(BigDecimal.valueOf(i1).multiply(BigDecimal.valueOf(0.001)))); //startIndex=4 ,endIndex=10 break; case 5: //蜂进出数量4 startIndex += 52; endIndex += 60; - beeHive.setCurrentNumber(String.valueOf(HexConversion.hexadecimal16Conversion(getSubData(data, startIndex, endIndex)))); + beeHive.setCurrentNumber(String.valueOf(HexUtil.hexToInt(getSubData(data, startIndex, endIndex)))); //startIndex=4 ,endIndex=10 break; case 6: diff --git a/src/main/java/com/hive/communication/netty/server/handler/EchoServerHandler.java b/src/main/java/com/hive/communication/netty/server/handler/EchoServerHandler.java index faf16e8..f22c866 100644 --- a/src/main/java/com/hive/communication/netty/server/handler/EchoServerHandler.java +++ b/src/main/java/com/hive/communication/netty/server/handler/EchoServerHandler.java @@ -31,7 +31,7 @@ public class EchoServerHandler extends ChannelInboundHandlerAdapter { private boolean isHeartbeatReceived = false; // 创建一个固定大小的线程池用于消息处理 // private final ExecutorService executorService = Executors.newFixedThreadPool(10); - private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10); // 使用AttributeKey定义设备编号属性的键 private static final AttributeKey DEVICE_ID = AttributeKey.valueOf("deviceId"); private static final ConcurrentHashMap pendingRequests = new ConcurrentHashMap<>(); diff --git a/src/main/java/com/hive/util/SpringContextUtil.java b/src/main/java/com/hive/util/SpringContextUtil.java new file mode 100644 index 0000000..f0f8800 --- /dev/null +++ b/src/main/java/com/hive/util/SpringContextUtil.java @@ -0,0 +1,16 @@ +package com.hive.util; + +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + +import javax.servlet.ServletContext; + +public class SpringContextUtil { + public static T getBean(ServletContext request, String name, Class clazz){ + WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request); + // 或者 +// webApplicationContext1.getBean(name, clazz) + T bean = webApplicationContext.getBean(name, clazz); + return bean; + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 02efe8a..613a029 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -44,4 +44,4 @@ minio: bucket-name: works # 默认的Bucket名称 secure: false # 是否使用HTTPS(如果使用HTTPS,请设置为true) hive-server: - port: 888 + port: 8899