From 3c9db2f713de21292a1109004f141744b789a660 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 8 Oct 2020 18:23:47 -0700 Subject: Atrack camera support --- .../org/traccar/protocol/AtrackFrameDecoder.java | 4 +- .../traccar/protocol/AtrackProtocolDecoder.java | 61 +++++++++++++++++----- 2 files changed, 50 insertions(+), 15 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/traccar/protocol/AtrackFrameDecoder.java b/src/main/java/org/traccar/protocol/AtrackFrameDecoder.java index f071e2d97..8ed1fc8e8 100644 --- a/src/main/java/org/traccar/protocol/AtrackFrameDecoder.java +++ b/src/main/java/org/traccar/protocol/AtrackFrameDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2020 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. @@ -39,7 +39,7 @@ public class AtrackFrameDecoder extends BaseFrameDecoder { return buf.readRetainedSlice(KEEPALIVE_LENGTH); } - } else if (buf.getUnsignedShort(buf.readerIndex()) == 0x4050 && buf.getByte(buf.readerIndex() + 2) != ',') { + } else if (buf.getUnsignedByte(buf.readerIndex()) == 0x40 && buf.getByte(buf.readerIndex() + 2) != ',') { if (buf.readableBytes() > 6) { int length = buf.getUnsignedShort(buf.readerIndex() + 4) + 4 + 2; diff --git a/src/main/java/org/traccar/protocol/AtrackProtocolDecoder.java b/src/main/java/org/traccar/protocol/AtrackProtocolDecoder.java index 428b69cd9..56c00c7c1 100644 --- a/src/main/java/org/traccar/protocol/AtrackProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/AtrackProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2020 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. @@ -50,10 +50,12 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { private static final int MIN_DATA_LENGTH = 40; private boolean longDate; - private boolean decimalFuel; + private final boolean decimalFuel; private boolean custom; private String form; + private ByteBuf photo; + private final Map alarmMap = new HashMap<>(); public AtrackProtocolDecoder(Protocol protocol) { @@ -510,20 +512,34 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { return position; } - private List decodeBinary(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { + private Position decodePhoto(DeviceSession deviceSession, ByteBuf buf, long id) { - buf.skipBytes(2); // prefix - buf.readUnsignedShort(); // checksum - buf.readUnsignedShort(); // length - int index = buf.readUnsignedShort(); + long time = buf.readUnsignedInt(); + int index = buf.readUnsignedByte(); + int count = buf.readUnsignedByte(); - long id = buf.readLong(); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(id)); - if (deviceSession == null) { - return null; + if (photo == null) { + photo = Unpooled.buffer(); + } + photo.writeBytes(buf.readSlice(buf.readUnsignedShort())); + + if (index == count - 1) { + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + getLastLocation(position, new Date(time * 1000)); + + position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(String.valueOf(id), photo, "jpg")); + photo.release(); + photo = null; + + return position; } - sendResponse(channel, remoteAddress, id, index); + return null; + } + + private List decodeBinary(DeviceSession deviceSession, ByteBuf buf) { List positions = new LinkedList<>(); @@ -613,7 +629,26 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { } else if (buf.getByte(buf.readerIndex() + 2) == ',') { return decodeText(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII).trim()); } else { - return decodeBinary(channel, remoteAddress, buf); + + String prefix = buf.readCharSequence(2, StandardCharsets.US_ASCII).toString(); + buf.readUnsignedShort(); // checksum + buf.readUnsignedShort(); // length + int index = buf.readUnsignedShort(); + + long id = buf.readLong(); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(id)); + if (deviceSession == null) { + return null; + } + + sendResponse(channel, remoteAddress, id, index); + + if (prefix.equals("@R")) { + return decodePhoto(deviceSession, buf, id); + } else { + return decodeBinary(deviceSession, buf); + } + } } -- cgit v1.2.3