package ru.r2cloud.satellite.decoder;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.r2cloud.jradio.BeaconInputStream;
import ru.r2cloud.model.DecoderResult;
import ru.r2cloud.model.LoraBeacon;
import ru.r2cloud.model.ObservationRequest;

public class R2loraDecoder implements Decoder {

	private static final Logger LOG = LoggerFactory.getLogger(R2loraDecoder.class);

	@Override
	public DecoderResult decode(File rawFile, ObservationRequest request) {
		DecoderResult result = new DecoderResult();
		result.setRawPath(null);
		try (BeaconInputStream<LoraBeacon> bis = new BeaconInputStream<>(new BufferedInputStream(new FileInputStream(rawFile)), LoraBeacon.class)) {
			long numberOfDecodedPackets = 0;
			while (bis.hasNext()) {
				bis.next();
				numberOfDecodedPackets++;
			}
			result.setNumberOfDecodedPackets(numberOfDecodedPackets);
			if (numberOfDecodedPackets >= 0) {
				result.setDataPath(rawFile);
			}
		} catch (IOException e) {
			LOG.error("unable to read lora beacons", e);
		}
		return result;
	}

}

@@ -370,16 +370,45 @@
 Request;
+%0Aimport ru.r2cloud.util.Util;
 %0A%0Apublic
@@ -689,16 +689,51 @@
 (null);%0A
+%09%09long numberOfDecodedPackets = 0;%0A
 %09%09try (B
@@ -871,44 +871,8 @@
 ) %7B%0A
-%09%09%09long numberOfDecodedPackets = 0;%0A
 %09%09%09w
@@ -1006,17 +1006,97 @@
 ckets);%0A
-%09
+%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09LOG.error(%22unable to read lora beacons%22, e);%0A%09%09%7D%0A
 %09%09if (nu
@@ -1116,17 +1116,17 @@
 Packets 
-%3E
+%3C
 = 0) %7B%0A%09
@@ -1131,115 +1131,76 @@
 %0A%09%09%09
-%09result.setDataPath(rawFile);%0A%09%09%09%7D%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09LOG.error(%22unable to read lora beacons%22, 
+Util.deleteQuietly(rawFile);%0A%09%09%7D else %7B%0A%09%09%09result.setDataPath(rawFil
 e);%0A

fix ui