From f91863b34ab32a70faf5bd5948a7fa0bad16ceeb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 12 Apr 2018 14:04:13 +1200 Subject: Convert PT502 to binary protocol --- src/org/traccar/protocol/Pt502Protocol.java | 3 +- src/org/traccar/protocol/Pt502ProtocolDecoder.java | 50 +++++++++++++++------- 2 files changed, 36 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/Pt502Protocol.java b/src/org/traccar/protocol/Pt502Protocol.java index 0116422c2..ba820a6a1 100644 --- a/src/org/traccar/protocol/Pt502Protocol.java +++ b/src/org/traccar/protocol/Pt502Protocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 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. @@ -45,7 +45,6 @@ public class Pt502Protocol extends BaseProtocol { protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("frameDecoder", new Pt502FrameDecoder()); pipeline.addLast("stringEncoder", new StringEncoder()); - pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("objectEncoder", new Pt502ProtocolEncoder()); pipeline.addLast("objectDecoder", new Pt502ProtocolDecoder(Pt502Protocol.this)); } diff --git a/src/org/traccar/protocol/Pt502ProtocolDecoder.java b/src/org/traccar/protocol/Pt502ProtocolDecoder.java index bc7647744..b60500c25 100644 --- a/src/org/traccar/protocol/Pt502ProtocolDecoder.java +++ b/src/org/traccar/protocol/Pt502ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2018 Anton Tananaev (anton@traccar.org) * Copyright 2012 Luis Parada (luis.parada@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,6 +16,8 @@ */ package org.traccar.protocol; +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.DeviceSession; @@ -25,13 +27,14 @@ import org.traccar.helper.PatternBuilder; import org.traccar.model.Position; import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; import java.util.regex.Pattern; public class Pt502ProtocolDecoder extends BaseProtocolDecoder { private static final int MAX_CHUNK_SIZE = 960; - private byte[] photo; + private ChannelBuffer photo; public Pt502ProtocolDecoder(Pt502Protocol protocol) { super(protocol); @@ -85,25 +88,15 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder { } } - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + private Position decodePosition(Channel channel, SocketAddress remoteAddress, String sentence) { - Parser parser = new Parser(PATTERN, (String) msg); + Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; } Position position = new Position(getProtocolName()); - - String type = parser.next(); - - if (type.startsWith("PHO") && channel != null) { - photo = new byte[Integer.parseInt(type.substring(3))]; - channel.write("#PHD0," + Math.min(photo.length, MAX_CHUNK_SIZE) + "\r\n"); - } - - position.set(Position.KEY_ALARM, decodeAlarm(type)); + position.set(Position.KEY_ALARM, decodeAlarm(parser.next())); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); if (deviceSession == null) { @@ -146,4 +139,31 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder { return position; } + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ChannelBuffer buf = (ChannelBuffer) msg; + + int typeEndIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); + String type = buf.toString(buf.readerIndex(), typeEndIndex - buf.readerIndex(), StandardCharsets.US_ASCII); + + if (type.startsWith("$PHD")) { + + // TODO decode photo + + } 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"); + } + + return decodePosition(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII)); + + } + + return null; + } + } -- cgit v1.2.3