@Test public void readUserEventsDatabasePagesShouldSucceed() throws Exception { when(serialPort.readBytes(HEADER_SIZE)).thenReturn(fromHexString("01 26 04 01")); byte[] payload = fromHexString("6D 0B 00 00 19 00 00 00 0B 01 75 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CB 20 DB B8 D4 08 91 56 D4 08 02 00 54 56 D4 08 5E 01 00 00 E9 9D 0D BA D4 08 C3 57 D4 08 01 00 BC 57 D4 08 0A 00 00 00 E8 E3 FA 06 D5 08 B0 A4 D4 08 02 00 9C A4 D4 08 5E 01 00 00 40 E5 AF 13 D5 08 65 B1 D4 08 01 00 44 B1 D4 08 0D 00 00 00 AE C5 67 3B D5 08 1D D9 D4 08 01 00 E0 D8 D4 08 04 00 00 00 78 96 BD 50 D5 08 73 EE D4 08 02 00 70 EE D4 08 F4 01 00 00 9C 0D 2D BE D5 08 E3 5B D5 08 02 00 A8 5B D5 08 58 02 00 00 38 0F 37 BE D5 08 ED 5B D5 08 02 00 E4 5B D5 08 2C 01 00 00 54 2A 41 CA D5 08 F7 67 D5 08 01 00 CC 64 D5 08 01 00 00 00 14 1F D9 DD D5 08 8F 7B D5 08 02 00 88 7B D5 08 FA 00 00 00 29 60 5C 05 D6 08 12 A3 D5 08 02 00 E8 A2 D5 08 2C 01 00 00 F8 2F D9 05 D6 08 8F A3 D5 08 01 00 60 A3 D5 08 02 00 00 00 86 7C 52 08 D6 08 08 A6 D5 08 01 00 F4 A5 D5 08 0A 00 00 00 F6 32 CB 22 D6 08 81 C0 D5 08 01 00 70 C0 D5 08 02 00 00 00 AB 3E AA 2C D6 08 60 CA D5 08 01 00 48 CA D5 08 02 00 00 00 3D 95 EA 2E D6 08 A0 CC D5 08 01 00 64 CC D5 08 02 00 00 00 36 77 BE 35 D6 08 74 D3 D5 08 01 00 6C D3 D5 08 02 00 00 00 FB 15 1B 53 D6 08 D1 F0 D5 08 02 00 B8 F0 D5 08 2C 01 00 00 48 13 90 59 D6 08 46 F7 D5 08 01 00 0C F7 D5 08 0A 00 00 00 45 B3 E1 6C D6 08 97 0A D6 08 01 00 80 0A D6 08 04 00 00 00 4C D0 A1 93 D6 08 57 31 D6 08 02 00 2C 31 D6 08 64 00 00 00 61 9A DE 93 D6 08 94 31 D6 08 02 00 68 31 D6 08 F4 01 00 00 14 9E DE 9C D6 08 94 3A D6 08 02 00 8C 3A D6 08 64 00 00 00 81 6A BB 12 D7 08 71 B0 D6 08 02 00 70 B0 D6 08 2C 01 00 00 33 52 10 13 D7 08 C6 B0 D6 08 02 00 AC B0 D6 08 58 02 00 00 CA 2B 86 0B 00 00 0A 00 00 00 0B 01 76 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 C9 5D E3 1B D7 08 99 B9 D6 08 01 00 94 B9 D6 08 01 00 00 00 3D A5 3A 2A D7 08 F0 C7 D6 08 01 00 E0 C7 D6 08 02 00 00 00 EE 29 2C 2B D7 08 E2 C8 D6 08 01 00 D0 C8 D6 08 02 00 00 00 45 F6 A6 5F D7 08 5C FD D6 08 02 00 50 FD D6 08 5E 01 00 00 FC FF AC 62 D7 08 62 00 D7 08 01 00 5C 00 D7 08 0A 00 00 00 60 87 1C 8E D7 08 D2 2B D7 08 01 00 B8 2B D7 08 02 00 00 00 56 2D 0F 8F D7 08 C5 2C D7 08 01 00 A8 2C D7 08 01 00 00 00 EC 0C 6A B8 D7 08 20 56 D7 08 02 00 E8 55 D7 08 5E 01 00 00 9A AB A6 BB D7 08 5C 59 D7 08 01 00 30 59 D7 08 0D 00 00 00 A3 32 35 C2 D7 08 EB 5F D7 08 02 00 C0 5F D7 08 96 00 00 00 08 9C FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"); when(serialPort.readBytes(payload.length)).thenReturn(payload); when(serialPort.readBytes(TRAILER_SIZE)).thenReturn(fromHexString("24 5F")); ResponseReader responseReader = new ResponseReader(new LittleEndianDataInputFactory()); UserEventsDatabasePagesResponse pagesResponse = responseReader.read(UserEventsDatabasePagesResponse.class, this.serialPort); assertThat(pagesResponse, not(nullValue())); assertThat(pagesResponse.getRecords().size(), is(35)); }
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; }