From bf68718e05f811ffeb2b4b57acab56dca3e7de56 Mon Sep 17 00:00:00 2001 From: Gábor Somogyi Date: Wed, 27 Apr 2016 22:22:08 +0200 Subject: Command framework ugly hack version --- src/org/traccar/BaseProtocol.java | 5 +++ src/org/traccar/ServerManager.java | 26 +++++++++++++--- src/org/traccar/api/resource/DeviceResource.java | 13 ++++++++ src/org/traccar/database/DataManager.java | 7 ++++- src/org/traccar/model/SupportedCommand.java | 39 ++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 src/org/traccar/model/SupportedCommand.java (limited to 'src/org/traccar') diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 3af39a07c..58ca837ce 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -16,6 +16,7 @@ package org.traccar; import java.util.Arrays; +import java.util.Collection; import java.util.HashSet; import java.util.Set; import org.traccar.database.ActiveDevice; @@ -39,6 +40,10 @@ public abstract class BaseProtocol implements Protocol { supportedCommands.addAll(Arrays.asList(commands)); } + public Collection 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..dae693080 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -15,19 +15,20 @@ */ package org.traccar; +import org.traccar.model.SupportedCommand; + import java.io.File; import java.net.URI; import java.net.URL; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; -import java.util.Enumeration; -import java.util.LinkedList; -import java.util.List; +import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class ServerManager { + private final Map protocols = new HashMap<>(); private final List serverList = new LinkedList<>(); public ServerManager() throws Exception { @@ -62,7 +63,9 @@ 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); } } } @@ -89,4 +92,19 @@ public class ServerManager { } } + public Collection getProtocolSuppportedCommands(String protocol) { + ArrayList result = new ArrayList<>(); + + if (protocol != null) { + BaseProtocol baseProtocol = protocols.get(protocol); + for (String commandKey : baseProtocol.getSupportedCommands()) { + SupportedCommand supportedCommand = new SupportedCommand(); + supportedCommand.setKey(commandKey); + supportedCommand.setName(commandKey); + result.add(supportedCommand); + } + } + + return result; + } } diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index a4bfc1030..90d3ef675 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -19,6 +19,7 @@ import org.traccar.Context; import org.traccar.api.BaseResource; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collection; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -31,7 +32,12 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; + +import org.traccar.helper.Log; +import org.traccar.model.Command; import org.traccar.model.Device; +import org.traccar.model.Position; +import org.traccar.model.SupportedCommand; @Path("devices") @Produces(MediaType.APPLICATION_JSON) @@ -81,4 +87,11 @@ public class DeviceResource extends BaseResource { return Response.noContent().build(); } + @Path("{id}/supportedcommands") + @GET + public Collection get(@PathParam("id") long id) throws SQLException { + Context.getPermissionsManager().checkDevice(getUserId(), id); + Position latestPosition = Context.getDataManager().getLatestPosition(id); + return Context.getServerManager().getProtocolSuppportedCommands(latestPosition.getProtocol()); + } } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index eb0be9274..3dba215f6 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -472,6 +472,12 @@ public class DataManager implements IdentityManager { .executeQuery(Position.class); } + public Position getLatestPosition(long deviceId) throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectLatestPosition")) + .setLong("deviceId", deviceId) + .executeQuerySingle(Position.class); + } + public Server getServer() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectServers")) .executeQuerySingle(Server.class); @@ -482,5 +488,4 @@ public class DataManager implements IdentityManager { .setObject(server) .executeUpdate(); } - } diff --git a/src/org/traccar/model/SupportedCommand.java b/src/org/traccar/model/SupportedCommand.java new file mode 100644 index 000000000..a8f31ab30 --- /dev/null +++ b/src/org/traccar/model/SupportedCommand.java @@ -0,0 +1,39 @@ +/* + * 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.model; + +public class SupportedCommand { + + private String key; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} -- cgit v1.2.3