Skip to content

Commit

Permalink
Correct FieldData de/serialization (#798)
Browse files Browse the repository at this point in the history
  • Loading branch information
Starlight220 authored Apr 4, 2024
1 parent be4f85f commit 0e49f00
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class FieldData extends ComplexData<FieldData> {
private final SimplePose2d robot;
Expand Down Expand Up @@ -60,8 +61,13 @@ public FieldData(Map<String, Object> map) {
}

double[] doubles;
if (entry.getValue() instanceof byte[]) {
byte[] data = (byte[]) entry.getValue();
Object value = entry.getValue();
if (value instanceof double[]) {
// Intended flow
doubles = (double[]) value;
} else if (value instanceof byte[]) {
// double[] encoded as byte[] is supported too (for some reason)
byte[] data = (byte[]) value;
doubles = new double[data.length / Double.BYTES];
for (int i = 0; i < doubles.length; i++) {
doubles[i] = ByteBuffer.allocate(Double.BYTES)
Expand All @@ -70,7 +76,13 @@ public FieldData(Map<String, Object> map) {
.getDouble();
}
} else {
doubles = (double[]) map.get(key);
// Some other type (might be an externally-published topic under the same table)
// Warn and drop, but don't raise an exception
doubles = new double[0];
Logger.getLogger(FieldData.class.getName())
.warning(() -> String.format(
"Failed to parse entry %s of type %s: %s",
key, value.getClass(), value));
}

SimplePose2d[] poses = new SimplePose2d[doubles.length / 3];
Expand All @@ -92,6 +104,8 @@ public Map<String, SimplePose2d[]> getObjects() {

@Override
public Map<String, Object> asMap() {
return ImmutableMap.of("Robot", robot);
HashMap<String, SimplePose2d[]> tmp = new HashMap<>(getObjects());
tmp.put("Robot", new SimplePose2d[]{getRobot()});
return ImmutableMap.copyOf(tmp);
}
}

0 comments on commit 0e49f00

Please sign in to comment.