@Test public void readPageRangeResponseShouldMatchExample() throws Exception { when(serialPort.readBytes(HEADER_SIZE)).thenReturn(fromHexString("01 0E 00 01")); when(serialPort.readBytes(14 - HEADER_SIZE - TRAILER_SIZE)).thenReturn(fromHexString("01 00 00 00 02 00 00 00")); when(serialPort.readBytes(TRAILER_SIZE)).thenReturn(fromHexString("97 11")); ResponseReader responseReader = new ResponseReader(new LittleEndianDataInputFactory()); PageRangeResponse pageRangeResponse = responseReader.read(PageRangeResponse.class, this.serialPort); assertThat(pageRangeResponse, not(nullValue())); assertThat(pageRangeResponse.getFirstPage(), is(1L)); assertThat(pageRangeResponse.getLastPage(), is(2L)); }
public <T extends Response> T read(Class<T> type, SerialPort serialPort) { try { T response = type.getConstructor(DataInputFactory.class).newInstance(this.dataInputFactory); byte[] header = serialPort.readBytes(HEADER_SIZE); LOGGER.debug(format("Read header from port: %s", toHexString(header))); ResponseHeader responseHeader = readHeader(header); int expectedPayloadSize = responseHeader.getPacketSize() - (HEADER_SIZE + TRAILER_SIZE); LOGGER.debug(format("Expected payload of [%d] bytes", expectedPayloadSize)); byte[] payload = new byte[0]; if (expectedPayloadSize > 0) { payload = serialPort.readBytes(expectedPayloadSize); LOGGER.debug(format("Read payload from port: %s", toHexString(payload))); response.fromBytes(payload); } else { LOGGER.debug("No payload expected, skipping to trailer..."); } byte[] crc16 = serialPort.readBytes(TRAILER_SIZE); LOGGER.debug(format("Read crc16 from port: %s", toHexString(crc16))); validateCrc(Bytes.concat(header, payload, crc16), crc16); return response; } catch (Exception e) { throw Throwables.propagate(e); } }
ResponseReader { public <T extends Response> T read(Class<T> type, SerialPort serialPort) { try { T response = type.getConstructor(DataInputFactory.class).newInstance(this.dataInputFactory); byte[] header = serialPort.readBytes(HEADER_SIZE); LOGGER.debug(format("Read header from port: %s", toHexString(header))); ResponseHeader responseHeader = readHeader(header); int expectedPayloadSize = responseHeader.getPacketSize() - (HEADER_SIZE + TRAILER_SIZE); LOGGER.debug(format("Expected payload of [%d] bytes", expectedPayloadSize)); byte[] payload = new byte[0]; if (expectedPayloadSize > 0) { payload = serialPort.readBytes(expectedPayloadSize); LOGGER.debug(format("Read payload from port: %s", toHexString(payload))); response.fromBytes(payload); } else { LOGGER.debug("No payload expected, skipping to trailer..."); } byte[] crc16 = serialPort.readBytes(TRAILER_SIZE); LOGGER.debug(format("Read crc16 from port: %s", toHexString(crc16))); validateCrc(Bytes.concat(header, payload, crc16), crc16); return response; } catch (Exception e) { throw Throwables.propagate(e); } } }
ResponseReader { public <T extends Response> T read(Class<T> type, SerialPort serialPort) { try { T response = type.getConstructor(DataInputFactory.class).newInstance(this.dataInputFactory); byte[] header = serialPort.readBytes(HEADER_SIZE); LOGGER.debug(format("Read header from port: %s", toHexString(header))); ResponseHeader responseHeader = readHeader(header); int expectedPayloadSize = responseHeader.getPacketSize() - (HEADER_SIZE + TRAILER_SIZE); LOGGER.debug(format("Expected payload of [%d] bytes", expectedPayloadSize)); byte[] payload = new byte[0]; if (expectedPayloadSize > 0) { payload = serialPort.readBytes(expectedPayloadSize); LOGGER.debug(format("Read payload from port: %s", toHexString(payload))); response.fromBytes(payload); } else { LOGGER.debug("No payload expected, skipping to trailer..."); } byte[] crc16 = serialPort.readBytes(TRAILER_SIZE); LOGGER.debug(format("Read crc16 from port: %s", toHexString(crc16))); validateCrc(Bytes.concat(header, payload, crc16), crc16); return response; } catch (Exception e) { throw Throwables.propagate(e); } } @Inject  ResponseReader(DataInputFactory dataInputFactory); }
ResponseReader { public <T extends Response> T read(Class<T> type, SerialPort serialPort) { try { T response = type.getConstructor(DataInputFactory.class).newInstance(this.dataInputFactory); byte[] header = serialPort.readBytes(HEADER_SIZE); LOGGER.debug(format("Read header from port: %s", toHexString(header))); ResponseHeader responseHeader = readHeader(header); int expectedPayloadSize = responseHeader.getPacketSize() - (HEADER_SIZE + TRAILER_SIZE); LOGGER.debug(format("Expected payload of [%d] bytes", expectedPayloadSize)); byte[] payload = new byte[0]; if (expectedPayloadSize > 0) { payload = serialPort.readBytes(expectedPayloadSize); LOGGER.debug(format("Read payload from port: %s", toHexString(payload))); response.fromBytes(payload); } else { LOGGER.debug("No payload expected, skipping to trailer..."); } byte[] crc16 = serialPort.readBytes(TRAILER_SIZE); LOGGER.debug(format("Read crc16 from port: %s", toHexString(crc16))); validateCrc(Bytes.concat(header, payload, crc16), crc16); return response; } catch (Exception e) { throw Throwables.propagate(e); } } @Inject  ResponseReader(DataInputFactory dataInputFactory); T read(Class<T> type, SerialPort serialPort); }
ResponseReader { public <T extends Response> T read(Class<T> type, SerialPort serialPort) { try { T response = type.getConstructor(DataInputFactory.class).newInstance(this.dataInputFactory); byte[] header = serialPort.readBytes(HEADER_SIZE); LOGGER.debug(format("Read header from port: %s", toHexString(header))); ResponseHeader responseHeader = readHeader(header); int expectedPayloadSize = responseHeader.getPacketSize() - (HEADER_SIZE + TRAILER_SIZE); LOGGER.debug(format("Expected payload of [%d] bytes", expectedPayloadSize)); byte[] payload = new byte[0]; if (expectedPayloadSize > 0) { payload = serialPort.readBytes(expectedPayloadSize); LOGGER.debug(format("Read payload from port: %s", toHexString(payload))); response.fromBytes(payload); } else { LOGGER.debug("No payload expected, skipping to trailer..."); } byte[] crc16 = serialPort.readBytes(TRAILER_SIZE); LOGGER.debug(format("Read crc16 from port: %s", toHexString(crc16))); validateCrc(Bytes.concat(header, payload, crc16), crc16); return response; } catch (Exception e) { throw Throwables.propagate(e); } } @Inject  ResponseReader(DataInputFactory dataInputFactory); T read(Class<T> type, SerialPort serialPort); static final int TRAILER_SIZE; }