Add TTN MQTT data structures.

koppelting
Bertrik Sikken 2017-08-21 10:03:25 +02:00
rodzic d1833a244b
commit 5c08920dbf
4 zmienionych plików z 163 dodań i 60 usunięć

Wyświetl plik

@ -1,59 +1,65 @@
package nl.sikken.bertrik.hab.ttn;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Representation of a message received from the TTN MQTT stream.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public final class MqttData {
@JsonProperty("app_id")
private String appId;
@JsonProperty("dev_id")
private String devId;
@JsonProperty("hardware_serial")
private String hardwareSerial;
@JsonProperty("port")
private int port;
@JsonProperty("counter")
private int counter;
@JsonProperty("payload_raw")
private byte[] payload;
@JsonProperty("metadata")
private MqttMetaData metaData;
private MqttData() {
// jackson constructor
}
@JsonProperty("app_id")
private String appId;
public String getAppId() {
return appId;
}
@JsonProperty("dev_id")
private String devId;
public String getDevId() {
return devId;
}
@JsonProperty("hardware_serial")
private String hardwareSerial;
public String getHardwareSerial() {
return hardwareSerial;
}
@JsonProperty("port")
private int port;
public int getCounter() {
return counter;
}
@JsonProperty("counter")
private int counter;
public byte[] getPayload() {
return payload;
}
@JsonProperty("payload_raw")
private byte[] payload;
@JsonProperty("metadata")
private MqttMetaData metaData;
private MqttData() {
// jackson constructor
}
public String getAppId() {
return appId;
}
public String getDevId() {
return devId;
}
public String getHardwareSerial() {
return hardwareSerial;
}
public int getPort() {
return port;
}
public int getCounter() {
return counter;
}
public byte[] getPayload() {
return payload;
}
public MqttMetaData getMetaData() {
return metaData;
}
public MqttMetaData getMetaData() {
return metaData;
}
}

Wyświetl plik

@ -0,0 +1,54 @@
package nl.sikken.bertrik.hab.ttn;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Representation of a gateway in the metadata of the TTN MQTT JSON format.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public final class MqttGateway {
@JsonProperty("gtw_id")
private String gatewayId;
@JsonProperty("gtw_trusted")
private boolean trusted;
@JsonProperty("time")
private String time;
@JsonProperty("latitude")
private double latitude;
@JsonProperty("longitude")
private double longitude;
@JsonProperty("altitude")
private double altitude;
public String getGatewayId() {
return gatewayId;
}
public boolean isTrusted() {
return trusted;
}
public String getTime() {
return time;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public double getAltitude() {
return altitude;
}
}

Wyświetl plik

@ -1,17 +1,27 @@
package nl.sikken.bertrik.hab.ttn;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Representation of meta-data part of MQTT message.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public final class MqttMetaData {
@JsonProperty("time")
private String time;
private MqttMetaData() {
// jackson constructor
}
private String time;
@JsonProperty("gateways")
private List<MqttGateway> gateways;
public String getTime() {
return time;
}
public List<MqttGateway> getMqttGateways() {
return gateways;
}
}

Wyświetl plik

@ -1,6 +1,10 @@
package nl.sikken.bertrik.hab.ttn;
import java.io.IOException;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
@ -14,16 +18,45 @@ import com.fasterxml.jackson.databind.ObjectMapper;
*/
public final class MqttDataTest {
@Test
public void testDeserialization() throws JsonParseException, JsonMappingException, IOException {
final String s = "{\"app_id\":\"ttnmapper\",\"dev_id\":\"mapper2\","
+ "\"hardware_serial\":\"0004A30B001ADBC5\",\"port\":1,\"counter\":0,"
+ "\"payload_raw\":\"AA==\",\"metadata\":{\"time\":\"2017-08-19T15:23:39.288816687Z\"}}";
@Test
public void testDeserialization() throws JsonParseException, JsonMappingException, IOException {
final String s = "{\"app_id\":\"ttnmapper\",\"dev_id\":\"mapper2\","
+ "\"hardware_serial\":\"0004A30B001ADBC5\",\"port\":1,\"counter\":0,"
+ "\"payload_raw\":\"AA==\",\"metadata\":{\"time\":\"2017-08-19T15:23:39.288816687Z\"}}";
final ObjectMapper mapper = new ObjectMapper();
final MqttData mqttData = mapper.readValue(s, MqttData.class);
Assert.assertEquals(0, mqttData.getCounter());
}
final ObjectMapper mapper = new ObjectMapper();
final MqttData mqttData = mapper.readValue(s, MqttData.class);
Assert.assertEquals(0, mqttData.getCounter());
}
/**
* Verify de-serializing some actual MQTT received from TTN.
*
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
@Test
public void testGateways() throws JsonParseException, JsonMappingException, IOException {
final String s = "{ \"app_id\": \"ttn-rfm95-ardumin1\", \"dev_id\": \"ttn-rfm95-2\", \"hardware_serial\": \"0034DA568C2A7566\", \"port\": 1, \"counter\": 11641, \"payload_raw\": \"EHs=\", \"payload_fields\": { \"value\": \"2500\" }, \"metadata\": { \"time\": \"2017-08-20T15:28:19.454240399Z\", \"frequency\": 868.3, \"modulation\": \"LORA\", \"data_rate\": \"SF9BW125\", \"coding_rate\": \"4\\/5\", \"gateways\": [ { \"gtw_id\": \"ttn-cgn_philippshof_1\", \"gtw_trusted\": true, \"timestamp\": 3953316716, \"time\": \"2017-08-20T15:28:18Z\", \"channel\": 1, \"rssi\": -63, \"snr\": 11.5, \"rf_chain\": 1, \"latitude\": 50.950233, \"longitude\": 6.9196253, \"altitude\": 18 } ] } }";
final ObjectMapper mapper = new ObjectMapper();
final MqttData mqttData = mapper.readValue(s, MqttData.class);
Assert.assertEquals(1, mqttData.getPort());
final List<MqttGateway> gws = mqttData.getMetaData().getMqttGateways();
final MqttGateway gw1 = gws.get(0);
Assert.assertEquals(18, gw1.getAltitude(), 0.1);
}
@Test
public void testDecode() throws JsonParseException, JsonMappingException, IOException {
final String s = "{\"app_id\":\"ttnmapper\",\"dev_id\":\"mapper2\",\"hardware_serial\":\"0004A30B001ADBC5\",\"port\":1,\"counter\":4,\"payload_raw\":\"loeaWW4T2+8BHzYZzAIeAA8A/QUS\",\"metadata\":{\"time\":\"2017-08-21T07:11:18.313946438Z\",\"frequency\":868.3,\"modulation\":\"LORA\",\"data_rate\":\"SF7BW125\",\"coding_rate\":\"4/5\",\"gateways\":[{\"gtw_id\":\"eui-008000000000b8b6\",\"timestamp\":1409115451,\"time\":\"2017-08-21T07:11:18.338662Z\",\"channel\":1,\"rssi\":-114,\"snr\":-0.2,\"rf_chain\":1,\"latitude\":52.0182,\"longitude\":4.70844,\"altitude\":27}]}}";
final ObjectMapper mapper = new ObjectMapper();
final MqttData mqttData = mapper.readValue(s, MqttData.class);
Assert.assertEquals(27, mqttData.getMetaData().getMqttGateways().get(0).getAltitude(), 0.1);
}
}