Skip to content

Commit

Permalink
Convert HeliumUplinkMessage to record.
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrik committed Dec 27, 2024
1 parent 40746fe commit cbc8f59
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,59 +1,48 @@
package nl.bertriksikken.helium;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public final class HeliumUplinkMessage {

@JsonProperty("app_eui")
String appEui = "";

@JsonProperty("dev_eui")
String devEui = "";

// device address with bytes in reverse order
@JsonProperty("devaddr")
String devAddr = "";

@JsonProperty("fcnt")
int fcnt;

@JsonProperty("port")
int port;

@JsonProperty("name")
String name = "";

@JsonProperty("payload")
byte[] payload = new byte[0];
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

// milliseconds
@JsonProperty("reported_at")
long reportedAt;
@SuppressWarnings("ArrayRecordComponent")
@JsonIgnoreProperties(ignoreUnknown = true)
public record HeliumUplinkMessage(
@JsonProperty("app_eui") String appEui,
@JsonProperty("dev_eui") String devEui,
@JsonProperty("devaddr") String devAddr, // device address with bytes in reverse order
@JsonProperty("fcnt") int fcnt,
@JsonProperty("port") int port,
@JsonProperty("name") String name,
@JsonProperty("payload") byte[] payload,
@JsonProperty("reported_at") long reportedAt, // milliseconds
@JsonProperty("hotspots") List<HotSpot> hotSpots) {
public HeliumUplinkMessage {
appEui = Objects.requireNonNullElse(appEui, "");
devEui = Objects.requireNonNullElse(devEui, "");
devAddr = Objects.requireNonNullElse(devAddr, "");
name = Objects.requireNonNullElse(name, "");
payload = Objects.requireNonNullElse(payload.clone(), new byte[0]);
hotSpots = Objects.requireNonNullElse(hotSpots, new ArrayList<>());
}

@JsonProperty("hotspots")
List<HotSpot> hotSpots = new ArrayList<>();
@Override
public byte[] payload() {
return payload.clone();
}

@JsonIgnoreProperties(ignoreUnknown = true)
static final class HotSpot {
@JsonProperty("name")
String name = "";

@JsonProperty("lat")
double latitude;

@JsonProperty("long")
double longitude;

@JsonProperty("rssi")
double rssi;

@JsonProperty("snr")
double snr;
record HotSpot(
@JsonProperty("name") String name,
@JsonProperty("lat") double latitude,
@JsonProperty("long") double longitude,
@JsonProperty("rssi") double rssi,
@JsonProperty("snr") double snr) {
HotSpot {
name = Objects.requireNonNullElse(name, "");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ public void testDecode() throws IOException {
HeliumUplinkMessage uplink = mapper.readValue(is, HeliumUplinkMessage.class);
Assertions.assertNotNull(uplink);

Assertions.assertEquals("6081F9D16837130E", uplink.appEui);
Assertions.assertEquals("5A010048", uplink.devAddr);
Assertions.assertEquals("0004A30B001F21FA", uplink.devEui);
Assertions.assertEquals(0, uplink.fcnt);
Assertions.assertEquals("kissmapper", uplink.name);
Assertions.assertEquals(1, uplink.port);
Assertions.assertEquals(1631457565832L, uplink.reportedAt);
Assertions.assertArrayEquals(new byte[]{3}, uplink.payload);

HotSpot hotSpot = uplink.hotSpots.get(0);
Assertions.assertEquals(52.01745, hotSpot.latitude, 0.00001);
Assertions.assertEquals(4.729876, hotSpot.longitude, 0.00001);
Assertions.assertEquals("melted-quartz-antelope", hotSpot.name);
Assertions.assertEquals(-120, hotSpot.rssi, 0.1);
Assertions.assertEquals(-7.5, hotSpot.snr, 0.1);
Assertions.assertEquals("6081F9D16837130E", uplink.appEui());
Assertions.assertEquals("5A010048", uplink.devAddr());
Assertions.assertEquals("0004A30B001F21FA", uplink.devEui());
Assertions.assertEquals(0, uplink.fcnt());
Assertions.assertEquals("kissmapper", uplink.name());
Assertions.assertEquals(1, uplink.port());
Assertions.assertEquals(1631457565832L, uplink.reportedAt());
Assertions.assertArrayEquals(new byte[]{3}, uplink.payload());

HotSpot hotSpot = uplink.hotSpots().get(0);
Assertions.assertEquals(52.01745, hotSpot.latitude(), 0.00001);
Assertions.assertEquals(4.729876, hotSpot.longitude(), 0.00001);
Assertions.assertEquals("melted-quartz-antelope", hotSpot.name());
Assertions.assertEquals(-120, hotSpot.rssi(), 0.1);
Assertions.assertEquals(-7.5, hotSpot.snr(), 0.1);
}

}
Expand Down

0 comments on commit cbc8f59

Please sign in to comment.