代码提交

main
zhangshengli 4 weeks ago
parent 7b9ffea60b
commit 54a4c84eb8

@ -2,16 +2,19 @@
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="iot_hive" />
<module name="sharp_core" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="iot_hive" options="-parameters" />
<module name="sharp_core" options="-parameters" />
</option>
</component>

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />

@ -57,12 +57,12 @@
<version>5.7.16</version> <!-- 请使用最新的版本 -->
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<!--缺少此jar包导致@Mapper注解无效-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
@ -89,5 +89,13 @@
<version>8.0.31</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

@ -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);
}
}

@ -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;
}

@ -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<BeeHive> {
public interface HiveService extends IService<BeeHive> {
void addAndEdit(BeeHive beeHive);
BeeHive getOneBeeHive(String deviceId);
}

@ -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<HiveMapper, BeeHive> 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<BeeHive> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(BeeHive::isDeleted,true).eq(BeeHive::getDeviceId,deviceId);
return this.getOne(queryWrapper);
}
}

@ -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());

@ -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<BeeHive> 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);
}
}

@ -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:

@ -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<String> DEVICE_ID = AttributeKey.valueOf("deviceId");
private static final ConcurrentHashMap<String, ChannelPromise> pendingRequests = new ConcurrentHashMap<>();

@ -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> T getBean(ServletContext request, String name, Class<T> clazz){
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request);
// 或者
// webApplicationContext1.getBean(name, clazz)
T bean = webApplicationContext.getBean(name, clazz);
return bean;
}
}

@ -44,4 +44,4 @@ minio:
bucket-name: works # 默认的Bucket名称
secure: false # 是否使用HTTPS如果使用HTTPS请设置为true
hive-server:
port: 888
port: 8899

Loading…
Cancel
Save