weiyigulu 4 éve
szülő
commit
66b380cdac

+ 50 - 0
protocol-cdt/src/test/java/DataContainer.java

@@ -0,0 +1,50 @@
+import com.alibaba.fastjson.JSON;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * 数据容器
+ *
+ * @author 修唯xiuwei
+ **/
+public class DataContainer {
+
+	private static class LazyHolder {
+		private static final DataContainer INSTANCE = new DataContainer();
+	}
+
+	private DataContainer() {
+	}
+
+	public static final DataContainer getInstance() {
+		return DataContainer.LazyHolder.INSTANCE;
+	}
+
+	private volatile Map<Integer, Float> ycData = new ConcurrentHashMap<>();
+
+	private volatile Map<Integer, Boolean> yxData = new ConcurrentHashMap<>();
+
+	public void putYc(int address, float f) {
+		this.ycData.put(address, f);
+	}
+
+	public void putYx(int address, boolean b) {
+		this.yxData.put(address, b);
+	}
+
+	public Map<Integer, Float> getYc() {
+		return this.ycData;
+	}
+
+	public Map<Integer, Boolean> getYx() {
+		return this.yxData;
+	}
+
+	@Override
+	public String toString() {
+		return (JSON.toJSONString(this.yxData) + JSON.toJSONString(this.ycData));
+	}
+
+
+}

+ 45 - 0
protocol-cdt/src/test/java/LocalCDTDataHandler.java

@@ -0,0 +1,45 @@
+import wei.yigulu.cdt.cdtframe.AbstractCDTDataHandler;
+import wei.yigulu.cdt.cdtframe.BaseDateType;
+
+import java.util.List;
+
+/**
+ * cdt数据处理类的基础类
+ *
+ * @author: xiuwei
+ * @version:
+ */
+public class LocalCDTDataHandler extends AbstractCDTDataHandler {
+	DataContainer dataContainer = DataContainer.getInstance();
+
+
+	@Override
+	protected void processImportantYc(List<BaseDateType> dates) {
+		saveYc(dates);
+	}
+
+	@Override
+	protected void processSecondYc(List<BaseDateType> dates) {
+		saveYc(dates);
+	}
+
+	@Override
+	protected void processCommonYc(List<BaseDateType> dates) {
+		saveYc(dates);
+	}
+
+	@Override
+	protected void processYx(List<BaseDateType> dates) {
+		for (BaseDateType dateType : dates) {
+			dateType.getDates().forEach((k, v) -> dataContainer.putYx((Integer) k, (Boolean) v));
+		}
+	}
+
+
+	private void saveYc(List<BaseDateType> dates) {
+		for (BaseDateType dateType : dates) {
+			dateType.getDates().forEach((k, v) -> dataContainer.putYc((Integer) k, (Integer) v));
+		}
+
+	}
+}

+ 60 - 0
protocol-cdt/src/test/java/LocalCDTDataTransmitter.java

@@ -0,0 +1,60 @@
+import wei.yigulu.cdt.cdtframe.*;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * cdt数据处理类的基础类
+ *
+ * @author: xiuwei
+ * @version:
+ */
+public class LocalCDTDataTransmitter extends AbstractCDTDataTransmitter {
+
+
+	@Override
+	public List<CDTFrameBean> transmitImportantYc() {
+		return null;
+	}
+
+	@Override
+	public List<CDTFrameBean> transmitSecondYc() {
+		List<CDTFrameBean> list = new ArrayList<>();
+		Map<Integer, Integer> map = new HashMap<>();
+		map.put(0, -1101);
+		map.put(1, 22);
+		List<BaseDateType> list1 = new ArrayList<>();
+		IntegerDataType integerDataType = new IntegerDataType(map, null);
+		list1.add(integerDataType);
+		CDTFrameBean cdtFrameBean = new CDTFrameBean(list1);
+		list.add(cdtFrameBean);
+		return list;
+	}
+
+	@Override
+	public List<CDTFrameBean> transmitCommonYc() {
+		return null;
+	}
+
+	@Override
+	public List<CDTFrameBean> transmitYx() {
+		List<CDTFrameBean> list = new ArrayList<>();
+		Map<Integer, Boolean> map = new HashMap<>();
+		map.put(0, false);
+		map.put(1, true);
+		map.put(2, true);
+		map.put(3, true);
+		map.put(4, false);
+		map.put(5, false);
+		map.put(6, false);
+		map.put(7, true);
+		List<BaseDateType> list1 = new ArrayList<>();
+		BooleanDataType dataType = new BooleanDataType(map);
+		list1.add(dataType);
+		CDTFrameBean cdtFrameBean = new CDTFrameBean(list1);
+		list.add(cdtFrameBean);
+		return list;
+	}
+}

