diff --git a/src/main/java/ru/r2cloud/jradio/BeaconInputStream.java b/src/main/java/ru/r2cloud/jradio/BeaconInputStream.java index fe9459ce..2b834df7 100644 --- a/src/main/java/ru/r2cloud/jradio/BeaconInputStream.java +++ b/src/main/java/ru/r2cloud/jradio/BeaconInputStream.java @@ -29,6 +29,8 @@ public boolean hasNext() { int protocolVersion = is.readInt(); if (protocolVersion == BeaconOutputStream.PROTOCOL_V2) { current = readProtocolv2(); + } else if (protocolVersion == BeaconOutputStream.PROTOCOL_V3) { + current = readProtocolv3(); } else { return false; } @@ -66,6 +68,12 @@ private T readProtocolv2() throws Exception { return result; } + private T readProtocolv3() throws Exception { + T result = readProtocolv2(); + result.setEndSample(is.readLong()); + return result; + } + @Override public T next() { if (current == null) { diff --git a/src/main/java/ru/r2cloud/jradio/BeaconOutputStream.java b/src/main/java/ru/r2cloud/jradio/BeaconOutputStream.java index 779f87c9..4a3621f4 100644 --- a/src/main/java/ru/r2cloud/jradio/BeaconOutputStream.java +++ b/src/main/java/ru/r2cloud/jradio/BeaconOutputStream.java @@ -8,6 +8,7 @@ public class BeaconOutputStream implements Closeable { public static final int PROTOCOL_V2 = 1; + public static final int PROTOCOL_V3 = 2; private final DataOutputStream dos; public BeaconOutputStream(OutputStream os) { @@ -19,7 +20,7 @@ public void write(Beacon beacon) throws IOException { return; } dos.writeInt(0); - dos.writeInt(PROTOCOL_V2); + dos.writeInt(PROTOCOL_V3); dos.writeInt(beacon.getRawData().length); dos.write(beacon.getRawData()); dos.writeLong(beacon.getBeginMillis()); @@ -46,6 +47,7 @@ public void write(Beacon beacon) throws IOException { dos.writeFloat(0.0f); dos.writeLong(0); } + dos.writeLong(beacon.getEndSample()); } @Override diff --git a/src/test/java/ru/r2cloud/jradio/BeaconInputStreamTest.java b/src/test/java/ru/r2cloud/jradio/BeaconInputStreamTest.java index d3d4a4ea..364c00cb 100644 --- a/src/test/java/ru/r2cloud/jradio/BeaconInputStreamTest.java +++ b/src/test/java/ru/r2cloud/jradio/BeaconInputStreamTest.java @@ -28,6 +28,7 @@ public void testReadWrite() throws Exception { meta.setSnr(1.22f); meta.setFrequencyError(1001l); data.setRxMeta(meta); + data.setEndSample(2L); bos.write(data); bos.close(); @@ -42,13 +43,33 @@ public void testReadWrite() throws Exception { assertEquals(data.getRxMeta().getRssi(), actual.getRxMeta().getRssi(), 0.0001); assertEquals(data.getRxMeta().getSnr(), actual.getRxMeta().getSnr(), 0.0001); assertEquals(data.getRxMeta().getFrequencyError(), actual.getRxMeta().getFrequencyError()); + assertEquals(data.getEndSample(), actual.getEndSample()); assertArrayEquals(data.getRawData(), actual.getRawData()); assertFalse(bis.hasNext()); bis.close(); } @Test - public void testOldFormat() throws Exception { + public void testv2Format() throws Exception { + byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 63, -99, 112, -92, 63, -100, 40, -10, 0, 0, 0, 0, 0, 0, 3, -23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + ByteArrayInputStream bais = new ByteArrayInputStream(data); + BeaconInputStream bis = new BeaconInputStream<>(bais, RawBeacon.class); + assertTrue(bis.hasNext()); + Beacon actual = bis.next(); + assertNotNull(actual); + assertEquals(1L, actual.getBeginMillis()); + assertEquals(1L, actual.getBeginSample()); + assertArrayEquals(new byte[] { 0x01, 0x02 }, actual.getRawData()); + assertNotNull(actual.getRxMeta()); + assertEquals(1.23f, actual.getRxMeta().getRssi(), 0.0f); + assertEquals(1.22f, actual.getRxMeta().getSnr(), 0.0f); + assertEquals(1001L, actual.getRxMeta().getFrequencyError().longValue()); + assertFalse(bis.hasNext()); + bis.close(); + } + + @Test + public void testv1Format() throws Exception { byte[] data = new byte[] { 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 }; ByteArrayInputStream bais = new ByteArrayInputStream(data); BeaconInputStream bis = new BeaconInputStream<>(bais, RawBeacon.class);