Search in sources :

Example 1 with ArgumentSyntaxException

use of org.openmuc.framework.config.ArgumentSyntaxException in project OpenMUC by isc-konstanz.

the class MqttDriverConnection method getMqttSettings.

private MqttSettings getMqttSettings(String host, String settings) throws ArgumentSyntaxException {
    settings = settings.replaceAll(";", "\n");
    try {
        this.settings.load(new StringReader(settings));
    } catch (IOException e) {
        throw new ArgumentSyntaxException("Could not read settings string");
    }
    int port = Integer.parseInt(this.settings.getProperty("port"));
    String username = this.settings.getProperty("username");
    String password = this.settings.getProperty("password");
    boolean ssl = Boolean.parseBoolean(this.settings.getProperty("ssl"));
    long maxBufferSize = Long.parseLong(this.settings.getProperty("maxBufferSize", "0"));
    long maxFileSize = Long.parseLong(this.settings.getProperty("maxFileSize", "0"));
    int maxFileCount = Integer.parseInt(this.settings.getProperty("maxFileCount", "1"));
    int connectionRetryInterval = Integer.parseInt(this.settings.getProperty("connectionRetryInterval", "10"));
    int connectionAliveInterval = Integer.parseInt(this.settings.getProperty("connectionAliveInterval", "10"));
    String persistenceDirectory = this.settings.getProperty("persistenceDirectory", "data/driver/mqtt");
    String lastWillTopic = this.settings.getProperty("lastWillTopic", "");
    byte[] lastWillPayload = this.settings.getProperty("lastWillPayload", "").getBytes();
    boolean lastWillAlways = Boolean.parseBoolean(this.settings.getProperty("lastWillAlways", "false"));
    String firstWillTopic = this.settings.getProperty("firstWillTopic", "");
    byte[] firstWillPayload = this.settings.getProperty("firstWillPayload", "").getBytes();
    boolean webSocket = Boolean.parseBoolean(this.settings.getProperty("webSocket", "false"));
    return new MqttSettings(host, port, username, password, ssl, maxBufferSize, maxFileSize, maxFileCount, connectionRetryInterval, connectionAliveInterval, persistenceDirectory, lastWillTopic, lastWillPayload, lastWillAlways, firstWillTopic, firstWillPayload, webSocket);
}
Also used : StringReader(java.io.StringReader) IOException(java.io.IOException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) MqttSettings(org.openmuc.framework.lib.mqtt.MqttSettings)

Example 2 with ArgumentSyntaxException

use of org.openmuc.framework.config.ArgumentSyntaxException in project OpenMUC by isc-konstanz.

the class GpioDriver method newDevice.

@Override
public GpioPin newDevice(Address address, Settings settings) throws ArgumentSyntaxException, ConnectionException {
    try {
        GpioConfigs configs = new GpioConfigs(address, settings);
        GpioPin connection;
        logger.trace("Connect Raspberry Pi {} pin {}", configs.getPinMode().getName(), configs.getPin());
        Pin p = RaspiPin.getPinByAddress(configs.getPin());
        if (p == null) {
            throw new ConnectionException("Unable to configure GPIO pin: " + configs.getPin());
        }
        GpioPinDigital pin = null;
        switch(configs.getPinMode()) {
            case DIGITAL_INPUT:
                pin = gpio.provisionDigitalInputPin(p, configs.getPullResistance());
                if (configs.isCounter()) {
                    connection = new EdgeCounter(pin, configs.getPullResistance(), configs.getBounceTime());
                } else {
                    connection = new InputPin(pin);
                }
                break;
            case DIGITAL_OUTPUT:
                pin = gpio.provisionDigitalOutputPin(p, configs.getDefaultState());
                connection = new OutputPin(pin);
                break;
            default:
                throw new ArgumentSyntaxException("GPIO pins not supported for mode: " + configs.getPinMode());
        }
        pin.setShutdownOptions(true, configs.getShutdownState(), configs.getShutdownPullResistance());
        return connection;
    } catch (RuntimeException e) {
        throw new ArgumentSyntaxException("Unable to configure GPIO pin: " + e);
    }
}
Also used : GpioPinDigital(com.pi4j.io.gpio.GpioPinDigital) Pin(com.pi4j.io.gpio.Pin) RaspiPin(com.pi4j.io.gpio.RaspiPin) EdgeCounter(org.openmuc.framework.driver.rpi.gpio.count.EdgeCounter) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException)

