Skip to content

Commit

Permalink
Decode AquilaB emergency packets
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Aug 3, 2018
1 parent a4ad137 commit 18e64c7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
78 changes: 67 additions & 11 deletions src/org/traccar/protocol/AquilaProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,12 @@ private Position decodeA(Channel channel, SocketAddress remoteAddress, String se
return position;
}

private static final Pattern PATTERN_B = new PatternBuilder()
.text("$Header,")
private static final Pattern PATTERN_B_1 = new PatternBuilder()
.text("$")
.expression("[^,]+,") // header
.expression("[^,]+,") // client
.expression("[^,]+,") // firmware version
.expression(".{2},") // type
.expression(".{2},") // packet type
.number("d+,") // message id
.expression("[LH],") // status
.number("(d+),") // imei
Expand Down Expand Up @@ -264,9 +265,62 @@ private Position decodeA(Channel channel, SocketAddress remoteAddress, String se
.any()
.compile();

private Position decodeB(Channel channel, SocketAddress remoteAddress, String sentence) {
private static final Pattern PATTERN_B_2 = new PatternBuilder()
.text("$")
.expression("[^,]+,") // header
.expression("[^,]+,") // client
.expression("(.{3}),") // message type
.number("(d+),") // imei
.expression(".{2},") // packet type
.number("(dd)(dd)(dddd)") // date (ddmmyyyy)
.number("(dd)(dd)(dd),") // time (hhmmss)
.expression("([AV]),") // validity
.number("(-?d+.d+),") // latitude
.expression("([NS]),")
.number("(-?d+.d+),") // longitude
.expression("([EW]),")
.number("(-?d+.d+),") // altitude
.number("(d+.d+),") // speed
.any()
.compile();

private Position decodeB2(Channel channel, SocketAddress remoteAddress, String sentence) {

Parser parser = new Parser(PATTERN_B_2, sentence);
if (!parser.matches()) {
return null;
}

String type = parser.next();
String id = parser.next();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
if (deviceSession == null) {
return null;
}

Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());

position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
position.setValid(parser.next().equals("A"));
position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
position.setAltitude(parser.nextDouble());
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));

Parser parser = new Parser(PATTERN_B, sentence);
if (type.equals("EMR") && channel != null) {
String password = Context.getIdentityManager().lookupAttributeString(
deviceSession.getDeviceId(), getProtocolName() + ".password", "aquila123", true);
channel.writeAndFlush(new NetworkMessage(
"#set$" + id + "@" + password + "#EMR_MODE:0*", remoteAddress));
}

return position;
}

private Position decodeB1(Channel channel, SocketAddress remoteAddress, String sentence) {

Parser parser = new Parser(PATTERN_B_1, sentence);
if (!parser.matches()) {
return null;
}
Expand Down Expand Up @@ -300,12 +354,6 @@ private Position decodeB(Channel channel, SocketAddress remoteAddress, String se

if (parser.nextInt() == 1) {
position.set(Position.KEY_ALARM, Position.ALARM_SOS);
if (channel != null) {
String password = Context.getIdentityManager().lookupAttributeString(
position.getDeviceId(), getProtocolName() + ".password", "aquila123", true);
channel.writeAndFlush(new NetworkMessage(
"#set$" + id + "@" + password + "#EMR_MODE:0*", remoteAddress));
}
}

Network network = new Network();
Expand All @@ -330,6 +378,14 @@ private Position decodeB(Channel channel, SocketAddress remoteAddress, String se
return position;
}

private Position decodeB(Channel channel, SocketAddress remoteAddress, String sentence) {
if (sentence.contains("EMR") || sentence.contains("SEM")) {
return decodeB2(channel, remoteAddress, sentence);
} else {
return decodeB1(channel, remoteAddress, sentence);
}
}

@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
Expand Down
6 changes: 6 additions & 0 deletions test/org/traccar/protocol/AquilaProtocolDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public void testDecodeB() throws Exception {

AquilaProtocolDecoder decoder = new AquilaProtocolDecoder(new AquilaProtocol());

verifyPosition(decoder, text(
"$Header,nliven,EMR,861693034634154,NM,09112017155133,A,12.976495,N,77.549713,E,906.0,0.0,23,G,KA01I2000,+919844098440*4B"));

verifyPosition(decoder, text(
"$EPB,iTriangle1,EMR,864495034445822,SP,03082018110730,A,22.829292,N,75.935806,E,543.0,0.0,0,G,KA01G1234,+9164061023*13"));

verifyPosition(decoder, text(
"$Header,iTriangle,1_37T02B0164MAIS_2,NR,1,L,864495034490141,KA01I2000,1,19042018,102926,22.846401,N,75.948952,E,0.0,311,5,578.0,3.80,3.67,AirTel,0,1,12.5,4.3,1,C,14,404,93,0456,16db,29,ebd8,0458,28,3843,18ab,25,072e,18ab,22,35da,0458,0000,00,031181,0.0,0.0,0,()*34"));

Expand Down

0 comments on commit 18e64c7

Please sign in to comment.