From 5a964d4adf67d2f49b58f0b14d4388d7aa2353d2 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 2 Mar 2017 12:16:17 +0500 Subject: Implement sms commands --- src/org/traccar/BaseProtocol.java | 42 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'src/org/traccar/BaseProtocol.java') 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 supportedCommands = new HashSet<>(); + private final Set 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 getSupportedCommands() { Set commands = new HashSet<>(supportedCommands); @@ -51,6 +63,13 @@ public abstract class BaseProtocol implements Protocol { return commands; } + @Override + public Collection getSupportedSmsCommands() { + Set commands = new HashSet<>(supportedSmsCommands); + commands.add(Command.TYPE_CUSTOM); + return commands; + } + @Override public void sendCommand(ActiveDevice activeDevice, Command command) { if (supportedCommands.contains(command.getType())) { @@ -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"); + } + } + } -- cgit v1.2.3