From c1de08db7773eef6b1ed4a6e2b62f15d1df6a2a5 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Tue, 12 Feb 2019 03:45:44 +0200 Subject: fixed 8bit voice message --- src/org/traccar/protocol/WatchProtocol.java | 4 +++- src/org/traccar/protocol/WatchProtocolEncoder.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/WatchProtocol.java b/src/org/traccar/protocol/WatchProtocol.java index 5d923106d..07a146572 100644 --- a/src/org/traccar/protocol/WatchProtocol.java +++ b/src/org/traccar/protocol/WatchProtocol.java @@ -22,6 +22,8 @@ import org.traccar.model.Command; import io.netty.handler.codec.string.StringEncoder; +import java.nio.charset.StandardCharsets; + public class WatchProtocol extends BaseProtocol { public WatchProtocol() { @@ -45,7 +47,7 @@ public class WatchProtocol extends BaseProtocol { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast(new WatchFrameDecoder()); - pipeline.addLast(new StringEncoder()); + pipeline.addLast(new StringEncoder(StandardCharsets.ISO_8859_1)); pipeline.addLast(new WatchProtocolEncoder()); pipeline.addLast(new WatchProtocolDecoder(WatchProtocol.this)); } diff --git a/src/org/traccar/protocol/WatchProtocolEncoder.java b/src/org/traccar/protocol/WatchProtocolEncoder.java index 431b0455a..28b0b9407 100644 --- a/src/org/traccar/protocol/WatchProtocolEncoder.java +++ b/src/org/traccar/protocol/WatchProtocolEncoder.java @@ -109,7 +109,7 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin index += 1; } - return new String(encodedData, StandardCharsets.US_ASCII); + return new String(encodedData, StandardCharsets.ISO_8859_1); } @Override -- cgit v1.2.3 From 4b9aa8e7c98a3420a698b1520ee63060409c7d88 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 01:24:44 +0200 Subject: binary encoder + test --- src/org/traccar/protocol/WatchProtocolEncoder.java | 74 ++++++++++++---------- test/org/traccar/ProtocolTest.java | 8 +++ .../traccar/protocol/WatchProtocolEncoderTest.java | 14 ++-- 3 files changed, 56 insertions(+), 40 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/WatchProtocolEncoder.java b/src/org/traccar/protocol/WatchProtocolEncoder.java index 28b0b9407..c81d81f91 100644 --- a/src/org/traccar/protocol/WatchProtocolEncoder.java +++ b/src/org/traccar/protocol/WatchProtocolEncoder.java @@ -15,6 +15,8 @@ */ package org.traccar.protocol; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.StringProtocolEncoder; import org.traccar.helper.DataConverter; @@ -38,13 +40,21 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin return fmt.format(offset); } else if (key.equals(Command.KEY_MESSAGE)) { return DataConverter.printHex(value.toString().getBytes(StandardCharsets.UTF_16BE)); + } else if (key.equals(Command.KEY_ENABLE)) { + return (boolean) value ? "1" : "0"; } return null; } - protected String formatCommand(Channel channel, Command command, String format, String... keys) { + protected String formatTextCommand(Channel channel, Command command, String format, String... keys) { + String content = formatCommand(command, format, this, keys); + ByteBuf buf = Unpooled.copiedBuffer(content, StandardCharsets.US_ASCII); + + return formatBinaryCommand(channel, command, "", buf).toString(StandardCharsets.US_ASCII); + } + protected ByteBuf formatBinaryCommand(Channel channel, Command command, String textPrefix, ByteBuf data) { boolean hasIndex = false; String manufacturer = "CS"; if (channel != null) { @@ -55,23 +65,23 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin } } - String content = formatCommand(command, format, this, keys); - + ByteBuf buf = Unpooled.buffer(); + buf.writeByte('['); + buf.writeCharSequence(manufacturer, StandardCharsets.US_ASCII); + buf.writeByte('*'); + buf.writeCharSequence(getUniqueId(command.getDeviceId()), StandardCharsets.US_ASCII); + buf.writeByte('*'); if (hasIndex) { - return String.format("[%s*%s*0001*%04x*%s]", - manufacturer, getUniqueId(command.getDeviceId()), content.length(), content); - } else { - return String.format("[%s*%s*%04x*%s]", - manufacturer, getUniqueId(command.getDeviceId()), content.length(), content); + buf.writeCharSequence("0001", StandardCharsets.US_ASCII); + buf.writeByte('*'); } - } + buf.writeCharSequence(String.format("%04x", data.readableBytes() + textPrefix.length()), StandardCharsets.US_ASCII); + buf.writeByte('*'); + buf.writeCharSequence(textPrefix, StandardCharsets.US_ASCII); + buf.writeBytes(data); + buf.writeByte(']'); - private int getEnableFlag(Command command) { - if (command.getBoolean(Command.KEY_ENABLE)) { - return 1; - } else { - return 0; - } + return buf; } private static Map mapping = new HashMap<>(); @@ -84,7 +94,7 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin mapping.put((byte) 0x2A, (byte) 0x05); } - private String getBinaryData(Command command) { + private ByteBuf getBinaryData(Command command) { byte[] data = DataConverter.parseHex(command.getString(Command.KEY_DATA)); int encodedLength = data.length; @@ -109,7 +119,7 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin index += 1; } - return new String(encodedData, StandardCharsets.ISO_8859_1); + return Unpooled.copiedBuffer(encodedData); } @Override @@ -117,35 +127,35 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin switch (command.getType()) { case Command.TYPE_CUSTOM: - return formatCommand(channel, command, command.getString(Command.KEY_DATA)); + return formatTextCommand(channel, command, command.getString(Command.KEY_DATA)); case Command.TYPE_POSITION_SINGLE: - return formatCommand(channel, command, "RG"); + return formatTextCommand(channel, command, "RG"); case Command.TYPE_SOS_NUMBER: - return formatCommand(channel, command, "SOS{%s},{%s}", Command.KEY_INDEX, Command.KEY_PHONE); + return formatTextCommand(channel, command, "SOS{%s},{%s}", Command.KEY_INDEX, Command.KEY_PHONE); case Command.TYPE_ALARM_SOS: - return formatCommand(channel, command, "SOSSMS," + getEnableFlag(command)); + return formatTextCommand(channel, command, "SOSSMS,{%s}", Command.KEY_ENABLE); case Command.TYPE_ALARM_BATTERY: - return formatCommand(channel, command, "LOWBAT," + getEnableFlag(command)); + return formatTextCommand(channel, command, "LOWBAT,{%s}", Command.KEY_ENABLE); case Command.TYPE_REBOOT_DEVICE: - return formatCommand(channel, command, "RESET"); + return formatTextCommand(channel, command, "RESET"); case Command.TYPE_ALARM_REMOVE: - return formatCommand(channel, command, "REMOVE," + getEnableFlag(command)); + return formatTextCommand(channel, command, "REMOVE,{%s}", Command.KEY_ENABLE); case Command.TYPE_SILENCE_TIME: - return formatCommand(channel, command, "SILENCETIME,{%s}", Command.KEY_DATA); + return formatTextCommand(channel, command, "SILENCETIME,{%s}", Command.KEY_DATA); case Command.TYPE_ALARM_CLOCK: - return formatCommand(channel, command, "REMIND,{%s}", Command.KEY_DATA); + return formatTextCommand(channel, command, "REMIND,{%s}", Command.KEY_DATA); case Command.TYPE_SET_PHONEBOOK: - return formatCommand(channel, command, "PHB,{%s}", Command.KEY_DATA); + return formatTextCommand(channel, command, "PHB,{%s}", Command.KEY_DATA); case Command.TYPE_MESSAGE: - return formatCommand(channel, command, "MESSAGE,{%s}", Command.KEY_MESSAGE); + return formatTextCommand(channel, command, "MESSAGE,{%s}", Command.KEY_MESSAGE); case Command.TYPE_VOICE_MESSAGE: - return formatCommand(channel, command, "TK,%s", getBinaryData(command)); + return formatBinaryCommand(channel, command, "TK,", getBinaryData(command)); case Command.TYPE_POSITION_PERIODIC: - return formatCommand(channel, command, "UPLOAD,{%s}", Command.KEY_FREQUENCY); + return formatTextCommand(channel, command, "UPLOAD,{%s}", Command.KEY_FREQUENCY); case Command.TYPE_SET_TIMEZONE: - return formatCommand(channel, command, "LZ,,{%s}", Command.KEY_TIMEZONE); + return formatTextCommand(channel, command, "LZ,,{%s}", Command.KEY_TIMEZONE); case Command.TYPE_SET_INDICATOR: - return formatCommand(channel, command, "FLOWER,{%s}", Command.KEY_DATA); + return formatTextCommand(channel, command, "FLOWER,{%s}", Command.KEY_DATA); default: return null; } diff --git a/test/org/traccar/ProtocolTest.java b/test/org/traccar/ProtocolTest.java index 1b5c0ea00..181af885d 100644 --- a/test/org/traccar/ProtocolTest.java +++ b/test/org/traccar/ProtocolTest.java @@ -52,6 +52,14 @@ public class ProtocolTest extends BaseTest { return builder.toString(); } + protected ByteBuf concatenateBuffers(ByteBuf... buffers) { + ByteBuf result = Unpooled.buffer(); + for (ByteBuf buf : buffers) { + result.writeBytes(buf); + } + return result; + } + protected ByteBuf binary(String... data) { return Unpooled.wrappedBuffer(DataConverter.parseHex(concatenateStrings(data))); } diff --git a/test/org/traccar/protocol/WatchProtocolEncoderTest.java b/test/org/traccar/protocol/WatchProtocolEncoderTest.java index 05faa1f53..e73b5bb96 100644 --- a/test/org/traccar/protocol/WatchProtocolEncoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolEncoderTest.java @@ -4,8 +4,6 @@ import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; -import java.nio.charset.StandardCharsets; - import static org.junit.Assert.assertEquals; public class WatchProtocolEncoderTest extends ProtocolTest { @@ -33,25 +31,25 @@ public class WatchProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_VOICE_MESSAGE); command.set(Command.KEY_DATA, "3333"); - assertEquals("[CS*123456789012345*0005*TK,33]", encoder.encodeCommand(null, command)); + assertEquals(buffer("[CS*123456789012345*0005*TK,33]"), encoder.encodeCommand(null, command)); command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_VOICE_MESSAGE); command.set(Command.KEY_DATA, "2321414d520a2573"); - assertEquals("[CS*123456789012345*000b*TK,#!AMR\n%s]", encoder.encodeCommand(null, command)); + assertEquals(buffer("[CS*123456789012345*000b*TK,#!AMR\n%s]"), encoder.encodeCommand(null, command)); command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_VOICE_MESSAGE); - command.set(Command.KEY_DATA, "ff"); - assertEquals("[CS*123456789012345*0004*TK," + binary("ff").toString(StandardCharsets.ISO_8859_1) + ']', encoder.encodeCommand(null, command)); + command.set(Command.KEY_DATA, "7d5b5d2c2a"); + verifyFrame(concatenateBuffers(buffer("[CS*123456789012345*000d*TK,"), binary("7d017d027d037d047d05"), buffer("]")), encoder.encodeCommand(null, command)); command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_VOICE_MESSAGE); - command.set(Command.KEY_DATA, "7d5b5d2c2a"); - assertEquals("[CS*123456789012345*000d*TK," + binary("7d017d027d037d047d05").toString(StandardCharsets.ISO_8859_1) + ']', encoder.encodeCommand(null, command)); + command.set(Command.KEY_DATA, "ff"); + verifyFrame(concatenateBuffers(buffer("[CS*123456789012345*0004*TK,"), binary("ff"), buffer("]")), encoder.encodeCommand(null, command)); command = new Command(); command.setDeviceId(1); -- cgit v1.2.3 From 1e0e3a43894466246d6042ec71b9c01f63c58b13 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 01:28:00 +0200 Subject: binary decoder test --- src/org/traccar/protocol/WatchFrameDecoder.java | 6 ++++-- test/org/traccar/protocol/WatchFrameDecoderTest.java | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/WatchFrameDecoder.java b/src/org/traccar/protocol/WatchFrameDecoder.java index b2ac5b4f7..b4a82c0be 100644 --- a/src/org/traccar/protocol/WatchFrameDecoder.java +++ b/src/org/traccar/protocol/WatchFrameDecoder.java @@ -33,7 +33,8 @@ public class WatchFrameDecoder extends BaseFrameDecoder { while (buf.readerIndex() < endIndex) { byte b = buf.readByte(); if (b == '}') { - switch (buf.readByte()) { + byte c = buf.readByte(); + switch (c) { case 0x01: frame.writeByte('}'); break; @@ -50,7 +51,8 @@ public class WatchFrameDecoder extends BaseFrameDecoder { frame.writeByte('*'); break; default: - throw new IllegalArgumentException(); + throw new IllegalArgumentException(String.format( + "unexpected byte at %d: 0x%02x", buf.readerIndex() - 1, c)); } } else { frame.writeByte(b); diff --git a/test/org/traccar/protocol/WatchFrameDecoderTest.java b/test/org/traccar/protocol/WatchFrameDecoderTest.java index 741807de2..4e40eea86 100644 --- a/test/org/traccar/protocol/WatchFrameDecoderTest.java +++ b/test/org/traccar/protocol/WatchFrameDecoderTest.java @@ -26,6 +26,10 @@ public class WatchFrameDecoderTest extends ProtocolTest { binary("5b5a4a2a3031343131313030313335303330342a303033342a303030392a4c4b2c302c302c31395d"), decoder.decode(null, null, binary("5b5a4a2a3031343131313030313335303330342a303033342a303030392a4c4b2c302c302c31395d"))); + verifyFrame( + concatenateBuffers(buffer("[CS*1234567890*000e*TK,#!AMR"), binary("7d5b5d2c2aff"), buffer("]")), + decoder.decode(null, null, concatenateBuffers(buffer("[CS*1234567890*000e*TK,#!AMR"), binary("7d017d027d037d047d05ff"), buffer("]")))); + } } -- cgit v1.2.3 From 2e130b374fed3edc10b448120be0db3e388bfb28 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 01:33:47 +0200 Subject: don't save response + decoder test --- src/org/traccar/Context.java | 4 ++ src/org/traccar/protocol/WatchProtocolDecoder.java | 36 +++++++++++++----- .../traccar/protocol/WatchProtocolDecoderTest.java | 43 ++++++++++++++++++++++ 3 files changed, 73 insertions(+), 10 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 95d433795..b0db90416 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -516,6 +516,10 @@ public final class Context { identityManager = testIdentityManager; } + public static void initMediaManager(MediaManager mediaManager) { + Context.mediaManager = mediaManager; + } + public static BaseObjectManager getManager(Class clazz) { if (clazz.equals(Device.class)) { return (BaseObjectManager) deviceManager; diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index 78b190030..bf4d99a49 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -17,6 +17,8 @@ package org.traccar.protocol; import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; import org.traccar.DeviceSession; @@ -38,6 +40,8 @@ import java.util.regex.Pattern; public class WatchProtocolDecoder extends BaseProtocolDecoder { + private static final Logger LOGGER = LoggerFactory.getLogger(WatchProtocolDecoder.class); + public WatchProtocolDecoder(Protocol protocol) { super(protocol); } @@ -174,41 +178,45 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { return manufacturer; } + protected int nextIndexOf(ByteBuf buf, char delimiter) { + return buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) delimiter); + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; - buf.skipBytes(1); // header + buf.skipBytes(1); // '[' header manufacturer = buf.readSlice(2).toString(StandardCharsets.US_ASCII); - buf.skipBytes(1); // delimiter + buf.skipBytes(1); // '*' delimiter - int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + int idIndex = nextIndexOf(buf, '*'); String id = buf.readSlice(idIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { return null; } - buf.skipBytes(1); // delimiter + buf.skipBytes(1); // '*' delimiter String index = null; - int contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + int contentIndex = nextIndexOf(buf, '*'); if (contentIndex + 5 < buf.writerIndex() && buf.getByte(contentIndex + 5) == '*' && buf.toString(contentIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) { - int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); + int indexLength = nextIndexOf(buf, '*') - buf.readerIndex(); hasIndex = true; index = buf.readSlice(indexLength).toString(StandardCharsets.US_ASCII); - buf.skipBytes(1); // delimiter + buf.skipBytes(1); // '*' delimiter } buf.skipBytes(4); // length - buf.skipBytes(1); // delimiter + buf.skipBytes(1); // '*' delimiter - buf.writerIndex(buf.writerIndex() - 1); // ignore ending + buf.writerIndex(buf.writerIndex() - 1); // ']' ignore ending - contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); + contentIndex = nextIndexOf(buf, ','); if (contentIndex < 0) { contentIndex = buf.writerIndex(); } @@ -296,6 +304,14 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { } else if (type.equals("TK")) { + if (buf.readableBytes() == 1) { + byte result = buf.readByte(); + if (result != '1') { + LOGGER.error(type + "," + result); + } + return null; + } + Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index b7c8d83f9..c4e5bf61a 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -1,7 +1,17 @@ package org.traccar.protocol; +import io.netty.buffer.ByteBuf; import org.junit.Test; +import org.traccar.Context; import org.traccar.ProtocolTest; +import org.traccar.database.MediaManager; +import org.traccar.model.Position; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class WatchProtocolDecoderTest extends ProtocolTest { @@ -101,7 +111,40 @@ public class WatchProtocolDecoderTest extends ProtocolTest { verifyPosition(decoder, buffer( "[ZJ*014111001350304*0038*008a*UD,070318,021027,V,00.000000,N,000.000000,E,0,0,0,0,100,18,1000,50,00000000,4,255,460,0,9346,5223,42,9346,5214,20,9784,4083,11,9346,5221,5]")); + } + + @Test + public void testDecodeTK() throws Exception { + WatchProtocolDecoder decoder = new WatchProtocolDecoder(null); + verifyNull(decoder.decode(null, null, buffer("[CS*1234567890*0004*TK,1]"))); + + MockMediaManager mockMediaManager = new MockMediaManager("/tmp"); + Context.initMediaManager(mockMediaManager); + String hex = "7d5b5d2c2aff"; + Object decodedObject = decoder.decode(null, null, concatenateBuffers(buffer("[CS*1234567890*000e*TK,#!AMR"), binary(hex), buffer("]"))); + assertTrue("not a position", decodedObject instanceof Position); + Position position = (Position) decodedObject; + assertEquals("1234567890/mock.amr", position.getAttributes().get("audio")); + verifyFrame(concatenateBuffers(buffer("#!AMR"), binary(hex)), mockMediaManager.readFile("1234567890/mock.amr")); } + private static class MockMediaManager extends MediaManager { + Map files = new HashMap<>(); + + MockMediaManager(String path) { + super(path); + } + + @Override + public String writeFile(String uniqueId, ByteBuf buf, String extension) { + String fileName = uniqueId + "/mock." + extension; + files.put(fileName, buf); + return fileName; + } + + ByteBuf readFile(String fileName) { + return files.get(fileName); + } + } } -- cgit v1.2.3 From 2a0dff15ead0c5d0cf5865acf500b8ebebe2c8e2 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 01:37:53 +0200 Subject: fixed checkstyle --- src/org/traccar/protocol/WatchProtocolEncoder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/WatchProtocolEncoder.java b/src/org/traccar/protocol/WatchProtocolEncoder.java index c81d81f91..8048d30ff 100644 --- a/src/org/traccar/protocol/WatchProtocolEncoder.java +++ b/src/org/traccar/protocol/WatchProtocolEncoder.java @@ -75,7 +75,8 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin buf.writeCharSequence("0001", StandardCharsets.US_ASCII); buf.writeByte('*'); } - buf.writeCharSequence(String.format("%04x", data.readableBytes() + textPrefix.length()), StandardCharsets.US_ASCII); + buf.writeCharSequence(String.format("%04x", data.readableBytes() + textPrefix.length()), + StandardCharsets.US_ASCII); buf.writeByte('*'); buf.writeCharSequence(textPrefix, StandardCharsets.US_ASCII); buf.writeBytes(data); -- cgit v1.2.3 From 5fe7762b7f17d8d19e86ffb20a3adfc09d700f1a Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 03:38:36 +0200 Subject: removed StringEncoder --- src/org/traccar/protocol/WatchProtocol.java | 5 ----- src/org/traccar/protocol/WatchProtocolEncoder.java | 4 ++-- test/org/traccar/ProtocolTest.java | 4 ++++ .../traccar/protocol/WatchProtocolEncoderTest.java | 23 +++++++++------------- 4 files changed, 15 insertions(+), 21 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/WatchProtocol.java b/src/org/traccar/protocol/WatchProtocol.java index 07a146572..b8c990b84 100644 --- a/src/org/traccar/protocol/WatchProtocol.java +++ b/src/org/traccar/protocol/WatchProtocol.java @@ -20,10 +20,6 @@ import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; import org.traccar.model.Command; -import io.netty.handler.codec.string.StringEncoder; - -import java.nio.charset.StandardCharsets; - public class WatchProtocol extends BaseProtocol { public WatchProtocol() { @@ -47,7 +43,6 @@ public class WatchProtocol extends BaseProtocol { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast(new WatchFrameDecoder()); - pipeline.addLast(new StringEncoder(StandardCharsets.ISO_8859_1)); pipeline.addLast(new WatchProtocolEncoder()); pipeline.addLast(new WatchProtocolDecoder(WatchProtocol.this)); } diff --git a/src/org/traccar/protocol/WatchProtocolEncoder.java b/src/org/traccar/protocol/WatchProtocolEncoder.java index 8048d30ff..df29d83a9 100644 --- a/src/org/traccar/protocol/WatchProtocolEncoder.java +++ b/src/org/traccar/protocol/WatchProtocolEncoder.java @@ -47,11 +47,11 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin return null; } - protected String formatTextCommand(Channel channel, Command command, String format, String... keys) { + protected ByteBuf formatTextCommand(Channel channel, Command command, String format, String... keys) { String content = formatCommand(command, format, this, keys); ByteBuf buf = Unpooled.copiedBuffer(content, StandardCharsets.US_ASCII); - return formatBinaryCommand(channel, command, "", buf).toString(StandardCharsets.US_ASCII); + return formatBinaryCommand(channel, command, "", buf); } protected ByteBuf formatBinaryCommand(Channel channel, Command command, String textPrefix, ByteBuf data) { diff --git a/test/org/traccar/ProtocolTest.java b/test/org/traccar/ProtocolTest.java index 181af885d..377474556 100644 --- a/test/org/traccar/ProtocolTest.java +++ b/test/org/traccar/ProtocolTest.java @@ -68,6 +68,10 @@ public class ProtocolTest extends BaseTest { return concatenateStrings(data); } + protected String text(ByteBuf data) { + return data.toString(StandardCharsets.ISO_8859_1); + } + protected ByteBuf buffer(String... data) { return Unpooled.copiedBuffer(concatenateStrings(data), StandardCharsets.ISO_8859_1); } diff --git a/test/org/traccar/protocol/WatchProtocolEncoderTest.java b/test/org/traccar/protocol/WatchProtocolEncoderTest.java index e73b5bb96..649f5a10d 100644 --- a/test/org/traccar/protocol/WatchProtocolEncoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolEncoderTest.java @@ -1,5 +1,6 @@ package org.traccar.protocol; +import io.netty.buffer.ByteBuf; import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; @@ -18,20 +19,14 @@ public class WatchProtocolEncoderTest extends ProtocolTest { command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_REBOOT_DEVICE); - assertEquals("[CS*123456789012345*0005*RESET]", encoder.encodeCommand(null, command)); + assertEquals("[CS*123456789012345*0005*RESET]", text((ByteBuf)encoder.encodeCommand(null, command))); command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_SOS_NUMBER); command.set(Command.KEY_INDEX, 1); command.set(Command.KEY_PHONE, "123456789"); - assertEquals("[CS*123456789012345*000e*SOS1,123456789]", encoder.encodeCommand(null, command)); - - command = new Command(); - command.setDeviceId(1); - command.setType(Command.TYPE_VOICE_MESSAGE); - command.set(Command.KEY_DATA, "3333"); - assertEquals(buffer("[CS*123456789012345*0005*TK,33]"), encoder.encodeCommand(null, command)); + assertEquals("[CS*123456789012345*000e*SOS1,123456789]", text((ByteBuf)encoder.encodeCommand(null, command))); command = new Command(); command.setDeviceId(1); @@ -55,28 +50,28 @@ public class WatchProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_MESSAGE); command.set(Command.KEY_MESSAGE, "text"); - assertEquals("[CS*123456789012345*0018*MESSAGE,0074006500780074]", encoder.encodeCommand(null, command)); + assertEquals("[CS*123456789012345*0018*MESSAGE,0074006500780074]", text((ByteBuf)encoder.encodeCommand(null, command))); command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_CUSTOM); command.set(Command.KEY_DATA, "WORK,6-9,11-13,13-15,17-19"); - assertEquals("[CS*123456789012345*001a*WORK,6-9,11-13,13-15,17-19]", encoder.encodeCommand(null, command)); + assertEquals("[CS*123456789012345*001a*WORK,6-9,11-13,13-15,17-19]", text((ByteBuf)encoder.encodeCommand(null, command))); command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_SET_TIMEZONE); command.set(Command.KEY_TIMEZONE, "Europe/Amsterdam"); - assertEquals("[CS*123456789012345*0006*LZ,,+1]", encoder.encodeCommand(null, command)); + assertEquals("[CS*123456789012345*0006*LZ,,+1]", text((ByteBuf)encoder.encodeCommand(null, command))); command.set(Command.KEY_TIMEZONE, "GMT+01:30"); - assertEquals("[CS*123456789012345*0008*LZ,,+1.5]", encoder.encodeCommand(null, command)); + assertEquals("[CS*123456789012345*0008*LZ,,+1.5]", text((ByteBuf)encoder.encodeCommand(null, command))); command.set(Command.KEY_TIMEZONE, "Atlantic/Azores"); - assertEquals("[CS*123456789012345*0006*LZ,,-1]", encoder.encodeCommand(null, command)); + assertEquals("[CS*123456789012345*0006*LZ,,-1]", text((ByteBuf)encoder.encodeCommand(null, command))); command.set(Command.KEY_TIMEZONE, "GMT-11:30"); - assertEquals("[CS*123456789012345*0009*LZ,,-11.5]", encoder.encodeCommand(null, command)); + assertEquals("[CS*123456789012345*0009*LZ,,-11.5]", text((ByteBuf)encoder.encodeCommand(null, command))); } -- cgit v1.2.3 From c80d01e1646b8cb1a626f33bbd488d02f15335cc Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 04:53:22 +0200 Subject: fixed WatchProtocolDecoder response after removed StringEncoder --- src/org/traccar/protocol/WatchProtocolDecoder.java | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index bf4d99a49..aaf63206c 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -16,6 +16,7 @@ package org.traccar.protocol; import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,15 +67,18 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { .expression("(.*)") // cell and wifi .compile(); - private void sendResponse(Channel channel, String id, String index, String content) { + private void sendTextResponse(Channel channel, String id, String index, String content) { if (channel != null) { + String response; if (index != null) { - channel.writeAndFlush(new NetworkMessage(String.format("[%s*%s*%s*%04x*%s]", - manufacturer, id, index, content.length(), content), channel.remoteAddress())); + response = String.format("[%s*%s*%s*%04x*%s]", + manufacturer, id, index, content.length(), content); } else { - channel.writeAndFlush(new NetworkMessage(String.format("[%s*%s*%04x*%s]", - manufacturer, id, content.length(), content), channel.remoteAddress())); + response = String.format("[%s*%s*%04x*%s]", + manufacturer, id, content.length(), content); } + ByteBuf buf = Unpooled.copiedBuffer(response, StandardCharsets.US_ASCII); + channel.writeAndFlush(new NetworkMessage(buf, channel.remoteAddress())); } } @@ -229,11 +233,11 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (type.equals("INIT")) { - sendResponse(channel, id, index, "INIT,1"); + sendTextResponse(channel, id, index, "INIT,1"); } else if (type.equals("LK")) { - sendResponse(channel, id, index, "LK"); + sendTextResponse(channel, id, index, "LK"); if (buf.isReadable()) { String[] values = buf.toString(StandardCharsets.US_ASCII).split(","); @@ -258,14 +262,14 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (position != null) { position.set(Position.KEY_ALARM, Position.ALARM_SOS); } - sendResponse(channel, id, index, "AL"); + sendTextResponse(channel, id, index, "AL"); } return position; } else if (type.equals("TKQ")) { - sendResponse(channel, id, index, "TKQ"); + sendTextResponse(channel, id, index, "TKQ"); } else if (type.equals("PULSE") || type.equals("heart") || type.equals("bphrt")) { -- cgit v1.2.3 From 074b1c302dcd6671737d44c69aa83d0fdd179168 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 21:42:29 +0200 Subject: resolving review issues --- src/org/traccar/protocol/WatchFrameDecoder.java | 12 +++++------ src/org/traccar/protocol/WatchProtocolDecoder.java | 24 +++++++++------------- .../traccar/protocol/WatchProtocolDecoderTest.java | 2 +- 3 files changed, 17 insertions(+), 21 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/WatchFrameDecoder.java b/src/org/traccar/protocol/WatchFrameDecoder.java index b4a82c0be..f99bd52e2 100644 --- a/src/org/traccar/protocol/WatchFrameDecoder.java +++ b/src/org/traccar/protocol/WatchFrameDecoder.java @@ -31,10 +31,10 @@ public class WatchFrameDecoder extends BaseFrameDecoder { if (endIndex > 0) { ByteBuf frame = Unpooled.buffer(); while (buf.readerIndex() < endIndex) { - byte b = buf.readByte(); - if (b == '}') { - byte c = buf.readByte(); - switch (c) { + byte b1 = buf.readByte(); + if (b1 == '}') { + byte b2 = buf.readByte(); + switch (b2) { case 0x01: frame.writeByte('}'); break; @@ -52,10 +52,10 @@ public class WatchFrameDecoder extends BaseFrameDecoder { break; default: throw new IllegalArgumentException(String.format( - "unexpected byte at %d: 0x%02x", buf.readerIndex() - 1, c)); + "unexpected byte at %d: 0x%02x", buf.readerIndex() - 1, b2)); } } else { - frame.writeByte(b); + frame.writeByte(b1); } } return frame; diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index aaf63206c..70b207e9b 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -67,7 +67,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { .expression("(.*)") // cell and wifi .compile(); - private void sendTextResponse(Channel channel, String id, String index, String content) { + private void sendResponse(Channel channel, String id, String index, String content) { if (channel != null) { String response; if (index != null) { @@ -182,10 +182,6 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { return manufacturer; } - protected int nextIndexOf(ByteBuf buf, char delimiter) { - return buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) delimiter); - } - @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -196,7 +192,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { manufacturer = buf.readSlice(2).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // '*' delimiter - int idIndex = nextIndexOf(buf, '*'); + int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); String id = buf.readSlice(idIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { @@ -206,10 +202,10 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(1); // '*' delimiter String index = null; - int contentIndex = nextIndexOf(buf, '*'); + int contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); if (contentIndex + 5 < buf.writerIndex() && buf.getByte(contentIndex + 5) == '*' && buf.toString(contentIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) { - int indexLength = nextIndexOf(buf, '*') - buf.readerIndex(); + int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); hasIndex = true; index = buf.readSlice(indexLength).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // '*' delimiter @@ -220,7 +216,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { buf.writerIndex(buf.writerIndex() - 1); // ']' ignore ending - contentIndex = nextIndexOf(buf, ','); + contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); if (contentIndex < 0) { contentIndex = buf.writerIndex(); } @@ -233,11 +229,11 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (type.equals("INIT")) { - sendTextResponse(channel, id, index, "INIT,1"); + sendResponse(channel, id, index, "INIT,1"); } else if (type.equals("LK")) { - sendTextResponse(channel, id, index, "LK"); + sendResponse(channel, id, index, "LK"); if (buf.isReadable()) { String[] values = buf.toString(StandardCharsets.US_ASCII).split(","); @@ -262,14 +258,14 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (position != null) { position.set(Position.KEY_ALARM, Position.ALARM_SOS); } - sendTextResponse(channel, id, index, "AL"); + sendResponse(channel, id, index, "AL"); } return position; } else if (type.equals("TKQ")) { - sendTextResponse(channel, id, index, "TKQ"); + sendResponse(channel, id, index, "TKQ"); } else if (type.equals("PULSE") || type.equals("heart") || type.equals("bphrt")) { @@ -311,7 +307,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (buf.readableBytes() == 1) { byte result = buf.readByte(); if (result != '1') { - LOGGER.error(type + "," + result); + LOGGER.warn(type + "," + result); } return null; } diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index c4e5bf61a..f1287d721 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -114,7 +114,7 @@ public class WatchProtocolDecoderTest extends ProtocolTest { } @Test - public void testDecodeTK() throws Exception { + public void testDecodeVoiceMessage() throws Exception { WatchProtocolDecoder decoder = new WatchProtocolDecoder(null); verifyNull(decoder.decode(null, null, buffer("[CS*1234567890*0004*TK,1]"))); -- cgit v1.2.3 From c82be0541481955a0832948574244f31757f7067 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 21:53:54 +0200 Subject: moved MockMediaManager to BaseTest --- src/org/traccar/Context.java | 7 ++---- test/org/traccar/BaseTest.java | 29 ++++++++++++++++++++-- .../traccar/protocol/WatchProtocolDecoderTest.java | 23 +---------------- 3 files changed, 30 insertions(+), 29 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index b0db90416..d679842cd 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -508,16 +508,13 @@ public final class Context { Context.getConfig().getBoolean("event.overspeed.preferLowest")); } - public static void init(IdentityManager testIdentityManager) { + public static void init(IdentityManager testIdentityManager, MediaManager testMediaManager) { config = new Config(); objectMapper = new ObjectMapper(); objectMapper.registerModule(new JSR353Module()); client = ClientBuilder.newClient().register(new ObjectMapperContextResolver()); identityManager = testIdentityManager; - } - - public static void initMediaManager(MediaManager mediaManager) { - Context.mediaManager = mediaManager; + mediaManager = testMediaManager; } public static BaseObjectManager getManager(Class clazz) { diff --git a/test/org/traccar/BaseTest.java b/test/org/traccar/BaseTest.java index fb1b5de8f..0b2c616ce 100644 --- a/test/org/traccar/BaseTest.java +++ b/test/org/traccar/BaseTest.java @@ -1,9 +1,34 @@ package org.traccar; +import io.netty.buffer.ByteBuf; +import org.traccar.database.MediaManager; + +import java.util.HashMap; +import java.util.Map; + public class BaseTest { - + + public static class MockMediaManager extends MediaManager { + Map files = new HashMap<>(); + + MockMediaManager() { + super(""); + } + + @Override + public String writeFile(String uniqueId, ByteBuf buf, String extension) { + String fileName = uniqueId + "/mock." + extension; + files.put(fileName, buf); + return fileName; + } + + public ByteBuf readFile(String fileName) { + return files.get(fileName); + } + } + static { - Context.init(new TestIdentityManager()); + Context.init(new TestIdentityManager(), new MockMediaManager()); } } diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index f1287d721..51846f51a 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -119,32 +119,11 @@ public class WatchProtocolDecoderTest extends ProtocolTest { verifyNull(decoder.decode(null, null, buffer("[CS*1234567890*0004*TK,1]"))); - MockMediaManager mockMediaManager = new MockMediaManager("/tmp"); - Context.initMediaManager(mockMediaManager); String hex = "7d5b5d2c2aff"; Object decodedObject = decoder.decode(null, null, concatenateBuffers(buffer("[CS*1234567890*000e*TK,#!AMR"), binary(hex), buffer("]"))); assertTrue("not a position", decodedObject instanceof Position); Position position = (Position) decodedObject; assertEquals("1234567890/mock.amr", position.getAttributes().get("audio")); - verifyFrame(concatenateBuffers(buffer("#!AMR"), binary(hex)), mockMediaManager.readFile("1234567890/mock.amr")); - } - - private static class MockMediaManager extends MediaManager { - Map files = new HashMap<>(); - - MockMediaManager(String path) { - super(path); - } - - @Override - public String writeFile(String uniqueId, ByteBuf buf, String extension) { - String fileName = uniqueId + "/mock." + extension; - files.put(fileName, buf); - return fileName; - } - - ByteBuf readFile(String fileName) { - return files.get(fileName); - } + verifyFrame(concatenateBuffers(buffer("#!AMR"), binary(hex)), ((MockMediaManager) Context.getMediaManager()).readFile("1234567890/mock.amr")); } } -- cgit v1.2.3