Search in sources :

Example 1 with XplBindingConfig

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);
}
Also used : XplBindingConfig(org.openhab.binding.xpl.XplBindingConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 2 with XplBindingConfig

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);
                        }
                    }
                }
            }
        }
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) StringItem(org.openhab.core.library.items.StringItem) NumberItem(org.openhab.core.library.items.NumberItem) SwitchItem(org.openhab.core.library.items.SwitchItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) ContactItem(org.openhab.core.library.items.ContactItem) NumberItem(org.openhab.core.library.items.NumberItem) XplBindingConfig(org.openhab.binding.xpl.XplBindingConfig) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DecimalType(org.openhab.core.library.types.DecimalType) SwitchItem(org.openhab.core.library.items.SwitchItem) XplBindingProvider(org.openhab.binding.xpl.XplBindingProvider)

Example 3 with XplBindingConfig

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);
    }
}
Also used : XplBindingConfig(org.openhab.binding.xpl.XplBindingConfig) XplBindingProvider(org.openhab.binding.xpl.XplBindingProvider)

Example 4 with XplBindingConfig

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;
}
Also used : NamedValueI(org.cdp1802.xpl.NamedValueI) XplBindingConfig(org.openhab.binding.xpl.XplBindingConfig) NamedValuesI(org.cdp1802.xpl.NamedValuesI) ArrayList(java.util.ArrayList)

Aggregations

XplBindingConfig (org.openhab.binding.xpl.XplBindingConfig)4 XplBindingProvider (org.openhab.binding.xpl.XplBindingProvider)2 ArrayList (java.util.ArrayList)1 NamedValueI (org.cdp1802.xpl.NamedValueI)1 NamedValuesI (org.cdp1802.xpl.NamedValuesI)1 Item (org.openhab.core.items.Item)1 ContactItem (org.openhab.core.library.items.ContactItem)1 NumberItem (org.openhab.core.library.items.NumberItem)1 StringItem (org.openhab.core.library.items.StringItem)1 SwitchItem (org.openhab.core.library.items.SwitchItem)1 DecimalType (org.openhab.core.library.types.DecimalType)1 OnOffType (org.openhab.core.library.types.OnOffType)1 OpenClosedType (org.openhab.core.library.types.OpenClosedType)1 StringType (org.openhab.core.library.types.StringType)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1