+ 16 - 0
protocol-cdt/src/test/java/TestBr.java

@@ -0,0 +1,16 @@
+/**
+ * @author: xiuwei
+ * @version:
+ */
+public class TestBr {
+
+
+	public static void main(String[] args) {
+
+
+		//byte b= (byte) ~2038+1;
+		//int i=~(byte)((-10 & 0xff) | ((15 & 0x07) << 8));
+		System.out.println(0x80);
+		//System.out.println(b>>6&0x01);
+	}
+}

+ 18 - 0
protocol-cdt/src/test/java/TestMaster.java

@@ -0,0 +1,18 @@
+import wei.yigulu.cdt.netty.CDTMaster;
+
+/**
+ * @author 修唯xiuwei
+ * @create 2019-06-26 13:44
+ * @Email 524710549@qq.com
+ **/
+public class TestMaster {
+	public static void main(String[] args) throws InterruptedException {
+		new Thread(() -> {
+				new CDTMaster("COM1", new LocalCDTDataHandler()).create();
+		}).start();
+       /*for (;;){
+           System.out.println(DataContainer.getInstance().toString());
+           Thread.sleep(3000);
+       }*/
+	}
+}

+ 21 - 0
protocol-cdt/src/test/java/TestSlaver.java

@@ -0,0 +1,21 @@
+import wei.yigulu.cdt.netty.CDTSlaver;
+
+
+/**
+ * 测试slaver
+ *
+ * @author: xiuwei
+ * @version:
+ */
+public class TestSlaver {
+
+	public static void main(String[] args) throws InterruptedException {
+		new Thread(() -> {
+			new CDTSlaver("COM2", new LocalCDTDataTransmitter()).create();
+		}).start();
+       /*for (;;){
+           System.out.println(DataContainer.getInstance().toString());
+           Thread.sleep(3000);
+       }*/
+	}
+}

+ 18 - 0
protocol-iec104/src/test/java/ClientTest.java

@@ -0,0 +1,18 @@
+import wei.yigulu.iec104.nettyconfig.Iec104HSMasterBuilder;
+
+
+/**
+ * 客户端测试
+ *
+ * @author 修唯xiuwei
+ * @create 2019-01-22 16:05
+ * @Email 524710549@qq.com
+ **/
+public class ClientTest {
+
+	public static void main(String[] args) {
+		new Iec104HSMasterBuilder("127.0.0.1", 2404).setSpareIp("127.0.0.2").create();
+		System.out.println(123);
+	}
+
+}

+ 32 - 0
protocol-iec104/src/test/java/MasterTest.java

@@ -0,0 +1,32 @@
+import wei.yigulu.iec104.apdumodel.Apdu;
+import wei.yigulu.iec104.apdumodel.Asdu;
+import wei.yigulu.iec104.asdudataframe.TotalSummonType;
+import wei.yigulu.iec104.nettyconfig.Iec104HSMasterBuilder;
+
+/**
+ * dad
+ *
+ * @author 修唯xiuwei
+ * @create 2019-03-14 16:46
+ * @Email 524710549@qq.com
+ **/
+public class MasterTest {
+
+	public static void main(String[] args) throws Exception {
+
+		Iec104HSMasterBuilder masterBuilder = new Iec104HSMasterBuilder("127.0.0.1", 2404);
+		masterBuilder.createByUnBlock();
+
+		//创建总召唤类型I帧
+		TotalSummonType totalSummonType = new TotalSummonType();
+		//反向生成asdu
+		Asdu asdu = totalSummonType.generateBack();
+		//配置总召唤发送原因
+		asdu.setNot(6);
+		//配置公共地址位
+		asdu.setCommonAddress(1);
+		Apdu apdu = new Apdu().setAsdu(asdu);
+		masterBuilder.sendFrameToOpposite(apdu.encode());
+
+	}
+}

