add:优化

This commit is contained in:
LSF 2024-08-20 10:46:57 +08:00
parent 577c8df126
commit 77039ce073
16 changed files with 172 additions and 208 deletions

View File

@ -4,9 +4,9 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* @Description TODO
* @Author LSF
* @Date 2024/8/14 17:24
* 设备ID
*
* @author LSF maku_lsf@163.com
*/
@Data
@Schema(description = "设备ID")

View File

@ -4,9 +4,9 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* @Description TODO
* @Author LSF
* @Date 2024/8/14 19:31
* tcp通讯数据包
*
* @author LSF maku_lsf@163.com
*/
@Data
@Schema(description = "tcp通讯数据包装类")

View File

@ -4,8 +4,11 @@ import net.maku.iot.entity.IotDeviceEntity;
import net.maku.iot.enums.DeviceCommandEnum;
import net.maku.iot.communication.dto.DeviceCommandResponseDTO;
/**
* 基础通信协议具备功能
* 通信协议具备功能
*
* @author LSF maku_lsf@163.com
*/
public interface BaseCommunication {

View File

@ -5,9 +5,9 @@ import net.maku.framework.common.exception.ServerException;
import org.springframework.stereotype.Service;
/**
* @Description TODO
* @Author LSF
* @Date 2024/8/9 14:53
* 设备协议服务工厂
*
* @author LSF maku_lsf@163.com
*/
@Service
@AllArgsConstructor

View File

@ -5,10 +5,10 @@ import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.maku.framework.common.exception.ServerException;
import net.maku.iot.communication.mqtt.MqttGateway;
import net.maku.iot.communication.dto.CommandResponseChan;
import net.maku.iot.communication.dto.DeviceCommandDTO;
import net.maku.iot.communication.dto.DeviceCommandResponseDTO;
import net.maku.iot.communication.mqtt.MqttGateway;
import net.maku.iot.dto.DeviceClientDTO;
import net.maku.iot.entity.IotDeviceEntity;
import net.maku.iot.enums.DeviceCommandEnum;
@ -19,9 +19,9 @@ import org.springframework.stereotype.Component;
import java.util.UUID;
/**
* @Description TODO
* @Author LSF
* @Date 2024/8/9 14:21
* MQTT协议服务类
*
* @author LSF maku_lsf@163.com
*/
@Slf4j
@Component

View File

@ -20,9 +20,9 @@ import org.springframework.stereotype.Component;
import java.util.UUID;
/**
* @Description TODO
* @Author LSF
* @Date 2024/8/9 14:21
* TCP协议服务类
*
* @author LSF maku_lsf@163.com
*/
@Slf4j
@Component

View File

@ -29,15 +29,6 @@ public class TcpGateway {
}
/**
* 获取设备通道
*
* @return
*/
public ConcurrentMap<String, Channel> getTcpDeviceChannels() {
return deviceChannels;
}
/**
* 发送命令到设备
* @param deviceId 设备ID
* @param commandTopic 命令主题

View File

@ -0,0 +1,41 @@
package net.maku.iot.communication.tcp.config;
import io.netty.bootstrap.Bootstrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
/**
* Netty启动顺序配置
*
* @author LSF maku_lsf@163.com
*/
@Configuration
@Slf4j
public class DeviceClientStartupConfig implements ApplicationListener<ContextRefreshedEvent> {
private final ObjectProvider<Bootstrap> deviceClientProvider;
private final DeviceEmulatorConfig deviceEmulatorConfig;
public DeviceClientStartupConfig(ObjectProvider<Bootstrap> deviceClientProvider, DeviceEmulatorConfig deviceEmulatorConfig) {
this.deviceClientProvider = deviceClientProvider;
this.deviceEmulatorConfig = deviceEmulatorConfig;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
try {
Thread.sleep(5000); // 延迟5秒以确保服务器启动
Bootstrap deviceClientBootstrap = deviceClientProvider.getIfAvailable();
if (deviceClientBootstrap != null) {
deviceEmulatorConfig.configureBootstrap(deviceClientBootstrap);
deviceClientBootstrap.connect("127.0.0.1", 8888).sync();
log.info("Connected to Netty server on port 8888");
}
} catch (InterruptedException e) {
log.error("Failed to connect to Netty server", e);
}
}
}

View File

@ -2,7 +2,10 @@ package net.maku.iot.communication.tcp.config;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
@ -26,9 +29,14 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 设备模拟器配置用于启动模拟设备方便调试默认启动系统所有的TCP设备
*
* @author LSF maku_lsf@163.com
*/
@Slf4j
@Configuration
public class NettyClientConfig {
public class DeviceEmulatorConfig {
@Autowired
private IotDeviceService deviceService;

View File

@ -1,36 +0,0 @@
package net.maku.iot.communication.tcp.config;
import io.netty.bootstrap.Bootstrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
@Configuration
@Slf4j
public class NettyClientStartupConfig implements ApplicationListener<ContextRefreshedEvent> {
private final ObjectProvider<Bootstrap> nettyClientProvider;
private final NettyClientConfig nettyClientConfig;
public NettyClientStartupConfig(ObjectProvider<Bootstrap> nettyClientProvider, NettyClientConfig nettyClientConfig) {
this.nettyClientProvider = nettyClientProvider;
this.nettyClientConfig = nettyClientConfig;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
try {
Thread.sleep(5000); // 延迟5秒以确保服务器启动
Bootstrap nettyClient = nettyClientProvider.getIfAvailable();
if (nettyClient != null) {
nettyClientConfig.configureBootstrap(nettyClient);
nettyClient.connect("127.0.0.1", 8888).sync();
log.info("Connected to Netty server on port 8888");
}
} catch (InterruptedException e) {
log.error("Failed to connect to Netty server", e);
}
}
}

View File

@ -10,9 +10,7 @@ import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import jakarta.annotation.Resource;
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;
@ -22,7 +20,11 @@ import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Netty服务配置
*
* @author LSF maku_lsf@163.com
*/
@Configuration
@Slf4j
public class NettyServerConfig {

View File

@ -1,42 +0,0 @@
package net.maku.iot.communication.tcp.config;
import cn.hutool.json.JSONUtil;
import net.maku.iot.communication.dto.DevicePropertyDTO;
import net.maku.iot.communication.dto.TcpMsgDTO;
import net.maku.iot.enums.DevicePropertyEnum;
import net.maku.iot.enums.DeviceTopicEnum;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) {
String serverAddress = ""; // 服务端的地址
int port = 8888; // 服务端的端口号
try (Socket socket = new Socket(serverAddress, port);
OutputStream outputStream = socket.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream, true)) {
DevicePropertyDTO dto = new DevicePropertyDTO();
dto.setDeviceId("123456");
dto.setPropertyType(DevicePropertyEnum.TEMPERATURE);
dto.setPayload("60");
TcpMsgDTO tcpMsgDTO = new TcpMsgDTO();
tcpMsgDTO.setMsg(dto);
tcpMsgDTO.setTopic(DeviceTopicEnum.PROPERTY.getTopic());
writer.println(JSONUtil.toJsonStr(tcpMsgDTO)); // 发送消息到服务端
System.out.println("Message sent: " + JSONUtil.toJsonStr(tcpMsgDTO));
Thread.sleep(100000);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -15,9 +15,9 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Description TCP服务器连接处理器
* @Author LSF
* @Date 2024/8/14 16:52
* TCP服务器连接处理器
*
* @author LSF maku_lsf@163.com
*/
@Slf4j
public class ConnectionHandler extends ChannelInboundHandlerAdapter {

View File

@ -13,9 +13,9 @@ import org.springframework.stereotype.Component;
import java.util.Optional;
/**
* @Description TODO
* @Author LSF
* @Date 2024/8/14 19:23
* 设备命令响应处理器
*
* @author LSF maku_lsf@163.com
*/
@Slf4j
@Component
@ -52,9 +52,6 @@ public class DeviceCommandResponseTCPMessageHandler implements TCPMessageHandler
log.error(StrUtil.format("调用设备命令响应响应处理器出错topic:{}, message:{}", topic, message), e);
}
});
}
private DeviceCommandResponseDTO parseCommandReplyMessage(String topic, Object message) {

View File

@ -12,9 +12,9 @@ import org.springframework.stereotype.Component;
import java.util.Optional;
/**
* @Description TODO
* @Author LSF
* @Date 2024/8/14 19:24
* 设备属性上报消息处理器
*
* @author LSF maku_lsf@163.com
*/
@Slf4j
@Component