add:优化
This commit is contained in:
parent
577c8df126
commit
77039ce073
|
|
@ -4,9 +4,9 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description TODO
|
* 设备ID
|
||||||
* @Author LSF
|
*
|
||||||
* @Date 2024/8/14 17:24
|
* @author LSF maku_lsf@163.com
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@Schema(description = "设备ID")
|
@Schema(description = "设备ID")
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description TODO
|
* tcp通讯数据包
|
||||||
* @Author LSF
|
*
|
||||||
* @Date 2024/8/14 19:31
|
* @author LSF maku_lsf@163.com
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@Schema(description = "tcp通讯数据包装类")
|
@Schema(description = "tcp通讯数据包装类")
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,11 @@ import net.maku.iot.entity.IotDeviceEntity;
|
||||||
import net.maku.iot.enums.DeviceCommandEnum;
|
import net.maku.iot.enums.DeviceCommandEnum;
|
||||||
import net.maku.iot.communication.dto.DeviceCommandResponseDTO;
|
import net.maku.iot.communication.dto.DeviceCommandResponseDTO;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基础通信协议具备功能
|
* 通信协议具备功能
|
||||||
|
*
|
||||||
|
* @author LSF maku_lsf@163.com
|
||||||
*/
|
*/
|
||||||
public interface BaseCommunication {
|
public interface BaseCommunication {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@ import net.maku.framework.common.exception.ServerException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description TODO
|
* 设备协议服务工厂
|
||||||
* @Author LSF
|
*
|
||||||
* @Date 2024/8/9 14:53
|
* @author LSF maku_lsf@163.com
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,10 @@ import cn.hutool.json.JSONUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.maku.framework.common.exception.ServerException;
|
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.CommandResponseChan;
|
||||||
import net.maku.iot.communication.dto.DeviceCommandDTO;
|
import net.maku.iot.communication.dto.DeviceCommandDTO;
|
||||||
import net.maku.iot.communication.dto.DeviceCommandResponseDTO;
|
import net.maku.iot.communication.dto.DeviceCommandResponseDTO;
|
||||||
|
import net.maku.iot.communication.mqtt.MqttGateway;
|
||||||
import net.maku.iot.dto.DeviceClientDTO;
|
import net.maku.iot.dto.DeviceClientDTO;
|
||||||
import net.maku.iot.entity.IotDeviceEntity;
|
import net.maku.iot.entity.IotDeviceEntity;
|
||||||
import net.maku.iot.enums.DeviceCommandEnum;
|
import net.maku.iot.enums.DeviceCommandEnum;
|
||||||
|
|
@ -19,9 +19,9 @@ import org.springframework.stereotype.Component;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description TODO
|
* MQTT协议服务类
|
||||||
* @Author LSF
|
*
|
||||||
* @Date 2024/8/9 14:21
|
* @author LSF maku_lsf@163.com
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,9 @@ import org.springframework.stereotype.Component;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description TODO
|
* TCP协议服务类
|
||||||
* @Author LSF
|
*
|
||||||
* @Date 2024/8/9 14:21
|
* @author LSF maku_lsf@163.com
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
|
|
|
||||||
|
|
@ -29,15 +29,6 @@ public class TcpGateway {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取设备通道
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public ConcurrentMap<String, Channel> getTcpDeviceChannels() {
|
|
||||||
return deviceChannels;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 发送命令到设备
|
* 发送命令到设备
|
||||||
* @param deviceId 设备ID
|
* @param deviceId 设备ID
|
||||||
* @param commandTopic 命令主题
|
* @param commandTopic 命令主题
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,10 @@ package net.maku.iot.communication.tcp.config;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import io.netty.bootstrap.Bootstrap;
|
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.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
|
|
@ -26,9 +29,14 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备模拟器配置,用于启动模拟设备,方便调试,默认启动系统所有的TCP设备
|
||||||
|
*
|
||||||
|
* @author LSF maku_lsf@163.com
|
||||||
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Configuration
|
@Configuration
|
||||||
public class NettyClientConfig {
|
public class DeviceEmulatorConfig {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IotDeviceService deviceService;
|
private IotDeviceService deviceService;
|
||||||
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -10,9 +10,7 @@ import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
import io.netty.handler.codec.string.StringDecoder;
|
import io.netty.handler.codec.string.StringDecoder;
|
||||||
import io.netty.handler.codec.string.StringEncoder;
|
import io.netty.handler.codec.string.StringEncoder;
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.factory.TcpMessageHandlerFactory;
|
||||||
import net.maku.iot.communication.tcp.handler.ConnectionHandler;
|
import net.maku.iot.communication.tcp.handler.ConnectionHandler;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Netty服务配置
|
||||||
|
*
|
||||||
|
* @author LSF maku_lsf@163.com
|
||||||
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class NettyServerConfig {
|
public class NettyServerConfig {
|
||||||
|
|
|
||||||
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -15,9 +15,9 @@ import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description TCP服务器连接处理器
|
* TCP服务器连接处理器
|
||||||
* @Author LSF
|
*
|
||||||
* @Date 2024/8/14 16:52
|
* @author LSF maku_lsf@163.com
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class ConnectionHandler extends ChannelInboundHandlerAdapter {
|
public class ConnectionHandler extends ChannelInboundHandlerAdapter {
|
||||||
|
|
|
||||||
|
|
@ -13,9 +13,9 @@ import org.springframework.stereotype.Component;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description TODO
|
* 设备命令响应处理器
|
||||||
* @Author LSF
|
*
|
||||||
* @Date 2024/8/14 19:23
|
* @author LSF maku_lsf@163.com
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
|
|
@ -52,9 +52,6 @@ public class DeviceCommandResponseTCPMessageHandler implements TCPMessageHandler
|
||||||
log.error(StrUtil.format("调用设备命令响应响应处理器出错,topic:{}, message:{}", topic, message), e);
|
log.error(StrUtil.format("调用设备命令响应响应处理器出错,topic:{}, message:{}", topic, message), e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private DeviceCommandResponseDTO parseCommandReplyMessage(String topic, Object message) {
|
private DeviceCommandResponseDTO parseCommandReplyMessage(String topic, Object message) {
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,9 @@ import org.springframework.stereotype.Component;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description TODO
|
* 设备属性上报消息处理器
|
||||||
* @Author LSF
|
*
|
||||||
* @Date 2024/8/14 19:24
|
* @author LSF maku_lsf@163.com
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user