Example 3 with ArgumentSyntaxException

use of org.openmuc.framework.config.ArgumentSyntaxException in project OpenMUC by isc-konstanz.

the class Driver method connect.

@Override
public Connection connect(String deviceAddress, String settingsString) throws ArgumentSyntaxException, ConnectionException {
    String[] deviceAddressTokens = deviceAddress.trim().split(":");
    String serialPortName = "";
    boolean isTCP = false;
    String host = "";
    int port = 0;
    int offset;
    if (deviceAddressTokens[0].equalsIgnoreCase(TCP)) {
        host = deviceAddressTokens[1];
        try {
            port = Integer.parseInt(deviceAddressTokens[2]);
        } catch (NumberFormatException e) {
            throw new ArgumentSyntaxException("Could not parse port.");
        }
        isTCP = true;
        offset = 2;
    } else {
        if (deviceAddressTokens.length != 2) {
            throw new ArgumentSyntaxException("The device address does not consist of two parameters.");
        }
        offset = 0;
        serialPortName = deviceAddressTokens[0 + offset];
    }
    Integer mBusAddress;
    SecondaryAddress secondaryAddress = null;
    try {
        if (deviceAddressTokens[1 + offset].length() == 16) {
            mBusAddress = 0xfd;
            byte[] saData = Helper.hexToBytes(deviceAddressTokens[1 + offset]);
            secondaryAddress = SecondaryAddress.newFromLongHeader(saData, 0);
        } else {
            mBusAddress = Integer.decode(deviceAddressTokens[1 + offset]);
        }
    } catch (Exception e) {
        throw new ArgumentSyntaxException("Settings: mBusAddress (" + deviceAddressTokens[1 + offset] + ") is not a number between 0 and 255 nor a 16 sign long hexadecimal secondary address");
    }
    ConnectionInterface connectionInterface;
    Settings settings = new Settings(settingsString, false);
    synchronized (this) {
        synchronized (interfaces) {
            connectionInterface = setConnectionInterface(deviceAddressTokens, serialPortName, isTCP, host, port, offset, settings);
        }
        synchronized (connectionInterface) {
            if (strictConnectionTest) {
                try {
                    testConnection(mBusAddress, secondaryAddress, connectionInterface, settings);
                } catch (IOException e) {
                    connectionInterface.close();
                    throw new ConnectionException(e);
                }
            }
            connectionInterface.increaseConnectionCounter();
        }
    }
    DriverConnection driverCon = new DriverConnection(connectionInterface, mBusAddress, secondaryAddress, settings.delay);
    driverCon.setResetLink(settings.resetLink);
    driverCon.setResetApplication(settings.resetApplication);
    return driverCon;
}
Also used : SecondaryAddress(org.openmuc.jmbus.SecondaryAddress) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) ScanException(org.openmuc.framework.config.ScanException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ScanInterruptedException(org.openmuc.framework.config.ScanInterruptedException) SerialPortTimeoutException(org.openmuc.jrxtx.SerialPortTimeoutException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException)

Example 4 with ArgumentSyntaxException

use of org.openmuc.framework.config.ArgumentSyntaxException in project OpenMUC by isc-konstanz.

the class GenericSetting method parseFields.

synchronized int parseFields(String settings, Class<? extends Enum<? extends OptionI>> options) throws ArgumentSyntaxException {
    String enclosingClassName = options.getEnclosingClass().getSimpleName();
    int enumValuesLength = options.getEnumConstants().length;
    Method prefixMethod;
    Method typeMethod;
    Method mandatorylMethod;
    String[] settingsArray = settings.trim().split(SEPARATOR);
    int settingsArrayLength = settingsArray.length;
    if (settingsArrayLength >= 1 && settingsArrayLength <= enumValuesLength) {
        try {
            prefixMethod = options.getMethod(PREFIX);
            typeMethod = options.getMethod(TYPE);
            mandatorylMethod = options.getMethod(MANDATORY);
        } catch (NoSuchMethodException | SecurityException e) {
            throw new ArgumentSyntaxException("Driver implementation error, \'" + enclosingClassName + "\' problem to find method in implementation. Report driver developer.\n" + e);
        }
        try {
            for (Enum<? extends OptionI> option : options.getEnumConstants()) {
                String prefix = (String) prefixMethod.invoke(option);
                Class<?> type = (Class<?>) typeMethod.invoke(option);
                boolean mandatory = (boolean) mandatorylMethod.invoke(option);
                boolean noOptionsPresent = true;
                String setting = "";
                for (String singlesetting : settingsArray) {
                    setting = singlesetting.trim();
                    String[] pair = setting.split(PAIR_SEP);
                    int pairLength = pair.length;
                    if (mandatory && pairLength != 2) {
                        throw new ArgumentSyntaxException("Parameter in " + enclosingClassName + " is not a pair of prefix and value: <prefix>" + PAIR_SEP + "<value> ");
                    }
                    if (pairLength == 2 && pair[0].trim().equals(prefix)) {
                        try {
                            noOptionsPresent = false;
                            setField(pair[1], option.name(), type, options);
                        } catch (NoSuchFieldException | IllegalAccessException e) {
                            throw new ArgumentSyntaxException("Driver implementation error, \'" + enclosingClassName + "\' has no corresponding field for parameter " + setting + ". Report driver developer.\n" + e);
                        }
                    }
                }
                if (noOptionsPresent && mandatory) {
                    throw new ArgumentSyntaxException("Mandatory parameter " + option.name() + " is nor present in " + this.getClass().getSimpleName());
                }
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new ArgumentSyntaxException("Driver implementation error, \'" + options.getName().toLowerCase(LOCALE) + "\' problem to invoke method. Report driver developer.\n" + e);
        }
    } else if (settingsArrayLength > enumValuesLength) {
        throw new ArgumentSyntaxException("Too much parameters in " + enclosingClassName + ".");
    }
    return settingsArrayLength;
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException)

Example 5 with ArgumentSyntaxException

use of org.openmuc.framework.config.ArgumentSyntaxException in project OpenMUC by isc-konstanz.

the class Iec62056Driver method getIntValue.

private int getIntValue(String[] args, int i, String parameter) throws ArgumentSyntaxException {
    int ret;
    checkParameter(args, i, parameter);
    try {
        ret = Integer.parseInt(args[i]);
    } catch (NumberFormatException e) {
        throw new ArgumentSyntaxException("Specified value of parameter'" + parameter + "' is not an integer");
    }
    return ret;
}
Also used : ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException)

Aggregations

ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)37 ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)8 ChannelRecordContainer (org.openmuc.framework.driver.spi.ChannelRecordContainer)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 ScanException (org.openmuc.framework.config.ScanException)4 Record (org.openmuc.framework.data.Record)3 ChannelAddress (org.openmuc.framework.driver.iec60870.settings.ChannelAddress)3 ChannelValueContainer (org.openmuc.framework.driver.spi.ChannelValueContainer)3 SecondaryAddress (org.openmuc.jmbus.SecondaryAddress)3 KNXException (tuwien.auto.calimero.exception.KNXException)3 HashMap (java.util.HashMap)2 ChannelScanInfo (org.openmuc.framework.config.ChannelScanInfo)2 ScanInterruptedException (org.openmuc.framework.config.ScanInterruptedException)2 ChannelAddress (org.openmuc.framework.driver.dlms.settings.ChannelAddress)2 SnmpDriver (org.openmuc.framework.driver.snmp.SnmpDriver)2 SnmpDevice (org.openmuc.framework.driver.snmp.implementation.SnmpDevice)2 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 GpioPinDigital (com.pi4j.io.gpio.GpioPinDigital)1