From ba7ae0c0b49b61805d21df6b64df9d013c396176 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 1 Aug 2018 16:21:22 +1200 Subject: Implement H02 time sync response --- src/org/traccar/protocol/H02ProtocolDecoder.java | 27 +++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/H02ProtocolDecoder.java b/src/org/traccar/protocol/H02ProtocolDecoder.java index 2abde7d6f..64c45a22f 100644 --- a/src/org/traccar/protocol/H02ProtocolDecoder.java +++ b/src/org/traccar/protocol/H02ProtocolDecoder.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBufUtil; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; import org.traccar.helper.BcdUtil; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; @@ -31,7 +32,10 @@ import org.traccar.model.Position; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Date; +import java.util.TimeZone; import java.util.regex.Pattern; public class H02ProtocolDecoder extends BaseProtocolDecoder { @@ -156,7 +160,7 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { .text("V4,") .expression("(.*),") // response .or() - .expression("V[^,]*,") + .expression("(V[^,]*),") .groupEnd() .number("(?:(dd)(dd)(dd))?,") // time (hhmmss) .groupEnd() @@ -254,6 +258,15 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { .text("#").optional() .compile(); + private void sendResponse(Channel channel, SocketAddress remoteAddress, String id, String type) { + if (channel != null && id != null) { + DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + String response = String.format("*HQ,%s,V4,%s,%s#", id, type, dateFormat.format(new Date())); + channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); + } + } + private Position decodeText(String sentence, Channel channel, SocketAddress remoteAddress) { Parser parser = new Parser(PATTERN, sentence); @@ -261,7 +274,8 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { return null; } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + String id = parser.next(); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { return null; } @@ -273,6 +287,10 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_RESULT, parser.next()); } + if (parser.hasNext() && parser.next().equals("V1")) { + sendResponse(channel, remoteAddress, id, "V1"); + } + DateBuilder dateBuilder = new DateBuilder(); if (parser.hasNext(3)) { dateBuilder.setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); @@ -341,11 +359,14 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { return null; } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + String id = parser.next(); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { return null; } + sendResponse(channel, remoteAddress, id, "NBR"); + Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); -- cgit v1.2.3