Search in sources :

Example 1 with BaseProtocol

use of org.traccar.BaseProtocol in project traccar by tananaev.

the class CommandsManager method getCommandTypes.

public Collection<Typed> getCommandTypes(long deviceId, boolean textChannel) {
    List<Typed> result = new ArrayList<>();
    Position lastPosition = Context.getIdentityManager().getLastPosition(deviceId);
    if (lastPosition != null) {
        BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol());
        Collection<String> commands;
        commands = textChannel ? protocol.getSupportedTextCommands() : protocol.getSupportedDataCommands();
        for (String commandKey : commands) {
            result.add(new Typed(commandKey));
        }
    } else {
        result.add(new Typed(Command.TYPE_CUSTOM));
    }
    return result;
}
Also used : Typed(org.traccar.model.Typed) Position(org.traccar.model.Position) ArrayList(java.util.ArrayList) BaseProtocol(org.traccar.BaseProtocol)

Example 2 with BaseProtocol

use of org.traccar.BaseProtocol in project traccar by tananaev.

the class CommandsManager method getSupportedCommands.

public Collection<Long> getSupportedCommands(long deviceId) {
    List<Long> result = new ArrayList<>();
    Position lastPosition = Context.getIdentityManager().getLastPosition(deviceId);
    for (long commandId : getAllDeviceItems(deviceId)) {
        Command command = getById(commandId);
        if (lastPosition != null) {
            BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol());
            if (command.getTextChannel() && protocol.getSupportedTextCommands().contains(command.getType()) || !command.getTextChannel() && protocol.getSupportedDataCommands().contains(command.getType())) {
                result.add(commandId);
            }
        } else if (command.getType().equals(Command.TYPE_CUSTOM)) {
            result.add(commandId);
        }
    }
    return result;
}
Also used : Position(org.traccar.model.Position) Command(org.traccar.model.Command) ArrayList(java.util.ArrayList) BaseProtocol(org.traccar.BaseProtocol)

Example 3 with BaseProtocol

use of org.traccar.BaseProtocol in project traccar by tananaev.

the class CommandsManager method sendCommand.

public boolean sendCommand(Command command) throws Exception {
    long deviceId = command.getDeviceId();
    if (command.getId() != 0) {
        command = getById(command.getId()).clone();
        command.setDeviceId(deviceId);
    }
    if (command.getTextChannel()) {
        Position lastPosition = Context.getIdentityManager().getLastPosition(deviceId);
        String phone = Context.getIdentityManager().getById(deviceId).getPhone();
        if (lastPosition != null) {
            BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol());
            protocol.sendTextCommand(phone, command);
        } else if (command.getType().equals(Command.TYPE_CUSTOM)) {
            if (Context.getSmppManager() != null) {
                Context.getSmppManager().sendMessageSync(phone, command.getString(Command.KEY_DATA), true);
            } else {
                throw new RuntimeException("SMPP client is not enabled");
            }
        } else {
            throw new RuntimeException("Command " + command.getType() + " is not supported");
        }
    } else {
        ActiveDevice activeDevice = Context.getConnectionManager().getActiveDevice(deviceId);
        if (activeDevice != null) {
            activeDevice.sendCommand(command);
        } else {
            getDeviceQueue(deviceId).add(command);
            return false;
        }
    }
    return true;
}
Also used : Position(org.traccar.model.Position) BaseProtocol(org.traccar.BaseProtocol)

Aggregations

BaseProtocol (org.traccar.BaseProtocol)3 Position (org.traccar.model.Position)3 ArrayList (java.util.ArrayList)2 Command (org.traccar.model.Command)1 Typed (org.traccar.model.Typed)1