diff options
author | Abyss777 <abyss@fox5.ru> | 2017-03-02 12:16:17 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2017-03-02 12:16:17 +0500 |
commit | 5a964d4adf67d2f49b58f0b14d4388d7aa2353d2 (patch) | |
tree | 54a3ddde7b08557ed2aa5bb0dfd35385b1242908 | |
parent | 5012663d8688fa521fa7de02116f42ddbb57e7fb (diff) | |
download | traccar-server-5a964d4adf67d2f49b58f0b14d4388d7aa2353d2.tar.gz traccar-server-5a964d4adf67d2f49b58f0b14d4388d7aa2353d2.tar.bz2 traccar-server-5a964d4adf67d2f49b58f0b14d4388d7aa2353d2.zip |
Implement sms commands
-rw-r--r-- | src/org/traccar/BaseProtocol.java | 42 | ||||
-rw-r--r-- | src/org/traccar/BaseProtocolSmsEncoder.java | 30 | ||||
-rw-r--r-- | src/org/traccar/Protocol.java | 10 | ||||
-rw-r--r-- | src/org/traccar/ServerManager.java | 10 | ||||
-rw-r--r-- | src/org/traccar/api/resource/CommandResource.java | 12 | ||||
-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 | 49 | ||||
-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/protocol/WondexProtocolEncoder.java | 11 | ||||
-rw-r--r-- | src/org/traccar/smpp/ClientSmppSessionHandler.java | 6 | ||||
-rw-r--r-- | src/org/traccar/smpp/SmppClient.java | 47 |
16 files changed, 259 insertions, 48 deletions
diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 59331d7cc..e9a8a9713 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. @@ -20,6 +20,11 @@ import org.jboss.netty.handler.codec.string.StringEncoder; import org.traccar.database.ActiveDevice; import org.traccar.model.Command; +import com.cloudhopper.smpp.type.RecoverablePduException; +import com.cloudhopper.smpp.type.SmppChannelException; +import com.cloudhopper.smpp.type.SmppTimeoutException; +import com.cloudhopper.smpp.type.UnrecoverablePduException; + import javax.xml.bind.DatatypeConverter; import java.util.Arrays; import java.util.Collection; @@ -30,6 +35,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 BaseProtocolSmsEncoder smsEncoder = null; public BaseProtocol(String name) { this.name = name; @@ -44,6 +52,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 +64,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 +86,25 @@ public abstract class BaseProtocol implements Protocol { } } + public void setSmsEncoder(BaseProtocolSmsEncoder smsEncoder) { + this.smsEncoder = smsEncoder; + } + + @Override + public void sendSmsCommand(String phone, Command command) throws RecoverablePduException, UnrecoverablePduException, + SmppTimeoutException, SmppChannelException, InterruptedException { + 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, smsEncoder.encodeSmsCommand(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/BaseProtocolSmsEncoder.java b/src/org/traccar/BaseProtocolSmsEncoder.java new file mode 100644 index 000000000..b5694794c --- /dev/null +++ b/src/org/traccar/BaseProtocolSmsEncoder.java @@ -0,0 +1,30 @@ +/* + * 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; + +import org.traccar.model.Command; + +public abstract class BaseProtocolSmsEncoder extends StringProtocolEncoder { + + protected abstract String encodeSmsCommand(Command command); + + @Override + protected Object encodeCommand(Command command) { + return null; + } + +} diff --git a/src/org/traccar/Protocol.java b/src/org/traccar/Protocol.java index c99fd8ecb..5f510b10b 100644 --- a/src/org/traccar/Protocol.java +++ b/src/org/traccar/Protocol.java @@ -3,6 +3,11 @@ package org.traccar; import org.traccar.database.ActiveDevice; import org.traccar.model.Command; +import com.cloudhopper.smpp.type.RecoverablePduException; +import com.cloudhopper.smpp.type.SmppChannelException; +import com.cloudhopper.smpp.type.SmppTimeoutException; +import com.cloudhopper.smpp.type.UnrecoverablePduException; + import java.util.Collection; import java.util.List; @@ -16,4 +21,9 @@ public interface Protocol { void initTrackerServers(List<TrackerServer> serverList); + Collection<String> getSupportedSmsCommands(); + + void sendSmsCommand(String phone, Command command) throws RecoverablePduException, UnrecoverablePduException, + SmppTimeoutException, SmppChannelException, InterruptedException; + } 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..e5468b5f5 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. @@ -19,6 +19,11 @@ import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.model.Command; +import com.cloudhopper.smpp.type.RecoverablePduException; +import com.cloudhopper.smpp.type.SmppChannelException; +import com.cloudhopper.smpp.type.SmppTimeoutException; +import com.cloudhopper.smpp.type.UnrecoverablePduException; + import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; @@ -32,10 +37,11 @@ import javax.ws.rs.core.Response; public class CommandResource extends BaseResource { @POST - public Response add(Command entity) { + public Response add(Command entity) throws RecoverablePduException, UnrecoverablePduException, + SmppTimeoutException, SmppChannelException, InterruptedException { 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..f60109770 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -29,12 +29,19 @@ import java.util.concurrent.atomic.AtomicLong; 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; import org.traccar.model.Position; import org.traccar.model.Server; +import com.cloudhopper.smpp.type.RecoverablePduException; +import com.cloudhopper.smpp.type.SmppChannelException; +import com.cloudhopper.smpp.type.SmppTimeoutException; +import com.cloudhopper.smpp.type.UnrecoverablePduException; + public class DeviceManager implements IdentityManager { public static final long DEFAULT_REFRESH_DELAY = 300; @@ -420,4 +427,46 @@ public class DeviceManager implements IdentityManager { throw new IllegalArgumentException(); } } + + public void sendCommand(Command command) throws RecoverablePduException, UnrecoverablePduException, + SmppTimeoutException, SmppChannelException, InterruptedException { + if (command.getSms()) { + Position lastPosition = getLastPosition(command.getDeviceId()); + if (lastPosition != null) { + Context.getServerManager().getProtocol(lastPosition.getProtocol()) + .sendSmsCommand(devicesById.get(command.getDeviceId()).getPhone(), command); + } else if (command.getType().equals(Command.TYPE_CUSTOM)) { + Context.getSmppManager().sendMessageSync(devicesById.get(command.getDeviceId()).getPhone(), + command.getString(Command.KEY_DATA), true); + } else { + throw new RuntimeException("Command " + command.getType() + " is not supported"); + } + } else { + ActiveDevice activeDevice = Context.getConnectionManager().getActiveDevice(command.getDeviceId()); + if (activeDevice != null) { + activeDevice.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) { + Collection<String> commands; + if (sms) { + commands = Context.getServerManager().getProtocol(lastPosition.getProtocol()).getSupportedSmsCommands(); + } else { + commands = Context.getServerManager().getProtocol(lastPosition.getProtocol()).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..05954d550 --- /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.BaseProtocolSmsEncoder; +import org.traccar.helper.Log; +import org.traccar.model.Command; + +public class GranitProtocolSmsEncoder extends BaseProtocolSmsEncoder { + + @Override + protected String encodeSmsCommand(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/protocol/WondexProtocolEncoder.java b/src/org/traccar/protocol/WondexProtocolEncoder.java index 8f9887b10..ddb5c1475 100644 --- a/src/org/traccar/protocol/WondexProtocolEncoder.java +++ b/src/org/traccar/protocol/WondexProtocolEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 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. @@ -15,11 +15,11 @@ */ package org.traccar.protocol; -import org.traccar.StringProtocolEncoder; +import org.traccar.BaseProtocolSmsEncoder; import org.traccar.helper.Log; import org.traccar.model.Command; -public class WondexProtocolEncoder extends StringProtocolEncoder { +public class WondexProtocolEncoder extends BaseProtocolSmsEncoder { @Override protected Object encodeCommand(Command command) { @@ -40,4 +40,9 @@ public class WondexProtocolEncoder extends StringProtocolEncoder { return null; } + @Override + protected String encodeSmsCommand(Command command) { + return (String) encodeCommand(command); + } + } diff --git a/src/org/traccar/smpp/ClientSmppSessionHandler.java b/src/org/traccar/smpp/ClientSmppSessionHandler.java index 721243f9f..abfd6120c 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()); } diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/smpp/SmppClient.java index 3680d20e2..bef2abbf6 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); @@ -184,31 +193,41 @@ public class SmppClient { } } - 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; + if (command) { + textBytes = CharsetUtil.encode(message, commandsCharsetName); + submit.setDataCoding(commandsDataCoding); + } else { + textBytes = CharsetUtil.encode(message, notificationsCharsetName); + submit.setDataCoding(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 submited, msg_id: " + submitResponce.getMessageId()); + } else { + throw new IllegalStateException(submitResponce.getResultMessage()); + } } else { 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); } } |