use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class SnmpGenericBindingProvider method parseAddress.
private Address parseAddress(String s) throws BindingConfigParseException {
String addressString = s.contains("/") ? s : s + "/161";
Address address = GenericAddress.parse("udp:" + addressString);
if (address == null) {
throw new BindingConfigParseException(getBindingType() + " binding configuration address is invalid: " + s);
}
return address;
}
use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class SnmpGenericBindingProvider method parseBindingConfig.
/**
* Parses a SNMP-OUT configuration by using the regular expression
* <code>([0-9.a-zA-Z/]+):([0-9.a-zA-Z]+):([0-9.a-zA-Z]+):([0-9]+)</code>.
* Where the groups should contain the following content:
* <ul>
* <li>Command</li>
* <li>url: ip[/port], port is optional, default: 161</li>
* <li>[Optional]Version: v1, v2c, v3</li>
* <li>SNMP community</li>
* <li>OID</li>
* <li>Value</li>
* </ul>
*
* Parses a SNMP-IN configuration by using the regular expression
* <code>([0-9.a-zA-Z/]+):([0-9.a-zA-Z]+):([0-9.a-zA-Z]+):([0-9]+)</code>.
* Where the groups should contain the following content:
* <ul>
* <li>url: ip[/port], port is optional, default: 161</li>
* <li>[Optional]Version: v1, v2c, v3</li>
* <li>SNMP community</li>
* <li>OID</li>
* <li>Refresh interval (ms)</li>
* <li>[Optional]transformation rule</li>
* </ul>
*
* Setting refresh interval to 0 will only receive SNMP traps
*
* @param config
* - the Configuration that needs to be updated with the parsing
* results
* @param item
* - the Item that this configuration is intended for
* @param bindingConfig
* - the configuration string that will be parsed
* @throws BindingConfigParseException
*/
private void parseBindingConfig(SnmpBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
config.itemType = item.getClass();
if (bindingConfig != null) {
// try in without version first
Matcher inMatcher = IN_BINDING_PATTERN.matcher(bindingConfig);
if (!inMatcher.matches()) {
inMatcher = IN_BINDING_PATTERN_TRANSFORM.matcher(bindingConfig);
}
if (inMatcher.matches()) {
SnmpBindingConfigElement newElement = new SnmpBindingConfigElement();
newElement.address = parseAddress(inMatcher.group(1).toString());
newElement.snmpVersion = SnmpConstants.version1;
newElement.community = new OctetString(inMatcher.group(2).toString());
newElement.oid = new OID(inMatcher.group(3).toString());
newElement.refreshInterval = Integer.valueOf(inMatcher.group(4)).intValue();
if (inMatcher.groupCount() == 5) {
newElement.setTransformationRule(inMatcher.group(5));
}
config.put(IN_BINDING_KEY, newElement);
} else {
// not matched, try with version
inMatcher = IN_BINDING_PATTERN_VERSION.matcher(bindingConfig);
if (!inMatcher.matches()) {
inMatcher = IN_BINDING_PATTERN_VERSION_TRANSFORM.matcher(bindingConfig);
}
if (inMatcher.matches()) {
SnmpBindingConfigElement newElement = new SnmpBindingConfigElement();
newElement.address = parseAddress(inMatcher.group(1).toString());
String version = inMatcher.group(2).toString();
if (version.equals("v3")) {
newElement.snmpVersion = SnmpConstants.version3;
} else if (version.equals("v2c")) {
newElement.snmpVersion = SnmpConstants.version2c;
} else {
newElement.snmpVersion = SnmpConstants.version1;
}
newElement.community = new OctetString(inMatcher.group(3).toString());
newElement.oid = new OID(inMatcher.group(4).toString());
newElement.refreshInterval = Integer.valueOf(inMatcher.group(5)).intValue();
if (inMatcher.groupCount() == 6) {
newElement.setTransformationRule(inMatcher.group(6));
}
config.put(IN_BINDING_KEY, newElement);
}
}
Matcher outMatcher = OUT_BINDING_PATTERN.matcher(bindingConfig);
if (outMatcher.matches()) {
SnmpBindingConfigElement newElement = new SnmpBindingConfigElement();
String commandAsString = outMatcher.group(1).toString();
newElement.address = parseAddress(outMatcher.group(2).toString());
newElement.snmpVersion = SnmpConstants.version1;
newElement.community = new OctetString(outMatcher.group(3).toString());
newElement.oid = new OID(outMatcher.group(4).toString());
// Only Integer commands accepted at this time.
newElement.value = new Integer32(Integer.parseInt(outMatcher.group(5).toString()));
Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandAsString);
if (command == null) {
logger.error("SNMP can't resolve command {} for item {}", commandAsString, item);
} else {
config.put(command, newElement);
}
} else {
outMatcher = OUT_BINDING_PATTERN_VERSION.matcher(bindingConfig);
if (outMatcher.matches()) {
SnmpBindingConfigElement newElement = new SnmpBindingConfigElement();
String commandAsString = outMatcher.group(1).toString();
newElement.address = parseAddress(outMatcher.group(2).toString());
String version = inMatcher.group(3).toString();
if (version.equals("v3")) {
newElement.snmpVersion = SnmpConstants.version3;
} else if (version.equals("v2c")) {
newElement.snmpVersion = SnmpConstants.version2c;
} else {
newElement.snmpVersion = SnmpConstants.version1;
}
newElement.community = new OctetString(outMatcher.group(4).toString());
newElement.oid = new OID(outMatcher.group(5).toString());
// Only Integer commands accepted at this time.
newElement.value = new Integer32(Integer.parseInt(outMatcher.group(6).toString()));
Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandAsString);
if (command == null) {
logger.error("SNMP can't resolve command {} for item {}", commandAsString, item);
} else {
config.put(command, newElement);
}
}
}
// have we found any matches?
if (!outMatcher.matches() && !inMatcher.matches()) {
throw new BindingConfigParseException(getBindingType() + " binding configuration must consist of four/five/six [config=" + inMatcher + "] or five/six parts [config=" + outMatcher + "]");
}
} else {
return;
}
}
use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class SonosBinding method processVariableMap.
@SuppressWarnings("rawtypes")
public void processVariableMap(RemoteDevice device, Map<String, StateVariableValue> values) {
if (device != null && values != null) {
SonosZonePlayer associatedPlayer = sonosZonePlayerCache.getByDevice(device);
if (associatedPlayer == null) {
logger.debug("There is no Sonos Player defined matching the device {}", device);
return;
}
for (String stateVariable : values.keySet()) {
// find all the CommandTypes that are defined for each
// StateVariable
List<SonosCommandType> supportedCommands = SonosCommandType.getCommandByVariable(stateVariable);
StateVariableValue status = values.get(stateVariable);
for (SonosCommandType sonosCommandType : supportedCommands) {
// create a new State based on the type of Sonos Command and
// the status value in the map
Type newState = null;
try {
newState = createStateForType((Class<? extends State>) sonosCommandType.getTypeClass(), status.getValue().toString());
} catch (BindingConfigParseException e) {
logger.error("Error parsing a value {} to a state variable of type {}", status.toString(), sonosCommandType.getTypeClass().toString());
}
for (SonosBindingProvider provider : providers) {
List<String> qualifiedItems = provider.getItemNames(sonosZonePlayerCache.getByDevice(device).getId(), sonosCommandType.getSonosCommand());
List<String> qualifiedItemsByUDN = provider.getItemNames(sonosZonePlayerCache.getByDevice(device).getUdn().getIdentifierString(), sonosCommandType.getSonosCommand());
for (String item : qualifiedItemsByUDN) {
if (!qualifiedItems.contains(item)) {
qualifiedItems.add(item);
}
}
for (String anItem : qualifiedItems) {
// get the openHAB commands attached to each Item at
// this given Provider
List<Command> commands = provider.getCommands(anItem, sonosCommandType.getSonosCommand());
if (provider.getAcceptedDataTypes(anItem).contains(sonosCommandType.getTypeClass())) {
if (newState != null) {
eventPublisher.postUpdate(anItem, (State) newState);
} else {
throw new IllegalClassException("Cannot process update for the command of type " + sonosCommandType.toString());
}
} else {
logger.warn("Cannot cast {} to an accepted state type for item {}", sonosCommandType.getTypeClass().toString(), anItem);
}
}
}
}
}
}
}
use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class SonosGenericBindingProvider method createCommandFromString.
/**
* Creates a {@link Command} out of the given <code>commandAsString</code>
* incorporating the {@link TypeParser}.
*
* @param item, or null if the Command has to be of the StringType type
* @param commandAsString
*
* @return an appropriate Command (see {@link TypeParser} for more
* information
*
* @throws BindingConfigParseException if the {@link TypeParser} couldn't
* create a command appropriately
*
* @see {@link TypeParser}
*/
private Command createCommandFromString(Item item, String commandAsString) throws BindingConfigParseException {
List<Class<? extends Command>> acceptedTypes = new ArrayList<Class<? extends Command>>();
if (item != null) {
acceptedTypes = item.getAcceptedCommandTypes();
} else {
acceptedTypes.add(StringType.class);
}
Command command = TypeParser.parseCommand(acceptedTypes, commandAsString);
if (command == null) {
throw new BindingConfigParseException("couldn't create Command from '" + commandAsString + "' ");
}
return command;
}
use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.
the class EpsonProjectorGenericBindingProvider method getCommandTypeFromString.
private EpsonProjectorCommandType getCommandTypeFromString(String commandTypeString, Item item) throws BindingConfigParseException {
EpsonProjectorCommandType commandType = null;
try {
EpsonProjectorCommandType.validateBinding(commandTypeString, item.getClass());
commandType = EpsonProjectorCommandType.getCommandType(commandTypeString);
} catch (IllegalArgumentException e) {
throw new BindingConfigParseException("Invalid command type '" + commandTypeString + "'!");
} catch (InvalidClassException e) {
throw new BindingConfigParseException("Invalid item type for command type '" + commandTypeString + "'!");
}
return commandType;
}
Aggregations