diff --git a/src/main/java/club/joylink/rtss/configuration/configProp/UDPConfig.java b/src/main/java/club/joylink/rtss/configuration/configProp/UDPConfig.java new file mode 100644 index 000000000..50e00d61a --- /dev/null +++ b/src/main/java/club/joylink/rtss/configuration/configProp/UDPConfig.java @@ -0,0 +1,16 @@ +package club.joylink.rtss.configuration.configProp; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix="udp") +@Getter +@Setter +public class UDPConfig { + private int serverPort; + + private int clientPort; +} diff --git a/src/main/java/club/joylink/rtss/constants/ProjectDeviceType.java b/src/main/java/club/joylink/rtss/constants/ProjectDeviceType.java index 9bdde8f32..088a5e4b3 100644 --- a/src/main/java/club/joylink/rtss/constants/ProjectDeviceType.java +++ b/src/main/java/club/joylink/rtss/constants/ProjectDeviceType.java @@ -12,6 +12,8 @@ import java.util.List; public enum ProjectDeviceType { /* -----------plc device start---------- */ + /** 区段 */ + SECTION, /** 道岔 */ SWITCH, /** 信号机 */ @@ -28,6 +30,8 @@ public enum ProjectDeviceType { PLC_GATEWAY, /** 单元控制器 */ DCU, + /** UDP下位机 */ + UDP_LOW, /* -----------plc device end---------- */ /* -----------client device start---------- */ @@ -68,8 +72,10 @@ public enum ProjectDeviceType { PSD, PSL, IBP, + SECTION, SWITCH, SIGNAL, - DCU); + DCU, + UDP_LOW); } } diff --git a/src/main/java/club/joylink/rtss/controller/project/DeviceController.java b/src/main/java/club/joylink/rtss/controller/project/DeviceController.java index be1d41a7a..f6411049b 100644 --- a/src/main/java/club/joylink/rtss/controller/project/DeviceController.java +++ b/src/main/java/club/joylink/rtss/controller/project/DeviceController.java @@ -236,4 +236,10 @@ public class DeviceController { AccountVO accountVO) { this.deviceService.addOrUpdateRichorDeviceConfig(accountVO); } + + @PostMapping("/sr/addOrUpdate") + public void addOrUpdateSrDeviceConfig(@RequestAttribute(name = AuthenticateInterceptor.LOGIN_USER_KEY) + AccountVO accountVO) { + this.deviceService.addOrUpdateSrDeviceConfig(accountVO); + } } diff --git a/src/main/java/club/joylink/rtss/services/project/DeviceService.java b/src/main/java/club/joylink/rtss/services/project/DeviceService.java index 28951a1cb..acfb06aea 100644 --- a/src/main/java/club/joylink/rtss/services/project/DeviceService.java +++ b/src/main/java/club/joylink/rtss/services/project/DeviceService.java @@ -181,4 +181,6 @@ public interface DeviceService { void addOrUpdateSdyDeviceConfig(AccountVO accountVO); void addOrUpdateRichorDeviceConfig(AccountVO accountVO); + + void addOrUpdateSrDeviceConfig(AccountVO accountVO); } diff --git a/src/main/java/club/joylink/rtss/services/project/DeviceServiceImpl.java b/src/main/java/club/joylink/rtss/services/project/DeviceServiceImpl.java index f02688b1c..349aac463 100644 --- a/src/main/java/club/joylink/rtss/services/project/DeviceServiceImpl.java +++ b/src/main/java/club/joylink/rtss/services/project/DeviceServiceImpl.java @@ -397,6 +397,34 @@ public class DeviceServiceImpl implements DeviceService { } } + @Override + public void addOrUpdateSrDeviceConfig(AccountVO accountVO) { + // 删除旧配置 + ProjectDeviceExample example = new ProjectDeviceExample(); + example.createCriteria() + .andProjectCodeEqualTo(Project.RICHOR_JOINT.name()) + .andTypeIn(ProjectDeviceType.PlcDeviceList().stream() + .map(Enum::name).collect(Collectors.toList())); + this.projectDeviceDAO.deleteByExample(example); +// // 保存新配置 +// List list = this.buildSrProjectDevices(accountVO); +// for (ProjectDevice projectDevice : list) { +// this.projectDeviceDAO.insert(projectDevice); +// } + } + +// private List buildSrProjectDevices(AccountVO accountVO) { +// LocalDateTime now = LocalDateTime.now(); +// List list = new ArrayList<>(); +// //UDP下位机 +// ProjectDevice udp = new ProjectDevice(); +// udp.setProjectCode(Project.SR_SANDBOX.name()); +// udp.setCode("sr-udp"); +// udp.setType(ProjectDeviceType.UDP_LOW.name()); +// udp.setCreator(accountVO.getId()); +// udp.setCreateTime(now); +// } + private List buildZjdProjectDevices(AccountVO accountVO) { LocalDateTime now = LocalDateTime.now(); List list = new ArrayList<>(); diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/udp/UDPClient.java b/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/udp/UDPClient.java new file mode 100644 index 000000000..eb1561806 --- /dev/null +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/udp/UDPClient.java @@ -0,0 +1,63 @@ +package club.joylink.rtss.simulation.cbtc.device.real.udp; + +import club.joylink.rtss.configuration.configProp.UDPConfig; +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.DatagramPacket; +import io.netty.channel.socket.nio.NioDatagramChannel; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; + +@Component +@Slf4j +public class UDPClient implements ApplicationRunner { + @Autowired + private UDPConfig udpConfig; + + @Override + public void run(ApplicationArguments args) throws Exception { + this.start(); + } + + public void start() throws InterruptedException { + Bootstrap bootstrap = new Bootstrap(); + NioEventLoopGroup group = new NioEventLoopGroup(); + bootstrap.group(group) + .channel(NioDatagramChannel.class) + .handler(new ChannelInitializer() { + @Override + protected void initChannel(NioDatagramChannel channel) { + } + }); + ChannelFuture future = bootstrap.bind(udpConfig.getClientPort()).sync(); + Channel channel = future.channel(); + byte[] bytes = new byte[]{(byte)127, (byte)0, (byte)0, (byte)1}; + try { + InetAddress inetAddress = InetAddress.getByAddress(bytes); + InetSocketAddress addr = new InetSocketAddress(inetAddress, 9999); + ByteBuf byteBuf = Unpooled.copiedBuffer("主动发送消息".getBytes()); + DatagramPacket data = new DatagramPacket(byteBuf, addr); + channel.writeAndFlush(data); + if(future.isSuccess()) { + log.info(String.format("udp client start on port [%s]", this.udpConfig.getClientPort())); + } else { + log.error("udp server start failed", future.cause()); + } + } catch (UnknownHostException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/udp/UDPServer.java b/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/udp/UDPServer.java new file mode 100644 index 000000000..9b58c0515 --- /dev/null +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/udp/UDPServer.java @@ -0,0 +1,51 @@ +package club.joylink.rtss.simulation.cbtc.device.real.udp; + +import club.joylink.rtss.configuration.configProp.UDPConfig; +import club.joylink.rtss.simulation.cbtc.device.real.udp.handler.UDPMessageHandler; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioDatagramChannel; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class UDPServer implements ApplicationRunner { + + @Autowired + private UDPConfig udpConfig; + + @Autowired + private UDPMessageHandler udpMessageHandler; + + @Override + public void run(ApplicationArguments args) throws Exception { + this.start(); + } + + public void start() throws InterruptedException { + Bootstrap bootstrap = new Bootstrap(); + NioEventLoopGroup group = new NioEventLoopGroup(); + bootstrap.group(group) + .channel(NioDatagramChannel.class) + .handler(new ChannelInitializer() { + @Override + protected void initChannel(NioDatagramChannel channel) { + ChannelPipeline pipeline = channel.pipeline(); + pipeline.addLast(udpMessageHandler); + } + }); + ChannelFuture future = bootstrap.bind(udpConfig.getServerPort()).sync(); + if(future.isSuccess()) { + log.info(String.format("udp server start on port [%s]", this.udpConfig.getServerPort())); + } else { + log.error("udp server start failed", future.cause()); + } + } +} diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/udp/handler/UDPMessageHandler.java b/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/udp/handler/UDPMessageHandler.java new file mode 100644 index 000000000..11050bde9 --- /dev/null +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/udp/handler/UDPMessageHandler.java @@ -0,0 +1,21 @@ +package club.joylink.rtss.simulation.cbtc.device.real.udp.handler; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.socket.DatagramPacket; +import org.springframework.stereotype.Component; + +import java.nio.charset.StandardCharsets; + +@Component +public class UDPMessageHandler extends SimpleChannelInboundHandler { + + @Override + protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket datagramPacket) { + System.out.println("收到消息:" + datagramPacket.content().toString(StandardCharsets.UTF_8)); + ByteBuf byteBuf = Unpooled.copiedBuffer("hi".getBytes()); + ctx.writeAndFlush(new DatagramPacket(byteBuf, datagramPacket.sender())); + } +} diff --git a/src/main/java/club/joylink/rtss/vo/client/project/UdpLowConfigVO.java b/src/main/java/club/joylink/rtss/vo/client/project/UdpLowConfigVO.java new file mode 100644 index 000000000..5b6dde9db --- /dev/null +++ b/src/main/java/club/joylink/rtss/vo/client/project/UdpLowConfigVO.java @@ -0,0 +1,5 @@ +package club.joylink.rtss.vo.client.project; + +public class UdpLowConfigVO { + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 34f82c1d8..d5f529687 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,6 +4,10 @@ server: modbus-tcp: port: 19000 +udp: + serverPort: 20000 + clientPort: 20001 + spring: profiles: active: dev