1. 增加设备管理功能

2. 增加数据库相关功能
This commit is contained in:
HuangXin 2023-08-10 23:07:55 +08:00
parent 91c3493f43
commit ec79edab23
16 changed files with 342 additions and 30 deletions

View File

@ -27,6 +27,8 @@ spring.datasource.dbcp2.connection-properties=characterEncoding=utf8
#mybatis.configuration.map-underscore-to-camel-case: true
mybatis.mapper-locations=classpath*:mappers/*.xml
mybatis.type-aliases-package=com.zjyr.beidouservice.pojo.entry
#mybatis.configuration.default-enum-type-handler=com.dispose.common.CommonEnumHandler
mybatis.configuration.default-enum-type-handler=com.zjyr.beidouservice.common.CommonEnumHandler
#mybatis.configuration.log-impl=lombok.extern.slf4j.Slf4j
#config log
logging.config=file:config/logback.xml
log4j.logger.org.mybatis=debug

View File

@ -4,6 +4,8 @@
<property name="LOG_PATH" value="./logs"/>
<property name="LOG_LEVEL" value="info"/>
<property name="SVR_LOG_LEVEL" value="info"/>
<property name="DEBUG_LOG_LEVEL" value="debug"/>
<property name="INFO_LOG_LEVEL" value="info"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="UTF-8">
@ -46,14 +48,7 @@
</encoder>
</appender>
<logger name="com.dispose.restful" level="${SVR_LOG_LEVEL}"
additivity="false">
<appender-ref ref="DATA"/>
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="com.dispose" level="${LOG_LEVEL}"
additivity="false">
<logger name="com.zjyr.beidouservice.mapper" level="debug" additivity="false">
<appender-ref ref="DATA"/>
<appender-ref ref="CONSOLE"/>
</logger>

16
pom.xml
View File

@ -20,6 +20,12 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
@ -70,11 +76,21 @@
<artifactId>mapper-spring-boot-starter</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>com.github.seancfoley</groupId>
<artifactId>ipaddress</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.4.0.jre11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
<build>

View File

@ -23,8 +23,8 @@ public class ChannelInit extends ChannelInitializer<SocketChannel> {
protected void initChannel(SocketChannel channel) {
channel.pipeline()
.addLast("idle", new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS))
.addLast("encode", new YuanRongProtocolDecode())
.addLast("decode", new StringDecoder(CharsetUtil.UTF_8))
.addLast("decode", new YuanRongProtocolDecode())
.addLast("encode", new StringDecoder(CharsetUtil.UTF_8))
.addLast("message", messageHandler);
}
}

View File

@ -1,6 +1,8 @@
package com.zjyr.beidouservice.adapter.impl.netty;
import com.zjyr.beidouservice.misc.HelperUtils;
import com.zjyr.beidouservice.common.impl.ControlDeviceTypeName;
import com.zjyr.beidouservice.mapper.ControlDeviceMapper;
import com.zjyr.beidouservice.pojo.entry.ControlDevice;
import com.zjyr.beidouservice.pojo.vo.ControlAdapterSocketCtx;
import com.zjyr.beidouservice.pojo.vo.YuanRongBinProtocol;
import io.netty.channel.ChannelHandler;
@ -8,10 +10,12 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
@ -20,6 +24,8 @@ import java.util.concurrent.ConcurrentHashMap;
@RequiredArgsConstructor
public class MessageHandler extends SimpleChannelInboundHandler<YuanRongBinProtocol> {
public static ConcurrentHashMap<Long, ControlAdapterSocketCtx> ctxMap = new ConcurrentHashMap<>();
@Resource
private ControlDeviceMapper controlDeviceMapper;
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
@ -40,20 +46,26 @@ public class MessageHandler extends SimpleChannelInboundHandler<YuanRongBinProto
@Override
public void run() {
//长时间操作不至于长时间的业务操作导致Handler阻塞
//log.info("{}:: Receive Message: {}", ctx.channel().id(), HelperUtils.bytesToHexString(message.getStart()));
//log.info("{}:: Receive Message: {}", ctx.channel().id(), HelperUtils.bytesToHexString(message
// .getStart()));
}
});
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
log.info("{}:: Connected", ctx.channel().id());
InetSocketAddress sa = (InetSocketAddress) ctx.channel().remoteAddress();
//List<ControlDevice> list = controlDeviceMapper.selectAll();
log.info("{}:: Connected <-- {}:{}", ctx.channel().id(), sa.getAddress().getHostAddress(), sa.getPort());
controlDeviceMapper.addControlDevice(ControlDevice.builder().deviceType(ControlDeviceTypeName.DEVICE_SOCKET_TCP)
.deviceAddr(sa.getAddress().getHostAddress()).build());
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("{}:: Disonnected", ctx.channel().id());
InetSocketAddress sa = (InetSocketAddress) ctx.channel().remoteAddress();
log.info("{}:: Disonnected <-- {}", ctx.channel().id(), sa.getAddress().getHostAddress());
super.channelActive(ctx);
ctx.close();
}

View File

@ -3,10 +3,12 @@ package com.zjyr.beidouservice.adapter.impl.netty.decode;
import com.zjyr.beidouservice.pojo.vo.ControlAdapterSocketCtx;
import com.zjyr.beidouservice.pojo.vo.YuanRongBinProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.nio.ByteBuffer;
@ -19,6 +21,7 @@ import java.util.regex.Pattern;
@Component
@RequiredArgsConstructor
@Slf4j
public class YuanRongProtocolDecode extends ByteToMessageDecoder {
public static final ByteBuf receiveBuffer = Unpooled.buffer(1024 * 1024);
@ -63,16 +66,31 @@ public class YuanRongProtocolDecode extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
// cache receive data
receiveBuffer.writeBytes(buffer.array());
//receiveBuffer.writeBytes(buffer.array());
log.info(ByteBufUtil.prettyHexDump(buffer));
ctx.channel().eventLoop().execute(new Runnable() {
@Override
public void run() {
if (receiveBuffer.readableBytes() > YuanRongBinProtocol.MIN_LEN) {
byte[] val = new byte[buffer.readableBytes()];
buffer.readBytes(val, 0, buffer.readableBytes());
if(buffer.readableBytes() > YuanRongBinProtocol.MIN_LEN) {
byte[] data = ByteBufUtil.getBytes(buffer);
byte[] startFlag = YuanRongBinProtocol.START.getBytes();
for(int i = 0; i < data.length - startFlag.length; i++) {
byte[] sclicedArr = Arrays.copyOfRange(data, i, i + startFlag.length);
if (Arrays.equals(sclicedArr, startFlag)) {
for (int j = i; j < data.length; j++) {
// Find package head end flag
if (data[j] == '$') {
// It must be a package
Matcher matcher = YuanRongBinProtocol.compile.matcher(new String(ByteBufUtil.getBytes(buffer, i, j - i + 1)));
if(matcher.find()) {
for(int k = 0; k <= matcher.groupCount(); k++){
System.out.println(matcher.group(k));
}
}
}
}
}
}
});
}
}
}

View File

@ -0,0 +1,139 @@
package com.zjyr.beidouservice.common;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
public final class CommonEnumHandler<E extends EnumerationBase> extends BaseTypeHandler<E> {
/**
* The Enum type.
*/
private final Class<E> enumType;
/**
* The Enums.
*/
private final List<E> enums;
/**
* Instantiates a new Common enum handler.
*
* @param type the type
*/
public CommonEnumHandler(Class<E> type) {
if (type == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.enumType = type;
this.enums = Arrays.asList(type.getEnumConstants());
}
/**
* Code of e.
*
* @param <E> the type parameter
* @param enumClass the enum class
* @param code the code
* @return the e
*/
public static <E extends Enum<?> & EnumerationBase> E codeOf(Class<E> enumClass, int code) {
E[] enumCodes = enumClass.getEnumConstants();
for (E e : enumCodes) {
if (e.getValue() == code) {
return e;
}
}
return null;
}
/**
* Sets non null parameter.
*
* @param preparedStatement the prepared statement
* @param i the
* @param e the e
* @param jdbcType the jdbc type
* @throws SQLException the sql exception
*/
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, E e, JdbcType jdbcType) throws SQLException {
preparedStatement.setInt(i, e.getValue());
}
/**
* Gets nullable result.
*
* @param resultSet the result set
* @param s the s
* @return the nullable result
* @throws SQLException the sql exception
*/
@Override
public E getNullableResult(ResultSet resultSet, String s) throws SQLException {
if (resultSet.getObject(s) == null) {
return null;
}
int val = resultSet.getInt(s);
return locateEnumStatus(val);
}
/**
* Gets nullable result.
*
* @param resultSet the result set
* @param index the index
* @return the nullable result
* @throws SQLException the sql exception
*/
@Override
public E getNullableResult(ResultSet resultSet, int index) throws SQLException {
if (resultSet.getObject(index) == null) {
return null;
}
int val = resultSet.getInt(index);
return locateEnumStatus(val);
}
/**
* Gets nullable result.
*
* @param callableStatement the callable statement
* @param index the index
* @return the nullable result
* @throws SQLException the sql exception
*/
@Override
public E getNullableResult(CallableStatement callableStatement, int index) throws SQLException {
if (callableStatement.getObject(index) == null) {
return null;
}
int val = callableStatement.getInt(index);
return locateEnumStatus(val);
}
/**
* Locate enum status e.
*
* @param index the index
* @return the e
*/
private E locateEnumStatus(int index) {
for (E e : enums) {
if (e.getValue() == index) {
return e;
}
}
throw new IllegalArgumentException(enumType.getName() + " unknown enumerated type index:" + index);
}
}

