From 2c604b9c9ee8f996902acfdd60626324a8414d34 Mon Sep 17 00:00:00 2001 From: Edward Valley Date: Wed, 10 Jul 2019 17:03:25 -0400 Subject: Changes after third review --- src/main/java/org/traccar/BaseProtocolEncoder.java | 13 +++----- .../java/org/traccar/database/DeviceManager.java | 18 +++++++++- .../java/org/traccar/database/IdentityManager.java | 6 +++- .../traccar/protocol/LaipacProtocolDecoder.java | 39 +++++++--------------- .../traccar/protocol/LaipacProtocolEncoder.java | 22 +++++------- 5 files changed, 46 insertions(+), 52 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/traccar/BaseProtocolEncoder.java b/src/main/java/org/traccar/BaseProtocolEncoder.java index d7625e4b8..6d96280f7 100644 --- a/src/main/java/org/traccar/BaseProtocolEncoder.java +++ b/src/main/java/org/traccar/BaseProtocolEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 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. @@ -22,7 +22,6 @@ import io.netty.channel.ChannelPromise; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.model.Command; -import org.traccar.model.Device; public abstract class BaseProtocolEncoder extends ChannelOutboundHandlerAdapter { @@ -34,13 +33,9 @@ public abstract class BaseProtocolEncoder extends ChannelOutboundHandlerAdapter protected void initDevicePassword(Command command, String defaultPassword) { if (!command.getAttributes().containsKey(Command.KEY_DEVICE_PASSWORD)) { - Device device = Context.getIdentityManager().getById(command.getDeviceId()); - String password = device.getString(Command.KEY_DEVICE_PASSWORD); - if (password != null) { - command.set(Command.KEY_DEVICE_PASSWORD, password); - } else { - command.set(Command.KEY_DEVICE_PASSWORD, defaultPassword); - } + String password = Context.getIdentityManager() + .getDevicePassword(command.getDeviceId(), defaultPassword); + command.set(Command.KEY_DEVICE_PASSWORD, password); } } diff --git a/src/main/java/org/traccar/database/DeviceManager.java b/src/main/java/org/traccar/database/DeviceManager.java index de4607d1f..d1a205b01 100644 --- a/src/main/java/org/traccar/database/DeviceManager.java +++ b/src/main/java/org/traccar/database/DeviceManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 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. @@ -29,6 +29,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.config.Config; import org.traccar.Context; +import org.traccar.model.Command; import org.traccar.model.Device; import org.traccar.model.DeviceState; import org.traccar.model.DeviceAccumulators; @@ -114,6 +115,21 @@ public class DeviceManager extends BaseObjectManager implements Identity return devicesByUniqueId.get(uniqueId); } + public String getDevicePassword(long id) { + + return getById(id).getString(Command.KEY_DEVICE_PASSWORD); + } + + public String getDevicePassword(long id, String defaultPassword) { + + String password = getDevicePassword(id); + if (password != null) { + return password; + } + + return defaultPassword; + } + public Device getDeviceByPhone(String phone) { return devicesByPhone.get(phone); } diff --git a/src/main/java/org/traccar/database/IdentityManager.java b/src/main/java/org/traccar/database/IdentityManager.java index 6228a0f75..331706669 100644 --- a/src/main/java/org/traccar/database/IdentityManager.java +++ b/src/main/java/org/traccar/database/IdentityManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 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. @@ -26,6 +26,10 @@ public interface IdentityManager { Device getByUniqueId(String uniqueId) throws Exception; + String getDevicePassword(long id); + + String getDevicePassword(long id, String defaultPassword); + Position getLastPosition(long deviceId); boolean isLatestPosition(Position position); diff --git a/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java b/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java index c028d78b6..381f497e4 100644 --- a/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java @@ -26,8 +26,6 @@ import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; import org.traccar.model.CellTower; -import org.traccar.model.Device; -import org.traccar.model.Command; import org.traccar.model.Network; import org.traccar.model.Position; @@ -123,21 +121,6 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { return event; } - private String getDevicePassword(DeviceSession deviceSession) { - - String devicePassword = DEFAULT_DEVICE_PASSWORD; - - Device device = Context.getIdentityManager().getById(deviceSession.getDeviceId()); - if (device != null) { - String password = device.getString(Command.KEY_DEVICE_PASSWORD); - if (password != null) { - devicePassword = password; - } - } - - return devicePassword; - } - private void sendEventResponse( String event, String devicePassword, Channel channel, SocketAddress remoteAddress) { @@ -247,7 +230,8 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { sendAcknowledge(status, event, checksum, channel, remoteAddress); - String devicePassword = getDevicePassword(deviceSession); + String devicePassword = Context.getIdentityManager() + .getDevicePassword(deviceSession.getDeviceId(), DEFAULT_DEVICE_PASSWORD); sendEventResponse(event, devicePassword, channel, remoteAddress); } @@ -260,18 +244,19 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { String sentence = (String) msg; - Parser parser = new Parser(PATTERN_ECHK, sentence); - if (parser.matches()) { - return handleEchk(sentence, parser, channel, remoteAddress); - } - - parser = new Parser(PATTERN_AVRMC, sentence); - if (parser.matches()) { - return handleAvrmc(sentence, parser, channel, remoteAddress); + if (sentence.startsWith("$ECHK")) { + Parser parser = new Parser(PATTERN_ECHK, sentence); + if (parser.matches()) { + return handleEchk(sentence, parser, channel, remoteAddress); + } + } else if (sentence.startsWith("$AVRMC")) { + Parser parser = new Parser(PATTERN_AVRMC, sentence); + if (parser.matches()) { + return handleAvrmc(sentence, parser, channel, remoteAddress); + } } return null; } - } diff --git a/src/main/java/org/traccar/protocol/LaipacProtocolEncoder.java b/src/main/java/org/traccar/protocol/LaipacProtocolEncoder.java index 6f540fa3e..9b99486f7 100644 --- a/src/main/java/org/traccar/protocol/LaipacProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/LaipacProtocolEncoder.java @@ -21,7 +21,11 @@ import org.traccar.helper.Checksum; public class LaipacProtocolEncoder extends StringProtocolEncoder { - public LaipacProtocolEncoder() { + @Override + protected String formatCommand(Command command, String format, String... keys) { + String sentence = super.formatCommand(command, "$" + format, keys); + sentence += Checksum.nmea(sentence) + "\r\n"; + return sentence; } @Override @@ -29,30 +33,20 @@ public class LaipacProtocolEncoder extends StringProtocolEncoder { initDevicePassword(command, LaipacProtocolDecoder.DEFAULT_DEVICE_PASSWORD); - String commandSentence = null; - switch (command.getType()) { case Command.TYPE_CUSTOM: - commandSentence = formatCommand(command, "${%s}", + return formatCommand(command, "{%s}", Command.KEY_DATA); - break; case Command.TYPE_POSITION_SINGLE: - commandSentence = formatCommand(command, "$AVREQ,{%s},1", + return formatCommand(command, "AVREQ,{%s},1", Command.KEY_DEVICE_PASSWORD); - break; case Command.TYPE_REBOOT_DEVICE: - commandSentence = formatCommand(command, "$AVRESET,{%s},{%s}", + return formatCommand(command, "AVRESET,{%s},{%s}", Command.KEY_UNIQUE_ID, Command.KEY_DEVICE_PASSWORD); - break; default: break; } - if (commandSentence != null) { - commandSentence += Checksum.nmea(commandSentence) + "\r\n"; - return commandSentence; - } - return null; } -- cgit v1.2.3