Skip to content

Commit

Permalink
Use java record in SensComMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrik committed Sep 22, 2024
1 parent a488038 commit daf0382
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package nl.bertriksikken.senscom;

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

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

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Sensor.community message as uploaded through a POST, suitable for JSON serialization by Jackson.
*/
@JsonAutoDetect(isGetterVisibility = JsonAutoDetect.Visibility.NONE)
public final class SensComMessage {

@JsonProperty("software_version")
private String softwareVersion;
private final String softwareVersion;

@JsonProperty("sensordatavalues")
private final List<SensComItem> items = new ArrayList<>();
Expand All @@ -32,53 +34,25 @@ public void addItem(String name, Double value) {
items.add(new SensComItem(name, value));
}

public List<SensComItem> getItems() {
return List.copyOf(items);
public boolean isEmpty() {
return items.isEmpty();
}

@Override
public String toString() {
return String.format(Locale.ROOT, "{softwareVersion=%s,items=%s}", softwareVersion, items);
}

static final class SensComItem {

@JsonProperty("value_type")
private String name;
@JsonProperty("value")
private String value;

private SensComItem() {
// jackson constructor
}

/**
* Constructor.
*
* @param name the item name
* @param value the item value
*/
SensComItem(String name, String value) {
this();
this.name = name;
this.value = value;
}

record SensComItem(@JsonProperty("value_type") String name, @JsonProperty("value") String value) {
/**
* Convenience constructor.
*
* @param name the item name
*
* @param name the item name
* @param value the item value as double, it will be rounded to 1 decimal
*/
public SensComItem(String name, Double value) {
this(name, String.format(Locale.ROOT, "%.1f", value));
}

@Override
public String toString() {
return String.format(Locale.ROOT, "{name=%s,value=%s}", name, value);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private boolean hasValidGps(SensorData data) {
}

private void uploadMeasurement(AppDeviceId appDeviceId, String sensorId, ESensComPin pin, SensComMessage message) {
if (message.getItems().isEmpty()) {
if (message.isEmpty()) {
// avoid sending empty message
LOG.info("Skipping upload for {} (id {}, pin {}): empty message", appDeviceId, sensorId, pin);
return;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nl.bertriksikken.senscom;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import nl.bertriksikken.senscom.SensComMessage.SensComItem;
import org.junit.Assert;
import org.junit.Test;

public final class SensComMessageTest {

private static final ObjectMapper MAPPER = new ObjectMapper();

@Test
public void testSerializeSensComItem() throws JsonProcessingException {
Double d = 0.1 * 174;
SensComItem item = new SensComItem("name", d);
String json = MAPPER.writeValueAsString(item);
Assert.assertEquals("{\"value_type\":\"name\",\"value\":\"17.4\"}", json);
}

@Test
public void testSerialize() throws JsonProcessingException {
SensComMessage message = new SensComMessage("version");
message.addItem("string", "svalue");
message.addItem("double", "dvalue");
String json = MAPPER.writeValueAsString(message);
Assert.assertEquals("{\"software_version\":\"version\",\"sensordatavalues\":[" +
"{\"value_type\":\"string\",\"value\":\"svalue\"},{\"value_type\":\"double\",\"value\":\"dvalue\"}]}", json);
}

}

0 comments on commit daf0382

Please sign in to comment.