use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class ProtocolGenericBindingProvider method parseBindingConfig.
/**
* Parses the configuration string and update the provided config
*
* @param config - the Configuration that needs to be updated with the parsing results
* @param item - the Item that this configuration is intented for
* @param bindingConfig - the configuration string that will be parsed
* @throws BindingConfigParseException
*/
private void parseBindingConfig(ProtocolBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
String direction = null;
Direction directionType = null;
String commandAsString = null;
String host = null;
String port = null;
String protocolCommand = null;
if (bindingConfig != null) {
Matcher actionMatcher = ACTION_CONFIG_PATTERN.matcher(bindingConfig);
Matcher statusMatcher = STATUS_CONFIG_PATTERN.matcher(bindingConfig);
if ((!actionMatcher.matches() && !statusMatcher.matches())) {
throw new BindingConfigParseException(getBindingType() + " binding configuration must consist of four [config=" + statusMatcher + "] or five parts [config=" + actionMatcher + "]");
} else {
if (actionMatcher.matches()) {
direction = actionMatcher.group(1);
commandAsString = actionMatcher.group(2);
host = actionMatcher.group(3);
port = actionMatcher.group(4);
protocolCommand = actionMatcher.group(5) != null ? actionMatcher.group(5) : actionMatcher.group(6);
} else if (statusMatcher.matches()) {
direction = statusMatcher.group(1);
commandAsString = null;
host = statusMatcher.group(2);
port = statusMatcher.group(3);
protocolCommand = statusMatcher.group(4) != null ? statusMatcher.group(4) : statusMatcher.group(5);
}
if (direction.equals(">")) {
directionType = Direction.OUT;
} else if (direction.equals("<")) {
directionType = Direction.IN;
}
ProtocolBindingConfigElement newElement = new ProtocolBindingConfigElement(host, port, directionType, protocolCommand, item.getAcceptedDataTypes());
Command command = null;
if (commandAsString == null) {
// for those configuration strings that are not really linked to a openHAB command we
// create a dummy Command to be able to store the configuration information
// I have choosen to do that with NumberItems
NumberItem dummy = new NumberItem(Integer.toString(counter));
command = createCommandFromString(dummy, Integer.toString(counter));
counter++;
} else {
command = createCommandFromString(item, commandAsString);
}
config.put(command, newElement);
}
} else {
return;
}
}
use of org.openhab.core.types.Command 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.core.types.Command 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.core.types.Command 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.core.types.Command in project openhab1-addons by openhab.
the class SonosGenericBindingProvider method getSonosID.
@Override
public List<String> getSonosID(String itemName) {
List<String> ids = new ArrayList<String>();
SonosBindingConfig aConfig = (SonosBindingConfig) bindingConfigs.get(itemName);
for (Command aCommand : aConfig.keySet()) {
ids.add(aConfig.get(aCommand).getSonosID());
}
return ids;
}
Aggregations