aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2019-04-30 23:20:06 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2019-04-30 23:20:06 -0700
commit8098c2ba47e36b1aa7293a5e3987fd24e94fecf4 (patch)
tree26cd34c9e2cf68eb3885ede2ea70339a68975719
parent996815954219a96aa864559271a17e24f7dfac77 (diff)
downloadtrackermap-server-8098c2ba47e36b1aa7293a5e3987fd24e94fecf4.tar.gz
trackermap-server-8098c2ba47e36b1aa7293a5e3987fd24e94fecf4.tar.bz2
trackermap-server-8098c2ba47e36b1aa7293a5e3987fd24e94fecf4.zip
Add API to get command types by protocol
-rw-r--r--src/main/java/org/traccar/api/resource/CommandResource.java11
-rw-r--r--src/main/java/org/traccar/database/CommandsManager.java21
-rw-r--r--swagger.json11
3 files changed, 31 insertions, 12 deletions
diff --git a/src/main/java/org/traccar/api/resource/CommandResource.java b/src/main/java/org/traccar/api/resource/CommandResource.java
index 703638701..a31345246 100644
--- a/src/main/java/org/traccar/api/resource/CommandResource.java
+++ b/src/main/java/org/traccar/api/resource/CommandResource.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 - 2019 Anton Tananaev (anton@traccar.org)
* Copyright 2016 Gabor Somogyi (gabor.g.somogyi@gmail.com)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
@@ -23,7 +23,6 @@ import org.traccar.database.CommandsManager;
import org.traccar.model.Command;
import org.traccar.model.Typed;
-import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
@@ -48,7 +47,7 @@ public class CommandResource extends ExtendedObjectResource<Command> {
@GET
@Path("send")
- public Collection<Command> get(@QueryParam("deviceId") long deviceId) throws SQLException {
+ public Collection<Command> get(@QueryParam("deviceId") long deviceId) {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
CommandsManager commandsManager = Context.getCommandsManager();
Set<Long> result = new HashSet<>(commandsManager.getUserItems(getUserId()));
@@ -77,11 +76,15 @@ public class CommandResource extends ExtendedObjectResource<Command> {
@GET
@Path("types")
- public Collection<Typed> get(@QueryParam("deviceId") long deviceId,
+ public Collection<Typed> get(
+ @QueryParam("deviceId") long deviceId,
+ @QueryParam("protocol") String protocol,
@QueryParam("textChannel") boolean textChannel) {
if (deviceId != 0) {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
return Context.getCommandsManager().getCommandTypes(deviceId, textChannel);
+ } else if (protocol != null) {
+ return Context.getCommandsManager().getCommandTypes(protocol, textChannel);
} else {
return Context.getCommandsManager().getAllCommandTypes();
}
diff --git a/src/main/java/org/traccar/database/CommandsManager.java b/src/main/java/org/traccar/database/CommandsManager.java
index d6fdd66ca..dc9512d9e 100644
--- a/src/main/java/org/traccar/database/CommandsManager.java
+++ b/src/main/java/org/traccar/database/CommandsManager.java
@@ -20,6 +20,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Queue;
@@ -106,17 +107,21 @@ public class CommandsManager extends ExtendedObjectManager<Command> {
}
public Collection<Typed> getCommandTypes(long deviceId, boolean textChannel) {
- List<Typed> result = new ArrayList<>();
Position lastPosition = Context.getIdentityManager().getLastPosition(deviceId);
if (lastPosition != null) {
- BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol());
- Collection<String> commands;
- commands = textChannel ? protocol.getSupportedTextCommands() : protocol.getSupportedDataCommands();
- for (String commandKey : commands) {
- result.add(new Typed(commandKey));
- }
+ return getCommandTypes(lastPosition.getProtocol(), textChannel);
} else {
- result.add(new Typed(Command.TYPE_CUSTOM));
+ return Collections.singletonList(new Typed(Command.TYPE_CUSTOM));
+ }
+ }
+
+ public Collection<Typed> getCommandTypes(String protocolName, boolean textChannel) {
+ List<Typed> result = new ArrayList<>();
+ BaseProtocol protocol = Context.getServerManager().getProtocol(protocolName);
+ Collection<String> commands;
+ commands = textChannel ? protocol.getSupportedTextCommands() : protocol.getSupportedDataCommands();
+ for (String commandKey : commands) {
+ result.add(new Typed(commandKey));
}
return result;
}
diff --git a/swagger.json b/swagger.json
index d3851a8df..53414c016 100644
--- a/swagger.json
+++ b/swagger.json
@@ -168,11 +168,22 @@
{
"name": "deviceId",
"in": "query",
+ "description" : "Internal device identifier. Only works if device has already reported some locations",
+ "required" : false,
"type": "integer"
},
{
+ "name": "protocol",
+ "in": "query",
+ "description" : "Protocol name. Can be used instead of device id",
+ "required" : false,
+ "type": "string"
+ },
+ {
"name": "textChannel",
"in": "query",
+ "description" : "When `true` return SMS commands. If not specified or `false` return data commands",
+ "required" : false,
"type": "boolean"
}
],