第一次提交

This commit is contained in:
wyw
2024-08-08 00:31:26 +08:00
commit c202e2b63d
1819 changed files with 221890 additions and 0 deletions

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>fastbee-server</artifactId>
<groupId>com.fastbee</groupId>
<version>3.8.5</version>
</parent>
<artifactId>boot-strap</artifactId>
<description>网关服务启动模块</description>
<dependencies>
<dependency>
<groupId>com.fastbee</groupId>
<artifactId>mqtt-broker</artifactId>
</dependency>
<dependency>
<groupId>com.fastbee</groupId>
<artifactId>http-server</artifactId>
</dependency>
<dependency>
<groupId>com.fastbee</groupId>
<artifactId>fastbee-protocol-collect</artifactId>
</dependency>
<dependency>
<groupId>com.fastbee</groupId>
<artifactId>base-server</artifactId>
<version>3.8.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fastbee</groupId>
<artifactId>coap-server</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,33 @@
package com.fastbee.bootstrap.coap;
import com.fastbee.coap.Coapserver;
import com.fastbee.common.enums.ServerType;
import com.fastbee.server.Server;
import com.fastbee.server.config.NettyConfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
@Order(13)
@Configuration
@ConditionalOnProperty(name = "server.coap.enabled", havingValue = "true")//设置是否启动
@ConfigurationProperties(value = "server.coap")
@Data
public class CoapBootStrap {
@Autowired
private Coapserver coapserver;
private int port;
@Bean(initMethod = "start",destroyMethod = "stop")
public Server coapServerBoot(){
return NettyConfig.custom()
.setPort(port)
.setName(ServerType.COAP.getDes())
.setType(ServerType.COAP)
.setServer(coapserver)
.build();
}
}

View File

@ -0,0 +1,34 @@
package com.fastbee.bootstrap.http;
import com.fastbee.common.enums.ServerType;
import com.fastbee.http.server.HttpServer;
import com.fastbee.server.Server;
import com.fastbee.server.config.NettyConfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
@Order(13)
@Configuration
@ConditionalOnProperty(name = "server.http.enabled", havingValue = "true")//设置是否启动
@ConfigurationProperties(value = "server.http")
@Data
public class HttpBootStrap {
@Autowired
private HttpServer httpServer;
private int port;
@Bean(initMethod = "start",destroyMethod = "stop")
public Server httpServerBoot(){
return NettyConfig.custom()
.setPort(port)
.setName(ServerType.HTTP.getDes())
.setType(ServerType.HTTP)
.setServer(httpServer)
.build();
}
}

View File

@ -0,0 +1,70 @@
package com.fastbee.bootstrap.mqtt;
import com.fastbee.mqtt.server.MqttServer;
import com.fastbee.mqtt.server.WebSocketServer;
import com.fastbee.server.Server;
import com.fastbee.server.config.NettyConfig;
import com.fastbee.common.enums.ServerType;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
/**
* MQTT-BROKER启动
* @author gsb
* @date 2022/9/17 17:25
*/
@Order(10)
@Configuration
@ConfigurationProperties(value = "server.broker")
@Data
public class MQTTBootStrap {
@Autowired
private MqttServer mqttServer;
@Autowired
private WebSocketServer webSocketServer;
/*服务器集群节点*/
private String brokerNode;
/*端口*/
private int port;
/*心跳时间*/
private int keepAlive;
/*webSocket端口*/
private int websocketPort;
/*webSocket路由*/
private String websocketPath;
/**
* 启动mqttBroker
* @return server
*/
@ConditionalOnProperty(value = "server.broker.enabled", havingValue = "true")
@Bean(initMethod = "start", destroyMethod = "stop")
public Server mqttBroker() {
return NettyConfig.custom()
.setIdleStateTime(0,0,keepAlive)
.setName(ServerType.MQTT.getDes())
.setType(ServerType.MQTT)
.setPort(port)
.setServer(mqttServer)
.build();
}
@ConditionalOnProperty(value = "server.broker.openws", havingValue = "true")
@Bean(initMethod = "start",destroyMethod = "stop")
public Server webSocket(){
return NettyConfig.custom()
.setIdleStateTime(0,0,keepAlive)
.setName(ServerType.WEBSOCKET.getDes())
.setType(ServerType.WEBSOCKET)
.setPort(websocketPort)
.setServer(webSocketServer)
.build();
}
}

