From 050941422cc6fe9581ac87f8cbd7196ed304ede8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 25 Jan 2021 22:46:13 -0800 Subject: Alternative identifier method --- .../java/org/traccar/protocol/KhdProtocolDecoder.java | 15 +++++++++++---- .../java/org/traccar/protocol/KhdProtocolDecoderTest.java | 3 +++ 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main/java/org/traccar/protocol/KhdProtocolDecoder.java b/src/main/java/org/traccar/protocol/KhdProtocolDecoder.java index 343b78cb5..16ad616ed 100644 --- a/src/main/java/org/traccar/protocol/KhdProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/KhdProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2014 - 2021 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.traccar.protocol; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; @@ -38,12 +39,18 @@ public class KhdProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private String readSerialNumber(ByteBuf buf) { + private String[] readIdentifiers(ByteBuf buf) { + String[] identifiers = new String[2]; + + identifiers[0] = ByteBufUtil.hexDump(buf, buf.readerIndex(), 4); + int b1 = buf.readUnsignedByte(); int b2 = buf.readUnsignedByte() - 0x80; int b3 = buf.readUnsignedByte() - 0x80; int b4 = buf.readUnsignedByte(); - return String.format("%02d%02d%02d%02d", b1, b2, b3, b4); + identifiers[1] = String.format("%02d%02d%02d%02d", b1, b2, b3, b4); + + return identifiers; } public static final int MSG_LOGIN = 0xB1; @@ -93,7 +100,7 @@ public class KhdProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(getProtocolName()); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readSerialNumber(buf)); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readIdentifiers(buf)); if (deviceSession == null) { return null; } diff --git a/src/test/java/org/traccar/protocol/KhdProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/KhdProtocolDecoderTest.java index aa80c1cd9..b82a277b3 100644 --- a/src/test/java/org/traccar/protocol/KhdProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/KhdProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class KhdProtocolDecoderTest extends ProtocolTest { KhdProtocolDecoder decoder = new KhdProtocolDecoder(null); + verifyPosition(decoder, binary( + "292980002825863156210105095059035109370460010100000211ffff000002fc0000001e780b12000034e70d")); + verifyPosition(decoder, binary( "2929a3003420b2ab46201115115601800115110350825100000133fb00df4bfdff0d000000000000000900000c180887d9ffffffffffff960d")); -- cgit v1.2.3