+ 95 - 0
protocol-iec104/src/test/java/NettyClient.java

@@ -0,0 +1,95 @@
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.*;
+import io.netty.channel.nio.NioEventLoopGroup;
+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;
+
+/**
+ * 测试客户端
+ *
+ * @author 修唯xiuwei
+ * @create 2018-02-05 15:56
+ * @Email 524710549@qq.com
+ **/
+public class NettyClient {
+	public static void main(String[] args) {
+
+		EventLoopGroup group = new NioEventLoopGroup();
+		try {
+			ServerBootstrap sb = new ServerBootstrap();
+			// 绑定线程池
+			sb.group(group)
+					// 指定使用的channel
+					.channel(NioServerSocketChannel.class)
+					// 绑定监听端口
+					.localAddress(9001)
+					// 绑定客户端连接时候触发操作
+					.childHandler(new ChannelInitializer<SocketChannel>() {
+						@Override
+						protected void initChannel(SocketChannel ch) throws Exception {
+							ch.pipeline().addLast(new StringDecoder());
+							//字符串编码器
+							ch.pipeline().addLast(new StringEncoder());
+							//处理类
+							ch.pipeline().addLast(new ClientHandler4());
+						}
+					});
+			// 服务器异步创建绑定
+			ChannelFuture cf = sb.bind().sync();
+			// 关闭服务器通道
+			cf.channel().closeFuture().sync();
+
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				// 释放线程池资源
+				group.shutdownGracefully().sync();
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+			}
+		}
+
+		//worker负责读写数据
+
+	}
+
+}
+
+class ClientHandler4 extends SimpleChannelInboundHandler<String> {
+
+	//接受服务端发来的消息
+	@Override
+	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
+		System.out.println("server response : " + msg);
+	}
+
+	//与服务器建立连接
+	@Override
+	public void channelActive(ChannelHandlerContext ctx) throws Exception {
+		//给服务器发消息
+		ctx.channel().writeAndFlush("i am client !\n");
+
+		System.out.println("channelActive");
+	}
+
+	//与服务器断开连接
+	@Override
+	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
+		System.out.println("channelInactive");
+	}
+
+	//异常
+	@Override
+	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+		//关闭管道
+		ctx.channel().close();
+		//打印异常信息
+		cause.printStackTrace();
+	}
+}
+
+
+

+ 101 - 0
protocol-iec104/src/test/java/Server4.java

@@ -0,0 +1,101 @@
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.*;
+import io.netty.channel.nio.NioEventLoopGroup;
+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;
+
+/**
+ * 测试客户端
+ *
+ * @author 修唯xiuwei
+ * @create 2018-02-05 15:56
+ * @Email 524710549@qq.com
+ **/
+
+public class Server4 {
+	public static void main(String[] args) {
+		EventLoopGroup boss = new NioEventLoopGroup();
+		EventLoopGroup worker = new NioEventLoopGroup();
+
+		try {
+			//辅助启动类
+			ServerBootstrap bootstrap = new ServerBootstrap();
+			//设置线程池
+			bootstrap.group(boss, worker);
+
+			//设置socket工厂
+			bootstrap.channel(NioServerSocketChannel.class);
+
+			//设置管道工厂
+			bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
+				@Override
+				protected void initChannel(SocketChannel socketChannel) throws Exception {
+					//获取管道
+					ChannelPipeline pipeline = socketChannel.pipeline();
+					//字符串解码器
+					pipeline.addLast(new StringDecoder());
+					//字符串编码器
+					pipeline.addLast(new StringEncoder());
+					//处理类
+					pipeline.addLast(new ServerHandler4());
+				}
+			});
+
+			//设置TCP参数
+			//1.链接缓冲池的大小(ServerSocketChannel的设置)
+			bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
+			//维持链接的活跃,清除死链接(SocketChannel的设置)
+			bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
+			//关闭延迟发送
+			bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
+
+			//绑定端口
+			ChannelFuture future = bootstrap.bind(9001).sync();
+			System.out.println("server start ...... ");
+
+			//等待服务端监听端口关闭
+			future.channel().closeFuture().sync();
+
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		} finally {
+			//优雅退出,释放线程池资源
+			boss.shutdownGracefully();
+			worker.shutdownGracefully();
+		}
+	}
+
+}
+
+
+class ServerHandler4 extends SimpleChannelInboundHandler<String> {
+
+	//读取客户端发送的数据
+	@Override
+	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
+		System.out.println("client response :" + msg);
+	}
+
+	//新客户端接入
+	@Override
+	public void channelActive(ChannelHandlerContext ctx) throws Exception {
+		System.out.println("channelActive");
+	}
+
+	//客户端断开
+	@Override
+	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
+		System.out.println("channelInactive");
+	}
+
+	//异常
+	@Override
+	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+		//关闭通道
+		ctx.channel().close();
+		//打印异常
+		cause.printStackTrace();
+	}
+}

