diff options
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/BaseProtocol.java | 36 | ||||
-rw-r--r-- | src/org/traccar/Protocol.java | 4 | ||||
-rw-r--r-- | src/org/traccar/ServerManager.java | 10 | ||||
-rw-r--r-- | src/org/traccar/api/resource/CommandResource.java | 6 | ||||
-rw-r--r-- | src/org/traccar/api/resource/CommandTypeResource.java | 6 | ||||
-rw-r--r-- | src/org/traccar/database/ActiveDevice.java | 16 | ||||
-rw-r--r-- | src/org/traccar/database/DeviceManager.java | 50 | ||||
-rw-r--r-- | src/org/traccar/model/Command.java | 12 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationSms.java | 4 | ||||
-rw-r--r-- | src/org/traccar/protocol/GranitProtocol.java | 7 | ||||
-rw-r--r-- | src/org/traccar/protocol/GranitProtocolSmsEncoder.java | 38 | ||||
-rw-r--r-- | src/org/traccar/protocol/WondexProtocol.java | 7 | ||||
-rw-r--r-- | src/org/traccar/smpp/ClientSmppSessionHandler.java | 8 | ||||
-rw-r--r-- | src/org/traccar/smpp/SmppClient.java | 50 |
14 files changed, 204 insertions, 50 deletions
diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 59331d7cc..bccc4e184 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,9 @@ public abstract class BaseProtocol implements Protocol { private final String name; private final Set<String> supportedCommands = new HashSet<>(); + private final Set<String> supportedSmsCommands = new HashSet<>(); + + private StringProtocolEncoder smsEncoder = null; public BaseProtocol(String name) { this.name = name; @@ -44,6 +47,10 @@ public abstract class BaseProtocol implements Protocol { supportedCommands.addAll(Arrays.asList(commands)); } + public void setSupportedSmsCommands(String... commands) { + supportedSmsCommands.addAll(Arrays.asList(commands)); + } + @Override public Collection<String> getSupportedCommands() { Set<String> commands = new HashSet<>(supportedCommands); @@ -52,6 +59,13 @@ public abstract class BaseProtocol implements Protocol { } @Override + public Collection<String> getSupportedSmsCommands() { + Set<String> commands = new HashSet<>(supportedSmsCommands); + commands.add(Command.TYPE_CUSTOM); + return commands; + } + + @Override public void sendCommand(ActiveDevice activeDevice, Command command) { if (supportedCommands.contains(command.getType())) { activeDevice.write(command); @@ -67,4 +81,24 @@ public abstract class BaseProtocol implements Protocol { } } + public void setSmsEncoder(StringProtocolEncoder smsEncoder) { + this.smsEncoder = smsEncoder; + } + + @Override + public void sendSmsCommand(String phone, Command command) throws Exception { + if (Context.getSmppManager() != null) { + if (command.getType().equals(Command.TYPE_CUSTOM)) { + Context.getSmppManager().sendMessageSync(phone, command.getString(Command.KEY_DATA), true); + } else if (supportedSmsCommands.contains(command.getType()) && smsEncoder != null) { + Context.getSmppManager().sendMessageSync(phone, (String) smsEncoder.encodeCommand(command), true); + } else { + throw new RuntimeException( + "Command " + command.getType() + " is not supported in protocol " + getName()); + } + } else { + throw new RuntimeException("SMPP client is not enabled"); + } + } + } diff --git a/src/org/traccar/Protocol.java b/src/org/traccar/Protocol.java index c99fd8ecb..f6a4fbebb 100644 --- a/src/org/traccar/Protocol.java +++ b/src/org/traccar/Protocol.java @@ -16,4 +16,8 @@ public interface Protocol { void initTrackerServers(List<TrackerServer> serverList); + Collection<String> getSupportedSmsCommands(); + + void sendSmsCommand(String phone, Command command) throws Exception; + } diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index 953428b8f..9b1e2650d 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,12 +23,15 @@ import java.nio.charset.StandardCharsets; import java.util.Enumeration; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class ServerManager { private final List<TrackerServer> serverList = new LinkedList<>(); + private final Map<String, BaseProtocol> protocolList = new ConcurrentHashMap<>(); public ServerManager() throws Exception { @@ -64,10 +67,15 @@ public class ServerManager { if (BaseProtocol.class.isAssignableFrom(protocolClass)) { BaseProtocol baseProtocol = (BaseProtocol) protocolClass.newInstance(); initProtocolServer(baseProtocol); + protocolList.put(baseProtocol.getName(), baseProtocol); } } } + public BaseProtocol getProtocol(String name) { + return protocolList.get(name); + } + public void start() { for (TrackerServer server: serverList) { server.start(); diff --git a/src/org/traccar/api/resource/CommandResource.java b/src/org/traccar/api/resource/CommandResource.java index 41b263bf9..9ed92d3d5 100644 --- a/src/org/traccar/api/resource/CommandResource.java +++ b/src/org/traccar/api/resource/CommandResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,10 +32,10 @@ import javax.ws.rs.core.Response; public class CommandResource extends BaseResource { @POST - public Response add(Command entity) { + public Response add(Command entity) throws Exception { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getConnectionManager().getActiveDevice(entity.getDeviceId()).sendCommand(entity); + Context.getDeviceManager().sendCommand(entity); return Response.ok(entity).build(); } diff --git a/src/org/traccar/api/resource/CommandTypeResource.java b/src/org/traccar/api/resource/CommandTypeResource.java index ce27f5241..3ee773fbf 100644 --- a/src/org/traccar/api/resource/CommandTypeResource.java +++ b/src/org/traccar/api/resource/CommandTypeResource.java @@ -1,5 +1,6 @@ /* * Copyright 2016 Gabor Somogyi (gabor.g.somogyi@gmail.com) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +26,6 @@ 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; @Path("commandtypes") @@ -34,9 +34,9 @@ import java.util.Collection; public class CommandTypeResource extends BaseResource { @GET - public Collection<CommandType> get(@QueryParam("deviceId") long deviceId) throws SQLException { + public Collection<CommandType> get(@QueryParam("deviceId") long deviceId, @QueryParam("sms") boolean sms) { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - return Context.getConnectionManager().getActiveDevice(deviceId).getCommandTypes(); + return Context.getDeviceManager().getCommandTypes(deviceId, sms); } } diff --git a/src/org/traccar/database/ActiveDevice.java b/src/org/traccar/database/ActiveDevice.java index 6109bc517..9c96382fe 100644 --- a/src/org/traccar/database/ActiveDevice.java +++ b/src/org/traccar/database/ActiveDevice.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,12 +18,8 @@ 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 { @@ -47,16 +43,6 @@ public class ActiveDevice { return deviceId; } - public Collection<CommandType> getCommandTypes() { - List<CommandType> 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/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 8e75903db..8b28bec9d 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -26,9 +26,12 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; +import org.traccar.BaseProtocol; import org.traccar.Config; import org.traccar.Context; import org.traccar.helper.Log; +import org.traccar.model.Command; +import org.traccar.model.CommandType; import org.traccar.model.Device; import org.traccar.model.DeviceTotalDistance; import org.traccar.model.Group; @@ -53,11 +56,14 @@ public class DeviceManager implements IdentityManager { private final Map<Long, Position> positions = new ConcurrentHashMap<>(); + private boolean fallbackToSms; + public DeviceManager(DataManager dataManager) { this.dataManager = dataManager; this.config = Context.getConfig(); dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000; lookupGroupsAttribute = config.getBoolean("deviceManager.lookupGroupsAttribute"); + fallbackToSms = config.getBoolean("command.fallbackToSms"); if (dataManager != null) { try { updateGroupCache(true); @@ -420,4 +426,48 @@ public class DeviceManager implements IdentityManager { throw new IllegalArgumentException(); } } + + public void sendCommand(Command command) throws Exception { + long deviceId = command.getDeviceId(); + if (command.getSms()) { + Position lastPosition = getLastPosition(deviceId); + if (lastPosition != null) { + BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol()); + protocol.sendSmsCommand(devicesById.get(deviceId).getPhone(), command); + } else if (command.getType().equals(Command.TYPE_CUSTOM)) { + Context.getSmppManager().sendMessageSync(devicesById.get(deviceId).getPhone(), + command.getString(Command.KEY_DATA), true); + } else { + throw new RuntimeException("Command " + command.getType() + " is not supported"); + } + } else { + ActiveDevice activeDevice = Context.getConnectionManager().getActiveDevice(deviceId); + if (activeDevice != null) { + activeDevice.sendCommand(command); + } else { + if (fallbackToSms) { + command.setSms(true); + sendCommand(command); + } else { + throw new RuntimeException("Device is not online"); + } + } + } + } + + public Collection<CommandType> getCommandTypes(long deviceId, boolean sms) { + List<CommandType> result = new ArrayList<>(); + Position lastPosition = Context.getDeviceManager().getLastPosition(deviceId); + if (lastPosition != null) { + BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol()); + Collection<String> commands; + commands = sms ? protocol.getSupportedSmsCommands() : protocol.getSupportedCommands(); + for (String commandKey : commands) { + result.add(new CommandType(commandKey)); + } + } else { + result.add(new CommandType(Command.TYPE_CUSTOM)); + } + return result; + } } diff --git a/src/org/traccar/model/Command.java b/src/org/traccar/model/Command.java index 84f5d95ce..8d4d24b89 100644 --- a/src/org/traccar/model/Command.java +++ b/src/org/traccar/model/Command.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,4 +66,14 @@ public class Command extends Message { public static final String KEY_INDEX = "index"; public static final String KEY_PHONE = "phone"; + private boolean sms; + + public boolean getSms() { + return sms; + } + + public void setSms(boolean sms) { + this.sms = sms; + } + } diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index cb5dd563a..7b265e3ce 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -35,7 +35,7 @@ public final class NotificationSms { User user = Context.getPermissionsManager().getUser(userId); if (Context.getSmppManager() != null && user.getPhone() != null) { Context.getSmppManager().sendMessageAsync(user.getPhone(), - NotificationFormatter.formatSmsMessage(userId, event, position)); + NotificationFormatter.formatSmsMessage(userId, event, position), false); } } @@ -44,7 +44,7 @@ public final class NotificationSms { User user = Context.getPermissionsManager().getUser(userId); if (Context.getSmppManager() != null && user.getPhone() != null) { Context.getSmppManager().sendMessageSync(user.getPhone(), - NotificationFormatter.formatSmsMessage(userId, event, position)); + NotificationFormatter.formatSmsMessage(userId, event, position), false); } } } diff --git a/src/org/traccar/protocol/GranitProtocol.java b/src/org/traccar/protocol/GranitProtocol.java index a5d5458f0..1ae42ba46 100644 --- a/src/org/traccar/protocol/GranitProtocol.java +++ b/src/org/traccar/protocol/GranitProtocol.java @@ -1,5 +1,6 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +33,10 @@ public class GranitProtocol extends BaseProtocol { Command.TYPE_IDENTIFICATION, Command.TYPE_REBOOT_DEVICE, Command.TYPE_POSITION_SINGLE); + setSmsEncoder(new GranitProtocolSmsEncoder()); + setSupportedSmsCommands( + Command.TYPE_REBOOT_DEVICE, + Command.TYPE_POSITION_PERIODIC); } @Override diff --git a/src/org/traccar/protocol/GranitProtocolSmsEncoder.java b/src/org/traccar/protocol/GranitProtocolSmsEncoder.java new file mode 100644 index 000000000..668e5d4d3 --- /dev/null +++ b/src/org/traccar/protocol/GranitProtocolSmsEncoder.java @@ -0,0 +1,38 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * 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.protocol; + +import org.traccar.StringProtocolEncoder; +import org.traccar.helper.Log; +import org.traccar.model.Command; + +public class GranitProtocolSmsEncoder extends StringProtocolEncoder { + + @Override + protected String encodeCommand(Command command) { + switch (command.getType()) { + case Command.TYPE_REBOOT_DEVICE: + return "BB+RESET"; + case Command.TYPE_POSITION_PERIODIC: + return formatCommand(command, "BB+BBMD={%s}", Command.KEY_FREQUENCY); + default: + Log.warning(new UnsupportedOperationException(command.getType())); + return null; + } + } + +} diff --git a/src/org/traccar/protocol/WondexProtocol.java b/src/org/traccar/protocol/WondexProtocol.java index 0cc21d2b4..25d8fb175 100644 --- a/src/org/traccar/protocol/WondexProtocol.java +++ b/src/org/traccar/protocol/WondexProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,11 @@ public class WondexProtocol extends BaseProtocol { Command.TYPE_REBOOT_DEVICE, Command.TYPE_POSITION_SINGLE, Command.TYPE_IDENTIFICATION); + setSmsEncoder(new WondexProtocolEncoder()); + setSupportedSmsCommands( + Command.TYPE_REBOOT_DEVICE, + Command.TYPE_POSITION_SINGLE, + Command.TYPE_IDENTIFICATION); } @Override diff --git a/src/org/traccar/smpp/ClientSmppSessionHandler.java b/src/org/traccar/smpp/ClientSmppSessionHandler.java index 721243f9f..2a538a40f 100644 --- a/src/org/traccar/smpp/ClientSmppSessionHandler.java +++ b/src/org/traccar/smpp/ClientSmppSessionHandler.java @@ -44,14 +44,14 @@ public class ClientSmppSessionHandler extends DefaultSmppSessionHandler { try { if (request instanceof DeliverSm) { if (request.getOptionalParameters() != null) { - Log.debug("Message Delivered: " + Log.debug("SMS Message Delivered: " + request.getOptionalParameter(SmppConstants.TAG_RECEIPTED_MSG_ID).getValueAsString() + ", State: " + request.getOptionalParameter(SmppConstants.TAG_MSG_STATE).getValueAsByte()); } else { - Log.debug("Message Received: " + Log.debug("SMS Message Received: " + CharsetUtil.decode(((DeliverSm) request).getShortMessage(), - smppClient.mapDataCodingToCharset(((DeliverSm) request).getDataCoding())) + smppClient.mapDataCodingToCharset(((DeliverSm) request).getDataCoding())).trim() + ", Source Address: " + ((DeliverSm) request).getSourceAddress().getAddress()); } @@ -68,7 +68,7 @@ public class ClientSmppSessionHandler extends DefaultSmppSessionHandler { @Override public void fireChannelUnexpectedlyClosed() { - Log.warning("Smpp session channel unexpectedly closed"); + Log.warning("SMPP session channel unexpectedly closed"); smppClient.scheduleReconnect(); } } diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/smpp/SmppClient.java index 0bec60fd2..317d6debf 100644 --- a/src/org/traccar/smpp/SmppClient.java +++ b/src/org/traccar/smpp/SmppClient.java @@ -60,8 +60,10 @@ public class SmppClient { private String sourceAddress; private int submitTimeout; - private String charsetName; - private byte dataCoding; + private String notificationsCharsetName; + private byte notificationsDataCoding; + private String commandsCharsetName; + private byte commandsDataCoding; private byte sourceTon; private byte sourceNpi; @@ -84,8 +86,15 @@ public class SmppClient { sourceAddress = Context.getConfig().getString("sms.smpp.sourceAddress", ""); submitTimeout = Context.getConfig().getInteger("sms.smpp.submitTimeout", 10000); - charsetName = Context.getConfig().getString("sms.smpp.charsetName", CharsetUtil.NAME_UCS_2); - dataCoding = (byte) Context.getConfig().getInteger("sms.smpp.dataCoding", SmppConstants.DATA_CODING_UCS2); + notificationsCharsetName = Context.getConfig().getString("sms.smpp.notificationsCharset", + CharsetUtil.NAME_UCS_2); + notificationsDataCoding = (byte) Context.getConfig().getInteger("sms.smpp.notificationsDataCoding", + SmppConstants.DATA_CODING_UCS2); + commandsCharsetName = Context.getConfig().getString("sms.smpp.commandsCharset", + CharsetUtil.NAME_GSM); + commandsDataCoding = (byte) Context.getConfig().getInteger("sms.smpp.commandsDataCoding", + SmppConstants.DATA_CODING_DEFAULT); + sourceTon = (byte) Context.getConfig().getInteger("sms.smpp.sourceTon", SmppConstants.TON_ALPHANUMERIC); sourceNpi = (byte) Context.getConfig().getInteger("sms.smpp.sourceNpi", SmppConstants.NPI_UNKNOWN); @@ -140,10 +149,10 @@ public class SmppClient { smppSession = clientBootstrap.bind(sessionConfig, sessionHandler); stopReconnectionkTask(); runEnquireLinkTask(); - Log.info("Smpp session connected"); + Log.info("SMPP session connected"); } catch (SmppTimeoutException | SmppChannelException | UnrecoverablePduException | InterruptedException error) { - Log.warning("Unable to connect to smpp server: ", error); + Log.warning("Unable to connect to SMPP server: ", error); } } @@ -180,37 +189,42 @@ public class SmppClient { private void destroySession() { if (smppSession != null) { - Log.debug("Cleaning up smpp session... "); + Log.debug("Cleaning up SMPP session... "); smppSession.destroy(); smppSession = null; } } - public synchronized void sendMessageSync(String destAddress, String message) throws RecoverablePduException, - UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException { + public synchronized void sendMessageSync(String destAddress, String message, boolean command) + throws RecoverablePduException, UnrecoverablePduException, SmppTimeoutException, SmppChannelException, + InterruptedException, IllegalStateException { if (getSession() != null && getSession().isBound()) { - byte[] textBytes = CharsetUtil.encode(message, charsetName); - SubmitSm submit = new SubmitSm(); + byte[] textBytes; + textBytes = CharsetUtil.encode(message, command ? commandsCharsetName : notificationsCharsetName); + submit.setDataCoding(command ? commandsDataCoding : notificationsDataCoding); + submit.setShortMessage(textBytes); submit.setSourceAddress(new Address(sourceTon, sourceNpi, sourceAddress)); submit.setDestAddress(new Address(destTon, destNpi, destAddress)); - submit.setDataCoding(dataCoding); - submit.setShortMessage(textBytes); SubmitSmResp submitResponce = getSession().submit(submit, submitTimeout); - Log.debug("SMS submited, msg_id: " + submitResponce.getMessageId()); + if (submitResponce.getCommandStatus() == SmppConstants.STATUS_OK) { + Log.debug("SMS submitted, message id: " + submitResponce.getMessageId()); + } else { + throw new IllegalStateException(submitResponce.getResultMessage()); + } } else { - throw new SmppChannelException("Smpp session is not connected"); + throw new SmppChannelException("SMPP session is not connected"); } } - public void sendMessageAsync(final String destAddress, final String message) { + public void sendMessageAsync(final String destAddress, final String message, final boolean command) { executorService.execute(new Runnable() { @Override public void run() { try { - sendMessageSync(destAddress, message); + sendMessageSync(destAddress, message, command); } catch (InterruptedException | RecoverablePduException | UnrecoverablePduException - | SmppTimeoutException | SmppChannelException error) { + | SmppTimeoutException | SmppChannelException | IllegalStateException error) { Log.warning(error); } } |