Plc4x2AdsProtocol extends MessageToMessageCodec<AmsPacket, PlcRequestContainer<InternalPlcRequest, InternalPlcResponse>> { @Override protected void encode(ChannelHandlerContext ctx, PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> msg, List<Object> out) throws Exception { LOGGER.trace("(<--OUT): {}, {}, {}", ctx, msg, out); PlcRequest request = msg.getRequest(); if (request instanceof PlcReadRequest) { encodeReadRequest(msg, out); } else if (request instanceof PlcWriteRequest) { encodeWriteRequest(msg, out); } else if (request instanceof PlcProprietaryRequest) { encodeProprietaryRequest(msg, out); } else { throw new PlcProtocolException("Unknown type " + request.getClass()); } } Plc4x2AdsProtocol(AmsNetId targetAmsNetId, AmsPort targetAmsPort, AmsNetId sourceAmsNetId, AmsPort sourceAmsPort, ConcurrentMap<SymbolicAdsField, DirectAdsField> fieldMapping); @Override void exceptionCaught(ChannelHandlerContext ctx, Throwable cause); boolean addConsumer(Consumer<AdsDeviceNotificationRequest> adsDeviceNotificationRequestConsumer); boolean removeConsumer(Consumer<AdsDeviceNotificationRequest> adsDeviceNotificationRequestConsumer);  }
@Test public void encode() throws Exception { assumeThat(payloadClazzName + " not yet implemented", notYetSupportedDataType, not(hasItem(payloadClazzName))); ArrayList<Object> out = new ArrayList<>(); SUT.encode(null, plcRequestContainer, out); assertThat(out, hasSize(1)); assertThat(out.get(0), instanceOf(AmsPacket.class)); AmsPacket amsPacket = (AmsPacket) out.get(0); LOGGER.info("{}\nHexDump:\n{}", amsPacket, amsPacket.dump()); if (amsPacket instanceof AdsWriteRequest) { AdsWriteRequest adsWriteRequest = (AdsWriteRequest) amsPacket; byte[] value = adsWriteRequest.getData().getBytes(); if (payloadClazzName.equals(Boolean.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x1})); } else if (payloadClazzName.equals(Byte.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x1})); } else if (payloadClazzName.equals(Short.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x1, 0x0})); } else if (payloadClazzName.equals(Integer.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x1, 0x0, 0x0, 0x0})); } else if (payloadClazzName.equals(Long.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x1, 0x0, 0x0, 0x0})); } else if (payloadClazzName.equals(BigInteger.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x1, 0x0, 0x0, 0x0})); } else if (payloadClazzName.equals(Float.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x0, 0x0, (byte) 0x80, 0x3F})); } else if (payloadClazzName.equals(Double.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, (byte) 0xF0, 0x3F})); } else if (payloadClazzName.equals(BigDecimal.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, (byte) 0xF0, 0x3F})); } else if (payloadClazzName.equals(String.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00})); } else if (payloadClazzName.equals(LocalTime.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00})); } else if (payloadClazzName.equals(LocalDate.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00})); } else if (payloadClazzName.equals(LocalDateTime.class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00})); } else if (payloadClazzName.equals(byte[].class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00})); } else if (payloadClazzName.equals(Byte[].class.getSimpleName())) { assertThat(value, equalTo(new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00})); } } }