use of org.openhab.binding.xpl.XplBindingConfig in project openhab1-addons by openhab.
the class XplGenericBindingProvider method processBindingConfiguration.
/**
* {@inheritDoc}
*/
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
String[] configParts = bindingConfig.trim().split(",");
if (configParts.length < 5) {
throw new BindingConfigParseException("xPL binding configuration must contain : target,message type,schema at at least one body key/value pair");
}
XplBindingConfig config = new XplBindingConfig();
config.Message = xPL_Utils.createMessage();
config.Message.setTarget(configParts[0]);
// Parse type
if (configParts[1].equalsIgnoreCase("TRIGGER")) {
config.Message.setType(xPL_MessageI.MessageType.TRIGGER);
} else if (configParts[1].equalsIgnoreCase("STATUS")) {
config.Message.setType(xPL_MessageI.MessageType.STATUS);
} else if (configParts[1].equalsIgnoreCase("COMMAND")) {
config.Message.setType(xPL_MessageI.MessageType.COMMAND);
} else {
config.Message.setType(xPL_MessageI.MessageType.UNKNOWN);
}
config.Message.setSchema(configParts[2]);
// Parse name/value pairs
String theName = null;
String theValue = null;
int delimPtr;
for (int pairPtr = 3; pairPtr < configParts.length; pairPtr++) {
delimPtr = configParts[pairPtr].indexOf("=");
theName = configParts[pairPtr].substring(0, delimPtr);
theValue = configParts[pairPtr].substring(delimPtr + 1);
config.Message.addNamedValue(theName, theValue);
if (theValue.equalsIgnoreCase("#COMMAND") || theValue.equalsIgnoreCase("#CURRENT")) {
config.NamedParameter = new String(theName);
}
}
addBindingConfig(item, config);
super.processBindingConfiguration(context, item, bindingConfig);
}
use of org.openhab.binding.xpl.XplBindingConfig in project openhab1-addons by openhab.
the class XplBinding method handleXPLMessage.
@Override
public void handleXPLMessage(xPL_MessageI theMessage) {
for (XplBindingProvider provider : providers) {
List<String> matchingItems = provider.hasMessage(theMessage);
for (String itemName : matchingItems) {
XplBindingConfig config = provider.getConfig(itemName);
if (config == null) {
continue;
}
String current = theMessage.getNamedValue(config.NamedParameter);
Item item = provider.getItem(itemName);
if (item != null) {
if (item instanceof SwitchItem) {
OnOffType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OnOffType.ON : OnOffType.OFF;
synchronized (item) {
if (!item.getState().equals(status)) {
eventPublisher.postUpdate(itemName, status);
((SwitchItem) item).setState(status);
}
}
} else if (item instanceof ContactItem) {
OpenClosedType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
synchronized (item) {
if (!item.getState().equals(status)) {
eventPublisher.postUpdate(itemName, status);
((ContactItem) item).setState(status);
}
}
} else if (item instanceof NumberItem) {
DecimalType value = new DecimalType(current);
synchronized (item) {
if (!item.getState().equals(value)) {
eventPublisher.postUpdate(itemName, value);
((NumberItem) item).setState(value);
}
}
} else if (item instanceof StringItem) {
StringType value = new StringType(current);
synchronized (item) {
if (!item.getState().equals(value)) {
eventPublisher.postUpdate(itemName, value);
((StringItem) item).setState(value);
}
}
}
}
}
}
}
use of org.openhab.binding.xpl.XplBindingConfig in project openhab1-addons by openhab.
the class XplBinding method internalReceiveCommand.
/**
* Sends an xPL message upon command received by an Item
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
for (XplBindingProvider provider : providers) {
XplBindingConfig config = provider.getConfig(itemName);
if ((config == null) || (config.NamedParameter == null)) {
continue;
}
if (config.Message.getSource() == null) {
config.Message.setSource(xplTransportService.getSourceIdentifier());
}
config.Message.setNamedValue(config.NamedParameter, command.toString().toLowerCase());
xplTransportService.sendMessage(config.Message);
}
}
use of org.openhab.binding.xpl.XplBindingConfig in project openhab1-addons by openhab.
the class XplGenericBindingProvider method hasMessage.
/**
* {@inheritDoc}
*/
@Override
public List<String> hasMessage(xPL_MessageI theMessage) {
List<String> matching = new ArrayList<String>();
if (theMessage.getType() != xPL_MessageI.MessageType.COMMAND) {
// the message must not be is not a xpl-cmnd
for (String key : bindingConfigs.keySet()) {
XplBindingConfig config = (XplBindingConfig) bindingConfigs.get(key);
NamedValuesI theBody = config.Message.getMessageBody();
if ((theBody != null) && (!theBody.isEmpty()) && (config.Message.getTarget().isBroadcastIdentifier() || config.Message.getTarget().equals(theMessage.getSource())) && config.Message.getSchemaClass().equalsIgnoreCase(theMessage.getSchemaClass()) && config.Message.getSchemaType().equalsIgnoreCase(theMessage.getSchemaType())) {
boolean bodyMatched = true;
for (NamedValueI theValue : theBody.getAllNamedValues()) {
// iterate through the item body to
// see if ...
String aKey = theValue.getName();
String aValue = theValue.getValue();
String bValue = theMessage.getNamedValue(aKey);
boolean lineMatched = (bValue != null) && (aKey.equalsIgnoreCase(config.NamedParameter) || aValue.equalsIgnoreCase(bValue));
bodyMatched = bodyMatched && lineMatched;
}
if (bodyMatched) {
matching.add(key);
}
}
}
}
return matching;
}
Aggregations