上饶沙盘(未完)
This commit is contained in:
parent
554246fa7d
commit
e4891a7b8f
@ -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;
|
||||||
|
}
|
@ -12,6 +12,8 @@ import java.util.List;
|
|||||||
public enum ProjectDeviceType {
|
public enum ProjectDeviceType {
|
||||||
|
|
||||||
/* -----------plc device start---------- */
|
/* -----------plc device start---------- */
|
||||||
|
/** 区段 */
|
||||||
|
SECTION,
|
||||||
/** 道岔 */
|
/** 道岔 */
|
||||||
SWITCH,
|
SWITCH,
|
||||||
/** 信号机 */
|
/** 信号机 */
|
||||||
@ -28,6 +30,8 @@ public enum ProjectDeviceType {
|
|||||||
PLC_GATEWAY,
|
PLC_GATEWAY,
|
||||||
/** 单元控制器 */
|
/** 单元控制器 */
|
||||||
DCU,
|
DCU,
|
||||||
|
/** UDP下位机 */
|
||||||
|
UDP_LOW,
|
||||||
/* -----------plc device end---------- */
|
/* -----------plc device end---------- */
|
||||||
|
|
||||||
/* -----------client device start---------- */
|
/* -----------client device start---------- */
|
||||||
@ -68,8 +72,10 @@ public enum ProjectDeviceType {
|
|||||||
PSD,
|
PSD,
|
||||||
PSL,
|
PSL,
|
||||||
IBP,
|
IBP,
|
||||||
|
SECTION,
|
||||||
SWITCH,
|
SWITCH,
|
||||||
SIGNAL,
|
SIGNAL,
|
||||||
DCU);
|
DCU,
|
||||||
|
UDP_LOW);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -236,4 +236,10 @@ public class DeviceController {
|
|||||||
AccountVO accountVO) {
|
AccountVO accountVO) {
|
||||||
this.deviceService.addOrUpdateRichorDeviceConfig(accountVO);
|
this.deviceService.addOrUpdateRichorDeviceConfig(accountVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/sr/addOrUpdate")
|
||||||
|
public void addOrUpdateSrDeviceConfig(@RequestAttribute(name = AuthenticateInterceptor.LOGIN_USER_KEY)
|
||||||
|
AccountVO accountVO) {
|
||||||
|
this.deviceService.addOrUpdateSrDeviceConfig(accountVO);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -181,4 +181,6 @@ public interface DeviceService {
|
|||||||
void addOrUpdateSdyDeviceConfig(AccountVO accountVO);
|
void addOrUpdateSdyDeviceConfig(AccountVO accountVO);
|
||||||
|
|
||||||
void addOrUpdateRichorDeviceConfig(AccountVO accountVO);
|
void addOrUpdateRichorDeviceConfig(AccountVO accountVO);
|
||||||
|
|
||||||
|
void addOrUpdateSrDeviceConfig(AccountVO accountVO);
|
||||||
}
|
}
|
||||||
|
@ -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<ProjectDevice> list = this.buildSrProjectDevices(accountVO);
|
||||||
|
// for (ProjectDevice projectDevice : list) {
|
||||||
|
// this.projectDeviceDAO.insert(projectDevice);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
// private List<ProjectDevice> buildSrProjectDevices(AccountVO accountVO) {
|
||||||
|
// LocalDateTime now = LocalDateTime.now();
|
||||||
|
// List<ProjectDevice> 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<ProjectDevice> buildZjdProjectDevices(AccountVO accountVO) {
|
private List<ProjectDevice> buildZjdProjectDevices(AccountVO accountVO) {
|
||||||
LocalDateTime now = LocalDateTime.now();
|
LocalDateTime now = LocalDateTime.now();
|
||||||
List<ProjectDevice> list = new ArrayList<>();
|
List<ProjectDevice> list = new ArrayList<>();
|
||||||
|
@ -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<NioDatagramChannel>() {
|
||||||
|
@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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<NioDatagramChannel>() {
|
||||||
|
@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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<DatagramPacket> {
|
||||||
|
|
||||||
|
@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()));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package club.joylink.rtss.vo.client.project;
|
||||||
|
|
||||||
|
public class UdpLowConfigVO {
|
||||||
|
|
||||||
|
}
|
@ -4,6 +4,10 @@ server:
|
|||||||
modbus-tcp:
|
modbus-tcp:
|
||||||
port: 19000
|
port: 19000
|
||||||
|
|
||||||
|
udp:
|
||||||
|
serverPort: 20000
|
||||||
|
clientPort: 20001
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
profiles:
|
profiles:
|
||||||
active: dev
|
active: dev
|
||||||
|
Loading…
Reference in New Issue
Block a user