View File

@ -0,0 +1,6 @@
package com.zjyr.beidouservice.common;
public interface EnumerationBase {
Integer getValue();
String getDescription();
}

View File

@ -0,0 +1,28 @@
package com.zjyr.beidouservice.common.impl;
import com.zjyr.beidouservice.common.EnumerationBase;
public enum ControlDeviceTypeName implements EnumerationBase {
DEVICE_SOCKET_TCP(0, "HOST_TOTAL_TRAFFIC"),
DEVICE_SOCKET_UDP(1, "HOST_TOTAL_TRAFFIC"),
;
private final Integer code;
private final String desc;
ControlDeviceTypeName(int val, String desc) {
this.code = val;
this.desc = desc;
}
@Override
public Integer getValue() {
return this.code;
}
@Override
public String getDescription() {
return this.desc;
}
}

View File

@ -0,0 +1,13 @@
package com.zjyr.beidouservice.mapper;
import com.zjyr.beidouservice.common.impl.ControlDeviceTypeName;
import com.zjyr.beidouservice.pojo.entry.ControlDevice;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ControlDeviceMapper {
List<ControlDevice> selectAll();
int addControlDevice(@Param("device") ControlDevice device);
}

View File

@ -2,6 +2,7 @@ package com.zjyr.beidouservice.pojo.entry;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.zjyr.beidouservice.common.impl.ControlDeviceTypeName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -25,5 +26,6 @@ public class ControlDevice {
@Id
@KeySql(useGeneratedKeys = true)
private Long id;
private Long deviceType;
private ControlDeviceTypeName deviceType;
private String deviceAddr;
}

