Skip to content

Commit

Permalink
feat:Metadata support enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
tzebrowski committed Jan 11, 2025
1 parent ecd1bae commit 1345bd5
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 158 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>11.0.0-SNAPSHOT</version>
<version>11.1.0-SNAPSHOT</version>

<packaging>jar</packaging>

Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/obd/metrics/api/CommandBufferInitHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.obd.metrics.buffer.CommandsBuffer;
import org.obd.metrics.command.ATCommand;
import org.obd.metrics.command.Command;
import org.obd.metrics.command.obd.ObdCommand;
import org.obd.metrics.command.process.DelayCommand;
import org.obd.metrics.command.process.InitCompletedCommand;
import org.obd.metrics.context.Context;
Expand Down Expand Up @@ -92,16 +93,20 @@ private void appendPIDsGroups(Init init, LinkedList<PIDsGroup> groups) {

@SuppressWarnings("unchecked")
private Optional<Command> mapToCommand(Class<?> defaultClass, PidDefinition pid) {

log.debug("Instantiating the PID: {} for the group: {}",pid.getPid(),pid.getGroup());

try {

final Class<?> commandClass = (pid.getCommandClass() == null) ? defaultClass
: Class.forName(pid.getCommandClass());
if (commandClass == null) {
return Optional.empty();
return Optional.of(new ObdCommand(pid));
}else {
final Constructor<? extends Command> constructor = (Constructor<? extends Command>) commandClass
.getConstructor(PidDefinition.class);
return Optional.of(constructor.newInstance(pid));
}
final Constructor<? extends Command> constructor = (Constructor<? extends Command>) commandClass
.getConstructor(PidDefinition.class);
return Optional.of(constructor.newInstance(pid));
} catch (Throwable e) {
log.error("Failed to initiate command class: {}", pid.getCommandClass(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,35 @@
**/
package org.obd.metrics.command.meta;

import java.util.Optional;

import org.obd.metrics.codec.Codec;
import org.obd.metrics.pid.PidDefinition;
import org.obd.metrics.transport.Characters;
import org.obd.metrics.transport.message.ConnectorResponse;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public final class HexCommand extends MetadataCommand implements Codec<String> {

public HexCommand(PidDefinition pid) {
super(pid);
}
public final class HexCodec implements Codec<String> {

@Override
public String decode(PidDefinition pid, ConnectorResponse connectorResponse) {

log.info("Decoding the message: {}", connectorResponse.getMessage());
final Optional<String> answer = decodeRawMessage(getQuery(),connectorResponse);

if (answer.isPresent()) {
final String decoded = Hex.decode(answer.get());
if (log.isTraceEnabled()) {
log.trace("PID: {}, received message: {}",pid.getPid(), connectorResponse.getMessage());
}

final String rawValue = connectorResponse.getRawValue(pid);
if (rawValue == null) {
return null;
}else {
final String answer = Characters.normalize(rawValue);
final String decoded = Hex.decode(answer);
final String result = (decoded == null) ? null : decoded.trim();
log.info("Decoded message: {} for: {}", result, connectorResponse.getMessage());

if (log.isTraceEnabled()) {
log.trace("Decoded message: {} for: {}", result, connectorResponse.getMessage());
}
return result;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,18 @@

import java.util.Optional;

import org.obd.metrics.command.Command;
import org.obd.metrics.pid.PidDefinition;
import org.obd.metrics.transport.Characters;
import org.obd.metrics.transport.message.ConnectorResponse;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class MetadataCommand extends Command {
public abstract class MetadataCommand {

protected final PidDefinition pid;

protected MetadataCommand(PidDefinition pid) {
super(pid.getQuery(), pid.getMode(),
pid.getDescription(), pid.getOverrides().getCanMode() != null ? pid.getOverrides().getCanMode() : "");
this.pid = pid;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,23 @@
**/
package org.obd.metrics.command.meta;

import java.util.Optional;

import org.obd.metrics.codec.Codec;
import org.obd.metrics.pid.PidDefinition;
import org.obd.metrics.transport.Characters;
import org.obd.metrics.transport.message.ConnectorResponse;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public final class NotEncodedCommand extends MetadataCommand implements Codec<String> {
public final class NotEncodedCodec implements Codec<String> {

public NotEncodedCommand(PidDefinition pid) {
super(pid);
}

@Override
public String decode(PidDefinition pid, ConnectorResponse connectorResponse) {

log.info("Decoding the message: {}", connectorResponse.getMessage());
final Optional<String> answer = decodeRawMessage(getQuery(), connectorResponse);
if (answer.isPresent()) {
log.info("Decoded message: {} for: {}", answer.get(), connectorResponse.getMessage());
return answer.get();
if (log.isTraceEnabled()) {
log.trace("Decoding the message: {}", connectorResponse.getMessage());
}
return null;
final String rawValue = connectorResponse.getRawValue(pid);
return rawValue == null ? null : Characters.normalize(rawValue);
}
}
25 changes: 8 additions & 17 deletions src/main/java/org/obd/metrics/command/meta/TimeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,23 @@
**/
package org.obd.metrics.command.meta;

import java.util.Optional;

import org.obd.metrics.codec.Codec;
import org.obd.metrics.pid.PidDefinition;
import org.obd.metrics.transport.message.ConnectorResponse;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public final class TimeCommand extends MetadataCommand implements Codec<Integer> {

public TimeCommand(PidDefinition pid) {
super(pid);
}
public final class TimeCommand implements Codec<String> {

@Override
public Integer decode(PidDefinition pid, ConnectorResponse connectorResponse) {

log.info("Decoding the message: {}", connectorResponse.getMessage());

final Optional<String> answer = decodeRawMessage(getQuery(), connectorResponse);
if (answer.isPresent()) {
final Integer result = Integer.parseInt(answer.get(), 16);
log.info("Decoded message: {} for: {}", result, connectorResponse.getMessage());
return result;
public String decode(PidDefinition pid, ConnectorResponse connectorResponse) {
if (log.isDebugEnabled()) {
log.debug("Decoding the message: {}", connectorResponse.getMessage());
}
return null;

final String answer = connectorResponse.getRawValue(pid);
final Integer result = Integer.parseInt(answer, 16);
return result.toString();
}
}
39 changes: 13 additions & 26 deletions src/main/java/org/obd/metrics/executor/MetadataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,26 @@
import java.util.HashMap;
import java.util.Map;

import org.obd.metrics.api.model.Reply;
import org.obd.metrics.codec.Codec;
import org.obd.metrics.api.model.ObdMetric;
import org.obd.metrics.api.model.ReplyObserver;
import org.obd.metrics.command.Command;
import org.obd.metrics.pid.PIDsGroup;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
final class MetadataReader extends PIDsGroupReader<Map<String, String>> {

MetadataReader() {
super(PIDsGroup.METADATA);
value = new HashMap<String, String>();
}
final class MetadataReader extends ReplyObserver<ObdMetric> {
@Getter
private final Map<String,String> value = new HashMap<String, String>();

@Override
public void onNext(Reply<?> reply) {
final Command command = (Command) reply.getCommand();
log.debug("Recieved vehicle metadata: {}", reply);

if (command instanceof Codec<?>) {
final Object decode = ((Codec<?>) command).decode(reply.getRaw());
if (decode == null) {
value.put(command.getLabel(), reply.getRaw().getMessage());
} else {
if (decode instanceof Map) {
value.putAll((Map) decode);
} else {
value.put(command.getLabel(), decode.toString());
}
}
} else {
value.put(command.getLabel(), reply.getRaw().getMessage());
}
public void onNext(ObdMetric reply) {

if (reply.getCommand().getPid().getGroup() == PIDsGroup.METADATA) {
final Command command = (Command) reply.getCommand();
log.debug("Recieved vehicle metadata: {}", reply);
value.put(command.getLabel(), reply.getValue().toString());
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/org/obd/metrics/executor/PIDsGroupReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public List<Class<?>> subscribeFor() {
})
.filter(p -> p != null)
.collect(Collectors.toSet());

collect.add(group.getDefaultCommandClass());

if (group.getDefaultCommandClass() != null) {
collect.add(group.getDefaultCommandClass());
}
return new ArrayList<>(collect);
}
}
3 changes: 1 addition & 2 deletions src/main/java/org/obd/metrics/pid/PIDsGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
import org.obd.metrics.command.SupportedPIDsCommand;
import org.obd.metrics.command.dtc.DiagnosticTroubleCodeClearCommand;
import org.obd.metrics.command.dtc.DiagnosticTroubleCodeCommand;
import org.obd.metrics.command.meta.HexCommand;

import lombok.Getter;

public enum PIDsGroup {

LIVEDATA(null),
ROUTINE(null),
METADATA(HexCommand.class),
METADATA(null),
DTC_READ(DiagnosticTroubleCodeCommand.class),
DTC_CLEAR(DiagnosticTroubleCodeClearCommand.class),
CAPABILITES(SupportedPIDsCommand.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ default int[] getColonPositions() {
}

default String getRawValue(final PidDefinition pid) {
return getMessage().subSequence(pid.getSuccessCode().length(), remaining()).toString();
final String message = getMessage();
final int indexOf = message.indexOf(pid.getSuccessCode());
return indexOf >= 0 ?
message.subSequence(indexOf + pid.getSuccessCode().length(), remaining()).toString()
: null;
}

default boolean isResponseCodeSuccess(PidDefinition pidDefinition) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/resources/alfa.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"mode": "22",
"pid": "F1A5",
"description": "ECU ISO Code",
"commandClass": "org.obd.metrics.command.meta.NotEncodedCommand"
"codecClass": "org.obd.metrics.command.meta.NotEncodedCodec"
},

{
Expand Down Expand Up @@ -64,30 +64,30 @@
"mode": "22",
"pid": "F195",
"description": "Software version",
"commandClass": "org.obd.metrics.command.meta.NotEncodedCommand"
"codecClass": "org.obd.metrics.command.meta.NotEncodedCodec"
},
{
"id": "16009",
"mode": "22",
"pid": "F193",
"description": "Hardware version",
"commandClass": "org.obd.metrics.command.meta.NotEncodedCommand"
"codecClass": "org.obd.metrics.command.meta.NotEncodedCodec"
},
{
"id": "16011",
"mode": "22",
"pid": "2008",
"units": "min",
"description": "Functioning time (EEPROM)",
"commandClass": "org.obd.metrics.command.meta.TimeCommand"
"codecClass": "org.obd.metrics.command.meta.TimeCommand"
},
{
"id": "16012",
"mode": "22",
"pid": "1008",
"units": "min",
"description": "Operating time",
"commandClass": "org.obd.metrics.command.meta.TimeCommand"
"codecClass": "org.obd.metrics.command.meta.TimeCommand"
}
],

Expand Down
Loading

0 comments on commit 1345bd5

Please sign in to comment.