View File

@ -0,0 +1,62 @@
package com.fastbee.bootstrap.tcp;
import com.fastbee.bootstrap.tcp.config.TcpHandlerInterceptor;
import com.fastbee.common.enums.ServerType;
import com.fastbee.modbus.codec.MessageAdapter;
import com.fastbee.server.Server;
import com.fastbee.base.codec.Delimiter;
import com.fastbee.server.config.NettyConfig;
import com.fastbee.base.core.HandlerMapping;
import com.fastbee.base.session.SessionManager;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
/**
* TCP服务端启动
* @author bill
*/
@Order(11)
@Configuration
@ConditionalOnProperty(value = "server.tcp.enabled", havingValue = "true")//设置是否启动
@ConfigurationProperties(value = "server.tcp")
@Data
public class TCPBootStrap {
private int port;
private int keepAlive;
private byte delimiter;
@Autowired
private MessageAdapter messageAdapter;
private HandlerMapping handlerMapping;
private TcpHandlerInterceptor handlerInterpolator;
private SessionManager sessionManager;
public TCPBootStrap(HandlerMapping handlerMapping, TcpHandlerInterceptor interpolator, SessionManager sessionManager){
this.handlerMapping = handlerMapping;
this.handlerInterpolator = interpolator;
this.sessionManager = sessionManager;
}
@Bean(initMethod = "start",destroyMethod = "stop")
public Server TCPServer(){
return NettyConfig.custom()
.setIdleStateTime(keepAlive,0,0)
.setPort(port)
//设置报文最大长度 modbus-rtu
.setMaxFrameLength(100)
.setDelimiters(new Delimiter(new byte[]{0x7e}, true))
.setDecoder(messageAdapter)
.setEncoder(messageAdapter)
.setHandlerMapping(handlerMapping)
.setHandlerInterceptor(handlerInterpolator)
.setSessionManager(sessionManager)
.setName(ServerType.TCP.getDes())
.setType(ServerType.TCP)
.build();
}
}

View File

@ -0,0 +1,43 @@
package com.fastbee.bootstrap.tcp.config;
import com.fastbee.protocol.WModelManager;
import com.fastbee.base.core.HandlerMapping;
import com.fastbee.base.core.SpringHandlerMapping;
import com.fastbee.base.session.SessionListener;
import com.fastbee.base.session.SessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author bill
*/
@Configuration
public class TcpBeanConfig {
@Bean
public HandlerMapping handlerMapping(){
return new SpringHandlerMapping();
}
@Bean
public TcpHandlerInterceptor handlerInterceptor(){
return new TcpHandlerInterceptor();
}
@Bean
public SessionListener sessionListener(){
return new TcpSessionListener();
}
@Bean
public SessionManager sessionManager(SessionListener sessionListener){
return new SessionManager(sessionListener);
}
@Bean
public WModelManager wModelManager(){
return new WModelManager("com.fastbee.modbus");
}
}

View File

@ -0,0 +1,52 @@
package com.fastbee.bootstrap.tcp.config;
import com.fastbee.common.core.protocol.Message;
import com.fastbee.modbus.pak.TcpDtu;
import com.fastbee.base.core.HandlerInterceptor;
import com.fastbee.base.session.Session;
import lombok.extern.slf4j.Slf4j;
/**
* 消息拦截应答
* @author bill
*/
@Slf4j
public class TcpHandlerInterceptor implements HandlerInterceptor<Message> {
@Override
public Message notSupported(Message request, Session session) {
return null;
}
@Override
public boolean beforeHandle(Message request, Session session) {
int messageId = Integer.parseInt(request.getMessageId());
if (messageId == TcpDtu.注册报文 || messageId == TcpDtu.心跳包 || messageId == TcpDtu.整包消息){
return true;
}
if (!session.isRegistered()){
log.warn("设备未注册,session={}",session);
return true;
}
return false;
}
@Override
public Message successful(Message request, Session session) {
return null;
}
/**
* 调用之后
*/
@Override
public void afterHandle(Message request, Message response, Session session) {
if (response != null){
//response.setSerialNo(session.nextSerialNO());
}
}
@Override
public Message exceptional(Message request, Session session, Exception e) {
return null;
}
}

