diff options
-rw-r--r-- | src/org/traccar/model/Command.java | 6 | ||||
-rw-r--r-- | src/org/traccar/protocol/TotemFrameDecoder.java | 18 | ||||
-rw-r--r-- | src/org/traccar/protocol/TotemProtocol.java | 8 | ||||
-rw-r--r-- | src/org/traccar/protocol/TotemProtocolEncoder.java | 40 | ||||
-rw-r--r-- | test/org/traccar/protocol/TotemProtocolEncoderTest.java | 46 |
5 files changed, 109 insertions, 9 deletions
diff --git a/src/org/traccar/model/Command.java b/src/org/traccar/model/Command.java index f7702dbd0..8c4e3f0f1 100644 --- a/src/org/traccar/model/Command.java +++ b/src/org/traccar/model/Command.java @@ -25,9 +25,11 @@ public class Command implements Factory { public static final String TYPE_POSITION_STOP = "positionStop"; public static final String TYPE_POSITION_FIX = "positionFix"; public static final String TYPE_ENGINE_STOP = "engineStop"; - public static final String TYPE_ENGINE_RESUME = "engineResume"; - + public static final String TYPE_ENGINE_RESUME = "engineResume"; + public static final String KEY_UNIQUE_ID = "uniqueId"; public static final String KEY_FREQUENCY = "frequency"; + public static final String KEY_GPS_PASSWORD = "gpsPassword"; + } diff --git a/src/org/traccar/protocol/TotemFrameDecoder.java b/src/org/traccar/protocol/TotemFrameDecoder.java index 5ac3064e6..6f7c3a37c 100644 --- a/src/org/traccar/protocol/TotemFrameDecoder.java +++ b/src/org/traccar/protocol/TotemFrameDecoder.java @@ -19,7 +19,7 @@ import java.nio.charset.Charset; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.handler.codec.frame.FrameDecoder; +import org.jboss.netty.handler.codec.frame.FrameDecoder; public class TotemFrameDecoder extends FrameDecoder { @@ -38,13 +38,17 @@ public class TotemFrameDecoder extends FrameDecoder { if (buf.getUnsignedShort(buf.readerIndex()) == 0x0d0a) { buf.skipBytes(2); } - - // Read message - int length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 2, Charset.defaultCharset()), 16); - if (length <= buf.readableBytes()) { - return buf.readBytes(length); + + if(buf.toString(buf.readerIndex(), 2, Charset.defaultCharset()).equals("$$")){ + int length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 2, Charset.defaultCharset()), 16); + if (length <= buf.readableBytes()) { + return buf.readBytes(length); + } + }else{ + //TODO: notify to user the GPS response + return buf.readBytes(buf.readableBytes()); } - + return null; } diff --git a/src/org/traccar/protocol/TotemProtocol.java b/src/org/traccar/protocol/TotemProtocol.java index cbc35a664..097f6a593 100644 --- a/src/org/traccar/protocol/TotemProtocol.java +++ b/src/org/traccar/protocol/TotemProtocol.java @@ -22,11 +22,17 @@ import org.traccar.BaseProtocol; import org.traccar.TrackerServer; import java.util.List; +import org.jboss.netty.handler.codec.string.StringEncoder; +import org.traccar.model.Command; public class TotemProtocol extends BaseProtocol { public TotemProtocol() { super("totem"); + setSupportedCommands( + Command.TYPE_ENGINE_RESUME, + Command.TYPE_ENGINE_STOP + ); } @Override @@ -36,7 +42,9 @@ public class TotemProtocol extends BaseProtocol { protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("frameDecoder", new TotemFrameDecoder()); pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectDecoder", new TotemProtocolDecoder(TotemProtocol.this)); + pipeline.addLast("objectEncoder", new TotemProtocolEncoder()); } }); } diff --git a/src/org/traccar/protocol/TotemProtocolEncoder.java b/src/org/traccar/protocol/TotemProtocolEncoder.java new file mode 100644 index 000000000..d236a7e2c --- /dev/null +++ b/src/org/traccar/protocol/TotemProtocolEncoder.java @@ -0,0 +1,40 @@ +/* + * Copyright 2015 alexis. + * + * 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.traccar.StringProtocolEncoder; +import org.traccar.model.Command; + +/** + * + * @author Irving Gonzalez [ialexis93@gmail.com] + */ +public class TotemProtocolEncoder extends StringProtocolEncoder{ + + @Override + protected Object encodeCommand(Command command) { + + switch (command.getType()) { + //Assuming PIN 8 (Output C) is the power wire, like manual says but it can be PIN 5,7,8 + case Command.TYPE_ENGINE_STOP: + return formatCommand(command, "*{%s},025,C,1#", Command.KEY_GPS_PASSWORD); + case Command.TYPE_ENGINE_RESUME: + return formatCommand(command, "*{%s},025,C,0#", Command.KEY_GPS_PASSWORD); + } + + return null; + } +} diff --git a/test/org/traccar/protocol/TotemProtocolEncoderTest.java b/test/org/traccar/protocol/TotemProtocolEncoderTest.java new file mode 100644 index 000000000..365173bd2 --- /dev/null +++ b/test/org/traccar/protocol/TotemProtocolEncoderTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2015 alexis. + * + * 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 java.util.HashMap; +import java.util.Map; +import org.junit.Assert; +import org.junit.Test; +import org.traccar.model.Command; + +/** + * + * @author alexis + */ +public class TotemProtocolEncoderTest { + @Test + public void testDecode() throws Exception { + + TotemProtocolEncoder encoder = new TotemProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(2); + command.setType(Command.TYPE_ENGINE_STOP); + + Map<String, Object> other = new HashMap<>(); + other.put(Command.KEY_GPS_PASSWORD, "000000"); + + command.setOther(other); + + Assert.assertEquals("*000000,025,C,1#", encoder.encodeCommand(command)); + + } +} |