diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-04-11 20:29:33 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-04-11 20:29:33 -0700 |
commit | 818fe3e3e85c2e19dfcd12770b7aaee5820c0099 (patch) | |
tree | 1b4628e6affcb84e3a6f62044a2166a3abaaffbb | |
parent | ffdf0984b87e2ee3154ccb3d013a6bbf2a629783 (diff) | |
download | trackermap-server-818fe3e3e85c2e19dfcd12770b7aaee5820c0099.tar.gz trackermap-server-818fe3e3e85c2e19dfcd12770b7aaee5820c0099.tar.bz2 trackermap-server-818fe3e3e85c2e19dfcd12770b7aaee5820c0099.zip |
Refactor Wialon decoder
-rw-r--r-- | src/main/java/org/traccar/protocol/WialonProtocolDecoder.java | 100 |
1 files changed, 55 insertions, 45 deletions
diff --git a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java index de7073b67..42ff3177e 100644 --- a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2019 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. @@ -59,9 +59,10 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { .groupEnd("?") .compile(); - private void sendResponse(Channel channel, SocketAddress remoteAddress, String prefix, Integer number) { + private void sendResponse(Channel channel, SocketAddress remoteAddress, String type, Integer number) { if (channel != null) { - StringBuilder response = new StringBuilder(prefix); + StringBuilder response = new StringBuilder("#A"); + response.append(type.substring(1)); if (number != null) { response.append(number); } @@ -134,60 +135,69 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { String sentence = (String) msg; + String type = sentence.substring(0, sentence.indexOf('#', 1) + 1); - if (sentence.startsWith("#L#")) { + switch (type) { - String[] values = sentence.substring(3).split(";"); + case "#L#": + String[] values = sentence.substring(3).split(";"); - String imei = values[0].indexOf('.') >= 0 ? values[1] : values[0]; - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); - if (deviceSession != null) { - sendResponse(channel, remoteAddress, "#AL#", 1); - } - - } else if (sentence.startsWith("#P#")) { - - sendResponse(channel, remoteAddress, "#AP#", null); // heartbeat + String imei = values[0].indexOf('.') >= 0 ? values[1] : values[0]; + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession != null) { + sendResponse(channel, remoteAddress, type, 1); + } + break; - } else if (sentence.startsWith("#SD#") || sentence.startsWith("#D#")) { + case "#P#": + sendResponse(channel, remoteAddress, type, null); // heartbeat + break; - Position position = decodePosition( - channel, remoteAddress, sentence.substring(sentence.indexOf('#', 1) + 1)); + case "#D#": + case "#SD#": + Position position = decodePosition( + channel, remoteAddress, sentence.substring(sentence.indexOf('#', 1) + 1)); - if (position != null) { - sendResponse(channel, remoteAddress, "#AD#", 1); - return position; - } + if (position != null) { + sendResponse(channel, remoteAddress, type, 1); + return position; + } + break; - } else if (sentence.startsWith("#B#")) { + case "#B#": + String[] messages = sentence.substring(sentence.indexOf('#', 1) + 1).split("\\|"); + List<Position> positions = new LinkedList<>(); - String[] messages = sentence.substring(sentence.indexOf('#', 1) + 1).split("\\|"); - List<Position> positions = new LinkedList<>(); + for (String message : messages) { + position = decodePosition(channel, remoteAddress, message); + if (position != null) { + position.set(Position.KEY_ARCHIVE, true); + positions.add(position); + } + } - for (String message : messages) { - Position position = decodePosition(channel, remoteAddress, message); - if (position != null) { - position.set(Position.KEY_ARCHIVE, true); - positions.add(position); + sendResponse(channel, remoteAddress, type, messages.length); + if (!positions.isEmpty()) { + return positions; } - } + break; + + case "#M#": + deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession != null) { + position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + getLastLocation(position, new Date()); + position.setValid(false); + position.set(Position.KEY_RESULT, sentence.substring(sentence.indexOf('#', 1) + 1)); + sendResponse(channel, remoteAddress, type, 1); + return position; + } + break; - sendResponse(channel, remoteAddress, "#AB#", messages.length); - if (!positions.isEmpty()) { - return positions; - } + default: + break; - } else if (sentence.startsWith("#M#")) { - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); - if (deviceSession != null) { - Position position = new Position(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - getLastLocation(position, new Date()); - position.setValid(false); - position.set(Position.KEY_RESULT, sentence.substring(sentence.indexOf('#', 1) + 1)); - sendResponse(channel, remoteAddress, "#AM#", 1); - return position; - } } return null; |