+ 28 - 0
protocol-iec104/src/test/java/SlaveTest.java

@@ -0,0 +1,28 @@
+import wei.yigulu.iec104.nettyconfig.Iec104SlaverBuilder;
+
+/**
+ * @author: xiuwei
+ * @version:
+ */
+public class SlaveTest {
+
+	public static void main(String[] args) throws Exception {
+		Iec104SlaverBuilder slaverBuilder = new Iec104SlaverBuilder(2404);
+
+		/*slaverBuilder.getConnectFilterManager().appendFilter((c)->{
+			if(slaverBuilder.getChannels().size()>=1){
+				return -1;
+			}
+			return 1;
+			*//*InetSocketAddress ipSocket = (InetSocketAddress) c.remoteAddress();
+			String clientIp = ipSocket.getAddress().getHostAddress();
+			Integer clientPort = ipSocket.getPort();
+			if(clientPort>30000){
+				return -1;
+			}else {return 1;}*//*
+		});*/
+		slaverBuilder.create();
+	}
+
+
+}

+ 9 - 0
protocol-modbus/src/test/java/Test.java

@@ -0,0 +1,9 @@
+/**
+ * @author: xiuwei
+ * @version:
+ */
+public class Test {
+	public static void main(String[] args) {
+		System.out.println();
+	}
+}

+ 45 - 0
protocol-modbus/src/test/java/TestMaster.java

