diff options
Diffstat (limited to 'src/org')
21 files changed, 123 insertions, 305 deletions
diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 86bebacc7..932c0d9cc 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -15,31 +15,31 @@ */ package org.traccar; +import org.traccar.model.Command; import org.traccar.database.ActiveDevice; -import org.traccar.command.CommandType; -import org.traccar.command.CommandTemplate; -import org.traccar.command.GpsCommand; -import java.util.HashMap; -import java.util.Map; +import java.util.*; public abstract class BaseProtocol implements Protocol { private final String name; - private Map<CommandType, CommandTemplate> commandTemplates = new HashMap<>(); + private final Set<String> supportedCommands = new HashSet<>(); public BaseProtocol(String name) { this.name = name; - this.initCommandsTemplates(commandTemplates); } public String getName() { return name; } + public void setSupportedCommands(String[] commands) { + supportedCommands.addAll(Arrays.asList(commands)); + } + @Override - public void sendCommand(ActiveDevice activeDevice, GpsCommand command) { - CommandTemplate commandMessage = commandTemplates.get(command.getType()); + public void sendCommand(ActiveDevice activeDevice, Command command) { + /*CommandTemplate commandMessage = commandTemplates.get(command.getType()); if (commandMessage == null) { throw new RuntimeException("The command " + command + " is not yet supported in protocol " + this.getName()); @@ -47,10 +47,7 @@ public abstract class BaseProtocol implements Protocol { Object response = commandMessage.applyTo(activeDevice, command); - activeDevice.write(response); - } - - protected void initCommandsTemplates(Map<CommandType, CommandTemplate> templates) { + activeDevice.write(response);*/ } } diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 61f11367c..285525e78 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -46,7 +46,7 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { Device device = Context.getIdentityManager().getDeviceByUniqueId(uniqueId); if (device != null) { deviceId = device.getId(); - Context.getConnectionManager().setActiveDevice(device.getUniqueId(), protocol, channel, remoteAddress); + Context.getConnectionManager().setActiveDevice(deviceId, protocol, channel, remoteAddress); return true; } else { deviceId = 0; diff --git a/src/org/traccar/Protocol.java b/src/org/traccar/Protocol.java index 08d75a0ea..ecda76142 100644 --- a/src/org/traccar/Protocol.java +++ b/src/org/traccar/Protocol.java @@ -1,7 +1,7 @@ package org.traccar; +import org.traccar.model.Command; import org.traccar.database.ActiveDevice; -import org.traccar.command.GpsCommand; import java.util.List; @@ -9,7 +9,7 @@ public interface Protocol { public String getName(); - void sendCommand(ActiveDevice activeDevice, GpsCommand command); + void sendCommand(ActiveDevice activeDevice, Command command); void initTrackerServers(List<TrackerServer> serverList); } diff --git a/src/org/traccar/command/CommandTemplate.java b/src/org/traccar/command/CommandTemplate.java deleted file mode 100644 index cdc9be7ab..000000000 --- a/src/org/traccar/command/CommandTemplate.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.traccar.command; - -import org.traccar.database.ActiveDevice; -import org.traccar.command.GpsCommand; - -public interface CommandTemplate<T extends GpsCommand> { - Object applyTo(ActiveDevice activeDevice, T command); -} diff --git a/src/org/traccar/command/CommandType.java b/src/org/traccar/command/CommandType.java deleted file mode 100644 index 56596fa41..000000000 --- a/src/org/traccar/command/CommandType.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.traccar.command; - -import org.traccar.model.Factory; - -public enum CommandType implements Factory { - STOP_POSITIONING(NoParameterCommand.class), - FIX_POSITIONING(FixPositioningCommand.class), - STOP_ENGINE(NoParameterCommand.class), - RESUME_ENGINE(NoParameterCommand.class); - - private final Class<? extends GpsCommand> commandClass; - - CommandType(Class<? extends GpsCommand> commandClass) { - this.commandClass = commandClass; - } - - @Override - public Object create() { - try { - return commandClass.newInstance(); - } catch (InstantiationException | IllegalAccessException error) { - throw new RuntimeException(error); - } - } -} diff --git a/src/org/traccar/command/CommandValueConversion.java b/src/org/traccar/command/CommandValueConversion.java deleted file mode 100644 index 5c8b75313..000000000 --- a/src/org/traccar/command/CommandValueConversion.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.traccar.command; - -public interface CommandValueConversion<T> { - public String convert(T value); -} diff --git a/src/org/traccar/command/Duration.java b/src/org/traccar/command/Duration.java deleted file mode 100644 index a2ed2c991..000000000 --- a/src/org/traccar/command/Duration.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.traccar.command; - -public class Duration { - - public enum TimeUnit { - SECOND("s"), MINUTE("m"), HOUR("h"); - - private final String commandFormat; - - TimeUnit(String commandFormat) { - this.commandFormat = commandFormat; - } - - public String getCommandFormat() { - return commandFormat; - } - } - - - private TimeUnit unit; - private int value; - - public TimeUnit getUnit() { - return unit; - } - - public void setUnit(TimeUnit unit) { - this.unit = unit; - } - - public int getValue() { - return value; - } - - public void setValue(int value) { - this.value = value; - } -} diff --git a/src/org/traccar/command/FixPositioningCommand.java b/src/org/traccar/command/FixPositioningCommand.java deleted file mode 100644 index 18d5f32fb..000000000 --- a/src/org/traccar/command/FixPositioningCommand.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.traccar.command; - -import java.util.HashMap; -import java.util.Map; - -public class FixPositioningCommand extends GpsCommand { - public static final String FREQUENCY = "frequency"; - - private Duration data; - - @Override - public Map<String, Object> getReplacements() { - Map<String, Object> replacements = new HashMap<>(); - replacements.put(FREQUENCY, data); - return replacements; - } - - public Duration getData() { - return data; - } - - public void setData(Duration data) { - this.data = data; - } -} diff --git a/src/org/traccar/command/GpsCommand.java b/src/org/traccar/command/GpsCommand.java deleted file mode 100644 index 7097c4807..000000000 --- a/src/org/traccar/command/GpsCommand.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.traccar.command; - -import java.util.Map; - -public abstract class GpsCommand { - public static final String UNIQUE_ID = "uniqueId"; - private String uniqueId; - private CommandType type; - - public String getUniqueId() { - return uniqueId; - } - - public void setUniqueId(String uniqueId) { - this.uniqueId = uniqueId; - } - - public CommandType getType() { - return type; - } - - public void setType(CommandType type) { - this.type = type; - } - - public abstract Map<String, Object> getReplacements(); -} diff --git a/src/org/traccar/command/NoParameterCommand.java b/src/org/traccar/command/NoParameterCommand.java deleted file mode 100644 index f4e139b7b..000000000 --- a/src/org/traccar/command/NoParameterCommand.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.traccar.command; - -import java.util.HashMap; -import java.util.Map; - -public class NoParameterCommand extends GpsCommand { - @Override - public Map<String, Object> getReplacements() { - return new HashMap<>(); - } -} diff --git a/src/org/traccar/command/StringCommandTemplate.java b/src/org/traccar/command/StringCommandTemplate.java deleted file mode 100644 index 3f326e534..000000000 --- a/src/org/traccar/command/StringCommandTemplate.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.traccar.command; - -import org.traccar.database.ActiveDevice; - -import java.util.HashMap; -import java.util.Map; - -public class StringCommandTemplate<T extends GpsCommand> implements CommandTemplate<T> { - - private String messageTemplate; - private Map<Class<?>, CommandValueConversion> converters = new HashMap<>(); - - public StringCommandTemplate(String template, Object... replacements) { - this.messageTemplate = String.format(template, replacements); - } - - @Override - public Object applyTo(ActiveDevice activeDevice, T command) { - String currentMessage = messageTemplate; - currentMessage = this.replace(currentMessage, GpsCommand.UNIQUE_ID, activeDevice.getUniqueId()); - - Map<String, Object> replacements = command.getReplacements(); - - for (Map.Entry<String, Object> entry : replacements.entrySet()) { - currentMessage = this.replace(currentMessage, entry.getKey(), entry.getValue()); - } - - return currentMessage; - } - - public CommandTemplate addConverter(Class<?> type, CommandValueConversion converter) { - converters.put(type, converter); - return this; - } - - protected CommandValueConversion getConverter(Class<?> type) { - return converters.containsKey(type) ? converters.get(type) : idConverter(); - } - - private CommandValueConversion idConverter() { - return new CommandValueConversion() { - @Override - public String convert(Object value) { - return value.toString(); - } - }; - } - - private String replace(String currentMessage, String key, Object value) { - String replacementValue = getConverter(value.getClass()).convert(value); - return currentMessage.replace("[" + key + "]", replacementValue); - } - -} diff --git a/src/org/traccar/database/ActiveDevice.java b/src/org/traccar/database/ActiveDevice.java index f94e1b12a..33d9adad3 100644 --- a/src/org/traccar/database/ActiveDevice.java +++ b/src/org/traccar/database/ActiveDevice.java @@ -17,19 +17,19 @@ package org.traccar.database; import org.jboss.netty.channel.Channel; import org.traccar.Protocol; -import org.traccar.command.GpsCommand; +import org.traccar.model.Command; import java.net.SocketAddress; public class ActiveDevice { - private String uniqueId; + private long deviceId; private Protocol protocol; private Channel channel; private SocketAddress remoteAddress; - public ActiveDevice(String uniqueId, Protocol protocol, Channel channel, SocketAddress remoteAddress) { - this.uniqueId = uniqueId; + public ActiveDevice(long deviceId, Protocol protocol, Channel channel, SocketAddress remoteAddress) { + this.deviceId = deviceId; this.protocol = protocol; this.channel = channel; this.remoteAddress = remoteAddress; @@ -39,11 +39,11 @@ public class ActiveDevice { return channel; } - public String getUniqueId() { - return uniqueId; + public long getDeviceId() { + return deviceId; } - public void sendCommand(GpsCommand command) { + public void sendCommand(Command command) { protocol.sendCommand(this, command); } diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index dc125d7b0..39ec465e6 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -31,7 +31,7 @@ import org.traccar.model.Position; public class ConnectionManager { - private final Map<String, ActiveDevice> activeDevices = new HashMap<>(); + private final Map<Long, ActiveDevice> activeDevices = new HashMap<>(); private final Map<Long, Position> positions = new HashMap<>(); private final Map<Long, Set<DataCacheListener>> listeners = new HashMap<>(); @@ -47,12 +47,12 @@ public class ConnectionManager { } } - public void setActiveDevice(String uniqueId, Protocol protocol, Channel channel, SocketAddress remoteAddress) { - activeDevices.put(uniqueId, new ActiveDevice(uniqueId, protocol, channel, remoteAddress)); + public void setActiveDevice(long deviceId, Protocol protocol, Channel channel, SocketAddress remoteAddress) { + activeDevices.put(deviceId, new ActiveDevice(deviceId, protocol, channel, remoteAddress)); } - public ActiveDevice getActiveDevice(String uniqueId) { - return activeDevices.get(uniqueId); + public ActiveDevice getActiveDevice(long deviceId) { + return activeDevices.get(deviceId); } public synchronized void update(Position position) { diff --git a/src/org/traccar/model/Command.java b/src/org/traccar/model/Command.java new file mode 100644 index 000000000..58aa61a7d --- /dev/null +++ b/src/org/traccar/model/Command.java @@ -0,0 +1,32 @@ +package org.traccar.model; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class Command implements Factory { + + @Override + public Command create() { + return new Command(); + } + + private long deviceId; + public long getDeviceId() { return deviceId; } + public void setDeviceId(long deviceId) { this.deviceId = deviceId; } + + private String type; + public String getType() { return type; } + public void setType(String type) { this.type = type; } + + private Map<String, Object> other = new LinkedHashMap<>(); + public Map<String, Object> getOther() { return other; } + public void setOther(Map<String, Object> other) { this.other = other; } + + public static final String TYPE_POSITION_STOP = "positionStop"; + public static final String TYPE_POSITION_FIX = "positionFix"; + public static final String TYPE_ENGINE_STOP = "engineStop"; + public static final String TYPE_ENGINE_RESUME = "engineResume"; + + public static final String KEY_FREQUENCY = "frequency"; + +} diff --git a/src/org/traccar/model/Event.java b/src/org/traccar/model/Event.java index 0768ec9f3..b42a776ae 100644 --- a/src/org/traccar/model/Event.java +++ b/src/org/traccar/model/Event.java @@ -42,13 +42,14 @@ public abstract class Event { public void setDeviceTime(Date deviceTime) { this.deviceTime = deviceTime; } private Map<String, Object> other = new LinkedHashMap<>(); + public Map<String, Object> getOther() { return other; } + public void setOther(Map<String, Object> other) { this.other = other; } + public void set(String key, Object value) { if (value != null && (!(value instanceof String) || !((String) value).isEmpty())) { other.put(key, value); } } - public void setOther(Map<String, Object> other) { this.other = other; } - public Map<String, Object> getOther() { return other; } public static final String KEY_INDEX = "index"; public static final String KEY_HDOP = "hdop"; diff --git a/src/org/traccar/protocol/Gps103Protocol.java b/src/org/traccar/protocol/Gps103Protocol.java index 84275a556..3f04ede4e 100644 --- a/src/org/traccar/protocol/Gps103Protocol.java +++ b/src/org/traccar/protocol/Gps103Protocol.java @@ -23,16 +23,8 @@ import org.jboss.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; import org.traccar.CharacterDelimiterFrameDecoder; import org.traccar.TrackerServer; -import org.traccar.command.CommandType; -import org.traccar.command.Duration; -import org.traccar.command.FixPositioningCommand; -import org.traccar.command.GpsCommand; -import org.traccar.command.CommandTemplate; -import org.traccar.command.CommandValueConversion; -import org.traccar.command.StringCommandTemplate; import java.util.List; -import java.util.Map; public class Gps103Protocol extends BaseProtocol { @@ -40,19 +32,19 @@ public class Gps103Protocol extends BaseProtocol { super("gps103"); } - @Override + /*@Override protected void initCommandsTemplates(Map<CommandType, CommandTemplate> templates) { - templates.put(CommandType.STOP_POSITIONING, new StringCommandTemplate("**,imei:[%s],A", GpsCommand.UNIQUE_ID)); - templates.put(CommandType.FIX_POSITIONING, new StringCommandTemplate("**,imei:[%s],C,[%s]", GpsCommand.UNIQUE_ID, FixPositioningCommand.FREQUENCY) + templates.put(CommandType.STOP_POSITIONING, new StringCommandTemplate("**,imei:[%s],A", Command.UNIQUE_ID)); + templates.put(CommandType.FIX_POSITIONING, new StringCommandTemplate("**,imei:[%s],C,[%s]", Command.UNIQUE_ID, FixPositioningCommand.FREQUENCY) .addConverter(Duration.class, new CommandValueConversion<Duration>() { @Override public String convert(Duration value) { return String.format("%02d%s", value.getValue(), value.getUnit().getCommandFormat()); } })); - templates.put(CommandType.RESUME_ENGINE, new StringCommandTemplate("**,imei:[%s],J", GpsCommand.UNIQUE_ID)); - templates.put(CommandType.STOP_ENGINE, new StringCommandTemplate("**,imei:[%s],K", GpsCommand.UNIQUE_ID)); - } + templates.put(CommandType.RESUME_ENGINE, new StringCommandTemplate("**,imei:[%s],J", Command.UNIQUE_ID)); + templates.put(CommandType.STOP_ENGINE, new StringCommandTemplate("**,imei:[%s],K", Command.UNIQUE_ID)); + }*/ @Override public void initTrackerServers(List<TrackerServer> serverList) { diff --git a/src/org/traccar/protocol/GpsMarkerProtocol.java b/src/org/traccar/protocol/GpsMarkerProtocol.java index 0ef8cad70..7961e2340 100644 --- a/src/org/traccar/protocol/GpsMarkerProtocol.java +++ b/src/org/traccar/protocol/GpsMarkerProtocol.java @@ -23,7 +23,6 @@ import org.jboss.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; import org.traccar.CharacterDelimiterFrameDecoder; import org.traccar.TrackerServer; -import org.traccar.command.*; import java.util.List; import java.util.Map; diff --git a/src/org/traccar/protocol/Gt06Protocol.java b/src/org/traccar/protocol/Gt06Protocol.java index d4bd48288..82d706e20 100644 --- a/src/org/traccar/protocol/Gt06Protocol.java +++ b/src/org/traccar/protocol/Gt06Protocol.java @@ -23,9 +23,6 @@ import org.traccar.BaseProtocol; import org.traccar.TrackerServer; import org.traccar.database.ActiveDevice; import org.traccar.helper.Crc; -import org.traccar.command.CommandType; -import org.traccar.command.NoParameterCommand; -import org.traccar.command.CommandTemplate; import java.util.List; import java.util.Map; @@ -36,11 +33,11 @@ public class Gt06Protocol extends BaseProtocol { super("gt06"); } - @Override + /*@Override protected void initCommandsTemplates(Map<CommandType, CommandTemplate> templates) { templates.put(CommandType.STOP_ENGINE, new Gt06CommandTemplate("DYD#")); templates.put(CommandType.RESUME_ENGINE, new Gt06CommandTemplate("HFYD#")); - } + }*/ @Override public void initTrackerServers(List<TrackerServer> serverList) { @@ -53,7 +50,7 @@ public class Gt06Protocol extends BaseProtocol { }); } - class Gt06CommandTemplate implements CommandTemplate<NoParameterCommand> { + /*class Gt06CommandTemplate implements CommandTemplate<NoParameterCommand> { private String commandContent; @@ -65,7 +62,7 @@ public class Gt06Protocol extends BaseProtocol { public Object applyTo(ActiveDevice activeDevice, NoParameterCommand command) { int serverFlagBit = 0x0; int commandLength = serverFlagBit + commandContent.length(); - int packetLength = 0x80 /*Protocol Number */ + commandContent.length() /* Information Content */ + 2 /*Information Serial Number */+ 2 /*Error Check */; + int packetLength = 0x80 /*Protocol Number + commandContent.length() /* Information Content + 2 /*Information Serial Number + 2 /*Error Check ; int informationSerialNumber = 1; @@ -90,6 +87,6 @@ public class Gt06Protocol extends BaseProtocol { return response; } - } + }*/ } diff --git a/src/org/traccar/web/CommandServlet.java b/src/org/traccar/web/CommandServlet.java new file mode 100644 index 000000000..049a0534d --- /dev/null +++ b/src/org/traccar/web/CommandServlet.java @@ -0,0 +1,51 @@ +package org.traccar.web; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.traccar.Context; +import org.traccar.model.Command; +import org.traccar.database.ActiveDevice; + +public class CommandServlet extends BaseServlet { + + @Override + protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { + + switch (command) { + case "/send": + send(req, resp); + return true; + case "/raw": + raw(req, resp); + return true; + default: + return false; + } + } + + public ActiveDevice getActiveDevice(long deviceId) { + ActiveDevice activeDevice = Context.getConnectionManager().getActiveDevice(deviceId); + if (activeDevice == null) { + throw new RuntimeException("The device is not registered on the server"); + } + return activeDevice; + } + + private void send(HttpServletRequest req, HttpServletResponse resp) throws Exception { + + Command command = JsonConverter.objectFromJson(req.getReader(), new Command()); + getActiveDevice(command.getDeviceId()).write(command); + sendResponse(resp.getWriter(), true); + } + + private void raw(HttpServletRequest req, HttpServletResponse resp) throws Exception { + + JsonObject json = Json.createReader(req.getReader()).readObject(); + long deviceId = json.getJsonNumber("deviceId").longValue(); + String command = json.getString("command"); + getActiveDevice(deviceId).write(command); + sendResponse(resp.getWriter(), true); + } +} diff --git a/src/org/traccar/web/CommandsServlet.java b/src/org/traccar/web/CommandsServlet.java deleted file mode 100644 index b12c0fa53..000000000 --- a/src/org/traccar/web/CommandsServlet.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.traccar.web; - -import javax.json.Json; -import javax.json.JsonObject; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.traccar.Context; -import org.traccar.command.CommandType; -import org.traccar.command.GpsCommand; -import org.traccar.database.ActiveDevice; - -public class CommandsServlet extends BaseServlet { - - @Override - protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { - - switch (command) { - case "/send": - send(req, resp); - return true; - case "/raw": - sendRawCommand(req, resp); - return true; - default: - return false; - } - } - - private void send(HttpServletRequest req, HttpServletResponse resp) throws Exception { - GpsCommand command = JsonConverter.<GpsCommand>enumObjectFromJson(req.getReader(), new EnumFactory(CommandType.class, "type")); - - String uniqueId = command.getUniqueId(); - - ActiveDevice activeDevice = Context.getConnectionManager().getActiveDevice(uniqueId); - if(activeDevice == null) { - throw new RuntimeException("The device has not yet registered to the server"); - } - - activeDevice.sendCommand(command); - - sendResponse(resp.getWriter(), JsonConverter.objectToJson(new Object())); - } - - private void sendRawCommand(HttpServletRequest req, HttpServletResponse resp) throws Exception { - JsonObject json = Json.createReader(req.getReader()).readObject(); - String uniqueId = json.getString("uniqueId"); - - ActiveDevice activeDevice = Context.getConnectionManager().getActiveDevice(uniqueId); - if(activeDevice == null) { - throw new RuntimeException("The device has not yet registered to the server"); - } - - String command = json.getString("command"); - activeDevice.write(command); - - sendResponse(resp.getWriter(), JsonConverter.objectToJson(new Object())); - } -} diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 698796467..38345ffe7 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -74,7 +74,7 @@ public class WebServer { servletHandler.addServlet(new ServletHolder(new UserServlet()), "/user/*"); servletHandler.addServlet(new ServletHolder(new DeviceServlet()), "/device/*"); servletHandler.addServlet(new ServletHolder(new PositionServlet()), "/position/*"); - servletHandler.addServlet(new ServletHolder(new CommandsServlet()), "/commands/*"); + servletHandler.addServlet(new ServletHolder(new CommandServlet()), "/command/*"); servletHandler.addServlet(new ServletHolder(new MainServlet()), "/*"); /*ResourceHandler mobileResourceHandler = new ResourceHandler(); |