use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class ExecGenericBindingProvider method processBindingConfiguration.
/**
* {@inheritDoc}
*/
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
super.processBindingConfiguration(context, item, bindingConfig);
ExecBindingConfig config = new ExecBindingConfig();
config.acceptedDataTypes = new ArrayList<Class<? extends State>>(item.getAcceptedDataTypes());
Matcher matcher = BASE_CONFIG_PATTERN.matcher(bindingConfig);
if (!matcher.matches()) {
if (bindingConfig.startsWith("<") || bindingConfig.startsWith(">")) {
throw new BindingConfigParseException("Exec binding legacy format cannot start with '<' or '>' ");
}
// backward compatibility for old format
parseLegacyOutBindingConfig(item, bindingConfig, config);
} else {
matcher.reset();
while (matcher.find()) {
String direction = matcher.group(1);
String bindingConfigPart = matcher.group(2);
if (direction.equals("<")) {
config = parseInBindingConfig(item, bindingConfigPart, config);
} else if (direction.equals(">")) {
config = parseOutBindingConfig(item, bindingConfigPart, config);
} else {
throw new BindingConfigParseException("Unknown command given! Configuration must start with '<' or '>' ");
}
}
}
addBindingConfig(item, config);
}
use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class KNXGenericBindingProvider method parseBindingConfigString.
/**
* This is the main method that takes care of parsing a binding configuration
* string for a given item. It returns a collection of {@link BindingConfig}
* instances, which hold all relevant data about the binding to KNX of an item.
*
* @param item the item for which the binding configuration string is provided
* @param bindingConfig a string which holds the binding information
* @return a knx binding config, a collection of {@link KNXBindingConfigItem}
* instances, which hold all relevant data about the binding
* @throws BindingConfigParseException if the configuration string has no valid syntax
*/
protected KNXBindingConfig parseBindingConfigString(Item item, String bindingConfig) throws BindingConfigParseException {
KNXBindingConfig config = new KNXBindingConfig();
String[] datapointConfigs = bindingConfig.trim().split(",");
// we can have one datapoint per accepted command type of this item
for (int i = 0; i < datapointConfigs.length; i++) {
try {
String datapointConfig = datapointConfigs[i].trim();
KNXBindingConfigItem configItem = new KNXBindingConfigItem();
configItem.itemName = item.getName();
if (datapointConfig.split("<").length > 2) {
throw new BindingConfigParseException("Only one readable GA allowed.");
}
String[] dataPoints = datapointConfig.split("\\+");
for (int j = 0; j < dataPoints.length; ++j) {
String dataPoint = dataPoints[j].trim();
// Just skip it, it will be handle in the next iteration.
if (dataPoint.isEmpty()) {
continue;
}
boolean isReadable = false;
int autoRefreshTimeInSecs = 0;
// check for the readable flag
if (dataPoint.startsWith("<")) {
isReadable = true;
dataPoint = dataPoint.substring(1);
// check for the auto refresh parameter
if (dataPoint.startsWith("(")) {
int endIndex = dataPoint.indexOf(")");
if (endIndex > -1) {
dataPoint = dataPoint.substring(1);
if (endIndex > 1) {
try {
autoRefreshTimeInSecs = Integer.parseInt(dataPoint.substring(0, endIndex - 1));
dataPoint = dataPoint.substring(endIndex);
if (autoRefreshTimeInSecs == 0) {
throw new BindingConfigParseException("Autorefresh time cannot be 0.");
}
} catch (NumberFormatException nfe) {
throw new BindingConfigParseException("Autorefresh time must be a number, but was '" + dataPoint.substring(1, endIndex) + "'.");
}
} else {
throw new BindingConfigParseException("Autorefresh time parameter: missing time. Empty brackets are not allowed.");
}
} else {
throw new BindingConfigParseException("Closing ')' missing on autorefresh time parameter.");
}
}
}
// find the DPT for this entry
String[] segments = dataPoint.split(":");
Class<? extends Type> typeClass = null;
String dptID = null;
if (segments.length == 1) {
//DatapointID NOT specified in binding config, so try to guess it
typeClass = item.getAcceptedCommandTypes().size() > 0 ? item.getAcceptedCommandTypes().get(i) : item.getAcceptedDataTypes().size() > 1 ? item.getAcceptedDataTypes().get(i) : item.getAcceptedDataTypes().get(0);
dptID = getDefaultDPTId(typeClass);
} else {
//DatapointID specified in binding config, so use it
dptID = segments[0];
}
if (dptID == null || dptID.trim().isEmpty()) {
throw new BindingConfigParseException("No DPT could be determined for the type '" + typeClass.getSimpleName() + "'.");
}
// check if this DPT is supported
if (KNXCoreTypeMapper.toTypeClass(dptID) == null) {
throw new BindingConfigParseException("DPT " + dptID + " is not supported by the KNX binding.");
}
String ga = (segments.length == 1) ? segments[0].trim() : segments[1].trim();
// determine start/stop behavior
Boolean startStopBehavior = Boolean.FALSE;
if (ga.endsWith(START_STOP_MARKER_SUFFIX)) {
startStopBehavior = Boolean.TRUE;
ga = ga.substring(0, ga.length() - START_STOP_MARKER_SUFFIX.length());
}
// create group address and datapoint
GroupAddress groupAddress = new GroupAddress(ga);
configItem.startStopMap.put(groupAddress, startStopBehavior);
Datapoint dp;
if (j != 0 || item.getAcceptedCommandTypes().size() == 0) {
dp = new StateDP(groupAddress, item.getName(), 0, dptID);
} else {
dp = new CommandDP(groupAddress, item.getName(), 0, dptID);
}
// assign datapoint to configuration item
if (configItem.mainDataPoint == null) {
configItem.mainDataPoint = dp;
}
if (isReadable) {
configItem.readableDataPoint = dp;
if (autoRefreshTimeInSecs > 0) {
configItem.autoRefreshInSecs = autoRefreshTimeInSecs;
}
}
if (!configItem.allDataPoints.contains(dp)) {
configItem.allDataPoints.add(dp);
} else {
throw new BindingConfigParseException("Datapoint '" + dp.getDPT() + "' already exists for item '" + item.getName() + "'.");
}
}
config.add(configItem);
} catch (IndexOutOfBoundsException e) {
throw new BindingConfigParseException("No more than " + i + " datapoint definitions are allowed for this item.");
} catch (KNXFormatException e) {
throw new BindingConfigParseException(e.getMessage());
}
}
return config;
}
use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class KM200GenericBindingProvider method parseBindingConfig.
/**
* Checks if the bindingConfig contains a valid binding type and returns an appropriate instance.
*
* @param item
* @param bindingConfig
*
* @throws BindingConfigParseException if bindingConfig is no valid binding type
*/
protected KM200BindingConfig parseBindingConfig(Item item, String bindingConfig) throws BindingConfigParseException {
String service = null, current = null;
HashMap<String, String> parameterMap = new HashMap<String, String>();
/* Check whether some defined services are used */
logger.info("Bind Config: {}", bindingConfig);
if (bindingConfig.trim().equals(KM200BindingProvider.DATE_TIME)) {
return new KM200BindingConfig(item.getClass(), KM200BindingProvider.DATE_TIME, "/gateway/DateTime", parameterMap);
}
if (bindingConfig.trim().equals(KM200BindingProvider.SYS_BRAND)) {
return new KM200BindingConfig(item.getClass(), KM200BindingProvider.SYS_BRAND, "/system/brand", parameterMap);
}
if (bindingConfig.trim().equals(KM200BindingProvider.SYS_TYPE)) {
return new KM200BindingConfig(item.getClass(), KM200BindingProvider.SYS_TYPE, "/system/appliance/type", parameterMap);
}
if (bindingConfig.trim().equals(KM200BindingProvider.SYS_STATE)) {
return new KM200BindingConfig(item.getClass(), KM200BindingProvider.SYS_STATE, "/system/healthStatus", parameterMap);
}
if (bindingConfig.trim().equals(KM200BindingProvider.VER_FIRMWARE)) {
return new KM200BindingConfig(item.getClass(), KM200BindingProvider.VER_FIRMWARE, "/gateway/versionFirmware", parameterMap);
}
if (bindingConfig.trim().equals(KM200BindingProvider.VER_HARDWARE)) {
return new KM200BindingConfig(item.getClass(), KM200BindingProvider.VER_HARDWARE, "/gateway/versionHardware", parameterMap);
}
/* Maybe it's a configuration */
/* Configuration string should be in the form "service:/gateway/versionFirmware" */
/* Available options for a SwitchItem are on:xxx and off:yyy with a allowed value xxx|yyy */
String[] keyTypeStructure = bindingConfig.split("\\s+");
logger.debug("Bind Config nbr: {}", keyTypeStructure.length);
if (keyTypeStructure.length > 3) {
logger.error("Incorrect number of structures in configuration string '{}'", bindingConfig);
throw new BindingConfigParseException("Incorrect number of structures in configuration string '" + bindingConfig + "'");
}
for (String structure : keyTypeStructure) {
String[] keyValueStructure = structure.split(":");
if (keyValueStructure.length != 2) {
logger.error("Incorrect key:value structure in configuration string '{}'", bindingConfig);
throw new BindingConfigParseException("Incorrect key:value structure in configuration string '" + bindingConfig + "'");
}
String key = keyValueStructure[0];
String value = keyValueStructure[1];
if (key.equals("service")) {
service = value;
if (!service.contains("/")) {
logger.error("Wrong service string without / in configuration string '{}'", bindingConfig);
throw new BindingConfigParseException("Wrong service string without / in configuration string '" + bindingConfig + "'");
}
} else if (key.equals("current")) {
current = value;
if (!current.contains("/")) {
logger.error("Wrong current string without / in configuration string '{}'", bindingConfig);
throw new BindingConfigParseException("Wrong current string without / in configuration string '" + bindingConfig + "'");
}
parameterMap.put(key, current);
} else if (key.equals("on") || key.equals("off")) {
parameterMap.put(key, value);
} else {
logger.error("Unsupported key: ({}) in configuration string: '{}'", key, bindingConfig);
throw new BindingConfigParseException("Unsupported key (" + key + ") in configuration string '" + bindingConfig + "'");
}
}
if (parameterMap.containsKey("on") || parameterMap.containsKey("off")) {
if (!(parameterMap.containsKey("on") && parameterMap.containsKey("off"))) {
throw new BindingConfigParseException("Unsupported combination, 'on' and 'off' have to be configured together '" + bindingConfig + "'");
}
}
return new KM200BindingConfig(item.getClass(), KM200BindingProvider.DIRECT_SERVICE, service, parameterMap);
}
use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class KoubachiGenericBindingProvider method processBindingConfiguration.
/**
* {@inheritDoc}
*/
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
super.processBindingConfiguration(context, item, bindingConfig);
String[] configParts = bindingConfig.split(":");
if (configParts.length < 3 || configParts.length > 4) {
throw new BindingConfigParseException("A Koubachi binding configuration for a property must consist of three or four parts - please verify your *.items file");
} else if (configParts[2].equals("action") && configParts.length != 4) {
throw new BindingConfigParseException("A Koubachi binding configuration for an action must consist of four parts - please verify your *.items file");
}
KoubachiBindingConfig config = new KoubachiBindingConfig();
config.resourceType = KoubachiResourceType.valueOf(configParts[0].toUpperCase());
config.resourceId = configParts[1];
if (configParts.length == 3) {
// this is a binding for a property
config.propertyName = configParts[2];
} else {
// this is a binding for a care action
config.actionType = configParts[3];
}
addBindingConfig(item, config);
}
use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class SmarthomaticGenericBindingProvider method parseConfig.
private SmarthomaticBindingConfig parseConfig(Item item, String bindingConfig) throws BindingConfigParseException {
SmarthomaticBindingConfig config = new SmarthomaticBindingConfig();
Matcher matcher = CONFIG_PATTERN.matcher(bindingConfig);
if (!matcher.matches()) {
throw new BindingConfigParseException("Config for item '" + item.getName() + "' could not be parsed.");
}
bindingConfig = matcher.group(1);
config.setItemName(item.getName());
config.setItem(item);
matcher = TRANSFORMATION_PATTERN.matcher(bindingConfig);
if (matcher.matches()) {
bindingConfig = matcher.group(1);
String transformation = matcher.group(2);
config.getConfigParams().put("transformation", transformation);
}
// parse bindingconfig here ...
StringTokenizer confItems = new StringTokenizer(bindingConfig, ",");
while (confItems.hasMoreTokens()) {
String[] token = confItems.nextToken().split("=");
String key = token[0];
String value = token[1];
config.getConfigParams().put(key, value);
// Strip all whitespaces from token[0]
key = key.replaceAll("\\s", "");
if ("deviceId".equals(key)) {
config.setDeviceId(value.replaceAll("\\s", ""));
} else if ("messageGroupId".equals(key)) {
config.setMessageGroupId(value.replaceAll("\\s", ""));
} else if ("messageId".equals(key)) {
config.setMessageId(value.replaceAll("\\s", ""));
} else if ("messagePart".equals(key)) {
config.setMessagePartId(value.replaceAll("\\s", ""));
} else if ("messageItem".equals(key)) {
config.setMessageItemId(value.replaceAll("\\s", ""));
}
}
return config;
}
Aggregations