diff options
Diffstat (limited to 'src/org/traccar/protocol/T800xProtocolDecoder.java')
-rw-r--r-- | src/org/traccar/protocol/T800xProtocolDecoder.java | 71 |
1 files changed, 35 insertions, 36 deletions
diff --git a/src/org/traccar/protocol/T800xProtocolDecoder.java b/src/org/traccar/protocol/T800xProtocolDecoder.java index 1fd37864e..d5504c190 100644 --- a/src/org/traccar/protocol/T800xProtocolDecoder.java +++ b/src/org/traccar/protocol/T800xProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,11 +15,13 @@ */ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.channel.Channel; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; import org.traccar.helper.BcdUtil; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; @@ -29,7 +31,6 @@ import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; -import java.nio.ByteOrder; public class T800xProtocolDecoder extends BaseProtocolDecoder { @@ -43,22 +44,15 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_ALARM = 0x04; public static final int MSG_COMMAND = 0x81; - private static float readSwappedFloat(ChannelBuffer buf) { - byte[] bytes = new byte[4]; - buf.readBytes(bytes); - return ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, bytes).readFloat(); - } - - private void sendResponse(Channel channel, int type, ChannelBuffer imei) { + private void sendResponse(Channel channel, short header, int type, ByteBuf imei) { if (channel != null) { - ChannelBuffer response = ChannelBuffers.directBuffer(15); - response.writeByte(0x23); - response.writeByte(0x23); // header + ByteBuf response = Unpooled.buffer(15); + response.writeShort(header); response.writeByte(type); response.writeShort(response.capacity()); // length response.writeShort(0x0001); // index response.writeBytes(imei); - channel.write(response); + channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); } } @@ -85,22 +79,22 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder { protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - ChannelBuffer buf = (ChannelBuffer) msg; + ByteBuf buf = (ByteBuf) msg; - buf.skipBytes(2); + short header = buf.readShort(); int type = buf.readUnsignedByte(); buf.readUnsignedShort(); // length int index = buf.readUnsignedShort(); - ChannelBuffer imei = buf.readBytes(8); + ByteBuf imei = buf.readSlice(8); DeviceSession deviceSession = getDeviceSession( - channel, remoteAddress, ChannelBuffers.hexDump(imei).substring(1)); + channel, remoteAddress, ByteBufUtil.hexDump(imei).substring(1)); if (deviceSession == null) { return null; } if (type == MSG_LOGIN || type == MSG_ALARM || type == MSG_HEARTBEAT) { - sendResponse(channel, type, imei); + sendResponse(channel, header, type, imei); } if (type == MSG_GPS || type == MSG_ALARM) { @@ -116,7 +110,7 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort(); // distance compensation buf.readUnsignedShort(); // speed alarm - int locationStatus = buf.readUnsignedByte(); + int status = buf.readUnsignedByte(); buf.readUnsignedByte(); // gsensor manager status buf.readUnsignedByte(); // other flags @@ -151,31 +145,36 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder { .setMinute(BcdUtil.readInteger(buf, 2)) .setSecond(BcdUtil.readInteger(buf, 2)); - if (BitUtil.check(locationStatus, 6)) { + if (BitUtil.check(status, 6)) { - position.setValid(!BitUtil.check(locationStatus, 7)); + position.setValid(!BitUtil.check(status, 7)); position.setTime(dateBuilder.getDate()); - position.setAltitude(readSwappedFloat(buf)); - position.setLongitude(readSwappedFloat(buf)); - position.setLatitude(readSwappedFloat(buf)); - position.setSpeed(UnitsConverter.knotsFromKph( - BcdUtil.readInteger(buf, 4) * 0.1)); + position.setAltitude(buf.readFloatLE()); + position.setLongitude(buf.readFloatLE()); + position.setLatitude(buf.readFloatLE()); + position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4) * 0.1)); position.setCourse(buf.readUnsignedShort()); } else { getLastLocation(position, dateBuilder.getDate()); - byte[] array = new byte[16]; - buf.readBytes(array); - ChannelBuffer swapped = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, array); + int mcc = buf.readUnsignedShortLE(); + int mnc = buf.readUnsignedShortLE(); - position.setNetwork(new Network(CellTower.from( - swapped.readUnsignedShort(), swapped.readUnsignedShort(), - swapped.readUnsignedShort(), swapped.readUnsignedShort()))); + if (mcc != 0xffff && mnc != 0xffff) { + Network network = new Network(); + for (int i = 0; i < 3; i++) { + network.addCellTower(CellTower.from( + mcc, mnc, buf.readUnsignedShortLE(), buf.readUnsignedShortLE())); + } + position.setNetwork(network); + } - // two more cell towers + } + if (buf.readableBytes() >= 2) { + position.set(Position.KEY_POWER, BcdUtil.readInteger(buf, 4) * 0.01); } return position; |