aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/protocol/T800xProtocol.java2
-rw-r--r--src/org/traccar/protocol/T800xProtocolDecoder.java349
-rw-r--r--test/org/traccar/protocol/T800xProtocolDecoderTest.java34
3 files changed, 134 insertions, 251 deletions
diff --git a/src/org/traccar/protocol/T800xProtocol.java b/src/org/traccar/protocol/T800xProtocol.java
index 599123df2..f98dfc943 100644
--- a/src/org/traccar/protocol/T800xProtocol.java
+++ b/src/org/traccar/protocol/T800xProtocol.java
@@ -17,6 +17,7 @@ package org.traccar.protocol;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
+import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
import org.traccar.BaseProtocol;
import org.traccar.TrackerServer;
@@ -33,6 +34,7 @@ public class T800xProtocol extends BaseProtocol {
serverList.add(new TrackerServer(new ServerBootstrap(), getName()) {
@Override
protected void addSpecificHandlers(ChannelPipeline pipeline) {
+ pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 3, 2, -5, 0));
pipeline.addLast("objectDecoder", new T800xProtocolDecoder(T800xProtocol.this));
}
});
diff --git a/src/org/traccar/protocol/T800xProtocolDecoder.java b/src/org/traccar/protocol/T800xProtocolDecoder.java
index 654d52eda..ab62110ba 100644
--- a/src/org/traccar/protocol/T800xProtocolDecoder.java
+++ b/src/org/traccar/protocol/T800xProtocolDecoder.java
@@ -1,19 +1,33 @@
+/*
+ * Copyright 2015 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.jboss.netty.channel.Channel;
-import org.jboss.netty.channel.ChannelHandlerContext;
import org.traccar.BaseProtocolDecoder;
-import org.traccar.database.DataManager;
+import org.traccar.helper.BitUtil;
import org.traccar.helper.ChannelBufferTools;
-import org.traccar.helper.Log;
+import org.traccar.helper.DateBuilder;
+import org.traccar.helper.UnitsConverter;
+import org.traccar.model.Event;
import org.traccar.model.Position;
-import javax.xml.bind.DatatypeConverter;
import java.net.SocketAddress;
-import java.text.SimpleDateFormat;
-import java.util.*;
+import java.nio.ByteOrder;
public class T800xProtocolDecoder extends BaseProtocolDecoder {
@@ -21,33 +35,26 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
- SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
-
private static final int MSG_LOGIN = 0x01;
private static final int MSG_GPS = 0x02;
private static final int MSG_HEARTBEAT = 0x03;
private static final int MSG_ALARM = 0x04;
- private static final int MSG_STATE = 0x05;
- private static final int MSG_SMS = 0x06;
- private static final int MSG_OBD = 0x07;
- private static final int MSG_INTERACTIVE = 0x80;
- private static final int MSG_DATA = 0x81;
- private Long deviceId;
+ private static float readSwappedFloat(ChannelBuffer buf) {
+ byte[] bytes = new byte[4];
+ buf.readBytes(bytes);
+ return ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, bytes).readFloat();
+ }
- private void sendLoginResponse(Channel channel, int type, int index, String imei) {
+ private void sendResponse(Channel channel, int type, ChannelBuffer imei) {
if (channel != null) {
- int length=7+(imei.length()/2+1);
- ChannelBuffer response = ChannelBuffers.buffer(length);
+ ChannelBuffer response = ChannelBuffers.directBuffer(15);
response.writeByte(0x23);
response.writeByte(0x23); // header
response.writeByte(type);
- response.writeByte(0x00);
- response.writeByte(length); // length
- response.writeShort(0x0001);//imei
- //IMEI
- response.writeBytes(DatatypeConverter.parseHexBinary("0" + imei));
+ response.writeShort(response.capacity()); // length
+ response.writeShort(0x0001); // index
+ response.writeBytes(imei);
channel.write(response);
}
}
@@ -58,226 +65,98 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder {
ChannelBuffer buf = (ChannelBuffer) msg;
- List<Position> positions=new ArrayList<Position>();
- //List<Status> statusList=new ArrayList<Status>();
- int i=0;
- while (buf.readable()){
- i++;
- buf.skipBytes(2); // header
- int type = buf.readUnsignedByte(); // type
- buf.readShort(); // length
- int index = buf.readUnsignedShort(); // index
- String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1); // imei
-
- if (type == MSG_LOGIN) {
- if (identify(imei, channel)) {
- sendLoginResponse(channel, type, index, imei); // send login response
- }
- return null;
- } else if (deviceId != null &&
- (type == MSG_GPS || type == MSG_ALARM)) {
-
- //ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter(getProtocol());
- //extendedInfo.set("index", index);
-
- //ACC ON Interval TODO
- buf.skipBytes(2);
- //Acc OFF Interval TODO
- buf.skipBytes(2);
-
- //Angel Degree TODO
- buf.skipBytes(1);
-
- //Angel Distance TODO
- buf.skipBytes(2);
-
- //Speed Alarm TODO
- buf.skipBytes(2);
-
- //GPS Status TODO
- String locationType = getBits(buf.readUnsignedByte());
-
- //Gsensor Manager TODO
- buf.skipBytes(1);
-
-
- //Reserve Bit
- buf.skipBytes(1);
-
- //HeartBit
- buf.skipBytes(1);
-
- //Relay Status
- buf.skipBytes(1);
-
- //Drag Alarm Setting
- buf.skipBytes(2);
-
- // Digital I/O Status
- String binaryDigitalIO = getBits(buf.readUnsignedByte());
- //extendedInfo.set("acc", binaryDigitalIO.charAt(1));
- //extendedInfo.set("ac", binaryDigitalIO.charAt(2));
- buf.skipBytes(1);
-
- // 2 Analog Input
- buf.skipBytes(4);
-
- // Alarm Data
- String alarmData = ChannelBuffers.hexDump(buf.readBytes(1));
- String statusType=getStatusType(alarmData);
- //extendedInfo.set(statusType,true);
- if(type == MSG_ALARM)
- {
- Log.debug("ALARM : "+statusType);
- if(alarmData.equals("08") || alarmData.equals("10")){
- sendAlarmPacketAfter16Sec(channel,type,index, imei + alarmData);
- }else{
- sendLoginResponse(channel, type, index, imei + alarmData);
- }
- }
-
- // Reserve
- buf.skipBytes(1);
-
- // Mileage
- buf.skipBytes(4);
-
-
- // Inner Battery Voltage
- double batteryVoltage = Double.parseDouble(ChannelBuffers.hexDump(buf.readBytes(1)));
- //extendedInfo.set("power", getBatteryPerc(batteryVoltage));
-
- // Time & Date Calculation
- Calendar time = Calendar.getInstance();
- time.clear();
- time.set(Calendar.YEAR, 2000 + Integer.parseInt(ChannelBuffers.hexDump(buf.readBytes(1))));
- time.set(Calendar.MONTH, (Integer.parseInt(ChannelBuffers.hexDump(buf.readBytes(1))) - 1));
- time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(ChannelBuffers.hexDump(buf.readBytes(1))));
- time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(ChannelBuffers.hexDump(buf.readBytes(1))));
- time.set(Calendar.MINUTE, Integer.parseInt(ChannelBuffers.hexDump(buf.readBytes(1))));
- time.set(Calendar.SECOND, Integer.parseInt(ChannelBuffers.hexDump(buf.readBytes(1))));
-
- if (locationType.charAt(1) == '1') {
- Log.debug("GPS DATA");
- Position position = new Position();
- position.setDeviceId(deviceId);
- position.setTime(time.getTime());
-
- // Read Altitude
- byte[] altitude = new byte[]{buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte()};
- position.setAltitude((double) bytes2Float(altitude, 0));
-
- // Read Longitude & Latitude
- byte[] longitude = new byte[]{buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte()};
- byte[] latitude = new byte[]{buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte()};
- position.setLongitude((double) bytes2Float(longitude, 0));
- position.setLatitude((double) bytes2Float(latitude, 0));
-
- // Read Speed
- String str_speed = ChannelBuffers.hexDump(buf.readBytes(2));
- String result = str_speed.substring(0, 3) + "." + str_speed.substring(3);
- position.setSpeed(Double.parseDouble(result) * 0.539957);
-
- //position.setExtendedInfo(extendedInfo.toString());
- positions.add(position);
-
- buf.skipBytes(2);
- }
- else {
- Log.debug("LBS DATA");
- if(type == MSG_ALARM)
- {
- /*Status status = new Status();
- status.setDate(time.getTime());
- status.setDeviceid(deviceId);
- status.setVoltage(getBatteryPerc(batteryVoltage));
- status.setStatusType(statusType);
- statusList.add(status);*/
- }
- buf.skipBytes(16);
- }
- } else if (deviceId != null &&
- (type == MSG_HEARTBEAT)) {
- dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
- /*Status status = new Status();
- status.setDate(dateFormatLocal.parse(dateFormatGmt.format(new Date())));
- status.setDeviceid(deviceId);
- statusList.add(status);*/
- sendLoginResponse(channel, type, index, imei);
- }
- }
-
- /*MixPacket mixPacket=new MixPacket();
- mixPacket.setPositions(positions);
- mixPacket.setStatus(statusList);*/
- return positions;
- }
+ buf.skipBytes(2);
+ int type = buf.readUnsignedByte();
+ buf.readUnsignedShort(); // length
+ int index = buf.readUnsignedShort();
+ ChannelBuffer imei = buf.readBytes(8);
+
+ if (!identify(ChannelBuffers.hexDump(imei).substring(1), channel, remoteAddress)) {
+ return null;
+ }
+
+ if (type == MSG_LOGIN || type == MSG_ALARM || type == MSG_HEARTBEAT) {
+ sendResponse(channel, type, imei);
+ }
+
+ if (type == MSG_GPS || type == MSG_ALARM) {
+
+ Position position = new Position();
+ position.setProtocol(getProtocolName());
+ position.setDeviceId(getDeviceId());
+
+ position.set(Event.KEY_INDEX, index);
+
+ buf.readUnsignedShort(); // acc on interval
+ buf.readUnsignedShort(); // acc off interval
+ buf.readUnsignedByte(); // angle compensation
+ buf.readUnsignedShort(); // distance compensation
+ buf.readUnsignedShort(); // speed alarm
+
+ int locationStatus = buf.readUnsignedByte();
+
+ buf.readUnsignedByte(); // gsensor manager status
+ buf.readUnsignedByte(); // other flags
+ buf.readUnsignedByte(); // heartbeat
+ buf.readUnsignedByte(); // relay status
+ buf.readUnsignedShort(); // drag alarm setting
+
+ int io = buf.readUnsignedShort();
+ position.set(Event.KEY_IGNITION, BitUtil.check(io, 14));
+ position.set("ac", BitUtil.check(io, 13));
+
+ position.set(Event.PREFIX_ADC + 1, buf.readUnsignedShort());
+ position.set(Event.PREFIX_ADC + 2, buf.readUnsignedShort());
- private void sendAlarmPacketAfter16Sec(final Channel channel,final int type,final int index,final String alarmData) {
- new Timer().schedule(new TimerTask() {
- @Override
- public void run() {
- sendLoginResponse(channel, type, index,alarmData);
+ position.set(Event.KEY_ALARM, buf.readUnsignedByte());
+
+ buf.readUnsignedByte(); // reserved
+
+ position.set(Event.KEY_ODOMETER, buf.readUnsignedInt());
+
+ int battery = ChannelBufferTools.readHexInteger(buf, 2);
+ if (battery == 0) {
+ battery = 100;
}
- }, 16001);
- }
+ position.set(Event.KEY_BATTERY, battery);
+ DateBuilder dateBuilder = new DateBuilder()
+ .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));
- private static String getBits(int flags) {
- int hexFlag = Integer.parseInt(Integer.toHexString(flags), 16);
- String binaryAlarm = Integer.toBinaryString(hexFlag);
- binaryAlarm = (String.format("%8s", binaryAlarm)).replace(' ', '0');
- return binaryAlarm;
- }
+ if (BitUtil.check(locationStatus, 6)) {
- public static float bytes2Float(byte[] bytes, int offset) {
- int value;
- value = bytes[offset];
- value &= 0xff;
- value |= ((long) bytes[offset + 1] << 8);
- value &= 0xffff;
- value |= ((long) bytes[offset + 2] << 16);
- value &= 0xffffff;
- value |= ((long) bytes[offset + 3] << 24);
-
- return Float.intBitsToFloat(value);
- }
+ position.setValid(!BitUtil.check(locationStatus, 7));
+ position.setTime(dateBuilder.getDate());
+ position.setAltitude(readSwappedFloat(buf));
+ position.setLongitude(readSwappedFloat(buf));
+ position.setLatitude(readSwappedFloat(buf));
+ position.setSpeed(UnitsConverter.knotsFromKph(
+ ChannelBufferTools.readHexInteger(buf, 4) * 0.1));
+ position.setCourse(buf.readUnsignedShort());
- public static int getBatteryPerc(double batteryPercentage) {
- if (batteryPercentage >= 1 && batteryPercentage < 6) {
- return 0;
- } else if (batteryPercentage > 5 && batteryPercentage < 11) {
- return 1;
- } else if (batteryPercentage > 10 && batteryPercentage < 21) {
- return 2;
- } else if (batteryPercentage > 20 && batteryPercentage < 36) {
- return 3;
- } else if (batteryPercentage > 35 && batteryPercentage < 71) {
- return 4;
- } else if (batteryPercentage > 70 && batteryPercentage < 96) {
- return 5;
- } else if (batteryPercentage > 95 && batteryPercentage < 101 || batteryPercentage == 0) {
- return 6;
- }
- return -1;
- }
+ } else {
+
+ getLastLocation(position, dateBuilder.getDate());
+
+ position.set(Event.KEY_MCC, buf.readUnsignedShort());
+ position.set(Event.KEY_MNC, buf.readUnsignedShort());
+ position.set(Event.KEY_LAC, buf.readUnsignedShort());
+ position.set(Event.KEY_CELL, buf.readUnsignedShort());
+
+ // two more cell towers
+
+ }
+
+ return position;
- private String getStatusType(String alarmCode) {
- String alarmCodeString = "";
- if (alarmCode.equals("02")) {
- alarmCodeString = "LBA";
- }else if (alarmCode.equals("03")) {
- alarmCodeString = "SOS";
- }else if (alarmCode.equals("08") || alarmCode.equals("10") ) {
- alarmCodeString = "VIBALM";
- } else if (alarmCode.equals("16")) {
- alarmCodeString = "PWROFF";
- } else if (alarmCode.equals("15")) {
- alarmCodeString = "PWRON";
- } else {
- alarmCodeString = "NDFND";
}
- return alarmCodeString;
+
+ return null;
}
}
diff --git a/test/org/traccar/protocol/T800xProtocolDecoderTest.java b/test/org/traccar/protocol/T800xProtocolDecoderTest.java
index 7bb149ae6..56efa5a92 100644
--- a/test/org/traccar/protocol/T800xProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/T800xProtocolDecoderTest.java
@@ -1,28 +1,30 @@
package org.traccar.protocol;
-import junit.framework.TestCase;
-import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.Test;
-import org.traccar.helper.ChannelBufferTools;
+import org.traccar.ProtocolDecoderTest;
-public class T800xProtocolDecoderTest extends TestCase {
+public class T800xProtocolDecoderTest extends ProtocolDecoderTest {
@Test
public void testDecode() throws Exception {
- /*T800xProtocolDecoder decoder = new T800xProtocolDecoder(new TestDataManager(), null, null);
+ T800xProtocolDecoder decoder = new T800xProtocolDecoder(new T800xProtocol());
- assertNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertHexString(
- "232301001500020357367031063979150208625010"))));
+ verifyNothing(decoder, binary(
+ "232301001500020357367031063979150208625010"));
- assertNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertHexString(
- "6767030004001A0001"))));*/
+ verifyNothing(decoder, binary(
+ "232303000f00000357367031063979"));
+
+ verifyPosition(decoder, binary(
+ "232304004200030357367031063979003c03842307d00000c80000050100008000008900890100000017b100151022121648b8ef0c4422969342cec5944100000110"));
+
+ verifyPosition(decoder, binary(
+ "232302004200150357367031063979003c03842307d000004a0000050100004001009500940000000285ab001510281350477f710d4452819342d1ba944101160038"));
+
+ verifyAttributes(decoder, binary(
+ "232302004200000357367031063979003c03842307d000008000000501000000010094009400000002a0b90015102814590694015a00620cf698620cf49e620cf498"));
- /* assertNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertHexString(
- "6767070088001050E2281400FFFFFFFF02334455660333445566043344556605AA00000007334455660A334455660B334455660C4E2000000DAA0000000E334455660F3344556610AAAA000011334455661C334455661F334455662133445566423344556646334455664D334455665C334455665E33445566880000000089000000008A000000008B00000000"))));
-*/
- /* verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertHexString(
- "676702001b03c5538086df0190c1790b3482df0f0157020800013beb00342401"))));
-*/
}
-} \ No newline at end of file
+
+}