From 301f2d314f446410da78615ac76ce0c07571d445 Mon Sep 17 00:00:00 2001 From: Gábor Somogyi Date: Sun, 8 May 2016 13:06:25 +0200 Subject: Review fixes --- src/org/traccar/BaseProtocol.java | 1 + src/org/traccar/Protocol.java | 3 +++ src/org/traccar/ServerManager.java | 25 ---------------------- .../traccar/api/resource/CommandTypeResource.java | 9 +------- src/org/traccar/database/ActiveDevice.java | 14 ++++++++++++ src/org/traccar/model/CommandType.java | 14 +++++++----- web/app/store/CommandTypes.js | 12 +++++------ web/app/view/CommandDialog.js | 2 +- 8 files changed, 35 insertions(+), 45 deletions(-) diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 46a3fad5f..c77e61a81 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -41,6 +41,7 @@ public abstract class BaseProtocol implements Protocol { supportedCommands.addAll(Arrays.asList(commands)); } + @Override public Collection getSupportedCommands() { return supportedCommands; } diff --git a/src/org/traccar/Protocol.java b/src/org/traccar/Protocol.java index edf9bfb36..c99fd8ecb 100644 --- a/src/org/traccar/Protocol.java +++ b/src/org/traccar/Protocol.java @@ -3,12 +3,15 @@ package org.traccar; import org.traccar.database.ActiveDevice; import org.traccar.model.Command; +import java.util.Collection; import java.util.List; public interface Protocol { String getName(); + Collection getSupportedCommands(); + void sendCommand(ActiveDevice activeDevice, Command command); void initTrackerServers(List serverList); diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index cf49b3e2c..010438a44 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -15,26 +15,19 @@ */ package org.traccar; -import org.traccar.model.CommandType; - import java.io.File; 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 protocols = new HashMap<>(); private final List serverList = new LinkedList<>(); public ServerManager() throws Exception { @@ -70,7 +63,6 @@ public class ServerManager { Class protocolClass = Class.forName(packageName + '.' + name); if (BaseProtocol.class.isAssignableFrom(protocolClass)) { BaseProtocol baseProtocol = (BaseProtocol) protocolClass.newInstance(); - protocols.put(baseProtocol.getName(), baseProtocol); initProtocolServer(baseProtocol); } } @@ -98,21 +90,4 @@ public class ServerManager { } } - public Collection getProtocolCommandTypes(String protocol) { - List result = new ArrayList<>(); - - if (protocol != null) { - BaseProtocol baseProtocol = protocols.get(protocol); - if (baseProtocol != null) { - for (String commandKey : baseProtocol.getSupportedCommands()) { - CommandType commandType = new CommandType(); - commandType.setKey(commandKey); - result.add(commandType); - } - } - } - - return result; - } - } diff --git a/src/org/traccar/api/resource/CommandTypeResource.java b/src/org/traccar/api/resource/CommandTypeResource.java index 9f9ecf249..ce27f5241 100644 --- a/src/org/traccar/api/resource/CommandTypeResource.java +++ b/src/org/traccar/api/resource/CommandTypeResource.java @@ -17,7 +17,6 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; -import org.traccar.model.Position; import org.traccar.model.CommandType; import javax.ws.rs.Consumes; @@ -28,7 +27,6 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import java.sql.SQLException; import java.util.Collection; -import java.util.Collections; @Path("commandtypes") @Produces(MediaType.APPLICATION_JSON) @@ -38,12 +36,7 @@ public class CommandTypeResource extends BaseResource { @GET public Collection get(@QueryParam("deviceId") long deviceId) throws SQLException { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - Position lastPosition = Context.getConnectionManager().getLastPosition(deviceId); - if (lastPosition != null) { - return Context.getServerManager().getProtocolCommandTypes(lastPosition.getProtocol()); - } else { - return Collections.EMPTY_LIST; - } + return Context.getConnectionManager().getActiveDevice(deviceId).getCommandTypes(); } } diff --git a/src/org/traccar/database/ActiveDevice.java b/src/org/traccar/database/ActiveDevice.java index 746d3b393..3f2510af1 100644 --- a/src/org/traccar/database/ActiveDevice.java +++ b/src/org/traccar/database/ActiveDevice.java @@ -18,8 +18,12 @@ package org.traccar.database; import org.jboss.netty.channel.Channel; import org.traccar.Protocol; import org.traccar.model.Command; +import org.traccar.model.CommandType; import java.net.SocketAddress; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; public class ActiveDevice { @@ -43,6 +47,16 @@ public class ActiveDevice { return deviceId; } + public Collection getCommandTypes() { + List result = new ArrayList<>(); + + for (String commandKey : protocol.getSupportedCommands()) { + result.add(new CommandType(commandKey)); + } + + return result; + } + public void sendCommand(Command command) { protocol.sendCommand(this, command); } diff --git a/src/org/traccar/model/CommandType.java b/src/org/traccar/model/CommandType.java index 057dda4c5..210316f71 100644 --- a/src/org/traccar/model/CommandType.java +++ b/src/org/traccar/model/CommandType.java @@ -17,13 +17,17 @@ package org.traccar.model; public class CommandType { - private String key; + private String type; - public String getKey() { - return key; + public CommandType(String type) { + this.type = type; } - public void setKey(String key) { - this.key = key; + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; } } diff --git a/web/app/store/CommandTypes.js b/web/app/store/CommandTypes.js index b8cb03668..2ef190edc 100644 --- a/web/app/store/CommandTypes.js +++ b/web/app/store/CommandTypes.js @@ -16,7 +16,7 @@ Ext.define('Traccar.store.CommandTypes', { extend: 'Ext.data.Store', - fields: ['key', 'name'], + fields: ['type', 'name'], listeners: { 'beforeload' : function(store) { @@ -33,10 +33,10 @@ Ext.define('Traccar.store.CommandTypes', { type: 'json', getData: function(data) { Ext.each(data, function(entry) { - entry.name = entry.key; - if (typeof entry.key !== "undefined") { - var key = 'command' + entry.key.charAt(0).toUpperCase() + entry.key.slice(1); - var name = Strings[key]; + entry.name = entry.type; + if (typeof entry.type !== "undefined") { + var nameKey = 'command' + entry.type.charAt(0).toUpperCase() + entry.type.slice(1); + var name = Strings[nameKey]; if (typeof name !== "undefined") { entry.name = name; } @@ -44,6 +44,6 @@ Ext.define('Traccar.store.CommandTypes', { }); return data; } - }, + } } }); diff --git a/web/app/view/CommandDialog.js b/web/app/view/CommandDialog.js index 66cd190ea..d23b50d4c 100644 --- a/web/app/view/CommandDialog.js +++ b/web/app/view/CommandDialog.js @@ -32,7 +32,7 @@ Ext.define('Traccar.view.CommandDialog', { fieldLabel: Strings.commandType, store: 'CommandTypes', displayField: 'name', - valueField: 'key', + valueField: 'type', listeners: { select: 'onSelect' } -- cgit v1.2.3