Pass payload encoding directly as an enum, not as string.

pull/3/head
Bertrik Sikken 2019-05-16 09:03:09 +02:00
rodzic d7eb552fa4
commit 7ef299bbcd
4 zmienionych plików z 14 dodań i 8 usunięć

Wyświetl plik

@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import nl.sikken.bertrik.hab.DecodeException;
import nl.sikken.bertrik.hab.EPayloadEncoding;
import nl.sikken.bertrik.hab.ExpiringCache;
import nl.sikken.bertrik.hab.PayloadDecoder;
import nl.sikken.bertrik.hab.Sentence;
@ -70,7 +71,7 @@ public final class TtnHabBridge {
HabitatUploader.newRestClient(config.getHabitatUrl(), config.getHabitatTimeout());
this.habUploader = new HabitatUploader(restApi);
this.mapper = new ObjectMapper();
this.decoder = new PayloadDecoder(config.getTtnPayloadEncoding());
this.decoder = new PayloadDecoder(EPayloadEncoding.parse(config.getTtnPayloadEncoding()));
this.gwCache = new ExpiringCache(config.getTtnGwCacheExpiry());
}

Wyświetl plik

@ -41,6 +41,11 @@ public enum EPayloadEncoding {
return name;
}
@Override
public String toString() {
return name;
}
/**
* Parses an encoding by name.
*

Wyświetl plik

@ -30,9 +30,9 @@ public final class PayloadDecoder {
*
* @param encodingName the payload encoding name
*/
public PayloadDecoder(String encodingName) {
LOG.info("Payload decoder initialised for '{}' format", encodingName);
this.encoding = Objects.requireNonNull(EPayloadEncoding.parse(encodingName));
public PayloadDecoder(EPayloadEncoding encoding) {
LOG.info("Payload decoder initialised for '{}' format", encoding);
this.encoding = Objects.requireNonNull(encoding);
}
/**

Wyświetl plik

@ -41,7 +41,7 @@ public final class PayloadDecoderTest {
TtnMessage message = mapper.readValue(data, TtnMessage.class);
Assert.assertEquals(3, message.getMetaData().getMqttGateways().size());
PayloadDecoder decoder = new PayloadDecoder(EPayloadEncoding.JSON.getName());
PayloadDecoder decoder = new PayloadDecoder(EPayloadEncoding.JSON);
Sentence sentence = decoder.decode(message);
Assert.assertEquals("$$devtrack,1707,19:02:46,51.564211,4.368230,660.0,1.1,3.148*B35B",
@ -71,7 +71,7 @@ public final class PayloadDecoderTest {
Assert.assertEquals(27, message.getMetaData().getMqttGateways().get(0).getAltitude(), 0.1);
// decode payload
PayloadDecoder decoder = new PayloadDecoder(EPayloadEncoding.SODAQ_ONE.getName());
PayloadDecoder decoder = new PayloadDecoder(EPayloadEncoding.SODAQ_ONE);
Sentence sentence = decoder.decode(message);
Assert.assertEquals("$$mapper2,4,07:11:18,52.022064,4.693023,30.0,19,4.10*81FD", sentence.format().trim());
@ -95,7 +95,7 @@ public final class PayloadDecoderTest {
TtnMessage message = mapper.readValue(data, TtnMessage.class);
// decode payload
PayloadDecoder decoder = new PayloadDecoder(EPayloadEncoding.CAYENNE.getName());
PayloadDecoder decoder = new PayloadDecoder(EPayloadEncoding.CAYENNE);
Sentence sentence = decoder.decode(message);
Assert.assertEquals("$$ttntest1,9,16:53:10,52.0220,4.6927,44.00,4.21,29.0*383E\n", sentence.format());
@ -106,7 +106,7 @@ public final class PayloadDecoderTest {
*/
@Test(expected = NullPointerException.class)
public void testInvalidEncoding() {
PayloadDecoder decoder = new PayloadDecoder("unknown");
PayloadDecoder decoder = new PayloadDecoder(null);
Assert.assertNotNull(decoder);
}
}