From ed72ff6e7f964bc56a4948c15c97f37c072bf326 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sun, 22 Oct 2017 18:55:34 +0500 Subject: Set deviceId in Saved Command before send --- src/org/traccar/api/resource/CommandResource.java | 9 +++------ src/org/traccar/database/CommandsManager.java | 14 +++++++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/org/traccar/api/resource/CommandResource.java b/src/org/traccar/api/resource/CommandResource.java index 996c15daf..a25421e31 100644 --- a/src/org/traccar/api/resource/CommandResource.java +++ b/src/org/traccar/api/resource/CommandResource.java @@ -62,17 +62,14 @@ public class CommandResource extends ExtendedObjectResource { Context.getPermissionsManager().checkReadonly(getUserId()); long deviceId = entity.getDeviceId(); long id = entity.getId(); - boolean sent; - if (deviceId != 0 && id != 0) { + Context.getPermissionsManager().checkDevice(getUserId(), deviceId); + if (id != 0) { Context.getPermissionsManager().checkPermission(Command.class, getUserId(), id); - Context.getPermissionsManager().checkDevice(getUserId(), deviceId); Context.getPermissionsManager().checkUserDeviceCommand(getUserId(), deviceId, id); - sent = Context.getCommandsManager().sendCommand(id, deviceId); } else { Context.getPermissionsManager().checkLimitCommands(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - sent = Context.getCommandsManager().sendCommand(entity, deviceId); } + boolean sent = Context.getCommandsManager().sendCommand(entity); if (!sent) { return Response.accepted(entity).build(); } diff --git a/src/org/traccar/database/CommandsManager.java b/src/org/traccar/database/CommandsManager.java index cae4ac6f7..07695cbf7 100644 --- a/src/org/traccar/database/CommandsManager.java +++ b/src/org/traccar/database/CommandsManager.java @@ -45,11 +45,15 @@ public class CommandsManager extends ExtendedObjectManager { return !getAllDeviceItems(deviceId).contains(commandId); } - public boolean sendCommand(long commandId, long deviceId) throws Exception { - return sendCommand(getById(commandId), deviceId); - } - - public boolean sendCommand(Command command, long deviceId) throws Exception { + public boolean sendCommand(Command command) throws Exception { + long deviceId = command.getDeviceId(); + if (command.getId() != 0) { + Command savedCommand = getById(command.getId()); + command.setTextChannel(savedCommand.getTextChannel()); + command.setType(savedCommand.getType()); + command.setAttributes(savedCommand.getAttributes()); + command.setDescription(savedCommand.getDescription()); + } boolean sent = true; if (command.getTextChannel()) { Position lastPosition = Context.getIdentityManager().getLastPosition(deviceId); -- cgit v1.2.3 From 0283458de495e8f9f5393b1184c7a670e9851dcc Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 23 Oct 2017 09:50:35 +0500 Subject: Implement command cloning --- src/org/traccar/api/resource/CommandResource.java | 3 +-- src/org/traccar/database/CommandsManager.java | 13 +++++-------- src/org/traccar/model/Command.java | 7 ++++++- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/org/traccar/api/resource/CommandResource.java b/src/org/traccar/api/resource/CommandResource.java index a25421e31..703638701 100644 --- a/src/org/traccar/api/resource/CommandResource.java +++ b/src/org/traccar/api/resource/CommandResource.java @@ -69,8 +69,7 @@ public class CommandResource extends ExtendedObjectResource { } else { Context.getPermissionsManager().checkLimitCommands(getUserId()); } - boolean sent = Context.getCommandsManager().sendCommand(entity); - if (!sent) { + if (!Context.getCommandsManager().sendCommand(entity)) { return Response.accepted(entity).build(); } return Response.ok(entity).build(); diff --git a/src/org/traccar/database/CommandsManager.java b/src/org/traccar/database/CommandsManager.java index 07695cbf7..9ceb995ef 100644 --- a/src/org/traccar/database/CommandsManager.java +++ b/src/org/traccar/database/CommandsManager.java @@ -48,13 +48,9 @@ public class CommandsManager extends ExtendedObjectManager { public boolean sendCommand(Command command) throws Exception { long deviceId = command.getDeviceId(); if (command.getId() != 0) { - Command savedCommand = getById(command.getId()); - command.setTextChannel(savedCommand.getTextChannel()); - command.setType(savedCommand.getType()); - command.setAttributes(savedCommand.getAttributes()); - command.setDescription(savedCommand.getDescription()); + command = getById(command.getId()).clone(); + command.setDeviceId(deviceId); } - boolean sent = true; if (command.getTextChannel()) { Position lastPosition = Context.getIdentityManager().getLastPosition(deviceId); String phone = Context.getIdentityManager().getById(deviceId).getPhone(); @@ -75,10 +71,11 @@ public class CommandsManager extends ExtendedObjectManager { if (activeDevice != null) { activeDevice.sendCommand(command); } else { - sent = !getDeviceQueue(deviceId).add(command); + getDeviceQueue(deviceId).add(command); + return false; } } - return sent; + return true; } public Collection getSupportedCommands(long deviceId) { diff --git a/src/org/traccar/model/Command.java b/src/org/traccar/model/Command.java index 67134dc7d..09f0f251b 100644 --- a/src/org/traccar/model/Command.java +++ b/src/org/traccar/model/Command.java @@ -20,7 +20,7 @@ import org.traccar.database.QueryIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) -public class Command extends Message { +public class Command extends Message implements Cloneable { public static final String TYPE_CUSTOM = "custom"; public static final String TYPE_IDENTIFICATION = "deviceIdentification"; @@ -77,6 +77,11 @@ public class Command extends Message { public static final String KEY_SERVER = "server"; public static final String KEY_PORT = "port"; + @Override + public Command clone() throws CloneNotSupportedException { + return (Command) super.clone(); + } + private boolean textChannel; public boolean getTextChannel() { -- cgit v1.2.3 From 22efd02c8a45963e08e887d402bd4b9161636ddf Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 23 Oct 2017 11:15:31 +0500 Subject: Fix indentation --- src/org/traccar/model/Command.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/traccar/model/Command.java b/src/org/traccar/model/Command.java index 09f0f251b..16205ede1 100644 --- a/src/org/traccar/model/Command.java +++ b/src/org/traccar/model/Command.java @@ -79,7 +79,7 @@ public class Command extends Message implements Cloneable { @Override public Command clone() throws CloneNotSupportedException { - return (Command) super.clone(); + return (Command) super.clone(); } private boolean textChannel; -- cgit v1.2.3