View File

@ -0,0 +1,55 @@
package com.fastbee.bootstrap.tcp.config;
import com.fastbee.base.session.Session;
import com.fastbee.base.session.SessionListener;
import com.fastbee.common.core.mq.DeviceStatusBo;
import com.fastbee.common.enums.DeviceStatus;
import com.fastbee.mq.redischannel.consumer.DeviceStatusConsumer;
import com.fastbee.base.util.DeviceUtils;
import com.fastbee.mqtt.manager.MqttRemoteManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author bill
*/
@Slf4j
@Service
public class TcpSessionListener implements SessionListener {
@Resource
private MqttRemoteManager remoteManager;
@Resource
private DeviceStatusConsumer statusConsumer;
/** 客户端建立连接 */
@Override
public void sessionCreated(Session session) {
}
/** 客户端完成注册或鉴权 */
@Override
public void sessionRegistered(Session session) {
DeviceStatusBo statusBo = DeviceUtils.buildStatusMsg(session.getChannel(), session.getClientId(),
DeviceStatus.ONLINE, session.getIp());
statusConsumer.consume(statusBo);
remoteManager.pushDeviceStatus(-1L,statusBo.getSerialNumber(),statusBo.getStatus());
log.info("TCP客户端:[{}],注册上线",session.getClientId());
}
/** 客户端注销或离线 */
@Override
public void sessionDestroyed(Session session) {
/*推送离线消息到mq处理*/
DeviceStatusBo statusBo = DeviceUtils.buildStatusMsg(session.getChannel(),
session.getClientId(), DeviceStatus.OFFLINE, session.getIp());
statusConsumer.consume(statusBo);
remoteManager.pushDeviceStatus(-1L,statusBo.getSerialNumber(),statusBo.getStatus());
log.info("TCP客户端:[{}],离线",session.getClientId());
}
}

View File

@ -0,0 +1,58 @@
package com.fastbee.bootstrap.udp;
import com.fastbee.bootstrap.tcp.config.TcpHandlerInterceptor;
import com.fastbee.common.enums.ServerType;
import com.fastbee.modbus.codec.MessageAdapter;
import com.fastbee.server.Server;
import com.fastbee.server.config.NettyConfig;
import com.fastbee.base.core.HandlerMapping;
import com.fastbee.base.session.SessionManager;
import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
/**
* UDP服务端启动
* @author bill
*/
@Order(12)
@Configuration
@ConditionalOnProperty(value = "server.udp.enabled", havingValue = "true")//设置是否启动
@ConfigurationProperties(value = "server.udp")
@Data
public class UDPBootStrap {
private int port;
private byte delimiter;
private MessageAdapter messageAdapter;
private HandlerMapping handlerMapping;
private TcpHandlerInterceptor handlerInterpolator;
private SessionManager sessionManager;
public UDPBootStrap(MessageAdapter messageAdapter, HandlerMapping handlerMapping, TcpHandlerInterceptor interpolator, SessionManager sessionManager){
this.messageAdapter = messageAdapter;
this.handlerMapping = handlerMapping;
this.handlerInterpolator = interpolator;
this.sessionManager = sessionManager;
}
@Bean(initMethod = "start",destroyMethod = "stop")
public Server UDPServer(){
return NettyConfig.custom()
.setPort(port)
//.setDelimiters(new Delimiter(new byte[]{0x0D},false)) //分隔符配置
.setDecoder(messageAdapter)
.setEncoder(messageAdapter)
.setHandlerMapping(handlerMapping)
.setHandlerInterceptor(handlerInterpolator)
.setSessionManager(sessionManager)
.setName(ServerType.UDP.getDes())
.setType(ServerType.UDP)
.build();
}
}