kopia lustrzana https://github.com/bertrik/ttnhabbridge
Fix spotbugs issue.
rodzic
a505632b57
commit
84fa25f4b1
|
@ -22,6 +22,7 @@ import nl.sikken.bertrik.hab.habitat.docs.ListenerInformationDoc;
|
|||
import nl.sikken.bertrik.hab.habitat.docs.ListenerTelemetryDoc;
|
||||
import nl.sikken.bertrik.hab.habitat.docs.PayloadTelemetryDoc;
|
||||
import okhttp3.OkHttpClient;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.jackson.JacksonConverterFactory;
|
||||
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
||||
|
@ -29,12 +30,11 @@ import retrofit2.converter.scalars.ScalarsConverterFactory;
|
|||
/**
|
||||
* Habitat uploader.
|
||||
*
|
||||
* Exchanges data with the habitat system.
|
||||
* Call to ScheduleXXX methods are non-blocking.
|
||||
* All actions run on a single background thread for simplicity.
|
||||
* Exchanges data with the habitat system. Call to ScheduleXXX methods are
|
||||
* non-blocking. All actions run on a single background thread for simplicity.
|
||||
*/
|
||||
public final class HabitatUploader {
|
||||
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(HabitatUploader.class);
|
||||
|
||||
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
|
@ -46,23 +46,17 @@ public final class HabitatUploader {
|
|||
/**
|
||||
* Creates an actual REST client. Can be used in the constructor.
|
||||
*
|
||||
* @param url the URL to connect to
|
||||
* @param url the URL to connect to
|
||||
* @param timeout the connect and read timeout (ms)
|
||||
* @return a new REST client
|
||||
*/
|
||||
public static IHabitatRestApi newRestClient(String url, Duration timeout) {
|
||||
// create the REST client
|
||||
LOG.info("Creating new habitat REST client with timeout {} for {}", timeout, url);
|
||||
OkHttpClient client = new OkHttpClient().newBuilder()
|
||||
.callTimeout(timeout)
|
||||
.build();
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl(url)
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.addConverterFactory(JacksonConverterFactory.create())
|
||||
.client(client)
|
||||
.build();
|
||||
|
||||
OkHttpClient client = new OkHttpClient().newBuilder().callTimeout(timeout).build();
|
||||
Retrofit retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(ScalarsConverterFactory.create())
|
||||
.addConverterFactory(JacksonConverterFactory.create()).client(client).build();
|
||||
|
||||
return retrofit.create(IHabitatRestApi.class);
|
||||
}
|
||||
|
||||
|
@ -76,7 +70,7 @@ public final class HabitatUploader {
|
|||
this.sha256 = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// this is fatal
|
||||
throw new IllegalStateException("No SHA-256 hash found", e);
|
||||
throw new IllegalStateException("No SHA-256 hash found", e);
|
||||
}
|
||||
this.restClient = restClient;
|
||||
}
|
||||
|
@ -102,9 +96,9 @@ public final class HabitatUploader {
|
|||
/**
|
||||
* Schedules a new sentence to be sent to the HAB network.
|
||||
*
|
||||
* @param sentence the ASCII sentence
|
||||
* @param sentence the ASCII sentence
|
||||
* @param receivers list of listener that received this sentence
|
||||
* @param instant the current date/time
|
||||
* @param instant the current date/time
|
||||
*/
|
||||
public void schedulePayloadTelemetryUpload(String sentence, List<HabReceiver> receivers, Instant instant) {
|
||||
LOG.info("Uploading for {} receivers: {}", receivers.size(), sentence.trim());
|
||||
|
@ -117,7 +111,8 @@ public final class HabitatUploader {
|
|||
|
||||
for (HabReceiver receiver : receivers) {
|
||||
// create Json
|
||||
PayloadTelemetryDoc doc = new PayloadTelemetryDoc(instant, receiver.getCallsign(), bytes);
|
||||
PayloadTelemetryDoc doc = new PayloadTelemetryDoc(instant, bytes);
|
||||
doc.addCallSign(receiver.getCallsign());
|
||||
String json = doc.format();
|
||||
|
||||
// submit it to our processing thread
|
||||
|
@ -126,20 +121,25 @@ public final class HabitatUploader {
|
|||
}
|
||||
|
||||
/**
|
||||
* Performs the actual payload telemetry upload as a REST-like call towards habitat.
|
||||
* Performs the actual payload telemetry upload as a REST-like call towards
|
||||
* habitat.
|
||||
*
|
||||
* @param docId the document id
|
||||
* @param json the JSON payload
|
||||
* @param json the JSON payload
|
||||
*/
|
||||
private void uploadPayloadTelemetry(String docId, String json) {
|
||||
LOG.info("Upload payload telemetry doc {}: {}", docId, json);
|
||||
try {
|
||||
String response = restClient.updateListener(docId, json).execute().body();
|
||||
LOG.info("Result payload telemetry doc {}: {}", docId, response);
|
||||
Response<String> response = restClient.updateListener(docId, json).execute();
|
||||
if (response.isSuccessful()) {
|
||||
LOG.info("Result payload telemetry doc {}: {}", docId, response.body());
|
||||
} else {
|
||||
LOG.warn("Result payload telemetry doc {}: {}", docId, response.message());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.warn("Caught IOException: {}", e.getMessage());
|
||||
} catch (Exception e) {
|
||||
LOG.error("Caught Exception: {}", e);
|
||||
LOG.error("Caught Exception: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -159,17 +159,17 @@ public final class HabitatUploader {
|
|||
* Schedules new listener data to be sent to habitat.
|
||||
*
|
||||
* @param receiver the receiver data
|
||||
* @param instant the current date/time
|
||||
* @param instant the current date/time
|
||||
*/
|
||||
public void scheduleListenerDataUpload(HabReceiver receiver, Instant instant) {
|
||||
executor.submit(() -> uploadListener(receiver, instant));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Uploads listener data (information and telemetry)
|
||||
*
|
||||
* @param receiver the receiver/listener
|
||||
* @param instant the current date/time
|
||||
* @param instant the current date/time
|
||||
*/
|
||||
private void uploadListener(HabReceiver receiver, Instant instant) {
|
||||
LOG.info("Upload listener data for {}", receiver);
|
||||
|
@ -186,12 +186,11 @@ public final class HabitatUploader {
|
|||
ListenerInformationDoc info = new ListenerInformationDoc(instant, receiver);
|
||||
UploadResult infoResult = restClient.uploadDocument(uuids.get(0), info.format()).execute().body();
|
||||
LOG.info("Result listener info: {}", infoResult);
|
||||
|
||||
|
||||
// upload payload telemetry
|
||||
LOG.info("Upload listener telemetry using UUID {}...", uuids.get(1));
|
||||
ListenerTelemetryDoc telem = new ListenerTelemetryDoc(instant, receiver);
|
||||
UploadResult telemResult =
|
||||
restClient.uploadDocument(uuids.get(1), telem.format()).execute().body();
|
||||
UploadResult telemResult = restClient.uploadDocument(uuids.get(1), telem.format()).execute().body();
|
||||
LOG.info("Result listener telemetry: {}", telemResult);
|
||||
} else {
|
||||
LOG.warn("Did not receive UUIDs for upload");
|
||||
|
@ -202,5 +201,5 @@ public final class HabitatUploader {
|
|||
LOG.error("Caught Exception: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ import java.time.Instant;
|
|||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
|
@ -20,23 +22,25 @@ public final class PayloadTelemetryDoc {
|
|||
|
||||
private final OffsetDateTime dateCreated;
|
||||
private final OffsetDateTime dateUploaded;
|
||||
private final String callSign;
|
||||
private final List<String> callSigns = new ArrayList<>();
|
||||
private final byte[] rawBytes;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param instant the creation/upload date/time
|
||||
* @param callSign the receiver call sign
|
||||
* @param instant the creation/upload date/time
|
||||
* @param rawBytes the raw telemetry string as bytes
|
||||
*/
|
||||
public PayloadTelemetryDoc(Instant instant, String callSign, byte[] rawBytes) {
|
||||
public PayloadTelemetryDoc(Instant instant, byte[] rawBytes) {
|
||||
this.dateCreated = OffsetDateTime.ofInstant(instant, ZoneId.systemDefault());
|
||||
this.dateUploaded = OffsetDateTime.ofInstant(instant, ZoneId.systemDefault());
|
||||
this.callSign = Objects.requireNonNull(callSign);
|
||||
this.rawBytes = rawBytes.clone();
|
||||
}
|
||||
|
||||
public void addCallSign(String callSign) {
|
||||
this.callSigns.add(callSign);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the payload telemetry doc as JSON string
|
||||
*/
|
||||
|
@ -49,11 +53,13 @@ public final class PayloadTelemetryDoc {
|
|||
dataNode.set("_raw", factory.binaryNode(rawBytes));
|
||||
|
||||
// create receivers node
|
||||
ObjectNode receiverNode = factory.objectNode();
|
||||
receiverNode.set("time_created", factory.textNode(dateFormat.format(dateCreated)));
|
||||
receiverNode.set("time_uploaded", factory.textNode(dateFormat.format(dateUploaded)));
|
||||
ObjectNode receiversNode = factory.objectNode();
|
||||
receiversNode.set(callSign, receiverNode);
|
||||
for (String callSign : callSigns) {
|
||||
ObjectNode receiverNode = factory.objectNode();
|
||||
receiverNode.set("time_created", factory.textNode(dateFormat.format(dateCreated)));
|
||||
receiverNode.set("time_uploaded", factory.textNode(dateFormat.format(dateUploaded)));
|
||||
receiversNode.set(callSign, receiverNode);
|
||||
}
|
||||
|
||||
// put it together in the top node
|
||||
topNode.set("data", dataNode);
|
||||
|
|
|
@ -70,7 +70,7 @@ public final class TtnMessage {
|
|||
}
|
||||
|
||||
public byte[] getPayloadRaw() {
|
||||
return payloadRaw;
|
||||
return payloadRaw.clone();
|
||||
}
|
||||
|
||||
public Map<String, String> getPayloadFields() {
|
||||
|
|
|
@ -16,7 +16,8 @@ public final class PayloadTelemetryDocTest {
|
|||
@Test
|
||||
public void testFormat() {
|
||||
Instant instant = Instant.now();
|
||||
PayloadTelemetryDoc doc = new PayloadTelemetryDoc(instant, "BERTRIK", new byte[] {1, 2, 3});
|
||||
PayloadTelemetryDoc doc = new PayloadTelemetryDoc(instant, new byte[] {1, 2, 3});
|
||||
doc.addCallSign("BERTRIK");
|
||||
String json = doc.format();
|
||||
|
||||
Assert.assertNotNull(json);
|
||||
|
|
Ładowanie…
Reference in New Issue