aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2018-04-12 14:20:26 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2018-04-12 14:20:26 +1200
commit617393cf9f052298f7fb35f0c58138c87a6dd5c3 (patch)
tree50ad1ab51b93915cff422a1834baa5a2c579fce8 /src/org/traccar/protocol
parentf91863b34ab32a70faf5bd5948a7fa0bad16ceeb (diff)
downloadtraccar-server-617393cf9f052298f7fb35f0c58138c87a6dd5c3.tar.gz
traccar-server-617393cf9f052298f7fb35f0c58138c87a6dd5c3.tar.bz2
traccar-server-617393cf9f052298f7fb35f0c58138c87a6dd5c3.zip
Complete PT502 photo decoding
Diffstat (limited to 'src/org/traccar/protocol')
-rw-r--r--src/org/traccar/protocol/Pt502ProtocolDecoder.java49
1 files changed, 45 insertions, 4 deletions
diff --git a/src/org/traccar/protocol/Pt502ProtocolDecoder.java b/src/org/traccar/protocol/Pt502ProtocolDecoder.java
index b60500c25..76e7ee1bf 100644
--- a/src/org/traccar/protocol/Pt502ProtocolDecoder.java
+++ b/src/org/traccar/protocol/Pt502ProtocolDecoder.java
@@ -20,6 +20,7 @@ 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.Context;
import org.traccar.DeviceSession;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.Parser;
@@ -139,6 +140,14 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder {
return position;
}
+ private void requestPhotoFragment(Channel channel) {
+ if (channel != null) {
+ int offset = photo.writerIndex();
+ int size = Math.min(photo.writableBytes(), MAX_CHUNK_SIZE);
+ channel.write("#PHD" + offset + "," + size + "\r\n");
+ }
+ }
+
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
@@ -150,13 +159,45 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder {
if (type.startsWith("$PHD")) {
- // TODO decode photo
+ int dataIndex = buf.indexOf(typeEndIndex + 1, buf.writerIndex(), (byte) ',') + 1;
+ buf.readerIndex(dataIndex);
+
+ if (photo != null) {
+
+ photo.writeBytes(buf.readBytes(buf.readableBytes()));
+
+ if (photo.writableBytes() > 0) {
+
+ requestPhotoFragment(channel);
+
+ } else {
+
+ DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
+ String uniqueId = Context.getIdentityManager().getById(deviceSession.getDeviceId()).getUniqueId();
+
+ Position position = new Position(getProtocolName());
+ position.setDeviceId(deviceSession.getDeviceId());
+
+ getLastLocation(position, null);
+
+ position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg"));
+
+ photo = null;
+
+ return position;
+
+ }
+
+ }
} else {
- if (type.startsWith("$PHO") && channel != null) {
- photo = ChannelBuffers.buffer(Integer.parseInt(type.substring(4, type.indexOf('-'))));
- channel.write("#PHD0," + Math.min(photo.capacity(), MAX_CHUNK_SIZE) + "\r\n");
+ if (type.startsWith("$PHO")) {
+ int size = Integer.parseInt(type.split("-")[0].substring(4));
+ if (size > 0) {
+ photo = ChannelBuffers.buffer(size);
+ requestPhotoFragment(channel);
+ }
}
return decodePosition(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII));