Refactor HabitatUploader to use the XXXDoc classes.

koppelting
Bertrik Sikken 2017-08-15 23:13:33 +02:00
rodzic 50f46d6158
commit a650089956
2 zmienionych plików z 36 dodań i 3 usunięć

Wyświetl plik

@ -25,6 +25,8 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import nl.sikken.bertrik.hab.habitat.docs.PayloadTelemetryDoc;
/**
* Habitat uploader.
*/
@ -93,10 +95,11 @@ public final class HabitatUploader {
LOG.info("Uploading for {}: {}", receiver.getCallsign(), sentence.trim());
// create Json
final String json = createJson(receiver, bytes, date, date);
final PayloadTelemetryDoc doc = new PayloadTelemetryDoc(date, receiver.getCallsign(), bytes);
final String json = doc.format();
// submit it to our processing thread
uploadTelemetry(docId, json);
executor.submit(() -> uploadTelemetry(docId, json));
}
}
@ -148,7 +151,7 @@ public final class HabitatUploader {
}
/**
* Creates the document id.
* Creates the document id from the raw payload telemetry sentence.
*
* @param bytes the raw sentence
* @return the document id
@ -159,6 +162,19 @@ public final class HabitatUploader {
return DatatypeConverter.printHexBinary(hash).toLowerCase();
}
public String createListenerInformation(String callSign, Date dateCreated, Date dateUploaded) {
final JsonNodeFactory factory = new JsonNodeFactory(false);
final ObjectNode topNode = factory.objectNode();
topNode.set("type", factory.textNode("listener_information"));
topNode.set("time_created", factory.textNode(dateFormat.format(dateCreated)));
topNode.set("time_uploaded", factory.textNode(dateFormat.format(dateUploaded)));
final ObjectNode callSignNode = factory.objectNode();
callSignNode.set("callsign", factory.textNode(callSign));
topNode.set("data", callSignNode);
return topNode.toString();
}
/**
* Creates an actual REST client.
* Can be used in the constructor.

Wyświetl plik

@ -3,6 +3,7 @@ package nl.sikken.bertrik.hab.habitat;
import java.util.Arrays;
import java.util.Date;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
@ -56,4 +57,20 @@ public final class HabitatUploaderTest {
return receiver;
}
@Test
@Ignore("this is not a junit test")
public void testActualHappyFlow() {
final IHabitatRestApi restClient = HabitatUploader.newRestClient("http://habitat.habhub.org/habitat", 3000);
final HabitatUploader uploader = new HabitatUploader(restClient);
uploader.start();
try {
final Date date = new Date();
final Sentence sentence = new Sentence("NOTAFLIGHT", 1, date, 52.0182307, 4.695772, 1000);
final IHabReceiver receiver = createReceiver("BERTRIK", new Location(52.0182307, 4.695772, 4));
uploader.upload(sentence.format(), Arrays.asList(receiver), date);
} finally {
uploader.stop();
}
}
}