diff options
Diffstat (limited to 'src/org/traccar/protocol/MeitrackProtocolDecoder.java')
-rw-r--r-- | src/org/traccar/protocol/MeitrackProtocolDecoder.java | 100 |
1 files changed, 51 insertions, 49 deletions
diff --git a/src/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/org/traccar/protocol/MeitrackProtocolDecoder.java index 6fee0fd9f..465cfd9fa 100644 --- a/src/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 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,12 +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.Unpooled; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; import org.traccar.helper.Checksum; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -38,7 +39,7 @@ import java.util.regex.Pattern; public class MeitrackProtocolDecoder extends BaseProtocolDecoder { - private ChannelBuffer photo; + private ByteBuf photo; public MeitrackProtocolDecoder(MeitrackProtocol protocol) { super(protocol); @@ -119,7 +120,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { } } - private Position decodeRegular(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { + private Position decodeRegular(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { Parser parser = new Parser(PATTERN, buf.toString(StandardCharsets.US_ASCII)); if (!parser.matches()) { @@ -270,7 +271,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { } - private List<Position> decodeBinaryC(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { + private List<Position> decodeBinaryC(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { List<Position> positions = new LinkedList<>(); String flag = buf.toString(2, 1, StandardCharsets.US_ASCII); @@ -291,38 +292,38 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_EVENT, buf.readUnsignedByte()); - position.setLatitude(buf.readInt() * 0.000001); - position.setLongitude(buf.readInt() * 0.000001); + position.setLatitude(buf.readIntLE() * 0.000001); + position.setLongitude(buf.readIntLE() * 0.000001); - position.setTime(new Date((946684800 + buf.readUnsignedInt()) * 1000)); // 946684800 = 2000-01-01 + position.setTime(new Date((946684800 + buf.readUnsignedIntLE()) * 1000)); // 946684800 = 2000-01-01 position.setValid(buf.readUnsignedByte() == 1); position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); int rssi = buf.readUnsignedByte(); - position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); - position.setCourse(buf.readUnsignedShort()); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE())); + position.setCourse(buf.readUnsignedShortLE()); - position.set(Position.KEY_HDOP, buf.readUnsignedShort() * 0.1); + position.set(Position.KEY_HDOP, buf.readUnsignedShortLE() * 0.1); - position.setAltitude(buf.readUnsignedShort()); + position.setAltitude(buf.readUnsignedShortLE()); - position.set(Position.KEY_ODOMETER, buf.readUnsignedInt()); - position.set("runtime", buf.readUnsignedInt()); + position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE()); + position.set("runtime", buf.readUnsignedIntLE()); position.setNetwork(new Network(CellTower.from( - buf.readUnsignedShort(), buf.readUnsignedShort(), - buf.readUnsignedShort(), buf.readUnsignedShort(), + buf.readUnsignedShortLE(), buf.readUnsignedShortLE(), + buf.readUnsignedShortLE(), buf.readUnsignedShortLE(), rssi))); - position.set(Position.KEY_STATUS, buf.readUnsignedShort()); + position.set(Position.KEY_STATUS, buf.readUnsignedShortLE()); - position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort()); - position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.01); - position.set(Position.KEY_POWER, buf.readUnsignedShort()); + position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE()); + position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.01); + position.set(Position.KEY_POWER, buf.readUnsignedShortLE()); - buf.readUnsignedInt(); // geo-fence + buf.readUnsignedIntLE(); // geo-fence positions.add(position); } @@ -337,17 +338,17 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { } command.append(String.format("%02x", checksum & 0xff).toUpperCase()); command.append("\r\n"); - channel.write(command.toString()); // delete processed data + channel.writeAndFlush(new NetworkMessage(command.toString(), remoteAddress)); // delete processed data } return positions; } - private List<Position> decodeBinaryE(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { + private List<Position> decodeBinaryE(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { List<Position> positions = new LinkedList<>(); buf.readerIndex(buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',') + 1); - String imei = buf.readBytes(15).toString(StandardCharsets.US_ASCII); + String imei = buf.readSlice(15).toString(StandardCharsets.US_ASCII); buf.skipBytes(1 + 3 + 1); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); @@ -355,15 +356,15 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { return null; } - buf.readUnsignedInt(); // remaining cache - int count = buf.readUnsignedShort(); + buf.readUnsignedIntLE(); // remaining cache + int count = buf.readUnsignedShortLE(); for (int i = 0; i < count; i++) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); - buf.readUnsignedShort(); // length - buf.readUnsignedShort(); // index + buf.readUnsignedShortLE(); // length + buf.readUnsignedShortLE(); // index int paramCount = buf.readUnsignedByte(); for (int j = 0; j < paramCount; j++) { @@ -392,22 +393,22 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { int id = buf.readUnsignedByte(); switch (id) { case 0x08: - position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE())); break; case 0x09: - position.setCourse(buf.readUnsignedShort() * 0.1); + position.setCourse(buf.readUnsignedShortLE()); break; case 0x0B: - position.setAltitude(buf.readShort()); + position.setAltitude(buf.readShortLE()); break; case 0x19: - position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.01); + position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.01); break; case 0x1A: - position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01); + position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.01); break; default: - buf.readUnsignedShort(); + buf.readUnsignedShortLE(); break; } } @@ -417,19 +418,19 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { int id = buf.readUnsignedByte(); switch (id) { case 0x02: - position.setLatitude(buf.readInt() * 0.000001); + position.setLatitude(buf.readIntLE() * 0.000001); break; case 0x03: - position.setLongitude(buf.readInt() * 0.000001); + position.setLongitude(buf.readIntLE() * 0.000001); break; case 0x04: - position.setTime(new Date((946684800 + buf.readUnsignedInt()) * 1000)); // 2000-01-01 + position.setTime(new Date((946684800 + buf.readUnsignedIntLE()) * 1000)); // 2000-01-01 break; case 0x0D: - position.set("runtime", buf.readUnsignedInt()); + position.set("runtime", buf.readUnsignedIntLE()); break; default: - buf.readUnsignedInt(); + buf.readUnsignedIntLE(); break; } } @@ -446,13 +447,13 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { return positions; } - private void requestPhotoPacket(Channel channel, String imei, String file, int index) { + private void requestPhotoPacket(Channel channel, SocketAddress socketAddress, String imei, String file, int index) { if (channel != null) { String content = "D00," + file + "," + index; int length = 1 + imei.length() + 1 + content.length() + 5; String response = String.format("@@O%02d,%s,%s*", length, imei, content); response += Checksum.sum(response) + "\r\n"; - channel.write(response); + channel.writeAndFlush(new NetworkMessage(response, socketAddress)); } } @@ -460,7 +461,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - ChannelBuffer buf = (ChannelBuffer) msg; + ByteBuf buf = (ByteBuf) msg; int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); String imei = buf.toString(index + 1, 15, StandardCharsets.US_ASCII); @@ -470,7 +471,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { switch (type) { case "D00": if (photo == null) { - photo = ChannelBuffers.dynamicBuffer(); + photo = Unpooled.buffer(); } index = index + 1 + type.length() + 1; @@ -484,7 +485,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { int current = Integer.parseInt(buf.toString(index, endIndex - index, StandardCharsets.US_ASCII)); buf.readerIndex(endIndex + 1); - photo.writeBytes(buf.readBytes(buf.readableBytes() - 1 - 2 - 2)); + photo.writeBytes(buf.readSlice(buf.readableBytes() - 1 - 2 - 2)); if (current == total - 1) { Position position = new Position(getProtocolName()); @@ -493,18 +494,19 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(imei, photo, "jpg")); + photo.release(); photo = null; return position; } else { if ((current + 1) % 8 == 0) { - requestPhotoPacket(channel, imei, file, current + 1); + requestPhotoPacket(channel, remoteAddress, imei, file, current + 1); } return null; } case "D03": - photo = ChannelBuffers.dynamicBuffer(); - requestPhotoPacket(channel, imei, "camera_picture.jpg", 0); + photo = Unpooled.buffer(); + requestPhotoPacket(channel, remoteAddress, imei, "camera_picture.jpg", 0); return null; case "CCC": return decodeBinaryC(channel, remoteAddress, buf); |