Skip to content

Commit

Permalink
don't leave empty files in the temp directory if decoding fails
Browse files Browse the repository at this point in the history
+ some refactoring
  • Loading branch information
dernasherbrezon committed Feb 13, 2025
1 parent 615e8d7 commit 0bdb8f7
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/main/java/ru/r2cloud/model/DecoderResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DecoderResult {

private String channelA;
private String channelB;
private Long numberOfDecodedPackets = 0L;
private int numberOfDecodedPackets;
private Long totalSize = 0L;

private File imagePath;
Expand Down Expand Up @@ -46,11 +46,11 @@ public void setChannelB(String channelB) {
this.channelB = channelB;
}

public Long getNumberOfDecodedPackets() {
public int getNumberOfDecodedPackets() {
return numberOfDecodedPackets;
}

public void setNumberOfDecodedPackets(Long numberOfDecodedPackets) {
public void setNumberOfDecodedPackets(int numberOfDecodedPackets) {
this.numberOfDecodedPackets = numberOfDecodedPackets;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private boolean decodeInternally(File rawFile, Observation observation) {
}
observation.setChannelA(result.getChannelA());
observation.setChannelB(result.getChannelB());
observation.setNumberOfDecodedPackets(result.getNumberOfDecodedPackets());
observation.setNumberOfDecodedPackets((long) result.getNumberOfDecodedPackets());
observation.setTotalSize(result.getTotalSize());
observation.setImagePath(result.getImagePath());
observation.setDataPath(result.getDataPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public DecoderResult decode(final File rawIq, final Observation req, final Trans
DecoderResult result = new DecoderResult();
result.setRawPath(rawIq);

long numberOfDecodedPackets = 0;
int numberOfDecodedPackets = 0;
File binFile = new File(config.getTempDirectory(), "lrpt-" + req.getId() + ".bin");
try {
DopplerCorrectedSource source = new DopplerCorrectedSource(predict, rawIq, req, transmitter, baudRate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public LoraDecoder(Class<? extends Beacon> beacon) {
public DecoderResult decode(File rawFile, Observation request, final Transmitter transmitter) {
DecoderResult result = new DecoderResult();
result.setRawPath(null);
long numberOfDecodedPackets = 0;
int numberOfDecodedPackets = 0;
try (BeaconInputStream<? extends Beacon> bis = new BeaconInputStream<>(new BufferedInputStream(new FileInputStream(rawFile)), beacon)) {
long totalSize = 0;
while (bis.hasNext()) {
Expand Down
39 changes: 21 additions & 18 deletions src/main/java/ru/r2cloud/satellite/decoder/TelemetryDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@ public DecoderResult decode(File rawIq, Observation req, final Transmitter trans
return result;
}

long numberOfDecodedPackets = 0;
long totalSize = 0;
float sampleRate = req.getSampleRate();
File binFile = new File(config.getTempDirectory(), req.getId() + ".bin");
List<BeaconSource<? extends Beacon>> input = null;
try (BeaconOutputStream aos = new BeaconOutputStream(new FileOutputStream(binFile))) {
for (Integer baudRate : transmitter.getBaudRates()) {
input = createBeaconSources(rawIq, req, transmitter, baudRate);
List<Beacon> allBeacons = new ArrayList<>();
for (Integer baudRate : transmitter.getBaudRates()) {
try {
List<BeaconSource<? extends Beacon>> input = createBeaconSources(rawIq, req, transmitter, baudRate);
for (int i = 0; i < input.size(); i++) {
if (Thread.currentThread().isInterrupted()) {
LOG.info("decoding thread interrupted. stopping...");
Expand All @@ -79,7 +77,6 @@ public DecoderResult decode(File rawIq, Observation req, final Transmitter trans
meta.setBaud(baudRate);
next.setRxMeta(meta);
beacons.add(next);
numberOfDecodedPackets++;
totalSize += next.getRawData().length;
}
} finally {
Expand All @@ -90,9 +87,6 @@ public DecoderResult decode(File rawIq, Observation req, final Transmitter trans
SnrCalculator.enrichSnr(next, beacons, transmitter.getBandwidth(), 1);
}
}
for (Beacon cur : beacons) {
aos.write(cur);
}
// decode only one image per observation
if (result.getImagePath() == null) {
BufferedImage image = decodeImage(beacons);
Expand All @@ -103,19 +97,28 @@ public DecoderResult decode(File rawIq, Observation req, final Transmitter trans
}
}
}
allBeacons.addAll(beacons);
}
} catch (Exception e) {
LOG.error("[{}] unable to process baud rate {}", req.getId(), baudRate, e);
}
} catch (Exception e) {
LOG.error("unable to process: {}", rawIq, e);
return result;
}
result.setNumberOfDecodedPackets(numberOfDecodedPackets);
result.setNumberOfDecodedPackets(allBeacons.size());
result.setTotalSize(totalSize);
if (numberOfDecodedPackets <= 0) {
Util.deleteQuietly(binFile);
} else {
result.setDataPath(binFile);
if (allBeacons.isEmpty()) {
return result;
}

File binFile = new File(config.getTempDirectory(), req.getId() + ".bin");
try (BeaconOutputStream aos = new BeaconOutputStream(new FileOutputStream(binFile))) {
for (Beacon cur : allBeacons) {
aos.write(cur);
}
} catch (Exception e) {
LOG.error("[{}] unable to process: {}", req.getId(), rawIq, e);
return result;
}
result.setDataPath(binFile);
return result;
}

Expand Down
53 changes: 53 additions & 0 deletions src/test/java/ru/r2cloud/satellite/decoder/Ax100DecoderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ru.r2cloud.satellite.decoder;

import static org.junit.Assert.assertFalse;

import java.io.File;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import ru.r2cloud.TestConfiguration;
import ru.r2cloud.TestUtil;
import ru.r2cloud.model.Observation;
import ru.r2cloud.model.Satellite;
import ru.r2cloud.model.Transmitter;
import ru.r2cloud.predict.PredictOreKit;
import ru.r2cloud.satellite.SatelliteDao;

public class Ax100DecoderTest {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

private SatelliteDao dao;
private Decoders decoders;

@Test
public void testNoExtraFilesAfterFailure() throws Exception {
Satellite satellite = dao.findById("44030");
Transmitter transmitter = satellite.getById("44030-0");
Decoder decoder = decoders.findByTransmitter(transmitter);
File wav = TestUtil.setupClasspathResource(tempFolder, "data/delphini1.raw.gz");
Observation req = TestUtil.loadObservation("data/delphini1.raw.gz.json");
transmitter.setBandwidth(transmitter.getBandwidth() * 100); // GfskDecoder will fail here
decoder.decode(wav, req, transmitter);
File bin = new File(tempFolder.getRoot(), req.getId() + ".bin");
assertFalse(bin.exists());
}

@Before
public void start() throws Exception {
TestConfiguration config = new TestConfiguration(tempFolder);
config.setProperty("server.tmp.directory", tempFolder.getRoot().getAbsolutePath());
config.setProperty("r2cloud.newLaunches", false);
config.setProperty("satellites.meta.location", "./src/test/resources/satellites-test.json");
config.update();
PredictOreKit predict = new PredictOreKit(config);
dao = new SatelliteDao(config);
decoders = new Decoders(predict, config, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public void testScheduleTwice() throws Exception {
Decoder decoder = mock(Decoder.class);
when(decoders.findByTransmitter(any())).thenReturn(decoder);
DecoderResult firstCall = new DecoderResult();
firstCall.setNumberOfDecodedPackets(1L);
firstCall.setNumberOfDecodedPackets(1);
DecoderResult secondCall = new DecoderResult();
secondCall.setNumberOfDecodedPackets(2L);
secondCall.setNumberOfDecodedPackets(2);
when(decoder.decode(any(), any(), any())).thenReturn(firstCall, secondCall);
service.retryObservations();
service.decode(observation.getSatelliteId(), observation.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void testSuccess() throws Exception {
Transmitter transmitter = satellite.getById(transmitterId);
Decoder decoder = decoders.findByTransmitter(transmitter);
DecoderResult result = decoder.decode(wav, req, transmitter);
assertEquals(expectedPackets, result.getNumberOfDecodedPackets().longValue());
assertEquals(expectedPackets, result.getNumberOfDecodedPackets());
assertNotNull(result.getDataPath());
assertNotNull(result.getRawPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testSuccess() throws Exception {
LoraDecoder decoder = new LoraDecoder(RawBeacon.class);
DecoderResult result = decoder.decode(rawFile, new Observation(), new Transmitter());
assertNotNull(result);
assertEquals(1, result.getNumberOfDecodedPackets().longValue());
assertEquals(1, result.getNumberOfDecodedPackets());
assertNotNull(result.getDataPath());
}

Expand All @@ -50,7 +50,7 @@ public void testNoDataOrInvalid() throws Exception {
LoraDecoder decoder = new LoraDecoder(RawBeacon.class);
DecoderResult result = decoder.decode(rawFile, new Observation(), new Transmitter());
assertNotNull(result);
assertEquals(0, result.getNumberOfDecodedPackets().longValue());
assertEquals(0, result.getNumberOfDecodedPackets());
assertNull(result.getDataPath());
assertFalse(rawFile.exists());
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/ru/r2cloud/sdrmodem/SdrModemClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void handleClient(Socket client) throws IOException {
File wav = TestUtil.setupClasspathResource(tempFolder, "data/delfipq.raw.gz");
Decoder decoder = decoders.findByTransmitter(transmitter);
DecoderResult result = decoder.decode(wav, req, transmitter);
assertEquals(1, result.getNumberOfDecodedPackets().longValue());
assertEquals(1, result.getNumberOfDecodedPackets());
try (BeaconInputStream<DelfiPqBeacon> source = new BeaconInputStream<>(new BufferedInputStream(new FileInputStream(result.getDataPath())), DelfiPqBeacon.class)) {
assertTrue(source.hasNext());
AssertJson.assertObjectsEqual("DelfiPqBeacon.json", source.next());
Expand Down

0 comments on commit 0bdb8f7

Please sign in to comment.