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);
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations