aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2017-03-02 17:05:25 +0500
committerAbyss777 <abyss@fox5.ru>2017-03-02 17:06:48 +0500
commit6ff73337236f126839e8ce936ba9d201e8a501c7 (patch)
treeeff0c6fc8b121e528ad80ce2ab573f8e08625b52 /src/org/traccar
parent5a964d4adf67d2f49b58f0b14d4388d7aa2353d2 (diff)
downloadtraccar-server-6ff73337236f126839e8ce936ba9d201e8a501c7.tar.gz
traccar-server-6ff73337236f126839e8ce936ba9d201e8a501c7.tar.bz2
traccar-server-6ff73337236f126839e8ce936ba9d201e8a501c7.zip
Remove BaseProtocolSmsEncoder class
Add fallback to SMS if device is not online Use more variables and ternary operators
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/BaseProtocol.java6
-rw-r--r--src/org/traccar/BaseProtocolSmsEncoder.java30
-rw-r--r--src/org/traccar/api/resource/CommandResource.java8
-rw-r--r--src/org/traccar/database/DeviceManager.java37
-rw-r--r--src/org/traccar/protocol/GranitProtocolSmsEncoder.java6
-rw-r--r--src/org/traccar/protocol/WondexProtocolEncoder.java11
-rw-r--r--src/org/traccar/smpp/SmppClient.java9
7 files changed, 31 insertions, 76 deletions
diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java
index e9a8a9713..9a03fbff4 100644
--- a/src/org/traccar/BaseProtocol.java
+++ b/src/org/traccar/BaseProtocol.java
@@ -37,7 +37,7 @@ public abstract class BaseProtocol implements Protocol {
private final Set<String> supportedCommands = new HashSet<>();
private final Set<String> supportedSmsCommands = new HashSet<>();
- private BaseProtocolSmsEncoder smsEncoder = null;
+ private StringProtocolEncoder smsEncoder = null;
public BaseProtocol(String name) {
this.name = name;
@@ -86,7 +86,7 @@ public abstract class BaseProtocol implements Protocol {
}
}
- public void setSmsEncoder(BaseProtocolSmsEncoder smsEncoder) {
+ public void setSmsEncoder(StringProtocolEncoder smsEncoder) {
this.smsEncoder = smsEncoder;
}
@@ -97,7 +97,7 @@ public abstract class BaseProtocol implements Protocol {
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);
+ Context.getSmppManager().sendMessageSync(phone, (String) smsEncoder.encodeCommand(command), true);
} else {
throw new RuntimeException(
"Command " + command.getType() + " is not supported in protocol " + getName());
diff --git a/src/org/traccar/BaseProtocolSmsEncoder.java b/src/org/traccar/BaseProtocolSmsEncoder.java
deleted file mode 100644
index b5694794c..000000000
--- a/src/org/traccar/BaseProtocolSmsEncoder.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * 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/api/resource/CommandResource.java b/src/org/traccar/api/resource/CommandResource.java
index e5468b5f5..9ed92d3d5 100644
--- a/src/org/traccar/api/resource/CommandResource.java
+++ b/src/org/traccar/api/resource/CommandResource.java
@@ -19,11 +19,6 @@ 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;
@@ -37,8 +32,7 @@ import javax.ws.rs.core.Response;
public class CommandResource extends BaseResource {
@POST
- public Response add(Command entity) throws RecoverablePduException, UnrecoverablePduException,
- SmppTimeoutException, SmppChannelException, InterruptedException {
+ public Response add(Command entity) throws Exception {
Context.getPermissionsManager().checkReadonly(getUserId());
Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId());
Context.getDeviceManager().sendCommand(entity);
diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java
index f60109770..8b28bec9d 100644
--- a/src/org/traccar/database/DeviceManager.java
+++ b/src/org/traccar/database/DeviceManager.java
@@ -26,6 +26,7 @@ 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;
@@ -37,11 +38,6 @@ 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;
@@ -60,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);
@@ -428,25 +427,30 @@ public class DeviceManager implements IdentityManager {
}
}
- public void sendCommand(Command command) throws RecoverablePduException, UnrecoverablePduException,
- SmppTimeoutException, SmppChannelException, InterruptedException {
+ public void sendCommand(Command command) throws Exception {
+ long deviceId = command.getDeviceId();
if (command.getSms()) {
- Position lastPosition = getLastPosition(command.getDeviceId());
+ Position lastPosition = getLastPosition(deviceId);
if (lastPosition != null) {
- Context.getServerManager().getProtocol(lastPosition.getProtocol())
- .sendSmsCommand(devicesById.get(command.getDeviceId()).getPhone(), command);
+ 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(command.getDeviceId()).getPhone(),
+ 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(command.getDeviceId());
+ ActiveDevice activeDevice = Context.getConnectionManager().getActiveDevice(deviceId);
if (activeDevice != null) {
activeDevice.sendCommand(command);
} else {
- throw new RuntimeException("Device is not online");
+ if (fallbackToSms) {
+ command.setSms(true);
+ sendCommand(command);
+ } else {
+ throw new RuntimeException("Device is not online");
+ }
}
}
}
@@ -455,12 +459,9 @@ public class DeviceManager implements IdentityManager {
List<CommandType> result = new ArrayList<>();
Position lastPosition = Context.getDeviceManager().getLastPosition(deviceId);
if (lastPosition != null) {
+ BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol());
Collection<String> commands;
- if (sms) {
- commands = Context.getServerManager().getProtocol(lastPosition.getProtocol()).getSupportedSmsCommands();
- } else {
- commands = Context.getServerManager().getProtocol(lastPosition.getProtocol()).getSupportedCommands();
- }
+ commands = sms ? protocol.getSupportedSmsCommands() : protocol.getSupportedCommands();
for (String commandKey : commands) {
result.add(new CommandType(commandKey));
}
diff --git a/src/org/traccar/protocol/GranitProtocolSmsEncoder.java b/src/org/traccar/protocol/GranitProtocolSmsEncoder.java
index 05954d550..668e5d4d3 100644
--- a/src/org/traccar/protocol/GranitProtocolSmsEncoder.java
+++ b/src/org/traccar/protocol/GranitProtocolSmsEncoder.java
@@ -16,14 +16,14 @@
*/
package org.traccar.protocol;
-import org.traccar.BaseProtocolSmsEncoder;
+import org.traccar.StringProtocolEncoder;
import org.traccar.helper.Log;
import org.traccar.model.Command;
-public class GranitProtocolSmsEncoder extends BaseProtocolSmsEncoder {
+public class GranitProtocolSmsEncoder extends StringProtocolEncoder {
@Override
- protected String encodeSmsCommand(Command command) {
+ protected String encodeCommand(Command command) {
switch (command.getType()) {
case Command.TYPE_REBOOT_DEVICE:
return "BB+RESET";
diff --git a/src/org/traccar/protocol/WondexProtocolEncoder.java b/src/org/traccar/protocol/WondexProtocolEncoder.java
index ddb5c1475..8f9887b10 100644
--- a/src/org/traccar/protocol/WondexProtocolEncoder.java
+++ b/src/org/traccar/protocol/WondexProtocolEncoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 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.BaseProtocolSmsEncoder;
+import org.traccar.StringProtocolEncoder;
import org.traccar.helper.Log;
import org.traccar.model.Command;
-public class WondexProtocolEncoder extends BaseProtocolSmsEncoder {
+public class WondexProtocolEncoder extends StringProtocolEncoder {
@Override
protected Object encodeCommand(Command command) {
@@ -40,9 +40,4 @@ public class WondexProtocolEncoder extends BaseProtocolSmsEncoder {
return null;
}
- @Override
- protected String encodeSmsCommand(Command command) {
- return (String) encodeCommand(command);
- }
-
}
diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/smpp/SmppClient.java
index bef2abbf6..7f004645d 100644
--- a/src/org/traccar/smpp/SmppClient.java
+++ b/src/org/traccar/smpp/SmppClient.java
@@ -199,13 +199,8 @@ public class SmppClient {
if (getSession() != null && getSession().isBound()) {
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);
- }
+ 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));