aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-11-17 11:36:08 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-11-17 11:36:08 +1300
commit3471e62b0f3949621265c80dcaf2ba714aade697 (patch)
tree8a50821821640def358e6fe7ca4a401327058357
parente4f936e9732f5ad2e99553425d8c5c9222e486f2 (diff)
downloadtraccar-server-3471e62b0f3949621265c80dcaf2ba714aade697.tar.gz
traccar-server-3471e62b0f3949621265c80dcaf2ba714aade697.tar.bz2
traccar-server-3471e62b0f3949621265c80dcaf2ba714aade697.zip
Finish Huabao protocol implementation
-rw-r--r--src/org/traccar/MainEventHandler.java1
-rw-r--r--src/org/traccar/protocol/HuabaoProtocolDecoder.java113
-rw-r--r--test/org/traccar/protocol/HuabaoProtocolDecoderTest.java9
3 files changed, 118 insertions, 5 deletions
diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java
index fa908a70c..f0ef36e5b 100644
--- a/src/org/traccar/MainEventHandler.java
+++ b/src/org/traccar/MainEventHandler.java
@@ -21,7 +21,6 @@ import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.socket.DatagramChannel;
-import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
import org.jboss.netty.handler.timeout.IdleStateEvent;
import org.traccar.helper.Log;
diff --git a/src/org/traccar/protocol/HuabaoProtocolDecoder.java b/src/org/traccar/protocol/HuabaoProtocolDecoder.java
index 931f985a5..792c89c8b 100644
--- a/src/org/traccar/protocol/HuabaoProtocolDecoder.java
+++ b/src/org/traccar/protocol/HuabaoProtocolDecoder.java
@@ -16,10 +16,20 @@
package org.traccar.protocol;
import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
+import org.traccar.helper.BitUtil;
+import org.traccar.helper.ChannelBufferTools;
+import org.traccar.helper.Checksum;
+import org.traccar.helper.DateBuilder;
+import org.traccar.helper.UnitsConverter;
+import org.traccar.model.Event;
+import org.traccar.model.Position;
import java.net.SocketAddress;
+import java.nio.charset.Charset;
+import java.util.TimeZone;
public class HuabaoProtocolDecoder extends BaseProtocolDecoder {
@@ -27,7 +37,38 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
+ public static final int MSG_GENERAL_RESPONSE = 0x8001;
public static final int MSG_TERMINAL_REGISTER = 0x0100;
+ public static final int MSG_TERMINAL_REGISTER_RESPONSE = 0x8100;
+ public static final int MSG_TERMINAL_AUTH = 0x0102;
+ public static final int MSG_LOCATION_REPORT = 0x0200;
+
+ public static final int RESULT_SUCCESS = 0;
+
+ private void sendResponse(
+ Channel channel, SocketAddress remoteAddress, int type, ChannelBuffer id, ChannelBuffer data) {
+ ChannelBuffer response = ChannelBuffers.dynamicBuffer();
+ response.writeByte(0x7e);
+ response.writeShort(type);
+ response.writeShort(data.readableBytes());
+ response.writeBytes(id);
+ response.writeShort(1); // index
+ response.writeBytes(data);
+ response.writeByte(Checksum.xor(response.toByteBuffer(1, response.readableBytes() - 1)));
+ response.writeByte(0x7e);
+ if (channel != null) {
+ channel.write(response, remoteAddress);
+ }
+ }
+
+ private void sendGeneralResponse(
+ Channel channel, SocketAddress remoteAddress, ChannelBuffer id, int type, int index) {
+ ChannelBuffer response = ChannelBuffers.dynamicBuffer();
+ response.writeShort(index);
+ response.writeShort(type);
+ response.writeByte(RESULT_SUCCESS);
+ sendResponse(channel, remoteAddress, MSG_GENERAL_RESPONSE, id, response);
+ }
@Override
protected Object decode(
@@ -36,10 +77,74 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder {
ChannelBuffer buf = (ChannelBuffer) msg;
buf.readUnsignedByte(); // start marker
- //int type = buf.readUnsignedShort();
- //int flags = buf.readUnsignedShort();
- buf.skipBytes(6); // phone number
- buf.readUnsignedShort(); // index
+ int type = buf.readUnsignedShort();
+ buf.readUnsignedShort(); // body length
+ ChannelBuffer id = buf.readBytes(6); // phone number
+ int index = buf.readUnsignedShort();
+
+ if (!identify(id.toString(Charset.defaultCharset()), channel, remoteAddress)) {
+ return null;
+ }
+
+ if (type == MSG_TERMINAL_REGISTER) {
+
+ ChannelBuffer response = ChannelBuffers.dynamicBuffer();
+ response.writeShort(index);
+ response.writeByte(RESULT_SUCCESS);
+ response.writeBytes("authentication".getBytes(Charset.defaultCharset()));
+ sendResponse(channel, remoteAddress, MSG_TERMINAL_REGISTER_RESPONSE, id, response);
+
+ } else if (type == MSG_TERMINAL_AUTH) {
+
+ sendGeneralResponse(channel, remoteAddress, id, type, index);
+
+ } else if (type == MSG_LOCATION_REPORT) {
+
+ Position position = new Position();
+ position.setProtocol(getProtocolName());
+ position.setDeviceId(getDeviceId());
+
+ position.set(Event.KEY_ALARM, buf.readUnsignedInt());
+
+ int flags = buf.readInt();
+
+ position.set(Event.KEY_IGNITION, BitUtil.check(flags, 0));
+
+ position.setValid(BitUtil.check(flags, 1));
+
+ double lat = buf.readUnsignedInt() * 0.000001;
+ double lon = buf.readUnsignedInt() * 0.000001;
+
+ if (BitUtil.check(flags, 2)) {
+ position.setLatitude(-lat);
+ } else {
+ position.setLatitude(lat);
+ }
+
+ if (BitUtil.check(flags, 3)) {
+ position.setLongitude(-lon);
+ } else {
+ position.setLongitude(lon);
+ }
+
+ position.setAltitude(buf.readShort());
+ position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort() * 0.1));
+ position.setCourse(buf.readUnsignedShort());
+
+ DateBuilder dateBuilder = new DateBuilder(TimeZone.getTimeZone("GMT+8"))
+ .setYear(ChannelBufferTools.readHexInteger(buf, 2))
+ .setMonth(ChannelBufferTools.readHexInteger(buf, 2))
+ .setDay(ChannelBufferTools.readHexInteger(buf, 2))
+ .setHour(ChannelBufferTools.readHexInteger(buf, 2))
+ .setMinute(ChannelBufferTools.readHexInteger(buf, 2))
+ .setSecond(ChannelBufferTools.readHexInteger(buf, 2));
+ position.setTime(dateBuilder.getDate());
+
+ // additional information
+
+ return position;
+
+ }
return null;
}
diff --git a/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java b/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java
index 0968d6d9c..1e2290851 100644
--- a/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java
@@ -11,8 +11,17 @@ public class HuabaoProtocolDecoderTest extends ProtocolDecoderTest {
HuabaoProtocolDecoder decoder = new HuabaoProtocolDecoder(new HuabaoProtocol());
verifyNothing(decoder, binary(
+ "7E0100002D013511221122000500000000373031303748422D52303347424400000000000000000000003233363631303402CBD5424136383630387E"));
+
+ verifyNothing(decoder, binary(
"7e0100002d007089994489002800000000000000000048422d523033474244000000000000000000000031393036373531024142433030303030d17e"));
+ verifyNothing(decoder, binary(
+ "7E0102000E013511221122000661757468656E7469636174696F6E3F7E"));
+
+ verifyPosition(decoder, binary(
+ "7E02000032013511221122000700000000000C000301578CC006CA3A5C00470000000014072317201501040000000030011631010BD07E"));
+
}
}