use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class SonosBinding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
SonosBindingProvider provider = findFirstMatchingBindingProvider(itemName);
String commandAsString = command.toString();
if (command != null) {
List<Command> commands = new ArrayList<Command>();
if (command instanceof StringType || command instanceof DecimalType) {
commands = provider.getVariableCommands(itemName);
} else {
commands.add(command);
}
for (Command someCommand : commands) {
String sonosID = provider.getSonosID(itemName, someCommand);
String sonosCommand = provider.getSonosCommand(itemName, someCommand);
SonosCommandType sonosCommandType = null;
try {
sonosCommandType = SonosCommandType.getCommandType(sonosCommand, Direction.OUT);
} catch (Exception e) {
logger.error("An exception occured while verifying command compatibility ({})", e.getMessage());
}
if (sonosID != null) {
if (sonosCommandType != null) {
logger.debug("Executing command: item:{}, command:{}, ID:{}, CommandType:{}, commandString:{}", new Object[] { itemName, someCommand, sonosID, sonosCommandType, commandAsString });
executeCommand(itemName, someCommand, sonosID, sonosCommandType, commandAsString);
} else {
logger.error("wrong command type for binding [Item={}, command={}]", itemName, commandAsString);
}
} else {
logger.error("{} is an unrecognised command for Item {}", commandAsString, itemName);
}
}
}
}
use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class SonosGenericBindingProvider method getVariableCommands.
@Override
public List<Command> getVariableCommands(String anItem) {
List<Command> commands = new ArrayList<Command>();
SonosBindingConfig aConfig = (SonosBindingConfig) bindingConfigs.get(anItem);
for (Command aCommand : aConfig.keySet()) {
if (aCommand instanceof StringType || aCommand instanceof DecimalType) {
commands.add(aCommand);
}
}
return commands;
}
use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class SonosGenericBindingProvider method getCommands.
@Override
public List<Command> getCommands(String anItem, String sonosCommand) {
List<Command> commands = new ArrayList<Command>();
SonosBindingConfig aConfig = (SonosBindingConfig) bindingConfigs.get(anItem);
for (Command aCommand : aConfig.keySet()) {
SonosBindingConfigElement anElement = aConfig.get(aCommand);
if (anElement.getSonosCommand().equals(sonosCommand)) {
commands.add(aCommand);
}
}
return commands;
}
use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class SonosGenericBindingProvider method parseBindingConfig.
private void parseBindingConfig(SonosBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
String sonosID = null;
String commandAsString = null;
String sonosCommand = 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("Sonos binding configuration must consist of either two [config=" + statusMatcher + "] or three parts [config=" + actionMatcher + "]");
} else {
if (actionMatcher.matches()) {
commandAsString = actionMatcher.group(1);
sonosID = actionMatcher.group(2);
sonosCommand = actionMatcher.group(3);
} else if (statusMatcher.matches()) {
commandAsString = null;
sonosID = statusMatcher.group(1);
sonosCommand = statusMatcher.group(2);
}
SonosBindingConfigElement newElement = new SonosBindingConfigElement(sonosCommand, sonosID, item.getAcceptedDataTypes());
Command command = null;
if (commandAsString == null) {
command = createCommandFromString(null, Integer.toString(counter));
counter++;
config.put(command, newElement);
} 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 IhcBinding method updateResource.
/**
* Update resource value to IHC controller.
*/
private void updateResource(String itemName, Type type, boolean updateOnlyExclusiveOutBinding) {
if (itemName != null) {
Command cmd = null;
try {
cmd = (Command) type;
} catch (Exception e) {
}
IhcBindingProvider provider = findFirstMatchingBindingProvider(itemName, cmd);
if (provider == null) {
// command not configured, skip
return;
}
if (updateOnlyExclusiveOutBinding && provider.hasInBinding(itemName)) {
logger.trace("Ignore in binding update for item '{}'", itemName);
return;
}
logger.debug("Received update/command (item='{}', state='{}', class='{}')", new Object[] { itemName, type.toString(), type.getClass().toString() });
if (ihc == null) {
logger.warn("Controller is not initialized, abort resource value update for item '{}'!", itemName);
return;
}
if (ihc.getConnectionState() != ConnectionState.CONNECTED) {
logger.warn("Connection to controller is not ok, abort resource value update for item '{}'!", itemName);
return;
}
try {
int resourceId = provider.getResourceId(itemName, (Command) type);
logger.trace("found resourceId {} (item='{}', state='{}', class='{}')", new Object[] { new Integer(resourceId).toString(), itemName, type.toString(), type.getClass().toString() });
if (resourceId > 0) {
WSResourceValue value = ihc.getResourceValueInformation(resourceId);
ArrayList<IhcEnumValue> enumValues = null;
if (value instanceof WSEnumValue) {
enumValues = ihc.getEnumValues(((WSEnumValue) value).getDefinitionTypeID());
}
// check if configuration has a custom value defined
// (0->OFF, 1->ON, >1->trigger)
// if that is the case, the type will be overridden with a
// new type
Integer val = provider.getValue(itemName, (Command) type);
boolean trigger = false;
if (val != null) {
if (val == 0) {
type = OnOffType.OFF;
} else if (val == 1) {
type = OnOffType.ON;
} else {
trigger = true;
}
} else {
// the original type is kept
}
if (!trigger) {
value = IhcDataConverter.convertCommandToResourceValue(type, value, enumValues);
boolean result = updateResource(value);
if (result == true) {
logger.debug("Item updated '{}' succesfully sent", itemName);
} else {
logger.error("Item '{}' update failed", itemName);
}
} else {
value = IhcDataConverter.convertCommandToResourceValue(OnOffType.ON, value, enumValues);
boolean result = updateResource(value);
if (result == true) {
logger.debug("Item '{}' trigger started", itemName);
Thread.sleep(val);
value = IhcDataConverter.convertCommandToResourceValue(OnOffType.OFF, value, enumValues);
result = updateResource(value);
if (result == true) {
logger.debug("Item '{}' trigger completed", itemName);
} else {
logger.error("Item '{}' trigger stop failed", itemName);
}
} else {
logger.error("Item '{}' update failed", itemName);
}
}
} else {
logger.error("resourceId invalid");
}
} catch (IhcExecption e) {
logger.error("Can't update Item '{}' value ", itemName, e);
} catch (Exception e) {
logger.error("Error occured during item update", e);
}
}
}
Aggregations