use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class EpsonProjectorGenericBindingProvider method processBindingConfiguration.
/**
* {@inheritDoc}
*/
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
super.processBindingConfiguration(context, item, bindingConfig);
EpsonProjectorBindingConfig config = new EpsonProjectorBindingConfig();
String[] configParts = bindingConfig.trim().split(":");
config.inBinding = true;
config.outBinding = true;
config.itemType = item.getClass();
if (bindingConfig.startsWith("<")) {
if (configParts.length != 3) {
throw new BindingConfigParseException("Epson projector in binding must contain 3 parts separated by ':'");
}
config.outBinding = false;
config.deviceID = configParts[0].trim().replace("<", "");
parseRefreshPeriod(configParts[2], config);
} else if (bindingConfig.startsWith(">")) {
if (configParts.length != 2) {
throw new BindingConfigParseException("Epson projector out binding must contain 2 parts separated by ':'");
}
config.inBinding = false;
config.deviceID = configParts[0].trim().replace(">", "");
} else {
if (configParts.length != 3) {
throw new BindingConfigParseException("Epson projector bi-directional binding must contain 3 parts separated by ':'");
}
config.deviceID = configParts[0].trim();
parseRefreshPeriod(configParts[2], config);
}
config.commandType = getCommandTypeFromString(configParts[1].trim(), item);
addBindingConfig(item, config);
}
use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class FHTGenericBindingProvider method processBindingConfiguration.
/**
* Binding in the type of
* {fht="housecode=<housecode>;address=<optional>;datapoint=<optional>"}
* {@inheritDoc}
*/
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
super.processBindingConfiguration(context, item, bindingConfig);
String[] configParts = bindingConfig.split(";");
String housecode = null;
String address = null;
Datapoint datapoint = null;
for (String s : configParts) {
String[] entryParts = s.split("=");
if ("housecode".equals(entryParts[0])) {
housecode = entryParts[1];
} else if ("address".equals(entryParts[0])) {
address = entryParts[1];
} else if ("datapoint".equals(entryParts[0])) {
datapoint = Datapoint.valueOf(entryParts[1]);
}
}
if (housecode == null) {
throw new BindingConfigParseException("housecode mustn't be null");
}
if (datapoint == null) {
throw new BindingConfigParseException("datapoint must be one of MEASURED_TEMP, DESIRED_TEMP, BATTERY, WINDOW or VALVE");
}
if ((datapoint == Datapoint.WINDOW || datapoint == Datapoint.VALVE) && address == null) {
throw new BindingConfigParseException("Address of window contact needed");
}
FHTBindingConfig config = new FHTBindingConfig(item, housecode, address, datapoint);
addBindingConfig(item, config);
}
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);
}
Aggregations