diff options
Diffstat (limited to 'src/org')
-rw-r--r-- | src/org/traccar/BaseProtocol.java | 5 | ||||
-rw-r--r-- | src/org/traccar/ServerManager.java | 30 | ||||
-rw-r--r-- | src/org/traccar/api/resource/DeviceResource.java | 4 | ||||
-rw-r--r-- | src/org/traccar/api/resource/SupportedCommandResource.java | 48 | ||||
-rw-r--r-- | src/org/traccar/database/DataManager.java | 1 | ||||
-rw-r--r-- | src/org/traccar/web/WebServer.java | 11 |
6 files changed, 86 insertions, 13 deletions
diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 7714f18db..46a3fad5f 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -19,6 +19,7 @@ import org.traccar.database.ActiveDevice; import org.traccar.model.Command; import java.util.Arrays; +import java.util.Collection; import java.util.HashSet; import java.util.Set; @@ -40,6 +41,10 @@ public abstract class BaseProtocol implements Protocol { supportedCommands.addAll(Arrays.asList(commands)); } + public Collection<String> getSupportedCommands() { + return supportedCommands; + } + @Override public void sendCommand(ActiveDevice activeDevice, Command command) { if (!supportedCommands.contains(command.getType())) { diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index cd6a071db..59bb88829 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -20,14 +20,19 @@ import java.net.URI; import java.net.URL; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collection; import java.util.Enumeration; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class ServerManager { + private final Map<String, BaseProtocol> protocols = new HashMap<>(); private final List<TrackerServer> serverList = new LinkedList<>(); public ServerManager() throws Exception { @@ -62,20 +67,22 @@ public class ServerManager { for (String name : names) { Class protocolClass = Class.forName(packageName + '.' + name); if (BaseProtocol.class.isAssignableFrom(protocolClass)) { - initProtocolServer((BaseProtocol) protocolClass.newInstance()); + BaseProtocol baseProtocol = (BaseProtocol) protocolClass.newInstance(); + protocols.put(baseProtocol.getName(), baseProtocol); + initProtocolServer(baseProtocol); } } } public void start() { - for (Object server: serverList) { - ((TrackerServer) server).start(); + for (TrackerServer server: serverList) { + server.start(); } } public void stop() { - for (Object server: serverList) { - ((TrackerServer) server).stop(); + for (TrackerServer server: serverList) { + server.stop(); } // Release resources @@ -89,4 +96,17 @@ public class ServerManager { } } + public Collection<String> getProtocolSuppportedCommands(String protocol) { + List<String> result = new ArrayList<>(); + + if (protocol != null) { + BaseProtocol baseProtocol = protocols.get(protocol); + for (String commandKey : baseProtocol.getSupportedCommands()) { + result.add(commandKey); + } + } + + return result; + } + } diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index 0017f13ee..18c4cb0ce 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -30,8 +30,8 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.sql.SQLException; -import java.util.Collection; + +import org.traccar.model.Device; @Path("devices") @Produces(MediaType.APPLICATION_JSON) diff --git a/src/org/traccar/api/resource/SupportedCommandResource.java b/src/org/traccar/api/resource/SupportedCommandResource.java new file mode 100644 index 000000000..393588d9b --- /dev/null +++ b/src/org/traccar/api/resource/SupportedCommandResource.java @@ -0,0 +1,48 @@ +/* + * Copyright 2016 Gabor Somogyi (gabor.g.somogyi@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.api.resource; + +import org.traccar.Context; +import org.traccar.api.BaseResource; +import org.traccar.model.Position; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import java.sql.SQLException; +import java.util.Collection; +import java.util.Collections; + +@Path("supportedcommands") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class SupportedCommandResource extends BaseResource { + + @GET + public Collection<String> get(@QueryParam("deviceId") long deviceId) throws SQLException { + Context.getPermissionsManager().checkDevice(getUserId(), deviceId); + Position lastPosition = Context.getConnectionManager().getLastPosition(deviceId); + if (lastPosition != null) { + return Context.getServerManager().getProtocolSuppportedCommands(lastPosition.getProtocol()); + } else { + return Collections.EMPTY_LIST; + } + } + +} diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 5a233411b..ac3be45ac 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -482,5 +482,4 @@ public class DataManager implements IdentityManager { .setObject(server) .executeUpdate(); } - } diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index fc78dce65..bcb0442f6 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -35,14 +35,15 @@ import org.traccar.api.ObjectMapperProvider; import org.traccar.api.ResourceErrorHandler; import org.traccar.api.SecurityRequestFilter; import org.traccar.api.resource.CommandResource; -import org.traccar.api.resource.DevicePermissionResource; -import org.traccar.api.resource.DeviceResource; import org.traccar.api.resource.GroupPermissionResource; -import org.traccar.api.resource.GroupResource; -import org.traccar.api.resource.PositionResource; import org.traccar.api.resource.ServerResource; import org.traccar.api.resource.SessionResource; +import org.traccar.api.resource.DevicePermissionResource; import org.traccar.api.resource.UserResource; +import org.traccar.api.resource.GroupResource; +import org.traccar.api.resource.DeviceResource; +import org.traccar.api.resource.PositionResource; +import org.traccar.api.resource.SupportedCommandResource; import org.traccar.helper.Log; import javax.naming.InitialContext; @@ -147,7 +148,7 @@ public class WebServer { resourceConfig.register(CorsResponseFilter.class); resourceConfig.registerClasses(ServerResource.class, SessionResource.class, CommandResource.class, GroupPermissionResource.class, DevicePermissionResource.class, UserResource.class, - GroupResource.class, DeviceResource.class, PositionResource.class); + GroupResource.class, DeviceResource.class, PositionResource.class, SupportedCommandResource.class); servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*"); handlers.addHandler(servletHandler); |