From eaca872f70921c63030d4758d52cf4fe2df2bfac Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 4 Jan 2021 21:18:22 -0800 Subject: Support network location --- .../traccar/protocol/EasyTrackProtocolDecoder.java | 61 +++++++++++++++++++--- .../protocol/EasyTrackProtocolDecoderTest.java | 3 ++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/traccar/protocol/EasyTrackProtocolDecoder.java b/src/main/java/org/traccar/protocol/EasyTrackProtocolDecoder.java index 9be6442a5..4fcc48944 100644 --- a/src/main/java/org/traccar/protocol/EasyTrackProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/EasyTrackProtocolDecoder.java @@ -25,6 +25,8 @@ import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; import org.traccar.helper.UnitsConverter; +import org.traccar.model.CellTower; +import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; @@ -65,6 +67,18 @@ public class EasyTrackProtocolDecoder extends BaseProtocolDecoder { .any() .compile(); + private static final Pattern PATTERN_CELL = new PatternBuilder() + .text("*").expression("..,") // manufacturer + .number("(d+),") // imei + .text("JZ,") // command + .number("([01]),") // result + .number("(d+),") // cid + .number("(d+),") // lac + .number("(d+),") // mcc + .number("(d+)") // mnc + .any() + .compile(); + private String decodeAlarm(long status) { if ((status & 0x02000000) != 0) { return Position.ALARM_GEOFENCE_ENTER; @@ -97,27 +111,30 @@ public class EasyTrackProtocolDecoder extends BaseProtocolDecoder { String sentence = (String) msg; String type = sentence.substring(20, 22); - if ((type.equals("TX") || type.equals("MQ")) && channel != null) { + if (channel != null && (type.equals("TX") || type.equals("MQ"))) { channel.writeAndFlush(new NetworkMessage(sentence + "#", remoteAddress)); } - return decodeLocation(channel, remoteAddress, sentence); + if (type.equals("JZ")) { + return decodeCell(channel, remoteAddress, sentence); + } else { + return decodeLocation(channel, remoteAddress, sentence); + } } - protected Object decodeLocation( - Channel channel, SocketAddress remoteAddress, String sentence) { + private Position decodeLocation(Channel channel, SocketAddress remoteAddress, String sentence) { Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; } - Position position = new Position(getProtocolName()); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); if (deviceSession == null) { return null; } + + Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_COMMAND, parser.next()); @@ -170,4 +187,36 @@ public class EasyTrackProtocolDecoder extends BaseProtocolDecoder { return position; } + private Position decodeCell(Channel channel, SocketAddress remoteAddress, String sentence) { + + Parser parser = new Parser(PATTERN_CELL, sentence); + if (!parser.matches()) { + return null; + } + + String imei = parser.next(); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + getLastLocation(position, null); + + if (channel != null && parser.nextInt() > 0) { + String response = String.format("*ET,%s,JZ,undefined#", imei); + channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); + } + + int cid = parser.nextInt(); + int lac = parser.nextInt(); + int mcc = parser.nextInt(); + int mnc = parser.nextInt(); + position.setNetwork(new Network(CellTower.from(mcc, mnc, lac, cid))); + + return position; + } + } diff --git a/src/test/java/org/traccar/protocol/EasyTrackProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/EasyTrackProtocolDecoderTest.java index 060483f5d..abe5c5532 100644 --- a/src/test/java/org/traccar/protocol/EasyTrackProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/EasyTrackProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class EasyTrackProtocolDecoderTest extends ProtocolTest { EasyTrackProtocolDecoder decoder = new EasyTrackProtocolDecoder(null); + verifyNotNull(decoder, text( + "*ET,354522180593498,JZ,0,20222,262,724,4#")); + verifyNull(decoder, text( "*ET,358155100132943,MQ")); -- cgit v1.2.3