From 5cbf84b30f9b55f7c199289782b28ac71e0b8776 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 2 Sep 2016 02:48:59 +1200 Subject: Implement custom command for Ruptela --- src/org/traccar/protocol/RuptelaProtocol.java | 6 ++- .../traccar/protocol/RuptelaProtocolDecoder.java | 5 +- .../traccar/protocol/RuptelaProtocolEncoder.java | 55 ++++++++++++++++++++++ .../protocol/RuptelaProtocolEncoderTest.java | 23 +++++++++ 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 src/org/traccar/protocol/RuptelaProtocolEncoder.java create mode 100644 test/org/traccar/protocol/RuptelaProtocolEncoderTest.java diff --git a/src/org/traccar/protocol/RuptelaProtocol.java b/src/org/traccar/protocol/RuptelaProtocol.java index d9ce4430b..d806c4c01 100644 --- a/src/org/traccar/protocol/RuptelaProtocol.java +++ b/src/org/traccar/protocol/RuptelaProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; import org.traccar.BaseProtocol; import org.traccar.TrackerServer; +import org.traccar.model.Command; import java.util.List; @@ -27,6 +28,8 @@ public class RuptelaProtocol extends BaseProtocol { public RuptelaProtocol() { super("ruptela"); + setSupportedCommands( + Command.TYPE_CUSTOM); } @Override @@ -35,6 +38,7 @@ public class RuptelaProtocol extends BaseProtocol { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 0, 2, 2, 0)); + pipeline.addLast("objectEncoder", new RuptelaProtocolEncoder()); pipeline.addLast("objectDecoder", new RuptelaProtocolDecoder(RuptelaProtocol.this)); } }); diff --git a/src/org/traccar/protocol/RuptelaProtocolDecoder.java b/src/org/traccar/protocol/RuptelaProtocolDecoder.java index a9cea8c64..224c027f3 100644 --- a/src/org/traccar/protocol/RuptelaProtocolDecoder.java +++ b/src/org/traccar/protocol/RuptelaProtocolDecoder.java @@ -34,7 +34,8 @@ public class RuptelaProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final int COMMAND_RECORDS = 0x01; + public static final int MSG_RECORDS = 1; + public static final int MSG_SMS_VIA_GPRS = 108; @Override protected Object decode( @@ -52,7 +53,7 @@ public class RuptelaProtocolDecoder extends BaseProtocolDecoder { int type = buf.readUnsignedByte(); - if (type == COMMAND_RECORDS) { + if (type == MSG_RECORDS) { List positions = new LinkedList<>(); buf.readUnsignedByte(); // records left diff --git a/src/org/traccar/protocol/RuptelaProtocolEncoder.java b/src/org/traccar/protocol/RuptelaProtocolEncoder.java new file mode 100644 index 000000000..c036870d9 --- /dev/null +++ b/src/org/traccar/protocol/RuptelaProtocolEncoder.java @@ -0,0 +1,55 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.traccar.BaseProtocolEncoder; +import org.traccar.helper.Checksum; +import org.traccar.helper.Log; +import org.traccar.model.Command; + +import java.nio.charset.StandardCharsets; + +public class RuptelaProtocolEncoder extends BaseProtocolEncoder { + + private ChannelBuffer encodeContent(String content) { + + ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); + + buf.writeShort(1 + content.length()); + buf.writeByte(RuptelaProtocolDecoder.MSG_SMS_VIA_GPRS); + buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII)); + buf.writeShort(Checksum.crc16(Checksum.CRC16_KERMIT, buf.toByteBuffer(2, buf.writerIndex() - 2))); + + return buf; + } + + @Override + protected Object encodeCommand(Command command) { + + switch (command.getType()) { + case Command.TYPE_CUSTOM: + return encodeContent((String) command.getAttributes().get(Command.KEY_DATA)); + default: + Log.warning(new UnsupportedOperationException(command.getType())); + break; + } + + return null; + } + +} diff --git a/test/org/traccar/protocol/RuptelaProtocolEncoderTest.java b/test/org/traccar/protocol/RuptelaProtocolEncoderTest.java new file mode 100644 index 000000000..8a00caa09 --- /dev/null +++ b/test/org/traccar/protocol/RuptelaProtocolEncoderTest.java @@ -0,0 +1,23 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; +import org.traccar.model.Command; + +public class RuptelaProtocolEncoderTest extends ProtocolTest { + + @Test + public void testEncode() throws Exception { + + RuptelaProtocolEncoder encoder = new RuptelaProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_CUSTOM); + command.set(Command.KEY_DATA, " Setio 2,1"); + + verifyCommand(encoder, command, binary("000b6c20536574696F20322C31eb3e")); + + } + +} -- cgit v1.2.3