add:优化

This commit is contained in:
LSF 2024-08-14 20:33:51 +08:00
parent 88f88807e2
commit 3a786fa2eb
4 changed files with 20 additions and 7 deletions

View File

@ -15,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
import net.maku.iot.communication.mqtt.factory.MqttMessageHandlerFactory;
import net.maku.iot.communication.tcp.factory.TcpMessageHandlerFactory;
import net.maku.iot.communication.tcp.handler.ConnectionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -31,6 +32,9 @@ public class NettyServerConfig {
return new ConcurrentHashMap<>();
}
@Autowired
public TcpMessageHandlerFactory tcpMessageHandlerFactory;
@Bean
public ServerBootstrap nettyServer(ConcurrentMap<String, Channel> deviceChannels) {
ServerBootstrap bootstrap = new ServerBootstrap();
@ -43,7 +47,7 @@ public class NettyServerConfig {
new StringDecoder(),
new StringEncoder(),
// new DeviceMsgHandler(deviceChannels), // 添加设备身份处理器
new ConnectionHandler(deviceChannels) // 添加设备连接处理器
new ConnectionHandler(deviceChannels,tcpMessageHandlerFactory) // 添加设备连接处理器
);
}
})

View File

@ -24,13 +24,12 @@ import java.util.concurrent.ConcurrentMap;
public class ConnectionHandler extends ChannelInboundHandlerAdapter {
@Resource
private TcpMessageHandlerFactory tcpMessageHandlerFactory;
private final ConcurrentMap<String, Channel> deviceChannels;
private final TcpMessageHandlerFactory tcpMessageHandlerFactory;
public ConnectionHandler(ConcurrentMap<String, Channel> deviceChannels) {
public ConnectionHandler(ConcurrentMap<String, Channel> deviceChannels,TcpMessageHandlerFactory tcpMessageHandlerFactory) {
this.deviceChannels = deviceChannels;
this.tcpMessageHandlerFactory = tcpMessageHandlerFactory;
}

View File

@ -1,14 +1,18 @@
package net.maku.iot.communication.tcp.handler;
import lombok.extern.slf4j.Slf4j;
import net.maku.iot.communication.mqtt.handler.MqttMessageHandler;
import net.maku.iot.enums.DeviceTopicEnum;
import org.springframework.stereotype.Component;
/**
* @Description TODO
* @Author LSF
* @Date 2024/8/14 19:23
*/
public class DeviceCommandResponseTCPMessageHandler implements MqttMessageHandler {
@Slf4j
@Component
public class DeviceCommandResponseTCPMessageHandler implements TCPMessageHandler {
@Override
public boolean supports(String topic) {
return DeviceTopicEnum.startsWith(topic, DeviceTopicEnum.COMMAND_RESPONSE.getTopic());
@ -17,5 +21,6 @@ public class DeviceCommandResponseTCPMessageHandler implements MqttMessageHandle
@Override
public void handle(String topic, String message) {
//TCP设备响应处理
System.out.printf("TCP设备响应处理");
}
}

View File

@ -1,14 +1,18 @@
package net.maku.iot.communication.tcp.handler;
import lombok.extern.slf4j.Slf4j;
import net.maku.iot.communication.mqtt.handler.MqttMessageHandler;
import net.maku.iot.enums.DeviceTopicEnum;
import org.springframework.stereotype.Component;
/**
* @Description TODO
* @Author LSF
* @Date 2024/8/14 19:24
*/
public class DevicePropertyTCPMessageHandler implements MqttMessageHandler {
@Slf4j
@Component
public class DevicePropertyTCPMessageHandler implements TCPMessageHandler {
@Override
public boolean supports(String topic) {
return DeviceTopicEnum.startsWith(topic, DeviceTopicEnum.PROPERTY.getTopic());
@ -17,5 +21,6 @@ public class DevicePropertyTCPMessageHandler implements MqttMessageHandler {
@Override
public void handle(String topic, String message) {
//TCP设备属性上报处理
System.out.printf("TCP设备属性上报处理");
}
}