View File

@ -2,12 +2,15 @@ package com.zjyr.beidouservice.pojo.vo;
import lombok.Data;
import java.util.regex.Pattern;
@Data
public class YuanRongBinProtocol {
public static int MIN_LEN = 25;
public static String START = "[length=";
public static String HEAD_PARTERN = "\\[length=\\d{1,4}\\]@\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}:\\d{1,5}\\|\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}@\\$";
private static String HEAD_PARTERN = "^\\[length=(\\d{1,4})]@(\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}):(\\d{1,5})\\|(\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3})@\\$.*$";
public static Pattern compile = Pattern.compile(HEAD_PARTERN);
private byte[] proHeader;
private byte[] proData;

View File

@ -0,0 +1,21 @@
<?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.zjyr.beidouservice.mapper.ControlDeviceMapper">
<resultMap id="control_device" type="com.zjyr.beidouservice.pojo.entry.ControlDevice">
<id column="id" property="id"/>
<result column="deviceType" property="deviceType"/>
<result column="deviceAddr" property="deviceAddr"/>
</resultMap>
<select id="selectAll" resultMap="control_device">
SELECT *
FROM control_device
</select>
<insert id="addControlDevice" useGeneratedKeys="true" keyProperty="id"
parameterType="com.zjyr.beidouservice.pojo.entry.ControlDevice">
INSERT IGNORE INTO control_device(deviceAddr, deviceType)
SELECT #{device.deviceAddr}, #{device.deviceType} FROM DUAL WHERE NOT EXISTS (SELECT deviceAddr FROM control_device
WHERE deviceAddr = #{device.deviceAddr})
</insert>
</mapper>

View File

@ -1,8 +1,11 @@
package com.zjyr.beidouservice;
import inet.ipaddr.HostName;
import inet.ipaddr.IPAddress;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
@ -14,13 +17,24 @@ class BaidouServiceApplicationTests {
@Test
void contextLoads() {
String str = "[length=108]@127.0.0.1:51141|192.168.1.80@$";
Pattern compile = Pattern.compile("\\[length=\\d{1,4}\\]@\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}:\\d{1,5}\\|\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}@\\$");
Pattern compile = Pattern.compile("^\\[length=(\\d{1,4})]@(\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}):(\\d{1,5})\\|(\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3})@\\$.*$");
Matcher matcher = compile.matcher(str);
List<String> targetList = new ArrayList<>();
while (matcher.find()) {
String substring = str.substring(matcher.start(), matcher.end());
targetList.add(substring);
for(int i = 0; i <= matcher.groupCount(); i++){
System.out.println(matcher.group(i));
}
}
}
@Test
void ipAddressTest() {
String host = "127.0.0.1:23355";
HostName host2 = new HostName(host);
IPAddress adr = host2.asAddress();
System.out.println(adr.toIPAddress());
}
}

View File

@ -0,0 +1,43 @@
package com.zjyr.beidouservice.mapper;
import com.zjyr.beidouservice.common.impl.ControlDeviceTypeName;
import com.zjyr.beidouservice.pojo.entry.ControlDevice;
import com.zjyr.beidouservice.pojo.entry.ControlDeviceType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
//@Transactional
//@Rollback
public class ControlDeviceMapperTest {
@Resource
private ControlDeviceMapper controlDeviceMapper;
@Test
public void a1_getAllControlDevice() {
List<ControlDevice> typeList = controlDeviceMapper.selectAll();
Assertions.assertNotEquals(typeList.size(), 0);
}
@Test
public void a2_addControlDevice() {
ControlDevice dev = ControlDevice.builder().deviceType(ControlDeviceTypeName.DEVICE_SOCKET_TCP).deviceAddr("127.0.0.2").build();
int i = controlDeviceMapper.addControlDevice(dev);
System.out.println("Add " + i);
}
@Test
public void a2_addNew() {
int i = controlDeviceMapper.addNew("127.0.0.3", 0L);
System.out.println("Add " + i);
}
}

View File

@ -1,15 +1,14 @@
package com.zjyr.beidouservice.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.zjyr.beidouservice.pojo.entry.ControlDeviceType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import org.junit.jupiter.api.Test;
import java.util.List;
@ -22,8 +21,9 @@ import java.util.List;
public class ControlDeviceTypeMapperTest {
@Resource
private ControlDeviceTypeMapper mapper;
@Test
public void a5_getTaskInfo() {
public void a1_getTaskInfo() {
List<ControlDeviceType> typeList = mapper.selectAll();
Assertions.assertNotEquals(typeList.size(), 0);
}