From cbc8f59b329f12303828a4a281e6836ac3ab5f45 Mon Sep 17 00:00:00 2001 From: Bertrik Sikken Date: Fri, 27 Dec 2024 23:23:25 +0100 Subject: [PATCH] Convert HeliumUplinkMessage to record. --- .../helium/HeliumUplinkMessage.java | 83 ++++++++----------- .../helium/HeliumUplinkMessageTest.java | 30 +++---- 2 files changed, 51 insertions(+), 62 deletions(-) diff --git a/sensor-data-bridge/src/main/java/nl/bertriksikken/helium/HeliumUplinkMessage.java b/sensor-data-bridge/src/main/java/nl/bertriksikken/helium/HeliumUplinkMessage.java index a8b92dd..7034efa 100644 --- a/sensor-data-bridge/src/main/java/nl/bertriksikken/helium/HeliumUplinkMessage.java +++ b/sensor-data-bridge/src/main/java/nl/bertriksikken/helium/HeliumUplinkMessage.java @@ -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 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 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, ""); + } } } diff --git a/sensor-data-bridge/src/test/java/nl/bertriksikken/helium/HeliumUplinkMessageTest.java b/sensor-data-bridge/src/test/java/nl/bertriksikken/helium/HeliumUplinkMessageTest.java index d007bb0..04e0bfc 100644 --- a/sensor-data-bridge/src/test/java/nl/bertriksikken/helium/HeliumUplinkMessageTest.java +++ b/sensor-data-bridge/src/test/java/nl/bertriksikken/helium/HeliumUplinkMessageTest.java @@ -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); } }