@@ -0,0 +1,45 @@
+import lombok.extern.slf4j.Slf4j;
+import wei.yigulu.modbus.domain.datatype.IModbusDataType;
+import wei.yigulu.modbus.domain.datatype.ModbusDataTypeEnum;
+import wei.yigulu.modbus.domain.datatype.NumericModbusData;
+import wei.yigulu.modbus.exceptiom.ModbusException;
+import wei.yigulu.modbus.netty.ModbusTcpMasterBuilder;
+import wei.yigulu.modbus.utils.ModbusRequestDataUtils;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author: xiuwei
+ * @version:
+ */
+@Slf4j
+public class TestMaster {
+	public static void main(String[] args) throws InterruptedException, ModbusException {
+
+
+		ModbusTcpMasterBuilder master = new ModbusTcpMasterBuilder("127.0.0.1", 5025);
+		master.createByUnBlock();
+		Thread.sleep(3000L);
+		Map<Integer, ModbusDataTypeEnum> map = new HashMap<>();
+		for (int i = 0; i <= 7; i++) {
+			map.put(i * 2, ModbusDataTypeEnum.P_CDAB);
+		}
+		List<ModbusRequestDataUtils.Obj4RequestData> ll = ModbusRequestDataUtils.splitModbusRequest(map, 1, 3);
+
+		for (; ; ) {
+			try {
+				Map<Integer, IModbusDataType> map1 = ModbusRequestDataUtils.getData(master, ll);
+				for (Integer i : map1.keySet()) {
+					System.out.println(i + " ============ " + ((NumericModbusData) map1.get(i)).getValue());
+				}
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+			Thread.sleep(3000L);
+		}
+
+
+	}
+}

+ 50 - 0
protocol-modbus/src/test/java/TestRtuMaster.java

@@ -0,0 +1,50 @@
+import lombok.extern.slf4j.Slf4j;
+import wei.yigulu.modbus.domain.datatype.IModbusDataType;
+import wei.yigulu.modbus.domain.datatype.ModbusDataTypeEnum;
+import wei.yigulu.modbus.domain.datatype.NumericModbusData;
+import wei.yigulu.modbus.exceptiom.ModbusException;
+import wei.yigulu.modbus.netty.ModbusRtuMasterBuilder;
+import wei.yigulu.modbus.utils.ModbusRequestDataUtils;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author: xiuwei
+ * @version:
+ */
+@Slf4j
+public class TestRtuMaster {
+	public static void main(String[] args) throws InterruptedException, ModbusException {
+		ModbusRtuMasterBuilder master = new ModbusRtuMasterBuilder("COM1");
+		master.createByUnBlock();
+
+		ModbusRtuMasterBuilder master1 = new ModbusRtuMasterBuilder("COM3");
+		master1.createByUnBlock();
+		ModbusRtuMasterBuilder master2 = new ModbusRtuMasterBuilder("COM5");
+		master2.createByUnBlock();
+		ModbusRtuMasterBuilder master3 = new ModbusRtuMasterBuilder("COM7");
+		master3.createByUnBlock();
+		Thread.sleep(5000L);
+		Map<Integer, ModbusDataTypeEnum> map = new HashMap<>();
+		for (int i = 0; i <= 30; i++) {
+			map.put(i * 2, ModbusDataTypeEnum.ABCD);
+		}
+		List<ModbusRequestDataUtils.Obj4RequestData> ll = ModbusRequestDataUtils.splitModbusRequest(map, 1, 3);
+
+		for (; ; ) {
+			try {
+				Map<Integer, IModbusDataType> map1 = ModbusRequestDataUtils.getData(master, ll);
+				for (Integer i : map1.keySet()) {
+					System.out.println(i + " ============ " + ((NumericModbusData) map1.get(i)).getValue());
+				}
+			} catch (ModbusException e) {
+				System.out.println(e.getMsg());
+			}
+			Thread.sleep(3000L);
+		}
+
+
+	}
+}

+ 34 - 0
protocol-modbus/src/test/java/TestRtuSlave.java

@@ -0,0 +1,34 @@
+import lombok.extern.slf4j.Slf4j;
+import wei.yigulu.modbus.domain.datatype.numeric.P_ABCD;
+import wei.yigulu.modbus.exceptiom.ModbusException;
+import wei.yigulu.modbus.netty.ModbusRtuSlaverBuilder;
+
+import java.math.BigDecimal;
+import java.util.Random;
+
+/**
+ * @author: xiuwei
+ * @version:
+ */
+@Slf4j
+public class TestRtuSlave {
+	public static void main(String[] args) throws InterruptedException, ModbusException {
+
+
+		ModbusRtuSlaverBuilder slaver = new ModbusRtuSlaverBuilder("COM1");
+		slaver.createByUnBlock();
+		Thread.sleep(3000L);
+		Random random = new Random();
+		double d;
+		for (; ; ) {
+			for (int i = 0; i < 10; i++) {
+				d = (0.5 - random.nextDouble()) * 100;
+				System.out.println(d);
+				slaver.getModbusSlaveDataContainer().setRegister(1, i * 2, new P_ABCD(BigDecimal.valueOf(d)));
+			}
+			Thread.sleep(1000000L);
+		}
+
+
+	}
+}

+ 24 - 0
protocol-modbus/src/test/java/TestSlaver.java

@@ -0,0 +1,24 @@
+import wei.yigulu.modbus.domain.datatype.numeric.ABCD;
+import wei.yigulu.modbus.netty.ModbusTcpSlaverBuilder;
+
+import java.math.BigDecimal;
+import java.util.Random;
+
+/**
+ * @author: xiuwei
+ * @version:
+ */
+public class TestSlaver {
+	public static void main(String[] args) throws InterruptedException {
+		ModbusTcpSlaverBuilder slaverBuilder = new ModbusTcpSlaverBuilder(2409);
+		slaverBuilder.createByUnBlock();
+
+		Random random = new Random();
+		for (; ; ) {
+			for (int i = 0; i < 10; i++) {
+				slaverBuilder.getModbusSlaveDataContainer().setRegister(1, i, new ABCD(BigDecimal.valueOf((0.5 - random.nextDouble()) * 100)));
+			}
+			Thread.sleep(200L);
+		}
+	}
+}