Skip to content

Commit

Permalink
fix: STN proto aggregation NPE fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tzebrowski committed Oct 13, 2024
1 parent e518654 commit ee1fc35
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 182 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<artifactId>obd-metrics</artifactId>

<version>9.33.0-SNAPSHOT</version>
<version>9.33.1-SNAPSHOT</version>

<packaging>jar</packaging>

Expand Down
19 changes: 12 additions & 7 deletions src/main/java/org/obd/metrics/codec/batch/STNxxxBatchCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -86,25 +87,29 @@ protected Map<String, Map<Integer, List<ObdCommand>>> groupByPriority() {
commands.stream().filter(p -> p.getPriority() == PRIORITY_0).map(p -> p.getPid().getId()).forEach(p -> {
priority0.add(p);
});

log.info("STNxxx: All P0 PIDs {}", priority0);
final Set<Long> all = commands.stream().map(p -> p.getPid().getId()).collect(Collectors.toSet());
final Set<Long> diff = new HashSet<Long>();
diff.addAll(CollectionUtils.subtract(all, priority0));

final int diffPrio =
final Optional<ObdCommand> diffPrio =
commands.stream().
filter(p->diff.contains(p.getPid().getId()) && p.getMode().equals(MODE_22))
.min(Comparator.comparing(ObdCommand::getPriority)).get().getPriority();
log.info("STNxxx: All P{} PIDs: {}",diffPrio, diff);
.min(Comparator.comparing(ObdCommand::getPriority));


log.info("STNxxx: All PIDs: {}", diff);


final Map<Long, Integer> maps = new HashMap<>();
all.forEach(p -> maps.put(p, PRIORITY_0));
diff.forEach(p -> maps.put(p, diffPrio));

if (diffPrio.isPresent()) {
diff.forEach(p -> maps.put(p, diffPrio.get().getPriority()));
}

return aggregate(maps);

} else if (adjustments.getStNxx().isPromoteSlowGroupsEnabled()) {
final Set<Long> promotedToPriority0 = findPromotedPIDs(MODE_22);
log.info("STNxxx: PIDs considered for aggregation: {}", promotedToPriority0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,181 +295,7 @@ public void headersIncludedQueryTest() {
Assertions.assertThat(collection.get(1).getQuery()).isEqualTo("STPX H:18DB33F1, D:01 0B 0C 11, R:2");
}

//7046, 7035, 7037, 7021, 7047, 7025, 7003, 7009, 7016, 7002, 17078, 7014, 7028, 7005, 7036, 7076, 7019, 7020, 7018
@Test
public void aggregateAllGroupsTest() {
final PidDefinitionRegistry pidRegistry = PIDsRegistryFactory.get("mode01.json","giulia_2.0_gme.json");
final Query query = Query.builder()
.pid(pidRegistry.findBy(7046l).getId())
.pid(pidRegistry.findBy(7035l).getId())
.pid(pidRegistry.findBy(7037l).getId())
.pid(pidRegistry.findBy(7021l).getId())
.pid(pidRegistry.findBy(7047l).getId())
.pid(pidRegistry.findBy(7025l).getId())
.pid(pidRegistry.findBy(7003l).getId())
.pid(pidRegistry.findBy(7009l).getId())
.pid(pidRegistry.findBy(7016l).getId())
.pid(pidRegistry.findBy(7002l).getId())
.pid(pidRegistry.findBy(7014l).getId())
.pid(pidRegistry.findBy(7028l).getId())
.pid(pidRegistry.findBy(7005l).getId())
.pid(pidRegistry.findBy(7036l).getId())
.pid(pidRegistry.findBy(7076l).getId())
.pid(pidRegistry.findBy(7019l).getId())
.pid(pidRegistry.findBy(7020l).getId())
.pid(pidRegistry.findBy(7018l).getId())


.build();

Adjustments extra = Adjustments.builder()
.stNxx(STNxxExtensions.builder()
.enabled(Boolean.TRUE)
.promoteAllGroupsEnabled(Boolean.TRUE).build())
.batchPolicy(BatchPolicy.builder()
.mode22BatchSize(10)
.enabled(Boolean.TRUE)
.responseLengthEnabled(false)
.build())
.build();

final Init init = Init.builder()
.header(Header.builder().header("18DB33F1").mode("01").build())
.header(Header.builder().header("18DA10F1").mode("22").build())
.delayAfterInit(0)
.protocol(Protocol.AUTO)
.sequence(DefaultCommandGroup.INIT)
.build();

List<ObdCommand> collection = new CommandsSuplier(pidRegistry, extra, query, init).get();
collection.stream().forEach(p -> System.out.println(p));

Assertions.assertThat(collection).isNotEmpty().hasSize(4);
Assertions.assertThat(collection.get(0).getQuery()).isEqualTo("STPX H:18DA10F1, D:22 1002 1942 1937 130A 18F0 2001 1302 1003 18BA 1935");
Assertions.assertThat(collection.get(1).getQuery()).isEqualTo("STPX H:18DA10F1, D:22 1004 1001 19BD 3A41 1956 0300");
Assertions.assertThat(collection.get(2).getQuery()).isEqualTo("STPX D:22 1018");
Assertions.assertThat(collection.get(3).getQuery()).isEqualTo("STPX D:22 04FE");

}

@Test
public void aggregationTest() {
final PidDefinitionRegistry pidRegistry = PIDsRegistryFactory.get("mode01.json","giulia_2.0_gme.json");
final Query query = Query.builder()
.pid(12l)
.pid(13l)
.pid(18l)
.pid(pidRegistry.findBy(7019l).getId())
.pid(pidRegistry.findBy(7016l).getId())
.pid(pidRegistry.findBy(7002l).getId())
.pid(pidRegistry.findBy(7003l).getId())
.pid(pidRegistry.findBy(7005l).getId())
.pid(pidRegistry.findBy(7007l).getId())
.pid(pidRegistry.findBy(7006l).getId())
.pid(pidRegistry.findBy(7018l).getId())
.pid(pidRegistry.findBy(7015l).getId())
.pid(pidRegistry.findBy(7014l).getId())
.pid(pidRegistry.findBy(7017l).getId())
.pid(pidRegistry.findBy(7020l).getId())

.build();

Adjustments extra = Adjustments.builder()
.stNxx(STNxxExtensions.builder()
.enabled(Boolean.TRUE)
.promoteSlowGroupsEnabled(Boolean.TRUE).build())
.batchPolicy(BatchPolicy.builder().enabled(Boolean.TRUE)
.responseLengthEnabled(true)
.build())
.build();

final Init init = Init.builder()
.header(Header.builder().header("18DB33F1").mode("01").build())
.header(Header.builder().header("18DA10F1").mode("22").build())
.delayAfterInit(0)
.protocol(Protocol.AUTO)
.sequence(DefaultCommandGroup.INIT)
.build();

List<ObdCommand> collection = new CommandsSuplier(pidRegistry, extra, query, init).get();
Assertions.assertThat(collection).isNotEmpty().hasSize(5);
Assertions.assertThat(collection.get(0).getQuery()).isEqualTo("STPX H:18DA10F1, D:22 1937 1924 181F 130A 1004 18BA 1935 1302 3A58, R:5");
Assertions.assertThat(collection.get(1).getQuery()).isEqualTo("STPX H:18DA10F1, D:22 19BD, R:1");
Assertions.assertThat(collection.get(2).getQuery()).isEqualTo("STPX H:18DA10F1, D:22 3A41, R:1");


extra = Adjustments.builder()
.stNxx(STNxxExtensions.builder()
.enabled(Boolean.TRUE)
.promoteSlowGroupsEnabled(Boolean.FALSE).build())
.batchPolicy(BatchPolicy.builder()
.enabled(Boolean.TRUE)
.responseLengthEnabled(true)
.build())
.build();

collection = new CommandsSuplier(pidRegistry, extra, query, init).get();
Assertions.assertThat(collection).isNotEmpty().hasSize(6);
Assertions.assertThat(collection.get(0).getQuery()).isEqualTo("STPX H:18DA10F1, D:22 1937 1924 181F 130A, R:3");
Assertions.assertThat(collection.get(1).getQuery()).isEqualTo("STPX H:18DA10F1, D:22 1004 18BA 1935 1302 3A58, R:3");
Assertions.assertThat(collection.get(2).getQuery()).isEqualTo("STPX H:18DA10F1, D:22 19BD, R:1");
}

@Test
public void priorityTestForNonBatchCommands() {
PidDefinitionRegistry pidRegistry = PIDsRegistryFactory.get("mode01.json","giulia_2.0_gme.json");
final Query query = Query.builder()
.pid(7018l)
.pid(7001l)
.pid(7005l)
.pid(7006l)
.pid(7007l)
.pid(7008l)
.pid(7010l)
.pid(7021l)
.pid(7022l)
.pid(7023l)
.pid(7025l)
.pid(7029l)
.build();

final Adjustments extra = Adjustments
.builder()
.stNxx(STNxxExtensions.builder()
.enabled(Boolean.TRUE)
.promoteSlowGroupsEnabled(Boolean.TRUE).build())
.batchPolicy(BatchPolicy.builder()
.responseLengthEnabled(true)
.enabled(Boolean.TRUE).build())
.build();

final Init init = Init.builder()
.header(Header.builder().header("18DB33F1").mode("01").build())
.header(Header.builder().header("18DA10F1").mode("22").build())
.header(Header.builder().header("18DA18F1").mode("555").build())
.delayAfterInit(0)
.protocol(Protocol.AUTO)
.sequence(DefaultCommandGroup.INIT)
.build();

final List<ObdCommand> collection = new CommandsSuplier(pidRegistry, extra ,query, init).get();

Assertions.assertThat(collection).isNotEmpty().hasSize(4);
Assertions.assertThat(collection.get(0).getQuery()).isEqualTo("STPX H:18DA10F1, D:22 130A 195A 1937 181F 1924 1000 182F 180E 1867, R:6");
Assertions.assertThat(collection.get(0).getPriority()).isEqualTo(0);

Assertions.assertThat(collection.get(1).getQuery()).isEqualTo("STPX H:18DA10F1, D:22 1956, R:1");
Assertions.assertThat(collection.get(1).getPriority()).isEqualTo(5);

Assertions.assertThat(collection.get(2).getQuery()).isEqualTo("STPX H:18DA18F1, D:22 051A, R:1");
Assertions.assertThat(collection.get(2).getPriority()).isEqualTo(0);

Assertions.assertThat(collection.get(3).getQuery()).isEqualTo("STPX H:18DA18F1, D:22 04FE, R:1");
Assertions.assertThat(collection.get(3).getPriority()).isEqualTo(2);

}



@ParameterizedTest
@CsvSource(value = {
Expand Down
Loading

0 comments on commit ee1fc35

Please sign in to comment.